* src/native/llni.h (LLNI_QUICKWRAP): Added new macro.
[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 #if defined(ENABLE_THREADS)
36 # include "threads/native/threads.h"
37 #else
38 # include "threads/none/threads.h"
39 #endif
40
41
42 /* LLNI macros *****************************************************************
43
44    The following macros should be used whenever a Java Object is
45    accessed in native code without the use of an JNI function.
46
47    LLNI_field_set_val, LLNI_field_get_val:
48      Deal with primitive values like integer and float values. Do
49      not use these macros to access pointers or references!
50
51    LLNI_field_set_ref, LLNI_field_get_ref:
52      Deal with references to other objects.
53
54    LLNI_field_set_cls, LLNI_field_get_cls:
55      Deal with references to Java Classes which are internally
56      represented by classinfo or java_lang_Class.
57
58 *******************************************************************************/
59
60 #define LLNI_field_set_val(obj, field, value) \
61         LLNI_field_direct(obj, field) = (value)
62
63 #define LLNI_field_set_ref(obj, field, reference) \
64         LLNI_field_direct(obj, field) = LLNI_UNWRAP(reference)
65
66 #define LLNI_field_set_cls(obj, field, value) \
67         LLNI_field_direct(obj, field) = (java_lang_Class *) (value)
68
69 #define LLNI_field_get_val(obj, field, variable) \
70         (variable) = LLNI_field_direct(obj, field)
71
72 #define LLNI_field_get_ref(obj, field, variable) \
73         (variable) = LLNI_WRAP(LLNI_field_direct(obj, field))
74
75 #define LLNI_field_get_cls(obj, field, variable) \
76         (variable) = (classinfo *) LLNI_field_direct(obj, field)
77
78 #define LLNI_class_get(obj, variable) \
79         (variable) = LLNI_field_direct((java_handle_t *) obj, vftbl->class)
80
81
82 /* LLNI_equals ****************************************************************
83  
84    Test if two java_handle_t* point to the same java_object_t*.
85
86 ******************************************************************************/
87
88 #define LLNI_equals(obj1, obj2, result) \
89         LLNI_CRITICAL_START; \
90         (result) = LLNI_UNWRAP(obj1) == LLNI_UNWRAP(obj2); \
91         LLNI_CRITICAL_END
92         
93
94 /* LLNI_classinfo_field_get ***************************************************
95  
96    Get a field from classinfo that is a java object.
97
98 ******************************************************************************/
99
100 #define LLNI_classinfo_field_get(cls, field, variable) \
101         LLNI_CRITICAL_START; \
102         (variable) = LLNI_WRAP((cls)->field); \
103         LLNI_CRITICAL_END
104
105
106 /* LLNI_classinfo_field_set ***************************************************
107  
108    Set a field from classinfo that is a java object.
109
110 ******************************************************************************/
111
112 #define LLNI_classinfo_field_set(cls, field, variable) \
113         LLNI_CRITICAL_START; \
114         (cls)->field = LLNI_UNWRAP(variable); \
115         LLNI_CRITICAL_END
116
117
118 /* LLNI classinfo wrapping / unwrapping macros *********************************
119
120    The following macros are used to wrap or unwrap a classinfo from
121    or into a handle (typically java_lang_Class). No critical sections
122    are needed here, because classinfos are not placed on the GC heap.
123
124    XXX This might change once we implement Class Unloading!
125
126 *******************************************************************************/
127
128 #define LLNI_classinfo_wrap(classinfo) \
129         ((java_lang_Class *) LLNI_WRAP(classinfo))
130
131 #define LLNI_classinfo_unwrap(clazz) \
132         ((classinfo *) LLNI_UNWRAP((java_handle_t *) (clazz)))
133
134
135 /* XXX the direct macros have to be used inside a critical section!!! */
136
137 #define LLNI_field_direct(obj, field) (LLNI_DIRECT(obj)->field)
138 #define LLNI_vftbl_direct(obj)        (LLNI_DIRECT((java_handle_t *) (obj))->vftbl)
139 #define LLNI_array_direct(arr, index) (LLNI_DIRECT(arr)->data[(index)])
140 #define LLNI_array_data(arr)          (LLNI_DIRECT(arr)->data)
141 #define LLNI_array_size(arr)          (LLNI_DIRECT((java_handle_objectarray_t *) (arr))->header.size)
142
143
144 /* LLNI wrapping / unwrapping macros *******************************************
145
146    ATTENTION: Only use these macros inside a LLNI critical section!
147    Once the ciritical section ends, all pointers onto the GC heap
148    retrieved through these macros are void!
149
150 *******************************************************************************/
151
152 #if defined(ENABLE_HANDLES)
153 # define LLNI_WRAP(obj)      ((obj) == NULL ? NULL : localref_add(obj))
154 # define LLNI_UNWRAP(hdl)    ((hdl) == NULL ? NULL : (hdl)->heap_object)
155 # define LLNI_QUICKWRAP(obj) ((obj) == NULL ? NULL : &(obj))
156 # define LLNI_DIRECT(hdl)    ((hdl)->heap_object)
157 #else
158 # define LLNI_WRAP(obj)      (obj)
159 # define LLNI_UNWRAP(hdl)    (hdl)
160 # define LLNI_QUICKWRAP(obj) (obj)
161 # define LLNI_DIRECT(hdl)    (hdl)
162 #endif
163
164
165 /* LLNI critical sections ******************************************************
166
167    These macros handle the LLNI critical sections. While a critical
168    section is active, the absolute position of objects on the GC heap
169    is not allowed to change (no moving Garbage Collection).
170
171    ATTENTION: Use a critical section whenever you have a direct
172    pointer onto the GC heap!
173
174 *******************************************************************************/
175
176 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
177 # define LLNI_CRITICAL_START           llni_critical_start()
178 # define LLNI_CRITICAL_END             llni_critical_end()
179 # define LLNI_CRITICAL_START_THREAD(t) llni_critical_start_thread(t)
180 # define LLNI_CRITICAL_END_THREAD(t)   llni_critical_end_thread(t)
181 #else
182 # define LLNI_CRITICAL_START
183 # define LLNI_CRITICAL_END
184 # define LLNI_CRITICAL_START_THREAD(t)
185 # define LLNI_CRITICAL_END_THREAD(t)
186 #endif
187
188 void llni_critical_start();
189 void llni_critical_end();
190 void llni_critical_start_thread(threadobject *t);
191 void llni_critical_end_thread(threadobject *t);
192
193
194 #endif /* _LLNI_H */
195
196
197 /*
198  * These are local overrides for various environment variables in Emacs.
199  * Please do not remove this and leave it at the end of the file, where
200  * Emacs will automagically detect them.
201  * ---------------------------------------------------------------------
202  * Local variables:
203  * mode: c
204  * indent-tabs-mode: t
205  * c-basic-offset: 4
206  * tab-width: 4
207  * End:
208  */