* Merged with default branch at rev 16f3633aaa5a.
[cacao.git] / src / native / llni.h
1 /* src/native/llni.h - low level native interfarce (LLNI)
2
3    Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #ifndef _LLNI_H
29 #define _LLNI_H
30
31 #include "config.h"
32
33 #include "native/localref.h"
34
35
36 /* LLNI macros *****************************************************************
37
38    The following macros should be used whenever a Java Object is
39    accessed in native code without the use of an JNI function.
40
41    LLNI_field_set_val, LLNI_field_get_val:
42      Deal with primitive values like integer and float values. Do
43      not use these macros to access pointers or references!
44
45    LLNI_field_set_ref, LLNI_field_get_ref:
46      Deal with references to other objects.
47
48    LLNI_field_set_cls, LLNI_field_get_cls:
49      Deal with references to Java Classes which are internally
50      represented by classinfo or java_lang_Class.
51
52 *******************************************************************************/
53
54 #define LLNI_field_set_val(obj, field, value) \
55         LLNI_field_direct(obj, field) = (value)
56
57 #define LLNI_field_set_ref(obj, field, reference) \
58         LLNI_field_direct(obj, field) = LLNI_DIRECT(reference)
59
60 #define LLNI_field_set_cls(obj, field, value) \
61         LLNI_field_direct(obj, field) = (java_lang_Class *) (value)
62
63 #define LLNI_field_get_val(obj, field, variable) \
64         (variable) = LLNI_field_direct(obj, field)
65
66 #define LLNI_field_get_ref(obj, field, variable) \
67         (variable) = LLNI_WRAP(LLNI_field_direct(obj, field))
68
69 #define LLNI_field_get_cls(obj, field, variable) \
70         (variable) = (classinfo *) LLNI_field_direct(obj, field)
71
72 #define LLNI_class_get(obj, variable) \
73         (variable) = LLNI_field_direct((java_handle_t *) obj, vftbl->class)
74
75
76 /* LLNI classinfo wrapping / unwrapping macros *********************************
77
78    The following macros are used to wrap or unwrap a classinfo from
79    or into a handle (typically java_lang_Class). No critical sections
80    are needed here, because classinfos are not placed on the GC heap.
81
82    XXX This might change once we implement Class Unloading!
83
84 *******************************************************************************/
85
86 #define LLNI_classinfo_wrap(classinfo) \
87         ((java_lang_Class *) LLNI_WRAP(classinfo))
88
89 #define LLNI_classinfo_unwrap(clazz) \
90         ((classinfo *) LLNI_UNWRAP((java_handle_t *) (clazz)))
91
92
93 /* XXX the direct macros have to be used inside a critical section!!! */
94
95 #define LLNI_field_direct(obj, field) (LLNI_DIRECT(obj)->field)
96 #define LLNI_vftbl_direct(obj)        (LLNI_DIRECT((java_handle_t *) (obj))->vftbl)
97 #define LLNI_array_direct(arr, index) (LLNI_DIRECT(arr)->data[(index)])
98 #define LLNI_array_data(arr)          (LLNI_DIRECT(arr)->data)
99 #define LLNI_array_size(arr)          (LLNI_DIRECT((java_handle_objectarray_t *) (arr))->header.size)
100
101
102 /* XXX document me */
103
104 #define LLNI_objectarray_element_set(arr, index, reference) \
105         LLNI_array_direct(arr, index) = LLNI_DIRECT(reference)
106
107 #define LLNI_objectarray_element_get(arr, index, variable) \
108         (variable) = LLNI_WRAP(LLNI_array_direct(arr, index))
109
110
111 /* LLNI wrapping / unwrapping macros *******************************************
112
113    ATTENTION: Only use these macros inside a LLNI critical section!
114    Once the ciritical section ends, all pointers onto the GC heap
115    retrieved through these macros are void!
116
117 *******************************************************************************/
118
119 #if defined(ENABLE_HANDLES)
120 # define LLNI_WRAP(obj)   ((obj) == NULL ? NULL : localref_add(obj))
121 # define LLNI_UNWRAP(hdl) ((hdl) == NULL ? NULL : (hdl)->heap_object)
122 # define LLNI_DIRECT(obj) ((obj)->heap_object)
123 #else
124 # define LLNI_WRAP(obj)   ((java_handle_t *) obj)
125 # define LLNI_UNWRAP(hdl) ((java_object_t *) hdl)
126 # define LLNI_DIRECT(obj) (obj)
127 #endif
128
129
130 /* LLNI critical sections ******************************************************
131
132    These macros handle the LLNI critical sections. While a critical
133    section is active, the absolute position of objects on the GC heap
134    is not allowed to change (no moving Garbage Collection).
135
136    ATTENTION: Use a critical section whenever you have a direct
137    pointer onto the GC heap!
138
139 *******************************************************************************/
140
141 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
142 void llni_critical_start(void);
143 void llni_critical_end(void);
144 # define LLNI_CRITICAL_START llni_critical_start()
145 # define LLNI_CRITICAL_END   llni_critical_end()
146 #else
147 # define LLNI_CRITICAL_START
148 # define LLNI_CRITICAL_END
149 #endif
150
151
152 #endif /* _LLNI_H */
153
154
155 /*
156  * These are local overrides for various environment variables in Emacs.
157  * Please do not remove this and leave it at the end of the file, where
158  * Emacs will automagically detect them.
159  * ---------------------------------------------------------------------
160  * Local variables:
161  * mode: c
162  * indent-tabs-mode: t
163  * c-basic-offset: 4
164  * tab-width: 4
165  * End:
166  */