* Merged twisti branch to default. This merge introduces C++ wrapper
[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 #define LLNI_class_get(obj, variable) \
64         (variable) = LLNI_field_direct((java_handle_t *) obj, vftbl->clazz)
65
66
67 /* LLNI_equals ****************************************************************
68  
69    Test if two java_handle_t* point to the same java_object_t*.
70
71 ******************************************************************************/
72
73 #define LLNI_equals(obj1, obj2, result) \
74         LLNI_CRITICAL_START; \
75         (result) = LLNI_UNWRAP(obj1) == LLNI_UNWRAP(obj2); \
76         LLNI_CRITICAL_END
77         
78
79 /* LLNI_classinfo_field_get ***************************************************
80  
81    Get a field from classinfo that is a java object.
82
83 ******************************************************************************/
84
85 #define LLNI_classinfo_field_get(cls, field, variable) \
86         LLNI_CRITICAL_START; \
87         (variable) = LLNI_WRAP((cls)->field); \
88         LLNI_CRITICAL_END
89
90
91 /* LLNI_classinfo_field_set ***************************************************
92  
93    Set a field from classinfo that is a java object.
94
95 ******************************************************************************/
96
97 #define LLNI_classinfo_field_set(cls, field, variable) \
98         LLNI_CRITICAL_START; \
99         (cls)->field = LLNI_UNWRAP(variable); \
100         LLNI_CRITICAL_END
101
102
103 /* LLNI classinfo wrapping / unwrapping macros *********************************
104
105    The following macros are used to wrap or unwrap a classinfo from
106    or into a handle (typically java_lang_Class). No critical sections
107    are needed here, because classinfos are not placed on the GC heap.
108
109    XXX This might change once we implement Class Unloading!
110
111 *******************************************************************************/
112
113 #define LLNI_classinfo_wrap(classinfo) \
114         ((java_handle_t*) LLNI_WRAP(classinfo))
115
116 #define LLNI_classinfo_unwrap(clazz) \
117         ((classinfo *) LLNI_UNWRAP((java_handle_t *) (clazz)))
118
119
120 /* XXX the direct macros have to be used inside a critical section!!! */
121
122 #define LLNI_field_direct(obj, field) (LLNI_DIRECT(obj)->field)
123 #define LLNI_vftbl_direct(obj)        (LLNI_DIRECT((java_handle_t *) (obj))->vftbl)
124 #define LLNI_array_direct(arr, index) (LLNI_DIRECT(arr)->data[(index)])
125 #define LLNI_array_data(arr)          (LLNI_DIRECT(arr)->data)
126 #define LLNI_array_size(arr)          (LLNI_DIRECT((java_handle_objectarray_t *) (arr))->header.size)
127
128
129 /* LLNI critical sections ******************************************************
130
131    These macros handle the LLNI critical sections. While a critical
132    section is active, the absolute position of objects on the GC heap
133    is not allowed to change (no moving Garbage Collection).
134
135    ATTENTION: Use a critical section whenever you have a direct
136    pointer onto the GC heap!
137
138 *******************************************************************************/
139
140 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
141 # define LLNI_CRITICAL_START           llni_critical_start()
142 # define LLNI_CRITICAL_END             llni_critical_end()
143 # define LLNI_CRITICAL_START_THREAD(t) llni_critical_start_thread(t)
144 # define LLNI_CRITICAL_END_THREAD(t)   llni_critical_end_thread(t)
145 #else
146 # define LLNI_CRITICAL_START
147 # define LLNI_CRITICAL_END
148 # define LLNI_CRITICAL_START_THREAD(t)
149 # define LLNI_CRITICAL_END_THREAD(t)
150 #endif
151
152 void llni_critical_start();
153 void llni_critical_end();
154 void llni_critical_start_thread(threadobject *t);
155 void llni_critical_end_thread(threadobject *t);
156
157 #ifdef __cplusplus
158 }
159 #endif
160
161 #endif /* _LLNI_H */
162
163
164 /*
165  * These are local overrides for various environment variables in Emacs.
166  * Please do not remove this and leave it at the end of the file, where
167  * Emacs will automagically detect them.
168  * ---------------------------------------------------------------------
169  * Local variables:
170  * mode: c
171  * indent-tabs-mode: t
172  * c-basic-offset: 4
173  * tab-width: 4
174  * End:
175  */