OSDN Git Service

* gnu/gcj/runtime/natFinalizerThread.cc: New file.
[pf3gnuchains/gcc-fork.git] / libjava / include / jvm.h
1 // jvm.h - Header file for private implementation information. -*- c++ -*-
2
3 /* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #ifndef __JAVA_JVM_H__
12 #define __JAVA_JVM_H__
13
14 #include <gcj/javaprims.h>
15
16 #include <java-assert.h>
17 #include <java-threads.h>
18 // Must include java-gc.h before Object.h for the implementation.
19 #include <java-gc.h>
20
21 #include <java/lang/Object.h>
22
23 // Include cni.h before field.h to enable all definitions.  FIXME.
24 #include <gcj/cni.h>
25 #include <gcj/field.h>
26
27 /* Structure of the virtual table.  */
28 struct _Jv_VTable
29 {
30 #ifdef __ia64__
31   jclass clas;
32   unsigned long : 64;
33   void *gc_descr;
34   unsigned long : 64;
35
36   typedef struct { void *pc, *gp; } vtable_elt;
37 #else
38   jclass clas;
39   void *gc_descr;
40
41   typedef void *vtable_elt;
42 #endif
43
44   // This must be last, as derived classes "extend" this by
45   // adding new data members.
46   vtable_elt method[1];
47
48 #ifdef __ia64__
49   void *get_method(int i) { return &method[i]; }
50   void set_method(int i, void *fptr) { method[i] = *(vtable_elt *)fptr; }
51 #else
52   void *get_method(int i) { return method[i]; }
53   void set_method(int i, void *fptr) { method[i] = fptr; }
54 #endif
55
56   void *get_finalizer() { return get_method(0); }
57   static size_t vtable_elt_size() { return sizeof(vtable_elt); }
58   static _Jv_VTable *new_vtable (int count);
59 };
60
61 // Number of virtual methods on object.  FIXME: it sucks that we have
62 // to keep this up to date by hand.
63 #define NUM_OBJECT_METHODS 5
64
65 // This structure is the type of an array's vtable.
66 struct _Jv_ArrayVTable : public _Jv_VTable
67 {
68   vtable_elt extra_method[NUM_OBJECT_METHODS - 1];
69 };
70
71 union _Jv_word
72 {
73   jobject o;
74   jint i;                       // Also stores smaller integral types.
75   jfloat f;
76   jint ia[1];                   // Half of _Jv_word2.
77   void* p;
78
79 #if SIZEOF_VOID_P == 8
80   // We can safely put a long or a double in here without increasing
81   // the size of _Jv_Word; we take advantage of this in the interpreter.
82   jlong l;
83   jdouble d;
84 #endif
85
86   jclass                     clazz;
87   jstring                    string;
88   struct _Jv_Field          *field;
89   struct _Jv_Utf8Const      *utf8;
90   struct _Jv_ResolvedMethod *rmethod;
91 };
92
93 union _Jv_word2
94 {
95   jint ia[2];
96   jlong l;
97   jdouble d;
98 };                              
99
100 /* Extract a character from a Java-style Utf8 string.
101  * PTR points to the current character.
102  * LIMIT points to the end of the Utf8 string.
103  * PTR is incremented to point after the character thta gets returns.
104  * On an error, -1 is returned. */
105 #define UTF8_GET(PTR, LIMIT) \
106   ((PTR) >= (LIMIT) ? -1 \
107    : *(PTR) < 128 ? *(PTR)++ \
108    : (*(PTR)&0xE0) == 0xC0 && ((PTR)+=2)<=(LIMIT) && ((PTR)[-1]&0xC0) == 0x80 \
109    ? (((PTR)[-2] & 0x1F) << 6) + ((PTR)[-1] & 0x3F) \
110    : (*(PTR) & 0xF0) == 0xE0 && ((PTR) += 3) <= (LIMIT) \
111    && ((PTR)[-2] & 0xC0) == 0x80 && ((PTR)[-1] & 0xC0) == 0x80 \
112    ? (((PTR)[-3]&0x0F) << 12) + (((PTR)[-2]&0x3F) << 6) + ((PTR)[-1]&0x3F) \
113    : ((PTR)++, -1))
114
115 extern int _Jv_strLengthUtf8(char* str, int len);
116
117 typedef struct _Jv_Utf8Const Utf8Const;
118 _Jv_Utf8Const *_Jv_makeUtf8Const (char *s, int len);
119 _Jv_Utf8Const *_Jv_makeUtf8Const (jstring string);
120 extern jboolean _Jv_equalUtf8Consts (_Jv_Utf8Const *, _Jv_Utf8Const *);
121 extern jboolean _Jv_equal (_Jv_Utf8Const *, jstring, jint);
122 extern jboolean _Jv_equaln (_Jv_Utf8Const *, jstring, jint);
123
124 // FIXME: remove this define.
125 #define StringClass java::lang::String::class$
126
127 /* Type of pointer used as finalizer.  */
128 typedef void _Jv_FinalizerFunc (jobject);
129
130 /* Allocate space for a new Java object.  */
131 void *_Jv_AllocObj (jsize size, jclass cl) __attribute__((__malloc__));
132 /* Allocate space for a potentially uninitialized pointer-free object.
133    Interesting only with JV_HASH_SYNCHRONIZATION.  */
134 void *_Jv_AllocPtrFreeObj (jsize size, jclass cl) __attribute__((__malloc__));
135 /* Allocate space for an array of Java objects.  */
136 void *_Jv_AllocArray (jsize size, jclass cl) __attribute__((__malloc__));
137 /* Allocate space that is known to be pointer-free.  */
138 void *_Jv_AllocBytes (jsize size) __attribute__((__malloc__));
139 /* Allocate space for a new non-Java object, which does not have the usual 
140    Java object header but may contain pointers to other GC'ed objects.  */
141 void *_Jv_AllocRawObj (jsize size) __attribute__((__malloc__));
142 /* Explicitly throw an out-of-memory exception. */
143 void _Jv_ThrowNoMemory() __attribute__((__noreturn__));
144 /* Allocate an object with a single pointer.  The first word is reserved
145    for the GC, and the second word is the traced pointer.  */
146 void *_Jv_AllocTraceOne (jsize size /* incl. reserved slot */);
147 /* Initialize the GC.  */
148 void _Jv_InitGC (void);
149 /* Register a finalizer.  */
150 void _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *method);
151 /* Compute the GC descriptor for a class */
152 #ifdef INTERPRETER
153 void * _Jv_BuildGCDescr(jclass);
154 #endif
155
156 /* Allocate some unscanned, unmoveable memory.  Return NULL if out of
157    memory.  */
158 void *_Jv_MallocUnchecked (jsize size) __attribute__((__malloc__));
159
160 /* Initialize finalizers.  The argument is a function to be called
161    when a finalizer is ready to be run.  */
162 void _Jv_GCInitializeFinalizers (void (*notifier) (void));
163 /* Run finalizers for objects ready to be finalized..  */
164 void _Jv_RunFinalizers (void);
165 /* Run all finalizers.  Should be called only before exit.  */
166 void _Jv_RunAllFinalizers (void);
167 /* Perform a GC.  */
168 void _Jv_RunGC (void);
169 /* Disable and enable GC.  */
170 void _Jv_DisableGC (void);
171 void _Jv_EnableGC (void);
172 /* Register a disappearing link.  This is a field F which should be
173    cleared when *F is found to be inaccessible.  This is used in the
174    implementation of java.lang.ref.Reference.  */
175 void _Jv_GCRegisterDisappearingLink (jobject *objp);
176 /* Return true if OBJECT should be reclaimed.  This is used to
177    implement soft references.  */
178 jboolean _Jv_GCCanReclaimSoftReference (jobject obj);
179
180 /* Return approximation of total size of heap.  */
181 long _Jv_GCTotalMemory (void);
182 /* Return approximation of total free memory.  */
183 long _Jv_GCFreeMemory (void);
184
185 /* Set initial heap size.  If SIZE==0, ignore.  Should be run before
186    _Jv_InitGC.  Not required to have any actual effect.  */
187 void _Jv_GCSetInitialHeapSize (size_t size);
188
189 /* Set maximum heap size.  If SIZE==0, unbounded.  Should be run
190    before _Jv_InitGC.  Not required to have any actual effect.  */
191 void _Jv_GCSetMaximumHeapSize (size_t size);
192
193 /* External interface to setting the heap size.  Parses ARG (a number
194    which can optionally have "k" or "m" appended and calls
195    _Jv_GCSetInitialHeapSize.  */
196 void _Jv_SetInitialHeapSize (const char *arg);
197
198 /* External interface to setting the maximum heap size.  Parses ARG (a
199    number which can optionally have "k" or "m" appended and calls
200    _Jv_GCSetMaximumHeapSize.  */
201 void _Jv_SetMaximumHeapSize (const char *arg);
202
203 extern "C" void JvRunMain (jclass klass, int argc, const char **argv);
204 void _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv, 
205                   bool is_jar);
206
207 // Delayed until after _Jv_AllocBytes is declared.
208 //
209 // Note that we allocate this as unscanned memory -- the vtables
210 // are handled specially by the GC.
211
212 inline _Jv_VTable *
213 _Jv_VTable::new_vtable (int count)
214 {
215   size_t size = sizeof(_Jv_VTable) + (count - 1) * vtable_elt_size ();
216   return (_Jv_VTable *) _Jv_AllocBytes (size);
217 }
218
219 // This function is used to determine the hash code of an object.
220 inline jint
221 _Jv_HashCode (jobject obj)
222 {
223   // This was chosen to yield relatively well distributed results on
224   // both 32- and 64-bit architectures.  Note 0x7fffffff is prime.
225   // FIXME: we assume sizeof(long) == sizeof(void *).
226   return (jint) ((unsigned long) obj % 0x7fffffff);
227 }
228
229 // Return a raw pointer to the elements of an array given the array
230 // and its element type.  You might think we could just pick a single
231 // array type and use elements() on it, but we can't because we must
232 // account for alignment of the element type.  When ARRAY is null, we
233 // obtain the number of bytes taken by the base part of the array.
234 inline char *
235 _Jv_GetArrayElementFromElementType (jobject array,
236                                     jclass element_type)
237 {
238   char *elts;
239   if (element_type == JvPrimClass (byte))
240     elts = (char *) elements ((jbyteArray) array);
241   else if (element_type == JvPrimClass (short))
242     elts = (char *) elements ((jshortArray) array);
243   else if (element_type == JvPrimClass (int))
244     elts = (char *) elements ((jintArray) array);
245   else if (element_type == JvPrimClass (long))
246     elts = (char *) elements ((jlongArray) array);
247   else if (element_type == JvPrimClass (boolean))
248     elts = (char *) elements ((jbooleanArray) array);
249   else if (element_type == JvPrimClass (char))
250     elts = (char *) elements ((jcharArray) array);
251   else if (element_type == JvPrimClass (float))
252     elts = (char *) elements ((jfloatArray) array);
253   else if (element_type == JvPrimClass (double))
254     elts = (char *) elements ((jdoubleArray) array);
255   else
256     elts = (char *) elements ((jobjectArray) array);
257   return elts;
258 }
259
260 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index);
261 extern "C" void _Jv_ThrowNullPointerException (void);
262 extern "C" jobject _Jv_NewArray (jint type, jint size)
263   __attribute__((__malloc__));
264 extern "C" jobject _Jv_NewMultiArray (jclass klass, jint dims, ...)
265   __attribute__((__malloc__));
266 extern "C" void *_Jv_CheckCast (jclass klass, jobject obj);
267 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass, Utf8Const *name,
268                                            Utf8Const *signature);
269 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass, jclass iface, 
270                                                int meth_idx);
271 extern "C" void _Jv_CheckArrayStore (jobject array, jobject obj);
272 extern "C" void _Jv_RegisterClass (jclass klass);
273 extern "C" void _Jv_RegisterClasses (jclass *classes);
274 extern "C" void _Jv_RegisterResource (void *vptr);
275 extern void _Jv_UnregisterClass (_Jv_Utf8Const*, java::lang::ClassLoader*);
276 extern void _Jv_ResolveField (_Jv_Field *, java::lang::ClassLoader*);
277
278 extern jclass _Jv_FindClass (_Jv_Utf8Const *name,
279                              java::lang::ClassLoader *loader);
280 extern jclass _Jv_FindClassFromSignature (char *,
281                                           java::lang::ClassLoader *loader);
282 extern void _Jv_GetTypesFromSignature (jmethodID method,
283                                        jclass declaringClass,
284                                        JArray<jclass> **arg_types_out,
285                                        jclass *return_type_out);
286
287 extern jobject _Jv_CallAnyMethodA (jobject obj, jclass return_type,
288                                    jmethodID meth, jboolean is_constructor,
289                                    JArray<jclass> *parameter_types,
290                                    jobjectArray args);
291
292 union jvalue;
293 extern jthrowable _Jv_CallAnyMethodA (jobject obj,
294                                       jclass return_type,
295                                       jmethodID meth,
296                                       jboolean is_constructor,
297                                       JArray<jclass> *parameter_types,
298                                       jvalue *args,
299                                       jvalue *result);
300
301 extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims)
302   __attribute__((__malloc__));
303
304 /* Checked divide subroutines. */
305 extern "C"
306 {
307   jint _Jv_divI (jint, jint);
308   jint _Jv_remI (jint, jint);
309   jlong _Jv_divJ (jlong, jlong);
310   jlong _Jv_remJ (jlong, jlong);
311 }
312
313 /* get/set the name of the running executable. */
314 extern char *_Jv_ThisExecutable (void);
315 extern void _Jv_ThisExecutable (const char *);
316
317 /* Return a pointer to a symbol in executable or loaded library.  */
318 void *_Jv_FindSymbolInExecutable (const char *);
319
320 /* Initialize JNI.  */
321 extern void _Jv_JNI_Init (void);
322
323 /* Get or set the per-thread JNIEnv used by the invocation API.  */
324 _Jv_JNIEnv *_Jv_GetCurrentJNIEnv ();
325 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *);
326
327 struct _Jv_JavaVM;
328 _Jv_JavaVM *_Jv_GetJavaVM (); 
329
330 #ifdef ENABLE_JVMPI
331 #include "jvmpi.h"
332
333 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
334 extern void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
335 extern void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
336 #endif
337
338 #endif /* __JAVA_JVM_H__ */