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/VMThrowable.h>
57 #include <java/lang/reflect/Modifier.h>
58 #include <java/io/PrintStream.h>
59 #include <java/lang/UnsatisfiedLinkError.h>
60 #include <java/lang/VirtualMachineError.h>
61 #include <gnu/gcj/runtime/VMClassLoader.h>
62 #include <gnu/gcj/runtime/FinalizerThread.h>
63 #include <gnu/gcj/runtime/FirstThread.h>
69 // We allocate a single OutOfMemoryError exception which we keep
70 // around for use if we run out of memory.
71 static java::lang::OutOfMemoryError
*no_memory
;
73 // Largest representable size_t.
74 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
76 static const char *no_properties
[] = { NULL
};
78 // Properties set at compile time.
79 const char **_Jv_Compiler_Properties
= no_properties
;
81 // The JAR file to add to the beginning of java.class.path.
82 const char *_Jv_Jar_Class_Path
;
84 #ifndef DISABLE_GETENV_PROPERTIES
85 // Property key/value pairs.
86 property_pair
*_Jv_Environment_Properties
;
89 // Stash the argv pointer to benefit native libraries that need it.
90 const char **_Jv_argv
;
94 // Pointer to JVMPI notification functions.
95 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (JVMPI_Event
*event
);
96 void (*_Jv_JVMPI_Notify_THREAD_START
) (JVMPI_Event
*event
);
97 void (*_Jv_JVMPI_Notify_THREAD_END
) (JVMPI_Event
*event
);
101 extern "C" void _Jv_ThrowSignal (jthrowable
) __attribute ((noreturn
));
103 // Just like _Jv_Throw, but fill in the stack trace first. Although
104 // this is declared extern in order that its name not be mangled, it
105 // is not intended to be used outside this file.
107 _Jv_ThrowSignal (jthrowable throwable
)
109 throwable
->fillInStackTrace ();
114 static java::lang::NullPointerException
*nullp
;
116 SIGNAL_HANDLER (catch_segv
)
118 MAKE_THROW_FRAME (nullp
);
119 _Jv_ThrowSignal (nullp
);
123 static java::lang::ArithmeticException
*arithexception
;
126 SIGNAL_HANDLER (catch_fpe
)
128 #ifdef HANDLE_DIVIDE_OVERFLOW
129 HANDLE_DIVIDE_OVERFLOW
;
131 MAKE_THROW_FRAME (arithexception
);
133 _Jv_ThrowSignal (arithexception
);
140 _Jv_equalUtf8Consts (Utf8Const
* a
, Utf8Const
*b
)
143 _Jv_ushort
*aptr
, *bptr
;
146 if (a
->hash
!= b
->hash
)
149 if (b
->length
!= len
)
151 aptr
= (_Jv_ushort
*)a
->data
;
152 bptr
= (_Jv_ushort
*)b
->data
;
153 len
= (len
+ 1) >> 1;
155 if (*aptr
++ != *bptr
++)
160 /* True iff A is equal to STR.
161 HASH is STR->hashCode().
165 _Jv_equal (Utf8Const
* a
, jstring str
, jint hash
)
167 if (a
->hash
!= (_Jv_ushort
) hash
)
169 jint len
= str
->length();
171 jchar
*sptr
= _Jv_GetStringChars (str
);
172 unsigned char* ptr
= (unsigned char*) a
->data
;
173 unsigned char* limit
= ptr
+ a
->length
;
176 int ch
= UTF8_GET (ptr
, limit
);
185 /* Like _Jv_equal, but stop after N characters. */
187 _Jv_equaln (Utf8Const
*a
, jstring str
, jint n
)
189 jint len
= str
->length();
191 jchar
*sptr
= _Jv_GetStringChars (str
);
192 unsigned char* ptr
= (unsigned char*) a
->data
;
193 unsigned char* limit
= ptr
+ a
->length
;
194 for (; n
-- > 0; i
++, sptr
++)
196 int ch
= UTF8_GET (ptr
, limit
);
205 /* Count the number of Unicode chars encoded in a given Ut8 string. */
207 _Jv_strLengthUtf8(char* str
, int len
)
210 unsigned char* limit
;
213 ptr
= (unsigned char*) str
;
216 for (; ptr
< limit
; str_length
++)
218 if (UTF8_GET (ptr
, limit
) < 0)
224 /* Calculate a hash value for a string encoded in Utf8 format.
225 * This returns the same hash value as specified or java.lang.String.hashCode.
228 hashUtf8String (char* str
, int len
)
230 unsigned char* ptr
= (unsigned char*) str
;
231 unsigned char* limit
= ptr
+ len
;
236 int ch
= UTF8_GET (ptr
, limit
);
237 /* Updated specification from
238 http://www.javasoft.com/docs/books/jls/clarify.html. */
239 hash
= (31 * hash
) + ch
;
245 _Jv_makeUtf8Const (char* s
, int len
)
249 Utf8Const
* m
= (Utf8Const
*) _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
250 memcpy (m
->data
, s
, len
);
253 m
->hash
= hashUtf8String (s
, len
) & 0xFFFF;
258 _Jv_makeUtf8Const (jstring string
)
260 jint hash
= string
->hashCode ();
261 jint len
= _Jv_GetStringUTFLength (string
);
263 Utf8Const
* m
= (Utf8Const
*)
264 _Jv_AllocBytes (sizeof(Utf8Const
) + len
+ 1);
269 _Jv_GetStringUTFRegion (string
, 0, string
->length (), m
->data
);
279 _Jv_Abort (const char *function
, const char *file
, int line
,
283 _Jv_Abort (const char *, const char *, int, const char *message
)
288 "libgcj failure: %s\n in function %s, file %s, line %d\n",
289 message
, function
, file
, line
);
291 fprintf (stderr
, "libgcj failure: %s\n", message
);
297 fail_on_finalization (jobject
)
299 JvFail ("object was finalized");
303 _Jv_GCWatch (jobject obj
)
305 _Jv_RegisterFinalizer (obj
, fail_on_finalization
);
309 _Jv_ThrowBadArrayIndex(jint bad_index
)
311 throw new java::lang::ArrayIndexOutOfBoundsException
312 (java::lang::String::valueOf (bad_index
));
316 _Jv_ThrowNullPointerException ()
318 throw new java::lang::NullPointerException
;
321 // Explicitly throw a no memory exception.
322 // The collector calls this when it encounters an out-of-memory condition.
323 void _Jv_ThrowNoMemory()
330 jvmpi_notify_alloc(jclass klass
, jint size
, jobject obj
)
332 // Service JVMPI allocation request.
333 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC
!= 0, false))
337 event
.event_type
= JVMPI_EVENT_OBJECT_ALLOC
;
339 event
.u
.obj_alloc
.arena_id
= 0;
340 event
.u
.obj_alloc
.class_id
= (jobjectID
) klass
;
341 event
.u
.obj_alloc
.is_array
= 0;
342 event
.u
.obj_alloc
.size
= size
;
343 event
.u
.obj_alloc
.obj_id
= (jobjectID
) obj
;
345 // FIXME: This doesn't look right for the Boehm GC. A GC may
346 // already be in progress. _Jv_DisableGC () doesn't wait for it.
347 // More importantly, I don't see the need for disabling GC, since we
348 // blatantly have a pointer to obj on our stack, ensuring that the
349 // object can't be collected. Even for a nonconservative collector,
350 // it appears to me that this must be true, since we are about to
351 // return obj. Isn't this whole approach way too intrusive for
352 // a useful profiling interface? - HB
354 (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (&event
);
358 #else /* !ENABLE_JVMPI */
359 # define jvmpi_notify_alloc(klass,size,obj) /* do nothing */
362 // Allocate a new object of class KLASS. SIZE is the size of the object
363 // to allocate. You might think this is redundant, but it isn't; some
364 // classes, such as String, aren't of fixed size.
365 // First a version that assumes that we have no finalizer, and that
366 // the class is already initialized.
367 // If we know that JVMPI is disabled, this can be replaced by a direct call
368 // to the allocator for the appropriate GC.
370 _Jv_AllocObjectNoInitNoFinalizer (jclass klass
, jint size
)
372 jobject obj
= (jobject
) _Jv_AllocObj (size
, klass
);
373 jvmpi_notify_alloc (klass
, size
, obj
);
377 // And now a version that initializes if necessary.
379 _Jv_AllocObjectNoFinalizer (jclass klass
, jint size
)
381 _Jv_InitClass (klass
);
382 jobject obj
= (jobject
) _Jv_AllocObj (size
, klass
);
383 jvmpi_notify_alloc (klass
, size
, obj
);
387 // And now the general version that registers a finalizer if necessary.
389 _Jv_AllocObject (jclass klass
, jint size
)
391 jobject obj
= _Jv_AllocObjectNoFinalizer (klass
, size
);
393 // We assume that the compiler only generates calls to this routine
394 // if there really is an interesting finalizer.
395 // Unfortunately, we still have to the dynamic test, since there may
396 // be cni calls to this routine.
397 // Note that on IA64 get_finalizer() returns the starting address of the
398 // function, not a function pointer. Thus this still works.
399 if (klass
->vtable
->get_finalizer ()
400 != java::lang::Object::class$
.vtable
->get_finalizer ())
401 _Jv_RegisterFinalizer (obj
, _Jv_FinalizeObject
);
405 // A version of the above that assumes the object contains no pointers,
406 // and requires no finalization. This can't happen if we need pointers
408 #ifdef JV_HASH_SYNCHRONIZATION
410 _Jv_AllocPtrFreeObject (jclass klass
, jint size
)
412 _Jv_InitClass (klass
);
414 jobject obj
= (jobject
) _Jv_AllocPtrFreeObj (size
, klass
);
417 // Service JVMPI request.
419 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC
!= 0, false))
423 event
.event_type
= JVMPI_EVENT_OBJECT_ALLOC
;
425 event
.u
.obj_alloc
.arena_id
= 0;
426 event
.u
.obj_alloc
.class_id
= (jobjectID
) klass
;
427 event
.u
.obj_alloc
.is_array
= 0;
428 event
.u
.obj_alloc
.size
= size
;
429 event
.u
.obj_alloc
.obj_id
= (jobjectID
) obj
;
432 (*_Jv_JVMPI_Notify_OBJECT_ALLOC
) (&event
);
439 #endif /* JV_HASH_SYNCHRONIZATION */
442 // Allocate a new array of Java objects. Each object is of type
443 // `elementClass'. `init' is used to initialize each slot in the
446 _Jv_NewObjectArray (jsize count
, jclass elementClass
, jobject init
)
448 if (__builtin_expect (count
< 0, false))
449 throw new java::lang::NegativeArraySizeException
;
451 JvAssert (! elementClass
->isPrimitive ());
453 // Ensure that elements pointer is properly aligned.
454 jobjectArray obj
= NULL
;
455 size_t size
= (size_t) elements (obj
);
456 size
+= count
* sizeof (jobject
);
458 jclass klass
= _Jv_GetArrayClass (elementClass
,
459 elementClass
->getClassLoaderInternal());
461 obj
= (jobjectArray
) _Jv_AllocArray (size
, klass
);
463 jsize
*lp
= const_cast<jsize
*> (&obj
->length
);
465 // We know the allocator returns zeroed memory. So don't bother
469 jobject
*ptr
= elements(obj
);
476 // Allocate a new array of primitives. ELTYPE is the type of the
477 // element, COUNT is the size of the array.
479 _Jv_NewPrimArray (jclass eltype
, jint count
)
481 int elsize
= eltype
->size();
482 if (__builtin_expect (count
< 0, false))
483 throw new java::lang::NegativeArraySizeException
;
485 JvAssert (eltype
->isPrimitive ());
486 jobject dummy
= NULL
;
487 size_t size
= (size_t) _Jv_GetArrayElementFromElementType (dummy
, eltype
);
489 // Check for overflow.
490 if (__builtin_expect ((size_t) count
>
491 (SIZE_T_MAX
- size
) / elsize
, false))
494 jclass klass
= _Jv_GetArrayClass (eltype
, 0);
496 # ifdef JV_HASH_SYNCHRONIZATION
497 // Since the vtable is always statically allocated,
498 // these are completely pointerfree! Make sure the GC doesn't touch them.
500 (__JArray
*) _Jv_AllocPtrFreeObj (size
+ elsize
* count
, klass
);
501 memset((char *)arr
+ size
, 0, elsize
* count
);
503 __JArray
*arr
= (__JArray
*) _Jv_AllocObj (size
+ elsize
* count
, klass
);
504 // Note that we assume we are given zeroed memory by the allocator.
507 jsize
*lp
= const_cast<jsize
*> (&arr
->length
);
514 _Jv_NewArray (jint type
, jint size
)
518 case 4: return JvNewBooleanArray (size
);
519 case 5: return JvNewCharArray (size
);
520 case 6: return JvNewFloatArray (size
);
521 case 7: return JvNewDoubleArray (size
);
522 case 8: return JvNewByteArray (size
);
523 case 9: return JvNewShortArray (size
);
524 case 10: return JvNewIntArray (size
);
525 case 11: return JvNewLongArray (size
);
527 throw new java::lang::InternalError
528 (JvNewStringLatin1 ("invalid type code in _Jv_NewArray"));
531 // Allocate a possibly multi-dimensional array but don't check that
532 // any array length is <0.
534 _Jv_NewMultiArrayUnchecked (jclass type
, jint dimensions
, jint
*sizes
)
536 JvAssert (type
->isArray());
537 jclass element_type
= type
->getComponentType();
539 if (element_type
->isPrimitive())
540 result
= _Jv_NewPrimArray (element_type
, sizes
[0]);
542 result
= _Jv_NewObjectArray (sizes
[0], element_type
, NULL
);
546 JvAssert (! element_type
->isPrimitive());
547 JvAssert (element_type
->isArray());
548 jobject
*contents
= elements ((jobjectArray
) result
);
549 for (int i
= 0; i
< sizes
[0]; ++i
)
550 contents
[i
] = _Jv_NewMultiArrayUnchecked (element_type
, dimensions
- 1,
558 _Jv_NewMultiArray (jclass type
, jint dimensions
, jint
*sizes
)
560 for (int i
= 0; i
< dimensions
; ++i
)
562 throw new java::lang::NegativeArraySizeException
;
564 return _Jv_NewMultiArrayUnchecked (type
, dimensions
, sizes
);
568 _Jv_NewMultiArray (jclass array_type
, jint dimensions
, ...)
571 jint sizes
[dimensions
];
572 va_start (args
, dimensions
);
573 for (int i
= 0; i
< dimensions
; ++i
)
575 jint size
= va_arg (args
, jint
);
577 throw new java::lang::NegativeArraySizeException
;
582 return _Jv_NewMultiArrayUnchecked (array_type
, dimensions
, sizes
);
587 // Ensure 8-byte alignment, for hash synchronization.
588 #define DECLARE_PRIM_TYPE(NAME) \
589 _Jv_ArrayVTable _Jv_##NAME##VTable; \
590 java::lang::Class _Jv_##NAME##Class __attribute__ ((aligned (8)));
592 DECLARE_PRIM_TYPE(byte
)
593 DECLARE_PRIM_TYPE(short)
594 DECLARE_PRIM_TYPE(int)
595 DECLARE_PRIM_TYPE(long)
596 DECLARE_PRIM_TYPE(boolean
)
597 DECLARE_PRIM_TYPE(char)
598 DECLARE_PRIM_TYPE(float)
599 DECLARE_PRIM_TYPE(double)
600 DECLARE_PRIM_TYPE(void)
603 _Jv_InitPrimClass (jclass cl
, char *cname
, char sig
, int len
,
604 _Jv_ArrayVTable
*array_vtable
)
606 using namespace java::lang::reflect
;
608 _Jv_InitNewClassFields (cl
);
610 // We must set the vtable for the class; the Java constructor
612 (*(_Jv_VTable
**) cl
) = java::lang::Class::class$
.vtable
;
614 // Initialize the fields we care about. We do this in the same
615 // order they are declared in Class.h.
616 cl
->name
= _Jv_makeUtf8Const ((char *) cname
, -1);
617 cl
->accflags
= Modifier::PUBLIC
| Modifier::FINAL
| Modifier::ABSTRACT
;
618 cl
->method_count
= sig
;
619 cl
->size_in_bytes
= len
;
620 cl
->vtable
= JV_PRIMITIVE_VTABLE
;
621 cl
->state
= JV_STATE_DONE
;
624 _Jv_NewArrayClass (cl
, NULL
, (_Jv_VTable
*) array_vtable
);
628 _Jv_FindClassFromSignature (char *sig
, java::lang::ClassLoader
*loader
)
633 return JvPrimClass (byte
);
635 return JvPrimClass (short);
637 return JvPrimClass (int);
639 return JvPrimClass (long);
641 return JvPrimClass (boolean
);
643 return JvPrimClass (char);
645 return JvPrimClass (float);
647 return JvPrimClass (double);
649 return JvPrimClass (void);
653 for (i
= 1; sig
[i
] && sig
[i
] != ';'; ++i
)
655 _Jv_Utf8Const
*name
= _Jv_makeUtf8Const (&sig
[1], i
- 1);
656 return _Jv_FindClass (name
, loader
);
660 jclass klass
= _Jv_FindClassFromSignature (&sig
[1], loader
);
663 return _Jv_GetArrayClass (klass
, loader
);
667 return NULL
; // Placate compiler.
673 JvConvertArgv (int argc
, const char **argv
)
677 jobjectArray ar
= JvNewObjectArray(argc
, &StringClass
, NULL
);
678 jobject
*ptr
= elements(ar
);
679 jbyteArray bytes
= NULL
;
680 for (int i
= 0; i
< argc
; i
++)
682 const char *arg
= argv
[i
];
683 int len
= strlen (arg
);
684 if (bytes
== NULL
|| bytes
->length
< len
)
685 bytes
= JvNewByteArray (len
);
686 jbyte
*bytePtr
= elements (bytes
);
687 // We assume jbyte == char.
688 memcpy (bytePtr
, arg
, len
);
690 // Now convert using the default encoding.
691 *ptr
++ = new java::lang::String (bytes
, 0, len
);
693 return (JArray
<jstring
>*) ar
;
696 // FIXME: These variables are static so that they will be
697 // automatically scanned by the Boehm collector. This is needed
698 // because with qthreads the collector won't scan the initial stack --
699 // it will only scan the qthreads stacks.
701 // Command line arguments.
702 static JArray
<jstring
> *arg_vec
;
704 // The primary thread.
705 static java::lang::Thread
*main_thread
;
707 #ifndef DISABLE_GETENV_PROPERTIES
710 next_property_key (char *s
, size_t *length
)
716 // Skip over whitespace
720 // If we've reached the end, return NULL. Also return NULL if for
721 // some reason we've come across a malformed property string.
727 // Determine the length of the property key.
745 next_property_value (char *s
, size_t *length
)
761 // If we've reached the end, return NULL.
765 // Determine the length of the property value.
784 process_gcj_properties ()
786 char *props
= getenv("GCJ_PROPERTIES");
789 size_t property_count
= 0;
794 // Whip through props quickly in order to count the number of
796 while (p
&& (p
= next_property_key (p
, &length
)))
798 // Skip to the end of the key
801 p
= next_property_value (p
, &length
);
808 // Allocate an array of property value/key pairs.
809 _Jv_Environment_Properties
=
810 (property_pair
*) malloc (sizeof(property_pair
)
811 * (property_count
+ 1));
813 // Go through the properties again, initializing _Jv_Properties
817 while (p
&& (p
= next_property_key (p
, &length
)))
819 _Jv_Environment_Properties
[property_count
].key
= p
;
820 _Jv_Environment_Properties
[property_count
].key_length
= length
;
822 // Skip to the end of the key
825 p
= next_property_value (p
, &length
);
827 _Jv_Environment_Properties
[property_count
].value
= p
;
828 _Jv_Environment_Properties
[property_count
].value_length
= length
;
835 memset ((void *) &_Jv_Environment_Properties
[property_count
],
836 0, sizeof (property_pair
));
840 // Null terminate the strings.
841 while (_Jv_Environment_Properties
[i
].key
)
843 _Jv_Environment_Properties
[i
].key
[_Jv_Environment_Properties
[i
].key_length
] = 0;
844 _Jv_Environment_Properties
[i
++].value
[_Jv_Environment_Properties
[i
].value_length
] = 0;
848 #endif // DISABLE_GETENV_PROPERTIES
852 _Jv_Utf8Const
*void_signature
;
853 _Jv_Utf8Const
*clinit_name
;
854 _Jv_Utf8Const
*init_name
;
855 _Jv_Utf8Const
*finit_name
;
857 bool runtimeInitialized
= false;
861 _Jv_CreateJavaVM (void* /*vm_args*/)
865 if (runtimeInitialized
)
868 runtimeInitialized
= true;
870 PROCESS_GCJ_PROPERTIES
;
874 _Jv_InitializeSyncMutex ();
876 /* Initialize Utf8 constants declared in jvm.h. */
877 void_signature
= _Jv_makeUtf8Const ("()V", 3);
878 clinit_name
= _Jv_makeUtf8Const ("<clinit>", 8);
879 init_name
= _Jv_makeUtf8Const ("<init>", 6);
880 finit_name
= _Jv_makeUtf8Const ("finit$", 6);
882 /* Initialize built-in classes to represent primitive TYPEs. */
883 _Jv_InitPrimClass (&_Jv_byteClass
, "byte", 'B', 1, &_Jv_byteVTable
);
884 _Jv_InitPrimClass (&_Jv_shortClass
, "short", 'S', 2, &_Jv_shortVTable
);
885 _Jv_InitPrimClass (&_Jv_intClass
, "int", 'I', 4, &_Jv_intVTable
);
886 _Jv_InitPrimClass (&_Jv_longClass
, "long", 'J', 8, &_Jv_longVTable
);
887 _Jv_InitPrimClass (&_Jv_booleanClass
, "boolean", 'Z', 1, &_Jv_booleanVTable
);
888 _Jv_InitPrimClass (&_Jv_charClass
, "char", 'C', 2, &_Jv_charVTable
);
889 _Jv_InitPrimClass (&_Jv_floatClass
, "float", 'F', 4, &_Jv_floatVTable
);
890 _Jv_InitPrimClass (&_Jv_doubleClass
, "double", 'D', 8, &_Jv_doubleVTable
);
891 _Jv_InitPrimClass (&_Jv_voidClass
, "void", 'V', 0, &_Jv_voidVTable
);
893 // Turn stack trace generation off while creating exception objects.
894 _Jv_InitClass (&java::lang::VMThrowable::class$
);
895 java::lang::VMThrowable::trace_enabled
= 0;
901 arithexception
= new java::lang::ArithmeticException
902 (JvNewStringLatin1 ("/ by zero"));
905 no_memory
= new java::lang::OutOfMemoryError
;
907 java::lang::VMThrowable::trace_enabled
= 1;
910 LTDL_SET_PRELOADED_SYMBOLS ();
913 _Jv_platform_initialize ();
917 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady
);
919 // Start the GC finalizer thread. A VirtualMachineError can be
920 // thrown by the runtime if, say, threads aren't available. In this
921 // case finalizers simply won't run.
924 using namespace gnu::gcj::runtime
;
925 FinalizerThread
*ft
= new FinalizerThread ();
928 catch (java::lang::VirtualMachineError
*ignore
)
936 _Jv_RunMain (jclass klass
, const char *name
, int argc
, const char **argv
,
942 java::lang::Runtime
*runtime
= NULL
;
946 // Set this very early so that it is seen when java.lang.System
949 _Jv_Jar_Class_Path
= strdup (name
);
950 _Jv_CreateJavaVM (NULL
);
952 // Get the Runtime here. We want to initialize it before searching
953 // for `main'; that way it will be set up if `main' is a JNI method.
954 runtime
= java::lang::Runtime::getRuntime ();
956 #ifdef DISABLE_MAIN_ARGS
957 arg_vec
= JvConvertArgv (0, 0);
959 arg_vec
= JvConvertArgv (argc
- 1, argv
+ 1);
962 using namespace gnu::gcj::runtime
;
964 main_thread
= new FirstThread (klass
, arg_vec
);
966 main_thread
= new FirstThread (JvNewStringLatin1 (name
),
969 catch (java::lang::Throwable
*t
)
971 java::lang::System::err
->println (JvNewStringLatin1
972 ("Exception during runtime initialization"));
973 t
->printStackTrace();
977 _Jv_AttachCurrentThread (main_thread
);
978 _Jv_ThreadRun (main_thread
);
981 int status
= (int) java::lang::ThreadGroup::had_uncaught_exception
;
982 runtime
->exit (status
);
986 JvRunMain (jclass klass
, int argc
, const char **argv
)
988 _Jv_RunMain (klass
, NULL
, argc
, argv
, false);
993 // Parse a string and return a heap size.
995 parse_heap_size (const char *spec
)
998 unsigned long val
= strtoul (spec
, &end
, 10);
999 if (*end
== 'k' || *end
== 'K')
1001 else if (*end
== 'm' || *end
== 'M')
1003 return (size_t) val
;
1006 // Set the initial heap size. This might be ignored by the GC layer.
1007 // This must be called before _Jv_RunMain.
1009 _Jv_SetInitialHeapSize (const char *arg
)
1011 size_t size
= parse_heap_size (arg
);
1012 _Jv_GCSetInitialHeapSize (size
);
1015 // Set the maximum heap size. This might be ignored by the GC layer.
1016 // This must be called before _Jv_RunMain.
1018 _Jv_SetMaximumHeapSize (const char *arg
)
1020 size_t size
= parse_heap_size (arg
);
1021 _Jv_GCSetMaximumHeapSize (size
);
1027 _Jv_Malloc (jsize size
)
1029 if (__builtin_expect (size
== 0, false))
1031 void *ptr
= malloc ((size_t) size
);
1032 if (__builtin_expect (ptr
== NULL
, false))
1038 _Jv_Realloc (void *ptr
, jsize size
)
1040 if (__builtin_expect (size
== 0, false))
1042 ptr
= realloc (ptr
, (size_t) size
);
1043 if (__builtin_expect (ptr
== NULL
, false))
1049 _Jv_MallocUnchecked (jsize size
)
1051 if (__builtin_expect (size
== 0, false))
1053 return malloc ((size_t) size
);
1057 _Jv_Free (void* ptr
)
1064 // In theory, these routines can be #ifdef'd away on machines which
1065 // support divide overflow signals. However, we never know if some
1066 // code might have been compiled with "-fuse-divide-subroutine", so we
1067 // always include them in libgcj.
1070 _Jv_divI (jint dividend
, jint divisor
)
1072 if (__builtin_expect (divisor
== 0, false))
1073 _Jv_ThrowSignal (arithexception
);
1075 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
1078 return dividend
/ divisor
;
1082 _Jv_remI (jint dividend
, jint divisor
)
1084 if (__builtin_expect (divisor
== 0, false))
1085 _Jv_ThrowSignal (arithexception
);
1087 if (dividend
== (jint
) 0x80000000L
&& divisor
== -1)
1090 return dividend
% divisor
;
1094 _Jv_divJ (jlong dividend
, jlong divisor
)
1096 if (__builtin_expect (divisor
== 0, false))
1097 _Jv_ThrowSignal (arithexception
);
1099 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
1102 return dividend
/ divisor
;
1106 _Jv_remJ (jlong dividend
, jlong divisor
)
1108 if (__builtin_expect (divisor
== 0, false))
1109 _Jv_ThrowSignal (arithexception
);
1111 if (dividend
== (jlong
) 0x8000000000000000LL
&& divisor
== -1)
1114 return dividend
% divisor
;