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 // Define this before including jni.h.
15 // jni.h is included by jvmpi.h, which might be included. We define
16 // this unconditionally because it is convenient and it lets other
17 // files include jni.h without difficulty.
18 #define __GCJ_JNI_IMPL__
20 #include <gcj/javaprims.h>
22 #include <java-assert.h>
23 #include <java-threads.h>
24 // Must include java-gc.h before Object.h for the implementation.
27 #include <java/lang/Object.h>
29 // Include cni.h before field.h to enable all definitions. FIXME.
31 #include <gcj/field.h>
33 /* Structure of the virtual table. */
37 typedef struct { void *pc
, *gp
; } vtable_elt
;
39 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
; }
53 // We know that get_finalizer is only used for checking whether
54 // this object needs to have a finalizer registered. So it is
55 // safe to simply return just the PC component of the vtable
57 return ((vtable_elt
*)(get_method(0)))->pc
;
60 void *get_method(int i
) { return method
[i
]; }
61 void set_method(int i
, void *fptr
) { method
[i
] = fptr
; }
62 void *get_finalizer() { return get_method(0); }
65 static size_t vtable_elt_size() { return sizeof(vtable_elt
); }
67 // Given a method index, return byte offset from the vtable pointer.
68 static jint
idx_to_offset (int index
)
70 return (2 * sizeof (void *)) + (index
* vtable_elt_size ());
72 static _Jv_VTable
*new_vtable (int count
);
75 // Number of virtual methods on object. FIXME: it sucks that we have
76 // to keep this up to date by hand.
77 #define NUM_OBJECT_METHODS 5
79 // This structure is the type of an array's vtable.
80 struct _Jv_ArrayVTable
: public _Jv_VTable
82 vtable_elt extra_method
[NUM_OBJECT_METHODS
- 1];
88 jint i
; // Also stores smaller integral types.
90 jint ia
[1]; // Half of _Jv_word2.
93 #if SIZEOF_VOID_P == 8
94 // We can safely put a long or a double in here without increasing
95 // the size of _Jv_Word; we take advantage of this in the interpreter.
102 struct _Jv_Field
*field
;
103 struct _Jv_Utf8Const
*utf8
;
104 struct _Jv_ResolvedMethod
*rmethod
;
114 /* Extract a character from a Java-style Utf8 string.
115 * PTR points to the current character.
116 * LIMIT points to the end of the Utf8 string.
117 * PTR is incremented to point after the character thta gets returns.
118 * On an error, -1 is returned. */
119 #define UTF8_GET(PTR, LIMIT) \
120 ((PTR) >= (LIMIT) ? -1 \
121 : *(PTR) < 128 ? *(PTR)++ \
122 : (*(PTR)&0xE0) == 0xC0 && ((PTR)+=2)<=(LIMIT) && ((PTR)[-1]&0xC0) == 0x80 \
123 ? (((PTR)[-2] & 0x1F) << 6) + ((PTR)[-1] & 0x3F) \
124 : (*(PTR) & 0xF0) == 0xE0 && ((PTR) += 3) <= (LIMIT) \
125 && ((PTR)[-2] & 0xC0) == 0x80 && ((PTR)[-1] & 0xC0) == 0x80 \
126 ? (((PTR)[-3]&0x0F) << 12) + (((PTR)[-2]&0x3F) << 6) + ((PTR)[-1]&0x3F) \
129 extern int _Jv_strLengthUtf8(char* str
, int len
);
131 typedef struct _Jv_Utf8Const Utf8Const
;
132 _Jv_Utf8Const
*_Jv_makeUtf8Const (char *s
, int len
);
133 _Jv_Utf8Const
*_Jv_makeUtf8Const (jstring string
);
134 extern jboolean
_Jv_equalUtf8Consts (_Jv_Utf8Const
*, _Jv_Utf8Const
*);
135 extern jboolean
_Jv_equal (_Jv_Utf8Const
*, jstring
, jint
);
136 extern jboolean
_Jv_equaln (_Jv_Utf8Const
*, jstring
, jint
);
138 // FIXME: remove this define.
139 #define StringClass java::lang::String::class$
143 /* Some constants used during lookup of special class methods. */
144 extern _Jv_Utf8Const
*void_signature
; /* "()V" */
145 extern _Jv_Utf8Const
*clinit_name
; /* "<clinit>" */
146 extern _Jv_Utf8Const
*init_name
; /* "<init>" */
147 extern _Jv_Utf8Const
*finit_name
; /* "finit$", */
149 /* Set to true by _Jv_CreateJavaVM. */
150 extern bool runtimeInitialized
;
153 /* Type of pointer used as finalizer. */
154 typedef void _Jv_FinalizerFunc (jobject
);
156 /* Allocate space for a new Java object. */
157 void *_Jv_AllocObj (jsize size
, jclass cl
) __attribute__((__malloc__
));
158 /* Allocate space for a potentially uninitialized pointer-free object.
159 Interesting only with JV_HASH_SYNCHRONIZATION. */
160 void *_Jv_AllocPtrFreeObj (jsize size
, jclass cl
) __attribute__((__malloc__
));
161 /* Allocate space for an array of Java objects. */
162 void *_Jv_AllocArray (jsize size
, jclass cl
) __attribute__((__malloc__
));
163 /* Allocate space that is known to be pointer-free. */
164 void *_Jv_AllocBytes (jsize size
) __attribute__((__malloc__
));
165 /* Allocate space for a new non-Java object, which does not have the usual
166 Java object header but may contain pointers to other GC'ed objects. */
167 void *_Jv_AllocRawObj (jsize size
) __attribute__((__malloc__
));
168 /* Explicitly throw an out-of-memory exception. */
169 void _Jv_ThrowNoMemory() __attribute__((__noreturn__
));
170 /* Allocate an object with a single pointer. The first word is reserved
171 for the GC, and the second word is the traced pointer. */
172 void *_Jv_AllocTraceOne (jsize size
/* incl. reserved slot */);
173 /* Ditto, but for two traced pointers. */
174 void *_Jv_AllocTraceTwo (jsize size
/* incl. reserved slot */);
175 /* Initialize the GC. */
176 void _Jv_InitGC (void);
177 /* Register a finalizer. */
178 void _Jv_RegisterFinalizer (void *object
, _Jv_FinalizerFunc
*method
);
179 /* Compute the GC descriptor for a class */
180 void * _Jv_BuildGCDescr(jclass
);
182 /* Allocate some unscanned, unmoveable memory. Return NULL if out of
184 void *_Jv_MallocUnchecked (jsize size
) __attribute__((__malloc__
));
186 /* Initialize finalizers. The argument is a function to be called
187 when a finalizer is ready to be run. */
188 void _Jv_GCInitializeFinalizers (void (*notifier
) (void));
189 /* Run finalizers for objects ready to be finalized.. */
190 void _Jv_RunFinalizers (void);
191 /* Run all finalizers. Should be called only before exit. */
192 void _Jv_RunAllFinalizers (void);
194 void _Jv_RunGC (void);
195 /* Disable and enable GC. */
196 void _Jv_DisableGC (void);
197 void _Jv_EnableGC (void);
198 /* Register a disappearing link. This is a field F which should be
199 cleared when *F is found to be inaccessible. This is used in the
200 implementation of java.lang.ref.Reference. */
201 void _Jv_GCRegisterDisappearingLink (jobject
*objp
);
202 /* Return true if OBJECT should be reclaimed. This is used to
203 implement soft references. */
204 jboolean
_Jv_GCCanReclaimSoftReference (jobject obj
);
206 /* Return approximation of total size of heap. */
207 long _Jv_GCTotalMemory (void);
208 /* Return approximation of total free memory. */
209 long _Jv_GCFreeMemory (void);
211 /* Set initial heap size. If SIZE==0, ignore. Should be run before
212 _Jv_InitGC. Not required to have any actual effect. */
213 void _Jv_GCSetInitialHeapSize (size_t size
);
215 /* Set maximum heap size. If SIZE==0, unbounded. Should be run
216 before _Jv_InitGC. Not required to have any actual effect. */
217 void _Jv_GCSetMaximumHeapSize (size_t size
);
219 /* External interface to setting the heap size. Parses ARG (a number
220 which can optionally have "k" or "m" appended and calls
221 _Jv_GCSetInitialHeapSize. */
222 void _Jv_SetInitialHeapSize (const char *arg
);
224 /* External interface to setting the maximum heap size. Parses ARG (a
225 number which can optionally have "k" or "m" appended and calls
226 _Jv_GCSetMaximumHeapSize. */
227 void _Jv_SetMaximumHeapSize (const char *arg
);
229 extern "C" void JvRunMain (jclass klass
, int argc
, const char **argv
);
230 void _Jv_RunMain (jclass klass
, const char *name
, int argc
, const char **argv
,
233 // Delayed until after _Jv_AllocBytes is declared.
235 // Note that we allocate this as unscanned memory -- the vtables
236 // are handled specially by the GC.
239 _Jv_VTable::new_vtable (int count
)
241 size_t size
= sizeof(_Jv_VTable
) + (count
- 1) * vtable_elt_size ();
242 return (_Jv_VTable
*) _Jv_AllocBytes (size
);
245 // This function is used to determine the hash code of an object.
247 _Jv_HashCode (jobject obj
)
249 // This was chosen to yield relatively well distributed results on
250 // both 32- and 64-bit architectures. Note 0x7fffffff is prime.
251 // FIXME: we assume sizeof(long) == sizeof(void *).
252 return (jint
) ((unsigned long) obj
% 0x7fffffff);
255 // Return a raw pointer to the elements of an array given the array
256 // and its element type. You might think we could just pick a single
257 // array type and use elements() on it, but we can't because we must
258 // account for alignment of the element type. When ARRAY is null, we
259 // obtain the number of bytes taken by the base part of the array.
261 _Jv_GetArrayElementFromElementType (jobject array
,
265 if (element_type
== JvPrimClass (byte
))
266 elts
= (char *) elements ((jbyteArray
) array
);
267 else if (element_type
== JvPrimClass (short))
268 elts
= (char *) elements ((jshortArray
) array
);
269 else if (element_type
== JvPrimClass (int))
270 elts
= (char *) elements ((jintArray
) array
);
271 else if (element_type
== JvPrimClass (long))
272 elts
= (char *) elements ((jlongArray
) array
);
273 else if (element_type
== JvPrimClass (boolean
))
274 elts
= (char *) elements ((jbooleanArray
) array
);
275 else if (element_type
== JvPrimClass (char))
276 elts
= (char *) elements ((jcharArray
) array
);
277 else if (element_type
== JvPrimClass (float))
278 elts
= (char *) elements ((jfloatArray
) array
);
279 else if (element_type
== JvPrimClass (double))
280 elts
= (char *) elements ((jdoubleArray
) array
);
282 elts
= (char *) elements ((jobjectArray
) array
);
286 extern "C" void _Jv_ThrowBadArrayIndex (jint bad_index
);
287 extern "C" void _Jv_ThrowNullPointerException (void);
288 extern "C" jobject
_Jv_NewArray (jint type
, jint size
)
289 __attribute__((__malloc__
));
290 extern "C" jobject
_Jv_NewMultiArray (jclass klass
, jint dims
, ...)
291 __attribute__((__malloc__
));
292 extern "C" void *_Jv_CheckCast (jclass klass
, jobject obj
);
293 extern "C" void *_Jv_LookupInterfaceMethod (jclass klass
, Utf8Const
*name
,
294 Utf8Const
*signature
);
295 extern "C" void *_Jv_LookupInterfaceMethodIdx (jclass klass
, jclass iface
,
297 extern "C" void _Jv_CheckArrayStore (jobject array
, jobject obj
);
298 extern "C" void _Jv_RegisterClass (jclass klass
);
299 extern "C" void _Jv_RegisterClasses (jclass
*classes
);
300 extern "C" void _Jv_RegisterResource (void *vptr
);
301 extern void _Jv_UnregisterClass (_Jv_Utf8Const
*, java::lang::ClassLoader
*);
302 extern void _Jv_ResolveField (_Jv_Field
*, java::lang::ClassLoader
*);
304 extern jclass
_Jv_FindClass (_Jv_Utf8Const
*name
,
305 java::lang::ClassLoader
*loader
);
306 extern jclass
_Jv_FindClassFromSignature (char *,
307 java::lang::ClassLoader
*loader
);
308 extern void _Jv_GetTypesFromSignature (jmethodID method
,
309 jclass declaringClass
,
310 JArray
<jclass
> **arg_types_out
,
311 jclass
*return_type_out
);
313 extern jobject
_Jv_CallAnyMethodA (jobject obj
, jclass return_type
,
314 jmethodID meth
, jboolean is_constructor
,
315 JArray
<jclass
> *parameter_types
,
319 extern jthrowable
_Jv_CallAnyMethodA (jobject obj
,
322 jboolean is_constructor
,
323 JArray
<jclass
> *parameter_types
,
327 extern jobject
_Jv_NewMultiArray (jclass
, jint ndims
, jint
* dims
)
328 __attribute__((__malloc__
));
330 /* Checked divide subroutines. */
333 jint
_Jv_divI (jint
, jint
);
334 jint
_Jv_remI (jint
, jint
);
335 jlong
_Jv_divJ (jlong
, jlong
);
336 jlong
_Jv_remJ (jlong
, jlong
);
339 /* get/set the name of the running executable. */
340 extern char *_Jv_ThisExecutable (void);
341 extern void _Jv_ThisExecutable (const char *);
343 /* Return a pointer to a symbol in executable or loaded library. */
344 void *_Jv_FindSymbolInExecutable (const char *);
346 /* Initialize JNI. */
347 extern void _Jv_JNI_Init (void);
349 /* Get or set the per-thread JNIEnv used by the invocation API. */
350 _Jv_JNIEnv
*_Jv_GetCurrentJNIEnv ();
351 void _Jv_SetCurrentJNIEnv (_Jv_JNIEnv
*);
354 _Jv_JavaVM
*_Jv_GetJavaVM ();
356 // Some verification functions from defineclass.cc.
357 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const
*sig
);
358 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const
*sig
);
359 bool _Jv_VerifyClassName (unsigned char* ptr
, _Jv_ushort length
);
360 bool _Jv_VerifyClassName (_Jv_Utf8Const
*name
);
361 bool _Jv_VerifyIdentifier (_Jv_Utf8Const
*);
362 bool _Jv_ClassNameSamePackage (_Jv_Utf8Const
*name1
, _Jv_Utf8Const
*name2
);
368 extern void (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (JVMPI_Event
*event
);
369 extern void (*_Jv_JVMPI_Notify_THREAD_START
) (JVMPI_Event
*event
);
370 extern void (*_Jv_JVMPI_Notify_THREAD_END
) (JVMPI_Event
*event
);
373 #endif /* __JAVA_JVM_H__ */