1 // jvm.h - Header file for private implementation information. -*- c++ -*-
3 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 #ifndef __JAVA_JVM_H__
12 #define __JAVA_JVM_H__
14 #include <gcj/javaprims.h>
16 #include <java-assert.h>
17 #include <java-threads.h>
18 // Must include java-gc.h before Object.h for the implementation.
21 #include <java/lang/Object.h>
23 // Include cni.h before field.h to enable all definitions. FIXME.
25 #include <gcj/field.h>
27 /* Structure of the virtual table. */
36 typedef struct { void *pc, *gp; } vtable_elt;
41 typedef void *vtable_elt;
44 // This must be last, as derived classes "extend" this by
45 // adding new data members.
49 void *get_method(int i) { return &method[i]; }
50 void set_method(int i, void *fptr) { method[i] = *(vtable_elt *)fptr; }
52 void *get_method(int i) { return method[i]; }
53 void set_method(int i, void *fptr) { method[i] = fptr; }
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);
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
65 // This structure is the type of an array's vtable.
66 struct _Jv_ArrayVTable : public _Jv_VTable
68 vtable_elt extra_method[NUM_OBJECT_METHODS - 1];
74 jint i; // Also stores smaller integral types.
76 jint ia[1]; // Half of _Jv_word2.
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.
88 struct _Jv_Field *field;
89 struct _Jv_Utf8Const *utf8;
90 struct _Jv_ResolvedMethod *rmethod;
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) \
115 extern int _Jv_strLengthUtf8(char* str, int len);
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);
124 // FIXME: remove this define.
125 #define StringClass java::lang::String::class$
127 /* Type of pointer used as finalizer. */
128 typedef void _Jv_FinalizerFunc (jobject);
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 */
153 void * _Jv_BuildGCDescr(jclass);
156 /* Allocate some unscanned, unmoveable memory. Return NULL if out of
158 void *_Jv_MallocUnchecked (jsize size) __attribute__((__malloc__));
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);
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);
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);
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);
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);
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);
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);
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,
207 // Delayed until after _Jv_AllocBytes is declared.
209 // Note that we allocate this as unscanned memory -- the vtables
210 // are handled specially by the GC.
213 _Jv_VTable::new_vtable (int count)
215 size_t size = sizeof(_Jv_VTable) + (count - 1) * vtable_elt_size ();
216 return (_Jv_VTable *) _Jv_AllocBytes (size);
219 // This function is used to determine the hash code of an object.
221 _Jv_HashCode (jobject obj)
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);
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.
235 _Jv_GetArrayElementFromElementType (jobject array,
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);
256 elts = (char *) elements ((jobjectArray) array);
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,
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*);
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);
287 extern jobject _Jv_CallAnyMethodA (jobject obj, jclass return_type,
288 jmethodID meth, jboolean is_constructor,
289 JArray<jclass> *parameter_types,
293 extern jthrowable _Jv_CallAnyMethodA (jobject obj,
296 jboolean is_constructor,
297 JArray<jclass> *parameter_types,
301 extern jobject _Jv_NewMultiArray (jclass, jint ndims, jint* dims)
302 __attribute__((__malloc__));
304 /* Checked divide subroutines. */
307 jint _Jv_divI (jint, jint);
308 jint _Jv_remI (jint, jint);
309 jlong _Jv_divJ (jlong, jlong);
310 jlong _Jv_remJ (jlong, jlong);
313 /* get/set the name of the running executable. */
314 extern char *_Jv_ThisExecutable (void);
315 extern void _Jv_ThisExecutable (const char *);
317 /* Return a pointer to a symbol in executable or loaded library. */
318 void *_Jv_FindSymbolInExecutable (const char *);
320 /* Initialize JNI. */
321 extern void _Jv_JNI_Init (void);
323 /* Get or set the per-thread JNIEnv used by the invocation API. */
324 _Jv_JNIEnv *_Jv_GetCurrentJNIEnv ();
325 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv *);
328 _Jv_JavaVM *_Jv_GetJavaVM ();
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);
338 #endif /* __JAVA_JVM_H__ */