85162111def807b3945e0942988175b1e2543230
[cacao.git] / src / native / llni.h
1 /* src/native/llni.h - low level native interfarce (LLNI)
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _LLNI_H
27 #define _LLNI_H
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #include "config.h"
34
35 /* forward defines ************************************************************/
36
37 /* LLNI wrapping / unwrapping macros *******************************************
38
39    ATTENTION: Only use these macros inside a LLNI critical section!
40    Once the ciritical section ends, all pointers onto the GC heap
41    retrieved through these macros are void!
42
43 *******************************************************************************/
44
45 #if defined(ENABLE_HANDLES)
46 # define LLNI_WRAP(obj)      ((obj) == NULL ? NULL : localref_add(obj))
47 # define LLNI_UNWRAP(hdl)    ((hdl) == NULL ? NULL : (hdl)->heap_object)
48 # define LLNI_QUICKWRAP(obj) ((obj) == NULL ? NULL : &(obj))
49 # define LLNI_DIRECT(hdl)    ((hdl)->heap_object)
50 #else
51 # define LLNI_WRAP(obj)      (obj)
52 # define LLNI_UNWRAP(hdl)    (hdl)
53 # define LLNI_QUICKWRAP(obj) (obj)
54 # define LLNI_DIRECT(hdl)    (hdl)
55 #endif
56
57
58 #include "native/localref.h"
59
60 #include "threads/thread.hpp"
61
62
63 /* LLNI macros *****************************************************************
64
65    The following macros should be used whenever a Java Object is
66    accessed in native code without the use of an JNI function.
67
68    LLNI_field_set_val, LLNI_field_get_val:
69      Deal with primitive values like integer and float values. Do
70      not use these macros to access pointers or references!
71
72    LLNI_field_set_ref, LLNI_field_get_ref:
73      Deal with references to other objects.
74
75    LLNI_field_set_cls, LLNI_field_get_cls:
76      Deal with references to Java Classes which are internally
77      represented by classinfo or java_lang_Class.
78
79 *******************************************************************************/
80
81 #define LLNI_field_set_val(obj, field, value) \
82         LLNI_field_direct(obj, field) = (value)
83
84 #define LLNI_field_set_ref(obj, field, reference) \
85         LLNI_field_direct(obj, field) = LLNI_UNWRAP(reference)
86
87 #define LLNI_field_set_cls(obj, field, value) \
88         LLNI_field_direct(obj, field) = (java_lang_Class *) (value)
89
90 #define LLNI_field_get_val(obj, field, variable) \
91         (variable) = LLNI_field_direct(obj, field)
92
93 #define LLNI_field_get_ref(obj, field, variable) \
94         (variable) = LLNI_WRAP(LLNI_field_direct(obj, field))
95
96 #define LLNI_field_get_cls(obj, field, variable) \
97         (variable) = (classinfo *) LLNI_field_direct(obj, field)
98
99 #define LLNI_class_get(obj, variable) \
100         (variable) = LLNI_field_direct((java_handle_t *) obj, vftbl->clazz)
101
102
103 /* LLNI_equals ****************************************************************
104  
105    Test if two java_handle_t* point to the same java_object_t*.
106
107 ******************************************************************************/
108
109 #define LLNI_equals(obj1, obj2, result) \
110         LLNI_CRITICAL_START; \
111         (result) = LLNI_UNWRAP(obj1) == LLNI_UNWRAP(obj2); \
112         LLNI_CRITICAL_END
113         
114
115 /* LLNI_classinfo_field_get ***************************************************
116  
117    Get a field from classinfo that is a java object.
118
119 ******************************************************************************/
120
121 #define LLNI_classinfo_field_get(cls, field, variable) \
122         LLNI_CRITICAL_START; \
123         (variable) = LLNI_WRAP((cls)->field); \
124         LLNI_CRITICAL_END
125
126
127 /* LLNI_classinfo_field_set ***************************************************
128  
129    Set a field from classinfo that is a java object.
130
131 ******************************************************************************/
132
133 #define LLNI_classinfo_field_set(cls, field, variable) \
134         LLNI_CRITICAL_START; \
135         (cls)->field = LLNI_UNWRAP(variable); \
136         LLNI_CRITICAL_END
137
138
139 /* LLNI classinfo wrapping / unwrapping macros *********************************
140
141    The following macros are used to wrap or unwrap a classinfo from
142    or into a handle (typically java_lang_Class). No critical sections
143    are needed here, because classinfos are not placed on the GC heap.
144
145    XXX This might change once we implement Class Unloading!
146
147 *******************************************************************************/
148
149 #define LLNI_classinfo_wrap(classinfo) \
150         ((java_lang_Class *) LLNI_WRAP(classinfo))
151
152 #define LLNI_classinfo_unwrap(clazz) \
153         ((classinfo *) LLNI_UNWRAP((java_handle_t *) (clazz)))
154
155
156 /* XXX the direct macros have to be used inside a critical section!!! */
157
158 #define LLNI_field_direct(obj, field) (LLNI_DIRECT(obj)->field)
159 #define LLNI_vftbl_direct(obj)        (LLNI_DIRECT((java_handle_t *) (obj))->vftbl)
160 #define LLNI_array_direct(arr, index) (LLNI_DIRECT(arr)->data[(index)])
161 #define LLNI_array_data(arr)          (LLNI_DIRECT(arr)->data)
162 #define LLNI_array_size(arr)          (LLNI_DIRECT((java_handle_objectarray_t *) (arr))->header.size)
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 #ifdef __cplusplus
194 }
195 #endif
196
197 #endif /* _LLNI_H */
198
199
200 /*
201  * These are local overrides for various environment variables in Emacs.
202  * Please do not remove this and leave it at the end of the file, where
203  * Emacs will automagically detect them.
204  * ---------------------------------------------------------------------
205  * Local variables:
206  * mode: c
207  * indent-tabs-mode: t
208  * c-basic-offset: 4
209  * tab-width: 4
210  * End:
211  */