1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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
26 #include <java-signal.h>
27 #include <java-threads.h>
31 #include <java/lang/ThreadGroup.h>
34 #ifndef DISABLE_GETENV_PROPERTIES
36 #include <java-props.h>
37 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
39 #define PROCESS_GCJ_PROPERTIES
40 #endif // DISABLE_GETENV_PROPERTIES
42 #include <java/lang/Class.h>
43 #include <java/lang/ClassLoader.h>
44 #include <java/lang/Runtime.h>
45 #include <java/lang/String.h>
46 #include <java/lang/Thread.h>
47 #include <java/lang/ThreadGroup.h>
48 #include <java/lang/ArrayIndexOutOfBoundsException.h>
49 #include <java/lang/ArithmeticException.h>
50 #include <java/lang/ClassFormatError.h>
51 #include <java/lang/InternalError.h>
52 #include <java/lang/NegativeArraySizeException.h>
53 #include <java/lang/NullPointerException.h>
54 #include <java/lang/OutOfMemoryError.h>
55 #include <java/lang/System.h>
56 #include <java/lang/reflect/Modifier.h>
57 #include <java/io/PrintStream.h>
58 #include <java/lang/UnsatisfiedLinkError.h>
59 #include <java/lang/VirtualMachineError.h>
60 #include <gnu/gcj/runtime/VMClassLoader.h>
61 #include <gnu/gcj/runtime/FinalizerThread.h>
62 #include <gnu/gcj/runtime/FirstThread.h>
68 // We allocate a single OutOfMemoryError exception which we keep
69 // around for use if we run out of memory.
70 static java::lang::OutOfMemoryError
*no_memory
;
72 // Largest representable size_t.
73 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
75 static const char *no_properties
[] = { NULL
};
77 // Properties set at compile time.
78 const char **_Jv_Compiler_Properties
= no_properties
;
80 // The JAR file to add to the beginning of java.class.path.
81 const char *_Jv_Jar_Class_Path
;
83 #ifndef DISABLE_GETENV_PROPERTIES
84 // Property key/value pairs.
85 property_pair
*_Jv_Environment_Properties
;
88 // The name of this executable.
89 static char *_Jv_execName
;
91 // Stash the argv pointer to benefit native libraries that need it.
92 const char **_Jv_argv
;
96 // Pointer to JVMPI notification functions.
97 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (JVMPI_Event
*event
);
98 void (*_Jv_JVMPI_Notify_THREAD_START
) (JVMPI_Event
*event
);
99 void (*_Jv_JVMPI_Notify_THREAD_END
) (JVMPI_Event
*event
);
103 extern "C" void _Jv_ThrowSignal (jthrowable
) __attribute ((noreturn
));
105 // Just like _Jv_Throw, but fill in the stack trace first. Although
106 // this is declared extern in order that its name not be mangled, it
107 // is not intended to be used outside this file.
109 _Jv_ThrowSignal (jthrowable throwable
)
111 throwable
->fillInStackTrace ();
116 static java::lang::NullPointerException
*nullp
;
118 SIGNAL_HANDLER (catch_segv
)
120 MAKE_THROW_FRAME (nullp
);
121 _Jv_ThrowSignal (nullp
);
125 static java::lang::ArithmeticException
*arithexception
;
128 SIGNAL_HANDLER (catch_fpe
)
130 #ifdef HANDLE_DIVIDE_OVERFLOW
131 HANDLE_DIVIDE_OVERFLOW
;
133 MAKE_THROW_FRAME (arithexception
);
135 _Jv_ThrowSignal (arithexception
);
142 _Jv_equalUtf8Consts (Utf8Const
* a
, Utf8Const
*b
)
145 _Jv_ushort
*aptr
, *bptr
;
148 if (a
->hash
!= b
->hash
)
151 if (b
->length
!= len
)
153 aptr
= (_Jv_ushort
*)a
->data
;
154 bptr
= (_Jv_ushort
*)b
->data
;
155 len
= (len
+ 1) >> 1;
157 if (*aptr
++ != *bptr
++)
162 /* True iff A is equal to STR.
163 HASH is STR->hashCode().
167 _Jv_equal (Utf8Const
* a
, jstring str
, jint hash
)
169 if (a
->hash
!= (_Jv_ushort
) hash
)
171 jint len
= str
->length();
173 jchar
*sptr
= _Jv_GetStringChars (str
);
174 unsigned char* ptr
= (unsigned char*) a
->data
;
175 unsigned char* limit
= ptr
+ a
->length
;
178 int ch
= UTF8_GET (ptr
, limit
);
187 /* Like _Jv_equal, but stop after N characters. */
189 _Jv_equaln (Utf8Const
*a
, jstring str
, jint n
)
191 jint len
= str
->length();
193 jchar
*sptr
= _Jv_GetStringChars (str
);
194 unsigned char* ptr
= (unsigned char*) a
->data
;
195 unsigned char* limit
= ptr
+ a
->length
;
196 for (; n
-- > 0; i
++, sptr
++)
198 int ch
= UTF8_GET (ptr
, limit
);
207 /* Count the number of Unicode chars encoded in a given Ut8 string. */
209 _Jv_strLengthUtf8(char* str
, int len
)
212 unsigned char* limit
;
215 ptr
= (unsigned char*) str
;
218 for (; ptr
< limit
; str_length
++)
220 if (UTF8_GET (ptr
, limit
) < 0)
226 /* Calculate a hash value for a string encoded in Utf8 format.
227 * This returns the same hash value as specified or java.lang.String.hashCode.
230 hashUtf8String (char* str
, int len
)
232 unsigned char* ptr
= (unsigned char*) str
;
233 unsigned char* limit
= ptr
+ len
;
238 int ch
= UTF8_GET (ptr
, limit
);
239 /* Updated specification from
240 http://www.javasoft.com/docs/books/jls/clarify.html. */
241 hash
= (31 * hash
) + ch
;
247 _Jv_makeUtf8Const (char* s
, int len
)
251 Utf8Const
* m
= (Utf8Const
*) _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
252 memcpy (m
->data
, s
, len
);
255 m
->hash
= hashUtf8String (s
, len
) & 0xFFFF;
260 _Jv_makeUtf8Const (jstring string
)
262 jint hash
= string
->hashCode ();
263 jint len
= _Jv_GetStringUTFLength (string
);
265 Utf8Const
* m
= (Utf8Const
*)
266 _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
271 _Jv_GetStringUTFRegion (string
, 0, string
->length (), m
->data
);
281 _Jv_Abort (const char *function
, const char *file
, int line
,
285 _Jv_Abort (const char *, const char *, int, const char *message
)
290 "libgcj failure: %s\n in function %s, file %s, line %d\n",
291 message
, function
, file
, line
);
293 fprintf (stderr
, "libgcj failure: %s\n", message
);
299 fail_on_finalization (jobject
)
301 JvFail ("object was finalized");
305 _Jv_GCWatch (jobject obj
)
307 _Jv_RegisterFinalizer (obj
, fail_on_finalization
);
311 _Jv_ThrowBadArrayIndex(jint bad_index
)
313 throw new java::lang::ArrayIndexOutOfBoundsException
314 (java::lang::String::valueOf (bad_index
));
318 _Jv_ThrowNullPointerException ()
320 throw new java::lang::NullPointerException
;
323 // Explicitly throw a no memory exception.
324 // The collector calls this when it encounters an out-of-memory condition.
325 void _Jv_ThrowNoMemory()
332 jvmpi_notify_alloc(jclass klass
, jint size
, jobject obj
)
334 // Service JVMPI allocation request.
335 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC
!= 0, false))
339 event
.event_type
= JVMPI_EVENT_OBJECT_ALLOC
;
341 event
.u
.obj_alloc
.arena_id
= 0;
342 event
.u
.obj_alloc
.class_id
= (jobjectID
) klass
;
343 event
.u
.obj_alloc
.is_array
= 0;
344 event
.u
.obj_alloc
.size
= size
;
345 event
.u
.obj_alloc
.obj_id
= (jobjectID
) obj
;
347 // FIXME: This doesn't look right for the Boehm GC. A GC may
348 // already be in progress. _Jv_DisableGC () doesn't wait for it.
349 // More importantly, I don't see the need for disabling GC, since we
350 // blatantly have a pointer to obj on our stack, ensuring that the
351 // object can't be collected. Even for a nonconservative collector,
352 // it appears to me that this must be true, since we are about to
353 // return obj. Isn't this whole approach way too intrusive for
354 // a useful profiling interface? - HB
356 (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (&event
);
360 #else /* !ENABLE_JVMPI */
361 # define jvmpi_notify_alloc(klass,size,obj) /* do nothing */
364 // Allocate a new object of class KLASS. SIZE is the size of the object
365 // to allocate. You might think this is redundant, but it isn't; some
366 // classes, such as String, aren't of fixed size.
367 // First a version that assumes that we have no finalizer, and that
368 // the class is already initialized.
369 // If we know that JVMPI is disabled, this can be replaced by a direct call
370 // to the allocator for the appropriate GC.
372 _Jv_AllocObjectNoInitNoFinalizer (jclass klass
, jint size
)
374 jobject obj
= (jobject
) _Jv_AllocObj (size
, klass
);
375 jvmpi_notify_alloc (klass
, size
, obj
);
379 // And now a version that initializes if necessary.
381 _Jv_AllocObjectNoFinalizer (jclass klass
, jint size
)
383 _Jv_InitClass (klass
);
384 jobject obj
= (jobject
) _Jv_AllocObj (size
, klass
);
385 jvmpi_notify_alloc (klass
, size
, obj
);
389 // And now the general version that registers a finalizer if necessary.
391 _Jv_AllocObject (jclass klass
, jint size
)
393 jobject obj
= _Jv_AllocObjectNoFinalizer (klass
, size
);
395 // We assume that the compiler only generates calls to this routine
396 // if there really is an interesting finalizer.
397 // Unfortunately, we still have to the dynamic test, since there may
398 // be cni calls to this routine.
399 // Nore that on IA64 get_finalizer() returns the starting address of the
400 // function, not a function pointer. Thus this still works.
401 if (klass
->vtable
->get_finalizer ()
402 != java::lang::Object::class$
.vtable
->get_finalizer ())
403 _Jv_RegisterFinalizer (obj
, _Jv_FinalizeObject
);
407 // A version of the above that assumes the object contains no pointers,
408 // and requires no finalization. This can't happen if we need pointers
410 #ifdef JV_HASH_SYNCHRONIZATION
412 _Jv_AllocPtrFreeObject (jclass klass
, jint size
)
414 _Jv_InitClass (klass
);
416 jobject obj
= (jobject
) _Jv_AllocPtrFreeObj (size
, klass
);
419 // Service JVMPI request.
421 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC
!= 0, false))
425 event
.event_type
= JVMPI_EVENT_OBJECT_ALLOC
;
427 event
.u
.obj_alloc
.arena_id
= 0;
428 event
.u
.obj_alloc
.class_id
= (jobjectID
) klass
;
429 event
.u
.obj_alloc
.is_array
= 0;
430 event
.u
.obj_alloc
.size
= size
;
431 event
.u
.obj_alloc
.obj_id
= (jobjectID
) obj
;
434 (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (&event
);
441 #endif /* JV_HASH_SYNCHRONIZATION */
444 // Allocate a new array of Java objects. Each object is of type
445 // `elementClass'. `init' is used to initialize each slot in the
448 _Jv_NewObjectArray (jsize count
, jclass elementClass
, jobject init
)
450 if (__builtin_expect (count
< 0, false))
451 throw new java::lang::NegativeArraySizeException
;
453 JvAssert (! elementClass
->isPrimitive ());
455 // Ensure that elements pointer is properly aligned.
456 jobjectArray obj
= NULL
;
457 size_t size
= (size_t) elements (obj
);
458 size
+= count
* sizeof (jobject
);
460 // FIXME: second argument should be "current loader"
461 jclass klass
= _Jv_GetArrayClass (elementClass
, 0);
463 obj
= (jobjectArray
) _Jv_AllocArray (size
, klass
);
465 jsize
*lp
= const_cast<jsize
*> (&obj
->length
);
467 // We know the allocator returns zeroed memory. So don't bother
471 jobject
*ptr
= elements(obj
);
478 // Allocate a new array of primitives. ELTYPE is the type of the
479 // element, COUNT is the size of the array.
481 _Jv_NewPrimArray (jclass eltype
, jint count
)
483 int elsize
= eltype
->size();
484 if (__builtin_expect (count
< 0, false))
485 throw new java::lang::NegativeArraySizeException
;
487 JvAssert (eltype
->isPrimitive ());
488 jobject dummy
= NULL
;
489 size_t size
= (size_t) _Jv_GetArrayElementFromElementType (dummy
, eltype
);
491 // Check for overflow.
492 if (__builtin_expect ((size_t) count
>
493 (SIZE_T_MAX
- size
) / elsize
, false))
496 jclass klass
= _Jv_GetArrayClass (eltype
, 0);
498 # ifdef JV_HASH_SYNCHRONIZATION
499 // Since the vtable is always statically allocated,
500 // these are completely pointerfree! Make sure the GC doesn't touch them.
502 (__JArray
*) _Jv_AllocPtrFreeObj (size
+ elsize
* count
, klass
);
503 memset((char *)arr
+ size
, 0, elsize
* count
);
505 __JArray
*arr
= (__JArray
*) _Jv_AllocObj (size
+ elsize
* count
, klass
);
506 // Note that we assume we are given zeroed memory by the allocator.
509 jsize
*lp
= const_cast<jsize
*> (&arr
->length
);
516 _Jv_NewArray (jint type
, jint size
)
520 case 4: return JvNewBooleanArray (size
);
521 case 5: return JvNewCharArray (size
);
522 case 6: return JvNewFloatArray (size
);
523 case 7: return JvNewDoubleArray (size
);
524 case 8: return JvNewByteArray (size
);
525 case 9: return JvNewShortArray (size
);
526 case 10: return JvNewIntArray (size
);
527 case 11: return JvNewLongArray (size
);
529 throw new java::lang::InternalError
530 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
533 // Allocate a possibly multi-dimensional array but don't check that
534 // any array length is <0.
536 _Jv_NewMultiArrayUnchecked (jclass type
, jint dimensions
, jint
*sizes
)
538 JvAssert (type
->isArray());
539 jclass element_type
= type
->getComponentType();
541 if (element_type
->isPrimitive())
542 result
= _Jv_NewPrimArray (element_type
, sizes
[0]);
544 result
= _Jv_NewObjectArray (sizes
[0], element_type
, NULL
);
548 JvAssert (! element_type
->isPrimitive());
549 JvAssert (element_type
->isArray());
550 jobject
*contents
= elements ((jobjectArray
) result
);
551 for (int i
= 0; i
< sizes
[0]; ++i
)
552 contents
[i
] = _Jv_NewMultiArrayUnchecked (element_type
, dimensions
- 1,
560 _Jv_NewMultiArray (jclass type
, jint dimensions
, jint
*sizes
)
562 for (int i
= 0; i
< dimensions
; ++i
)
564 throw new java::lang::NegativeArraySizeException
;
566 return _Jv_NewMultiArrayUnchecked (type
, dimensions
, sizes
);
570 _Jv_NewMultiArray (jclass array_type
, jint dimensions
, ...)
573 jint sizes
[dimensions
];
574 va_start (args
, dimensions
);
575 for (int i
= 0; i
< dimensions
; ++i
)
577 jint size
= va_arg (args
, jint
);
579 throw new java::lang::NegativeArraySizeException
;
584 return _Jv_NewMultiArrayUnchecked (array_type
, dimensions
, sizes
);
589 // Ensure 8-byte alignment, for hash synchronization.
590 #define DECLARE_PRIM_TYPE(NAME) \
591 _Jv_ArrayVTable _Jv_##NAME##VTable; \
592 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
594 DECLARE_PRIM_TYPE(byte
);
595 DECLARE_PRIM_TYPE(short);
596 DECLARE_PRIM_TYPE(int);
597 DECLARE_PRIM_TYPE(long);
598 DECLARE_PRIM_TYPE(boolean
);
599 DECLARE_PRIM_TYPE(char);
600 DECLARE_PRIM_TYPE(float);
601 DECLARE_PRIM_TYPE(double);
602 DECLARE_PRIM_TYPE(void);
605 _Jv_InitPrimClass (jclass cl
, char *cname
, char sig
, int len
,
606 _Jv_ArrayVTable
*array_vtable
)
608 using namespace java::lang::reflect
;
610 _Jv_InitNewClassFields (cl
);
612 // We must set the vtable for the class; the Java constructor
614 (*(_Jv_VTable
**) cl
) = java::lang::Class::class$
.vtable
;
616 // Initialize the fields we care about. We do this in the same
617 // order they are declared in Class.h.
618 cl
->name
= _Jv_makeUtf8Const ((char *) cname
, -1);
619 cl
->accflags
= Modifier::PUBLIC
| Modifier::FINAL
| Modifier::ABSTRACT
;
620 cl
->method_count
= sig
;
621 cl
->size_in_bytes
= len
;
622 cl
->vtable
= JV_PRIMITIVE_VTABLE
;
623 cl
->state
= JV_STATE_DONE
;
626 _Jv_NewArrayClass (cl
, NULL
, (_Jv_VTable
*) array_vtable
);
630 _Jv_FindClassFromSignature (char *sig
, java::lang::ClassLoader
*loader
)
635 return JvPrimClass (byte
);
637 return JvPrimClass (short);
639 return JvPrimClass (int);
641 return JvPrimClass (long);
643 return JvPrimClass (boolean
);
645 return JvPrimClass (char);
647 return JvPrimClass (float);
649 return JvPrimClass (double);
651 return JvPrimClass (void);
655 for (i
= 1; sig
[i
] && sig
[i
] != ';'; ++i
)
657 _Jv_Utf8Const
*name
= _Jv_makeUtf8Const (&sig
[1], i
- 1);
658 return _Jv_FindClass (name
, loader
);
663 jclass klass
= _Jv_FindClassFromSignature (&sig
[1], loader
);
666 return _Jv_GetArrayClass (klass
, loader
);
670 return NULL
; // Placate compiler.
676 JvConvertArgv (int argc
, const char **argv
)
680 jobjectArray ar
= JvNewObjectArray(argc
, &StringClass
, NULL
);
681 jobject
*ptr
= elements(ar
);
682 jbyteArray bytes
= NULL
;
683 for (int i
= 0; i
< argc
; i
++)
685 const char *arg
= argv
[i
];
686 int len
= strlen (arg
);
687 if (bytes
== NULL
|| bytes
->length
< len
)
688 bytes
= JvNewByteArray (len
);
689 jbyte
*bytePtr
= elements (bytes
);
690 // We assume jbyte == char.
691 memcpy (bytePtr
, arg
, len
);
693 // Now convert using the default encoding.
694 *ptr
++ = new java::lang::String (bytes
, 0, len
);
696 return (JArray
<jstring
>*) ar
;
699 // FIXME: These variables are static so that they will be
700 // automatically scanned by the Boehm collector. This is needed
701 // because with qthreads the collector won't scan the initial stack --
702 // it will only scan the qthreads stacks.
704 // Command line arguments.
705 static JArray
<jstring
> *arg_vec
;
707 // The primary thread.
708 static java::lang::Thread
*main_thread
;
711 _Jv_ThisExecutable (void)
717 _Jv_ThisExecutable (const char *name
)
721 _Jv_execName
= (char *) _Jv_Malloc (strlen (name
) + 1);
722 strcpy (_Jv_execName
, name
);
726 #ifndef DISABLE_GETENV_PROPERTIES
729 next_property_key (char *s
, size_t *length
)
735 // Skip over whitespace
739 // If we've reached the end, return NULL. Also return NULL if for
740 // some reason we've come across a malformed property string.
746 // Determine the length of the property key.
764 next_property_value (char *s
, size_t *length
)
780 // If we've reached the end, return NULL.
784 // Determine the length of the property value.
803 process_gcj_properties ()
805 char *props
= getenv("GCJ_PROPERTIES");
808 size_t property_count
= 0;
813 // Whip through props quickly in order to count the number of
815 while (p
&& (p
= next_property_key (p
, &length
)))
817 // Skip to the end of the key
820 p
= next_property_value (p
, &length
);
827 // Allocate an array of property value/key pairs.
828 _Jv_Environment_Properties
=
829 (property_pair
*) malloc (sizeof(property_pair
)
830 * (property_count
+ 1));
832 // Go through the properties again, initializing _Jv_Properties
836 while (p
&& (p
= next_property_key (p
, &length
)))
838 _Jv_Environment_Properties
[property_count
].key
= p
;
839 _Jv_Environment_Properties
[property_count
].key_length
= length
;
841 // Skip to the end of the key
844 p
= next_property_value (p
, &length
);
846 _Jv_Environment_Properties
[property_count
].value
= p
;
847 _Jv_Environment_Properties
[property_count
].value_length
= length
;
854 memset ((void *) &_Jv_Environment_Properties
[property_count
],
855 0, sizeof (property_pair
));
859 // Null terminate the strings.
860 while (_Jv_Environment_Properties
[i
].key
)
862 _Jv_Environment_Properties
[i
].key
[_Jv_Environment_Properties
[i
].key_length
] = 0;
863 _Jv_Environment_Properties
[i
++].value
[_Jv_Environment_Properties
[i
].value_length
] = 0;
867 #endif // DISABLE_GETENV_PROPERTIES
871 _Jv_Utf8Const
*void_signature
;
872 _Jv_Utf8Const
*clinit_name
;
873 _Jv_Utf8Const
*init_name
;
874 _Jv_Utf8Const
*finit_name
;
876 bool runtimeInitialized
= false;
880 _Jv_CreateJavaVM (void* /*vm_args*/)
884 if (runtimeInitialized
)
887 runtimeInitialized
= true;
889 PROCESS_GCJ_PROPERTIES
;
893 _Jv_InitializeSyncMutex ();
895 /* Initialize Utf8 constants declared in jvm.h. */
896 void_signature
= _Jv_makeUtf8Const ("()V", 3);
897 clinit_name
= _Jv_makeUtf8Const ("<clinit>", 8);
898 init_name
= _Jv_makeUtf8Const ("<init>", 6);
899 finit_name
= _Jv_makeUtf8Const ("finit$", 6);
901 /* Initialize built-in classes to represent primitive TYPEs. */
902 _Jv_InitPrimClass (&_Jv_byteClass
, "byte", 'B', 1, &_Jv_byteVTable
);
903 _Jv_InitPrimClass (&_Jv_shortClass
, "short", 'S', 2, &_Jv_shortVTable
);
904 _Jv_InitPrimClass (&_Jv_intClass
, "int", 'I', 4, &_Jv_intVTable
);
905 _Jv_InitPrimClass (&_Jv_longClass
, "long", 'J', 8, &_Jv_longVTable
);
906 _Jv_InitPrimClass (&_Jv_booleanClass
, "boolean", 'Z', 1, &_Jv_booleanVTable
);
907 _Jv_InitPrimClass (&_Jv_charClass
, "char", 'C', 2, &_Jv_charVTable
);
908 _Jv_InitPrimClass (&_Jv_floatClass
, "float", 'F', 4, &_Jv_floatVTable
);
909 _Jv_InitPrimClass (&_Jv_doubleClass
, "double", 'D', 8, &_Jv_doubleVTable
);
910 _Jv_InitPrimClass (&_Jv_voidClass
, "void", 'V', 0, &_Jv_voidVTable
);
912 // Turn stack trace generation off while creating exception objects.
913 _Jv_InitClass (&java::lang::Throwable::class$
);
914 java::lang::Throwable::trace_enabled
= 0;
920 arithexception
= new java::lang::ArithmeticException
921 (JvNewStringLatin1 ("/ by zero"));
924 no_memory
= new java::lang::OutOfMemoryError
;
926 java::lang::Throwable::trace_enabled
= 1;
929 LTDL_SET_PRELOADED_SYMBOLS ();
932 _Jv_platform_initialize ();
936 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady
);
938 // Start the GC finalizer thread. A VirtualMachineError can be
939 // thrown by the runtime if, say, threads aren't available. In this
940 // case finalizers simply won't run.
943 using namespace gnu::gcj::runtime
;
944 FinalizerThread
*ft
= new FinalizerThread ();
947 catch (java::lang::VirtualMachineError
*ignore
)
955 _Jv_RunMain (jclass klass
, const char *name
, int argc
, const char **argv
,
961 java::lang::Runtime
*runtime
= NULL
;
964 #ifdef DISABLE_MAIN_ARGS
965 _Jv_ThisExecutable ("[Embedded App]");
967 #ifdef HAVE_PROC_SELF_EXE
969 sprintf (exec_name
, "/proc/%d/exe", getpid ());
970 _Jv_ThisExecutable (exec_name
);
972 _Jv_ThisExecutable (argv
[0]);
973 #endif /* HAVE_PROC_SELF_EXE */
974 #endif /* DISABLE_MAIN_ARGS */
978 // Set this very early so that it is seen when java.lang.System
981 _Jv_Jar_Class_Path
= strdup (name
);
982 _Jv_CreateJavaVM (NULL
);
984 // Get the Runtime here. We want to initialize it before searching
985 // for `main'; that way it will be set up if `main' is a JNI method.
986 runtime
= java::lang::Runtime::getRuntime ();
988 #ifdef DISABLE_MAIN_ARGS
989 arg_vec
= JvConvertArgv (0, 0);
991 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
994 using namespace gnu::gcj::runtime
;
996 main_thread
= new FirstThread (klass
, arg_vec
);
998 main_thread
= new FirstThread (JvNewStringLatin1 (name
),
1001 catch (java::lang::Throwable
*t
)
1003 java::lang::System::err
->println (JvNewStringLatin1
1004 ("Exception during runtime initialization"));
1005 t
->printStackTrace();
1009 _Jv_AttachCurrentThread (main_thread
);
1010 _Jv_ThreadRun (main_thread
);
1013 int status
= (int) java::lang::ThreadGroup::had_uncaught_exception
;
1014 runtime
->exit (status
);
1018 JvRunMain (jclass klass
, int argc
, const char **argv
)
1020 _Jv_RunMain (klass
, NULL
, argc
, argv
, false);
1025 // Parse a string and return a heap size.
1027 parse_heap_size (const char *spec
)
1030 unsigned long val
= strtoul (spec
, &end
, 10);
1031 if (*end
== 'k' || *end
== 'K')
1033 else if (*end
== 'm' || *end
== 'M')
1035 return (size_t) val
;
1038 // Set the initial heap size. This might be ignored by the GC layer.
1039 // This must be called before _Jv_RunMain.
1041 _Jv_SetInitialHeapSize (const char *arg
)
1043 size_t size
= parse_heap_size (arg
);
1044 _Jv_GCSetInitialHeapSize (size
);
1047 // Set the maximum heap size. This might be ignored by the GC layer.
1048 // This must be called before _Jv_RunMain.
1050 _Jv_SetMaximumHeapSize (const char *arg
)
1052 size_t size
= parse_heap_size (arg
);
1053 _Jv_GCSetMaximumHeapSize (size
);
1059 _Jv_Malloc (jsize size
)
1061 if (__builtin_expect (size
== 0, false))
1063 void *ptr
= malloc ((size_t) size
);
1064 if (__builtin_expect (ptr
== NULL
, false))
1070 _Jv_Realloc (void *ptr
, jsize size
)
1072 if (__builtin_expect (size
== 0, false))
1074 ptr
= realloc (ptr
, (size_t) size
);
1075 if (__builtin_expect (ptr
== NULL
, false))
1081 _Jv_MallocUnchecked (jsize size
)
1083 if (__builtin_expect (size
== 0, false))
1085 return malloc ((size_t) size
);
1089 _Jv_Free (void* ptr
)
1096 // In theory, these routines can be #ifdef'd away on machines which
1097 // support divide overflow signals. However, we never know if some
1098 // code might have been compiled with "-fuse-divide-subroutine", so we
1099 // always include them in libgcj.
1102 _Jv_divI (jint dividend
, jint divisor
)
1104 if (__builtin_expect (divisor
== 0, false))
1105 _Jv_ThrowSignal (arithexception
);
1107 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
1110 return dividend
/ divisor
;
1114 _Jv_remI (jint dividend
, jint divisor
)
1116 if (__builtin_expect (divisor
== 0, false))
1117 _Jv_ThrowSignal (arithexception
);
1119 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
1122 return dividend
% divisor
;
1126 _Jv_divJ (jlong dividend
, jlong divisor
)
1128 if (__builtin_expect (divisor
== 0, false))
1129 _Jv_ThrowSignal (arithexception
);
1131 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
1134 return dividend
/ divisor
;
1138 _Jv_remJ (jlong dividend
, jlong divisor
)
1140 if (__builtin_expect (divisor
== 0, false))
1141 _Jv_ThrowSignal (arithexception
);
1143 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
1146 return dividend
% divisor
;