1 // prims.cc - Code for core of runtime environment.
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
13 #ifdef USE_WIN32_SIGNALLING
15 #endif /* USE_WIN32_SIGNALLING */
18 #undef __INSIDE_CYGWIN__
20 #endif /* USE_WINSOCK */
34 #include <java-signal.h>
35 #include <java-threads.h>
39 #include <java/lang/ThreadGroup.h>
42 #ifndef DISABLE_GETENV_PROPERTIES
44 #include <java-props.h>
45 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
47 #define PROCESS_GCJ_PROPERTIES
48 #endif // DISABLE_GETENV_PROPERTIES
50 #include <java/lang/Class.h>
51 #include <java/lang/ClassLoader.h>
52 #include <java/lang/Runtime.h>
53 #include <java/lang/String.h>
54 #include <java/lang/Thread.h>
55 #include <java/lang/ThreadGroup.h>
56 #include <java/lang/ArrayIndexOutOfBoundsException.h>
57 #include <java/lang/ArithmeticException.h>
58 #include <java/lang/ClassFormatError.h>
59 #include <java/lang/NegativeArraySizeException.h>
60 #include <java/lang/NullPointerException.h>
61 #include <java/lang/OutOfMemoryError.h>
62 #include <java/lang/System.h>
63 #include <java/lang/reflect/Modifier.h>
64 #include <java/io/PrintStream.h>
65 #include <java/lang/UnsatisfiedLinkError.h>
66 #include <java/lang/VirtualMachineError.h>
67 #include <gnu/gcj/runtime/VMClassLoader.h>
68 #include <gnu/gcj/runtime/FinalizerThread.h>
69 #include <gnu/gcj/runtime/FirstThread.h>
75 // We allocate a single OutOfMemoryError exception which we keep
76 // around for use if we run out of memory.
77 static java::lang::OutOfMemoryError *no_memory;
79 // Largest representable size_t.
80 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
82 static const char *no_properties[] = { NULL };
84 // Properties set at compile time.
85 const char **_Jv_Compiler_Properties = no_properties;
87 // The JAR file to add to the beginning of java.class.path.
88 const char *_Jv_Jar_Class_Path;
90 #ifndef DISABLE_GETENV_PROPERTIES
91 // Property key/value pairs.
92 property_pair *_Jv_Environment_Properties;
95 // The name of this executable.
96 static char *_Jv_execName;
98 // Stash the argv pointer to benefit native libraries that need it.
99 const char **_Jv_argv;
103 // Pointer to JVMPI notification functions.
104 void (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (JVMPI_Event *event);
105 void (*_Jv_JVMPI_Notify_THREAD_START) (JVMPI_Event *event);
106 void (*_Jv_JVMPI_Notify_THREAD_END) (JVMPI_Event *event);
110 extern "C" void _Jv_ThrowSignal (jthrowable) __attribute ((noreturn));
112 // Just like _Jv_Throw, but fill in the stack trace first. Although
113 // this is declared extern in order that its name not be mangled, it
114 // is not intended to be used outside this file.
116 _Jv_ThrowSignal (jthrowable throwable)
118 throwable->fillInStackTrace ();
123 static java::lang::NullPointerException *nullp;
125 SIGNAL_HANDLER (catch_segv)
127 MAKE_THROW_FRAME (nullp);
128 _Jv_ThrowSignal (nullp);
132 static java::lang::ArithmeticException *arithexception;
135 SIGNAL_HANDLER (catch_fpe)
137 #ifdef HANDLE_DIVIDE_OVERFLOW
138 HANDLE_DIVIDE_OVERFLOW;
140 MAKE_THROW_FRAME (arithexception);
142 _Jv_ThrowSignal (arithexception);
149 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
152 _Jv_ushort *aptr, *bptr;
155 if (a->hash != b->hash)
158 if (b->length != len)
160 aptr = (_Jv_ushort *)a->data;
161 bptr = (_Jv_ushort *)b->data;
162 len = (len + 1) >> 1;
164 if (*aptr++ != *bptr++)
169 /* True iff A is equal to STR.
170 HASH is STR->hashCode().
174 _Jv_equal (Utf8Const* a, jstring str, jint hash)
176 if (a->hash != (_Jv_ushort) hash)
178 jint len = str->length();
180 jchar *sptr = _Jv_GetStringChars (str);
181 unsigned char* ptr = (unsigned char*) a->data;
182 unsigned char* limit = ptr + a->length;
185 int ch = UTF8_GET (ptr, limit);
194 /* Like _Jv_equal, but stop after N characters. */
196 _Jv_equaln (Utf8Const *a, jstring str, jint n)
198 jint len = str->length();
200 jchar *sptr = _Jv_GetStringChars (str);
201 unsigned char* ptr = (unsigned char*) a->data;
202 unsigned char* limit = ptr + a->length;
203 for (; n-- > 0; i++, sptr++)
205 int ch = UTF8_GET (ptr, limit);
214 /* Count the number of Unicode chars encoded in a given Ut8 string. */
216 _Jv_strLengthUtf8(char* str, int len)
219 unsigned char* limit;
222 ptr = (unsigned char*) str;
225 for (; ptr < limit; str_length++)
227 if (UTF8_GET (ptr, limit) < 0)
233 /* Calculate a hash value for a string encoded in Utf8 format.
234 * This returns the same hash value as specified or java.lang.String.hashCode.
237 hashUtf8String (char* str, int len)
239 unsigned char* ptr = (unsigned char*) str;
240 unsigned char* limit = ptr + len;
245 int ch = UTF8_GET (ptr, limit);
246 /* Updated specification from
247 http://www.javasoft.com/docs/books/jls/clarify.html. */
248 hash = (31 * hash) + ch;
254 _Jv_makeUtf8Const (char* s, int len)
258 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
259 memcpy (m->data, s, len);
262 m->hash = hashUtf8String (s, len) & 0xFFFF;
267 _Jv_makeUtf8Const (jstring string)
269 jint hash = string->hashCode ();
270 jint len = _Jv_GetStringUTFLength (string);
272 Utf8Const* m = (Utf8Const*)
273 _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
278 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
288 _Jv_Abort (const char *function, const char *file, int line,
292 _Jv_Abort (const char *, const char *, int, const char *message)
297 "libgcj failure: %s\n in function %s, file %s, line %d\n",
298 message, function, file, line);
300 fprintf (stderr, "libgcj failure: %s\n", message);
306 fail_on_finalization (jobject)
308 JvFail ("object was finalized");
312 _Jv_GCWatch (jobject obj)
314 _Jv_RegisterFinalizer (obj, fail_on_finalization);
318 _Jv_ThrowBadArrayIndex(jint bad_index)
320 throw new java::lang::ArrayIndexOutOfBoundsException
321 (java::lang::String::valueOf (bad_index));
325 _Jv_ThrowNullPointerException ()
327 throw new java::lang::NullPointerException;
330 // Explicitly throw a no memory exception.
331 // The collector calls this when it encounters an out-of-memory condition.
332 void _Jv_ThrowNoMemory()
339 jvmpi_notify_alloc(jclass klass, jint size, jobject obj)
341 // Service JVMPI allocation request.
342 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
346 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
348 event.u.obj_alloc.arena_id = 0;
349 event.u.obj_alloc.class_id = (jobjectID) klass;
350 event.u.obj_alloc.is_array = 0;
351 event.u.obj_alloc.size = size;
352 event.u.obj_alloc.obj_id = (jobjectID) obj;
354 // FIXME: This doesn't look right for the Boehm GC. A GC may
355 // already be in progress. _Jv_DisableGC () doesn't wait for it.
356 // More importantly, I don't see the need for disabling GC, since we
357 // blatantly have a pointer to obj on our stack, ensuring that the
358 // object can't be collected. Even for a nonconservative collector,
359 // it appears to me that this must be true, since we are about to
360 // return obj. Isn't this whole approach way too intrusive for
361 // a useful profiling interface? - HB
363 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
367 #else /* !ENABLE_JVMPI */
368 # define jvmpi_notify_alloc(klass,size,obj) /* do nothing */
371 // Allocate a new object of class KLASS. SIZE is the size of the object
372 // to allocate. You might think this is redundant, but it isn't; some
373 // classes, such as String, aren't of fixed size.
374 // First a version that assumes that we have no finalizer, and that
375 // the class is already initialized.
376 // If we know that JVMPI is disabled, this can be replaced by a direct call
377 // to the allocator for the appropriate GC.
379 _Jv_AllocObjectNoInitNoFinalizer (jclass klass, jint size)
381 jobject obj = (jobject) _Jv_AllocObj (size, klass);
382 jvmpi_notify_alloc (klass, size, obj);
386 // And now a version that initializes if necessary.
388 _Jv_AllocObjectNoFinalizer (jclass klass, jint size)
390 _Jv_InitClass (klass);
391 jobject obj = (jobject) _Jv_AllocObj (size, klass);
392 jvmpi_notify_alloc (klass, size, obj);
396 // And now the general version that registers a finalizer if necessary.
398 _Jv_AllocObject (jclass klass, jint size)
400 jobject obj = _Jv_AllocObjectNoFinalizer (klass, size);
402 // We assume that the compiler only generates calls to this routine
403 // if there really is an interesting finalizer.
404 // Unfortunately, we still have to the dynamic test, since there may
405 // be cni calls to this routine.
406 // Nore that on IA64 get_finalizer() returns the starting address of the
407 // function, not a function pointer. Thus this still works.
408 if (klass->vtable->get_finalizer ()
409 != java::lang::Object::class$.vtable->get_finalizer ())
410 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
414 // A version of the above that assumes the object contains no pointers,
415 // and requires no finalization. This can't happen if we need pointers
417 #ifdef JV_HASH_SYNCHRONIZATION
419 _Jv_AllocPtrFreeObject (jclass klass, jint size)
421 _Jv_InitClass (klass);
423 jobject obj = (jobject) _Jv_AllocPtrFreeObj (size, klass);
426 // Service JVMPI request.
428 if (__builtin_expect (_Jv_JVMPI_Notify_OBJECT_ALLOC != 0, false))
432 event.event_type = JVMPI_EVENT_OBJECT_ALLOC;
434 event.u.obj_alloc.arena_id = 0;
435 event.u.obj_alloc.class_id = (jobjectID) klass;
436 event.u.obj_alloc.is_array = 0;
437 event.u.obj_alloc.size = size;
438 event.u.obj_alloc.obj_id = (jobjectID) obj;
441 (*_Jv_JVMPI_Notify_OBJECT_ALLOC) (&event);
448 #endif /* JV_HASH_SYNCHRONIZATION */
451 // Allocate a new array of Java objects. Each object is of type
452 // `elementClass'. `init' is used to initialize each slot in the
455 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
457 if (__builtin_expect (count < 0, false))
458 throw new java::lang::NegativeArraySizeException;
460 JvAssert (! elementClass->isPrimitive ());
462 // Ensure that elements pointer is properly aligned.
463 jobjectArray obj = NULL;
464 size_t size = (size_t) elements (obj);
465 size += count * sizeof (jobject);
467 // FIXME: second argument should be "current loader"
468 jclass klass = _Jv_GetArrayClass (elementClass, 0);
470 obj = (jobjectArray) _Jv_AllocArray (size, klass);
472 jsize *lp = const_cast<jsize *> (&obj->length);
474 // We know the allocator returns zeroed memory. So don't bother
478 jobject *ptr = elements(obj);
485 // Allocate a new array of primitives. ELTYPE is the type of the
486 // element, COUNT is the size of the array.
488 _Jv_NewPrimArray (jclass eltype, jint count)
490 int elsize = eltype->size();
491 if (__builtin_expect (count < 0, false))
492 throw new java::lang::NegativeArraySizeException;
494 JvAssert (eltype->isPrimitive ());
495 jobject dummy = NULL;
496 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
498 // Check for overflow.
499 if (__builtin_expect ((size_t) count >
500 (SIZE_T_MAX - size) / elsize, false))
503 jclass klass = _Jv_GetArrayClass (eltype, 0);
505 # ifdef JV_HASH_SYNCHRONIZATION
506 // Since the vtable is always statically allocated,
507 // these are completely pointerfree! Make sure the GC doesn't touch them.
509 (__JArray*) _Jv_AllocPtrFreeObj (size + elsize * count, klass);
510 memset((char *)arr + size, 0, elsize * count);
512 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count, klass);
513 // Note that we assume we are given zeroed memory by the allocator.
516 jsize *lp = const_cast<jsize *> (&arr->length);
523 _Jv_NewArray (jint type, jint size)
527 case 4: return JvNewBooleanArray (size);
528 case 5: return JvNewCharArray (size);
529 case 6: return JvNewFloatArray (size);
530 case 7: return JvNewDoubleArray (size);
531 case 8: return JvNewByteArray (size);
532 case 9: return JvNewShortArray (size);
533 case 10: return JvNewIntArray (size);
534 case 11: return JvNewLongArray (size);
536 JvFail ("newarray - bad type code");
537 return NULL; // Placate compiler.
540 // Allocate a possibly multi-dimensional array but don't check that
541 // any array length is <0.
543 _Jv_NewMultiArrayUnchecked (jclass type, jint dimensions, jint *sizes)
545 JvAssert (type->isArray());
546 jclass element_type = type->getComponentType();
548 if (element_type->isPrimitive())
549 result = _Jv_NewPrimArray (element_type, sizes[0]);
551 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
555 JvAssert (! element_type->isPrimitive());
556 JvAssert (element_type->isArray());
557 jobject *contents = elements ((jobjectArray) result);
558 for (int i = 0; i < sizes[0]; ++i)
559 contents[i] = _Jv_NewMultiArrayUnchecked (element_type, dimensions - 1,
567 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
569 for (int i = 0; i < dimensions; ++i)
571 throw new java::lang::NegativeArraySizeException;
573 return _Jv_NewMultiArrayUnchecked (type, dimensions, sizes);
577 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
580 jint sizes[dimensions];
581 va_start (args, dimensions);
582 for (int i = 0; i < dimensions; ++i)
584 jint size = va_arg (args, jint);
586 throw new java::lang::NegativeArraySizeException;
591 return _Jv_NewMultiArrayUnchecked (array_type, dimensions, sizes);
596 #define DECLARE_PRIM_TYPE(NAME) \
597 _Jv_ArrayVTable _Jv_##NAME##VTable; \
598 java::lang::Class _Jv_##NAME##Class;
600 DECLARE_PRIM_TYPE(byte);
601 DECLARE_PRIM_TYPE(short);
602 DECLARE_PRIM_TYPE(int);
603 DECLARE_PRIM_TYPE(long);
604 DECLARE_PRIM_TYPE(boolean);
605 DECLARE_PRIM_TYPE(char);
606 DECLARE_PRIM_TYPE(float);
607 DECLARE_PRIM_TYPE(double);
608 DECLARE_PRIM_TYPE(void);
611 _Jv_InitPrimClass (jclass cl, char *cname, char sig, int len,
612 _Jv_ArrayVTable *array_vtable)
614 using namespace java::lang::reflect;
616 // We must initialize every field of the class. We do this in the
617 // same order they are declared in Class.h, except for fields that
618 // are initialized to NULL.
619 cl->name = _Jv_makeUtf8Const ((char *) cname, -1);
620 cl->accflags = Modifier::PUBLIC | Modifier::FINAL | Modifier::ABSTRACT;
621 cl->method_count = sig;
622 cl->size_in_bytes = len;
623 cl->vtable = JV_PRIMITIVE_VTABLE;
624 cl->state = JV_STATE_DONE;
627 _Jv_NewArrayClass (cl, NULL, (_Jv_VTable *) array_vtable);
631 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
636 return JvPrimClass (byte);
638 return JvPrimClass (short);
640 return JvPrimClass (int);
642 return JvPrimClass (long);
644 return JvPrimClass (boolean);
646 return JvPrimClass (char);
648 return JvPrimClass (float);
650 return JvPrimClass (double);
652 return JvPrimClass (void);
656 for (i = 1; sig[i] && sig[i] != ';'; ++i)
658 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
659 return _Jv_FindClass (name, loader);
664 jclass klass = _Jv_FindClassFromSignature (&sig[1], loader);
667 return _Jv_GetArrayClass (klass, loader);
671 return NULL; // Placate compiler.
677 JvConvertArgv (int argc, const char **argv)
681 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
682 jobject *ptr = elements(ar);
683 jbyteArray bytes = NULL;
684 for (int i = 0; i < argc; i++)
686 const char *arg = argv[i];
687 int len = strlen (arg);
688 if (bytes == NULL || bytes->length < len)
689 bytes = JvNewByteArray (len);
690 jbyte *bytePtr = elements (bytes);
691 // We assume jbyte == char.
692 memcpy (bytePtr, arg, len);
694 // Now convert using the default encoding.
695 *ptr++ = new java::lang::String (bytes, 0, len);
697 return (JArray<jstring>*) ar;
700 // FIXME: These variables are static so that they will be
701 // automatically scanned by the Boehm collector. This is needed
702 // because with qthreads the collector won't scan the initial stack --
703 // it will only scan the qthreads stacks.
705 // Command line arguments.
706 static JArray<jstring> *arg_vec;
708 // The primary thread.
709 static java::lang::Thread *main_thread;
712 _Jv_ThisExecutable (void)
718 _Jv_ThisExecutable (const char *name)
722 _Jv_execName = (char *) _Jv_Malloc (strlen (name) + 1);
723 strcpy (_Jv_execName, name);
727 #ifdef USE_WIN32_SIGNALLING
729 extern "C" int* win32_get_restart_frame (void *);
732 win32_exception_handler (LPEXCEPTION_POINTERS e)
735 if (e->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
736 setjmp_buf = win32_get_restart_frame (nullp);
737 else if (e->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO)
738 setjmp_buf = win32_get_restart_frame (arithexception);
740 return EXCEPTION_CONTINUE_SEARCH;
742 e->ContextRecord->Ebp = setjmp_buf[0];
743 // FIXME: Why does i386-signal.h increment the PC here, do we need to do it?
744 e->ContextRecord->Eip = setjmp_buf[1];
745 // FIXME: Is this the stack pointer? Do we need it?
746 e->ContextRecord->Esp = setjmp_buf[2];
748 return EXCEPTION_CONTINUE_EXECUTION;
753 #ifndef DISABLE_GETENV_PROPERTIES
756 next_property_key (char *s, size_t *length)
762 // Skip over whitespace
766 // If we've reached the end, return NULL. Also return NULL if for
767 // some reason we've come across a malformed property string.
773 // Determine the length of the property key.
791 next_property_value (char *s, size_t *length)
807 // If we've reached the end, return NULL.
811 // Determine the length of the property value.
830 process_gcj_properties ()
832 char *props = getenv("GCJ_PROPERTIES");
835 size_t property_count = 0;
840 // Whip through props quickly in order to count the number of
842 while (p && (p = next_property_key (p, &length)))
844 // Skip to the end of the key
847 p = next_property_value (p, &length);
854 // Allocate an array of property value/key pairs.
855 _Jv_Environment_Properties =
856 (property_pair *) malloc (sizeof(property_pair)
857 * (property_count + 1));
859 // Go through the properties again, initializing _Jv_Properties
863 while (p && (p = next_property_key (p, &length)))
865 _Jv_Environment_Properties[property_count].key = p;
866 _Jv_Environment_Properties[property_count].key_length = length;
868 // Skip to the end of the key
871 p = next_property_value (p, &length);
873 _Jv_Environment_Properties[property_count].value = p;
874 _Jv_Environment_Properties[property_count].value_length = length;
881 memset ((void *) &_Jv_Environment_Properties[property_count],
882 0, sizeof (property_pair));
886 // Null terminate the strings.
887 while (_Jv_Environment_Properties[i].key)
889 _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
890 _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
894 #endif // DISABLE_GETENV_PROPERTIES
898 _Jv_Utf8Const *void_signature;
899 _Jv_Utf8Const *clinit_name;
900 _Jv_Utf8Const *init_name;
901 _Jv_Utf8Const *finit_name;
903 bool runtimeInitialized = false;
907 _Jv_CreateJavaVM (void* /*vm_args*/)
911 if (runtimeInitialized)
914 runtimeInitialized = true;
916 PROCESS_GCJ_PROPERTIES;
920 _Jv_InitializeSyncMutex ();
922 /* Initialize Utf8 constants declared in jvm.h. */
923 void_signature = _Jv_makeUtf8Const ("()V", 3);
924 clinit_name = _Jv_makeUtf8Const ("<clinit>", 8);
925 init_name = _Jv_makeUtf8Const ("<init>", 6);
926 finit_name = _Jv_makeUtf8Const ("finit$", 6);
928 /* Initialize built-in classes to represent primitive TYPEs. */
929 _Jv_InitPrimClass (&_Jv_byteClass, "byte", 'B', 1, &_Jv_byteVTable);
930 _Jv_InitPrimClass (&_Jv_shortClass, "short", 'S', 2, &_Jv_shortVTable);
931 _Jv_InitPrimClass (&_Jv_intClass, "int", 'I', 4, &_Jv_intVTable);
932 _Jv_InitPrimClass (&_Jv_longClass, "long", 'J', 8, &_Jv_longVTable);
933 _Jv_InitPrimClass (&_Jv_booleanClass, "boolean", 'Z', 1, &_Jv_booleanVTable);
934 _Jv_InitPrimClass (&_Jv_charClass, "char", 'C', 2, &_Jv_charVTable);
935 _Jv_InitPrimClass (&_Jv_floatClass, "float", 'F', 4, &_Jv_floatVTable);
936 _Jv_InitPrimClass (&_Jv_doubleClass, "double", 'D', 8, &_Jv_doubleVTable);
937 _Jv_InitPrimClass (&_Jv_voidClass, "void", 'V', 0, &_Jv_voidVTable);
939 // Turn stack trace generation off while creating exception objects.
940 _Jv_InitClass (&java::lang::Throwable::class$);
941 java::lang::Throwable::trace_enabled = 0;
947 arithexception = new java::lang::ArithmeticException
948 (JvNewStringLatin1 ("/ by zero"));
951 no_memory = new java::lang::OutOfMemoryError;
953 java::lang::Throwable::trace_enabled = 1;
956 LTDL_SET_PRELOADED_SYMBOLS ();
960 // Initialise winsock for networking
962 if (WSAStartup (MAKEWORD (1, 1), &data))
963 MessageBox (NULL, "Error initialising winsock library.", "Error", MB_OK | MB_ICONEXCLAMATION);
964 #endif /* USE_WINSOCK */
966 #ifdef USE_WIN32_SIGNALLING
967 // Install exception handler
968 SetUnhandledExceptionFilter (win32_exception_handler);
969 #elif defined(HAVE_SIGACTION)
970 // We only want this on POSIX systems.
971 struct sigaction act;
972 act.sa_handler = SIG_IGN;
973 sigemptyset (&act.sa_mask);
975 sigaction (SIGPIPE, &act, NULL);
977 signal (SIGPIPE, SIG_IGN);
982 _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
984 // Start the GC finalizer thread. A VirtualMachineError can be
985 // thrown by the runtime if, say, threads aren't available. In this
986 // case finalizers simply won't run.
989 using namespace gnu::gcj::runtime;
990 FinalizerThread *ft = new FinalizerThread ();
993 catch (java::lang::VirtualMachineError *ignore)
1001 _Jv_RunMain (jclass klass, const char *name, int argc, const char **argv,
1007 java::lang::Runtime *runtime = NULL;
1009 #ifdef HAVE_PROC_SELF_EXE
1011 sprintf (exec_name, "/proc/%d/exe", getpid ());
1012 _Jv_ThisExecutable (exec_name);
1014 _Jv_ThisExecutable (argv[0]);
1019 // Set this very early so that it is seen when java.lang.System
1022 _Jv_Jar_Class_Path = strdup (name);
1023 _Jv_CreateJavaVM (NULL);
1025 // Get the Runtime here. We want to initialize it before searching
1026 // for `main'; that way it will be set up if `main' is a JNI method.
1027 runtime = java::lang::Runtime::getRuntime ();
1029 arg_vec = JvConvertArgv (argc - 1, argv + 1);
1031 using namespace gnu::gcj::runtime;
1033 main_thread = new FirstThread (klass, arg_vec);
1035 main_thread = new FirstThread (JvNewStringLatin1 (name),
1038 catch (java::lang::Throwable *t)
1040 java::lang::System::err->println (JvNewStringLatin1
1041 ("Exception during runtime initialization"));
1042 t->printStackTrace();
1046 _Jv_AttachCurrentThread (main_thread);
1047 _Jv_ThreadRun (main_thread);
1050 int status = (int) java::lang::ThreadGroup::had_uncaught_exception;
1051 runtime->exit (status);
1055 JvRunMain (jclass klass, int argc, const char **argv)
1057 _Jv_RunMain (klass, NULL, argc, argv, false);
1062 // Parse a string and return a heap size.
1064 parse_heap_size (const char *spec)
1067 unsigned long val = strtoul (spec, &end, 10);
1068 if (*end == 'k' || *end == 'K')
1070 else if (*end == 'm' || *end == 'M')
1072 return (size_t) val;
1075 // Set the initial heap size. This might be ignored by the GC layer.
1076 // This must be called before _Jv_RunMain.
1078 _Jv_SetInitialHeapSize (const char *arg)
1080 size_t size = parse_heap_size (arg);
1081 _Jv_GCSetInitialHeapSize (size);
1084 // Set the maximum heap size. This might be ignored by the GC layer.
1085 // This must be called before _Jv_RunMain.
1087 _Jv_SetMaximumHeapSize (const char *arg)
1089 size_t size = parse_heap_size (arg);
1090 _Jv_GCSetMaximumHeapSize (size);
1096 _Jv_Malloc (jsize size)
1098 if (__builtin_expect (size == 0, false))
1100 void *ptr = malloc ((size_t) size);
1101 if (__builtin_expect (ptr == NULL, false))
1107 _Jv_Realloc (void *ptr, jsize size)
1109 if (__builtin_expect (size == 0, false))
1111 ptr = realloc (ptr, (size_t) size);
1112 if (__builtin_expect (ptr == NULL, false))
1118 _Jv_MallocUnchecked (jsize size)
1120 if (__builtin_expect (size == 0, false))
1122 return malloc ((size_t) size);
1126 _Jv_Free (void* ptr)
1133 // In theory, these routines can be #ifdef'd away on machines which
1134 // support divide overflow signals. However, we never know if some
1135 // code might have been compiled with "-fuse-divide-subroutine", so we
1136 // always include them in libgcj.
1139 _Jv_divI (jint dividend, jint divisor)
1141 if (__builtin_expect (divisor == 0, false))
1142 _Jv_ThrowSignal (arithexception);
1144 if (dividend == (jint) 0x80000000L && divisor == -1)
1147 return dividend / divisor;
1151 _Jv_remI (jint dividend, jint divisor)
1153 if (__builtin_expect (divisor == 0, false))
1154 _Jv_ThrowSignal (arithexception);
1156 if (dividend == (jint) 0x80000000L && divisor == -1)
1159 return dividend % divisor;
1163 _Jv_divJ (jlong dividend, jlong divisor)
1165 if (__builtin_expect (divisor == 0, false))
1166 _Jv_ThrowSignal (arithexception);
1168 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1171 return dividend / divisor;
1175 _Jv_remJ (jlong dividend, jlong divisor)
1177 if (__builtin_expect (divisor == 0, false))
1178 _Jv_ThrowSignal (arithexception);
1180 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
1183 return dividend % divisor;