* src/native/llni.h (LLNI_equals): Added macro.
[cacao.git] / src / native / llni.h
index 9795bd194d4db27ef35825b3f056c4127cf21e24..c5dfe0751a0fe69f64f6e30959e6e82d64a05c2d 100644 (file)
@@ -1,4 +1,4 @@
-/* src/native/llni.h - low level native interfarce (LLNI) macros
+/* src/native/llni.h - low level native interfarce (LLNI)
 
    Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
@@ -22,8 +22,6 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   $Id: llni.h 8343 2007-08-17 21:39:32Z michi $
-
 */
 
 
@@ -32,6 +30,8 @@
 
 #include "config.h"
 
+#include "native/localref.h"
+
 
 /* LLNI macros *****************************************************************
 
@@ -55,7 +55,7 @@
        LLNI_field_direct(obj, field) = (value)
 
 #define LLNI_field_set_ref(obj, field, reference) \
-       LLNI_field_set_val(obj, field, reference)
+       LLNI_field_direct(obj, field) = LLNI_DIRECT(reference)
 
 #define LLNI_field_set_cls(obj, field, value) \
        LLNI_field_direct(obj, field) = (java_lang_Class *) (value)
        (variable) = LLNI_field_direct(obj, field)
 
 #define LLNI_field_get_ref(obj, field, variable) \
-       LLNI_field_get_val(obj, field, variable)
+       (variable) = LLNI_WRAP(LLNI_field_direct(obj, field))
 
 #define LLNI_field_get_cls(obj, field, variable) \
        (variable) = (classinfo *) LLNI_field_direct(obj, field)
 
 #define LLNI_class_get(obj, variable) \
-       (variable) = LLNI_field_direct(obj, header.vftbl->class)
+       (variable) = LLNI_field_direct((java_handle_t *) obj, vftbl->class)
+
+
+/* LLNI_equals ****************************************************************
+   Test if two java_handle_t* point to the same java_object_t*.
+
+******************************************************************************/
+
+#define LLNI_equals(obj1, obj2, result) \
+       LLNI_CRITICAL_START; \
+       (result) = LLNI_UNWRAP(obj1) == LLNI_UNWRAP(obj2); \
+       LLNI_CRITICAL_END
+       
+
+/* LLNI_classinfo_field_get ***************************************************
+   Get a field from classinfo that is a java object.
+
+******************************************************************************/
+
+#define LLNI_classinfo_field_get(cls, field, variable) \
+       LLNI_CRITICAL_START; \
+       (variable) = LLNI_WRAP((cls)->field); \
+       LLNI_CRITICAL_END
+
+
+/* LLNI_classinfo_field_set ***************************************************
+   Set a field from classinfo that is a java object.
+
+******************************************************************************/
+
+#define LLNI_classinfo_field_set(cls, field, variable) \
+       LLNI_CRITICAL_START; \
+       (cls)->field = LLNI_UNWRAP(variable); \
+       LLNI_CRITICAL_END
 
 
 /* LLNI classinfo wrapping / unwrapping macros *********************************
 
    The following macros are used to wrap or unwrap a classinfo from
-   or into a handle (typically java_lang_Class).
+   or into a handle (typically java_lang_Class). No critical sections
+   are needed here, because classinfos are not placed on the GC heap.
+
+   XXX This might change once we implement Class Unloading!
 
 *******************************************************************************/
 
 #define LLNI_classinfo_wrap(classinfo) \
-       ((java_lang_Class *) (classinfo))
+       ((java_lang_Class *) LLNI_WRAP(classinfo))
 
 #define LLNI_classinfo_unwrap(clazz) \
-       ((classinfo *) (clazz))
+       ((classinfo *) LLNI_UNWRAP((java_handle_t *) (clazz)))
 
 
 /* XXX the direct macros have to be used inside a critical section!!! */
 
-#define LLNI_field_direct(obj, field) ((obj)->field) 
-
-#define LLNI_vftbl_direct(obj) (((java_handle_t *) (obj))->vftbl)
+#define LLNI_field_direct(obj, field) (LLNI_DIRECT(obj)->field)
+#define LLNI_vftbl_direct(obj)        (LLNI_DIRECT((java_handle_t *) (obj))->vftbl)
+#define LLNI_array_direct(arr, index) (LLNI_DIRECT(arr)->data[(index)])
+#define LLNI_array_data(arr)          (LLNI_DIRECT(arr)->data)
+#define LLNI_array_size(arr)          (LLNI_DIRECT((java_handle_objectarray_t *) (arr))->header.size)
 
-#define LLNI_array_direct(arr, index) ((arr)->data[(index)])
-
-#define LLNI_array_data(arr) ((arr)->data)
-
-#define LLNI_array_size(arr) ((java_array_t *) (arr))->size
 
 /* XXX document me */
 
 #define LLNI_objectarray_element_set(arr, index, reference) \
-       LLNI_array_direct(arr, index) = reference
+       LLNI_array_direct(arr, index) = (java_object_t *) LLNI_DIRECT(reference)
 
 #define LLNI_objectarray_element_get(arr, index, variable) \
-       (variable) = (java_handle_t *) LLNI_array_direct(arr, index)
+       (variable) = LLNI_WRAP(LLNI_array_direct(arr, index))
+
+
+/* LLNI wrapping / unwrapping macros *******************************************
+
+   ATTENTION: Only use these macros inside a LLNI critical section!
+   Once the ciritical section ends, all pointers onto the GC heap
+   retrieved through these macros are void!
+
+*******************************************************************************/
+
+#if defined(ENABLE_HANDLES)
+# define LLNI_WRAP(obj)   ((obj) == NULL ? NULL : localref_add(obj))
+# define LLNI_UNWRAP(hdl) ((hdl) == NULL ? NULL : (hdl)->heap_object)
+# define LLNI_DIRECT(obj) ((obj)->heap_object)
+#else
+# define LLNI_WRAP(obj)   (obj)
+# define LLNI_UNWRAP(hdl) (hdl)
+# define LLNI_DIRECT(obj) (obj)
+#endif
+
+
+/* LLNI critical sections ******************************************************
+
+   These macros handle the LLNI critical sections. While a critical
+   section is active, the absolute position of objects on the GC heap
+   is not allowed to change (no moving Garbage Collection).
+
+   ATTENTION: Use a critical section whenever you have a direct
+   pointer onto the GC heap!
+
+*******************************************************************************/
 
+#if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
+void llni_critical_start(void);
+void llni_critical_end(void);
+# define LLNI_CRITICAL_START llni_critical_start()
+# define LLNI_CRITICAL_END   llni_critical_end()
+#else
+# define LLNI_CRITICAL_START
+# define LLNI_CRITICAL_END
+#endif
 
 
 #endif /* _LLNI_H */