remove extraneous code checked in with previous delta
[official-gcc.git] / libjava / prims.cc
blob81668a86c887d5c89e724b5b2f37a757d2d4c066
1 // prims.cc - Code for core of runtime environment.
3 /* Copyright (C) 1998, 1999, 2000 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
9 details. */
11 #include <config.h>
13 #ifdef USE_WIN32_SIGNALLING
14 #include <windows.h>
15 #endif /* USE_WIN32_SIGNALLING */
17 #ifdef USE_WINSOCK
18 #undef __INSIDE_CYGWIN__
19 #include <winsock.h>
20 #endif /* USE_WINSOCK */
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <signal.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
32 #include <gcj/cni.h>
33 #include <jvm.h>
34 #include <java-signal.h>
35 #include <java-threads.h>
37 #ifndef DISABLE_GETENV_PROPERTIES
38 #include <ctype.h>
39 #include <java-props.h>
40 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
41 #else
42 #define PROCESS_GCJ_PROPERTIES
43 #endif // DISABLE_GETENV_PROPERTIES
45 #include <java/lang/Class.h>
46 #include <java/lang/Runtime.h>
47 #include <java/lang/String.h>
48 #include <java/lang/Thread.h>
49 #include <java/lang/ThreadGroup.h>
50 #include <gnu/gcj/runtime/FirstThread.h>
51 #include <java/lang/ArrayIndexOutOfBoundsException.h>
52 #include <java/lang/ArithmeticException.h>
53 #include <java/lang/ClassFormatError.h>
54 #include <java/lang/NegativeArraySizeException.h>
55 #include <java/lang/NullPointerException.h>
56 #include <java/lang/OutOfMemoryError.h>
57 #include <java/lang/System.h>
58 #include <java/lang/reflect/Modifier.h>
59 #include <java/io/PrintStream.h>
61 #ifdef USE_LTDL
62 #include <ltdl.h>
63 #endif
65 #define ObjectClass _CL_Q34java4lang6Object
66 extern java::lang::Class ObjectClass;
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 // Properties set at compile time.
76 const char **_Jv_Compiler_Properties;
78 #ifndef DISABLE_GETENV_PROPERTIES
79 // Property key/value pairs.
80 property_pair *_Jv_Environment_Properties;
81 #endif
83 // The name of this executable.
84 static char * _Jv_execName;
88 #ifdef HANDLE_SEGV
89 static java::lang::NullPointerException *nullp;
90 SIGNAL_HANDLER (catch_segv)
92 MAKE_THROW_FRAME;
93 nullp->fillInStackTrace ();
94 _Jv_Throw (nullp);
96 #endif
98 static java::lang::ArithmeticException *arithexception;
100 #ifdef HANDLE_FPE
101 SIGNAL_HANDLER (catch_fpe)
103 #ifdef HANDLE_DIVIDE_OVERFLOW
104 HANDLE_DIVIDE_OVERFLOW;
105 #else
106 MAKE_THROW_FRAME;
107 #endif
108 arithexception->fillInStackTrace ();
109 _Jv_Throw (arithexception);
111 #endif
115 jboolean
116 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
118 register int len;
119 register _Jv_ushort *aptr, *bptr;
120 if (a == b)
121 return true;
122 if (a->hash != b->hash)
123 return false;
124 len = a->length;
125 if (b->length != len)
126 return false;
127 aptr = (_Jv_ushort *)a->data;
128 bptr = (_Jv_ushort *)b->data;
129 len = (len + 1) >> 1;
130 while (--len >= 0)
131 if (*aptr++ != *bptr++)
132 return false;
133 return true;
136 /* True iff A is equal to STR.
137 HASH is STR->hashCode().
140 jboolean
141 _Jv_equal (Utf8Const* a, jstring str, jint hash)
143 if (a->hash != (_Jv_ushort) hash)
144 return false;
145 jint len = str->length();
146 jint i = 0;
147 jchar *sptr = _Jv_GetStringChars (str);
148 register unsigned char* ptr = (unsigned char*) a->data;
149 register unsigned char* limit = ptr + a->length;
150 for (;; i++, sptr++)
152 int ch = UTF8_GET (ptr, limit);
153 if (i == len)
154 return ch < 0;
155 if (ch != *sptr)
156 return false;
158 return true;
161 /* Like _Jv_equal, but stop after N characters. */
162 jboolean
163 _Jv_equaln (Utf8Const *a, jstring str, jint n)
165 jint len = str->length();
166 jint i = 0;
167 jchar *sptr = _Jv_GetStringChars (str);
168 register unsigned char* ptr = (unsigned char*) a->data;
169 register unsigned char* limit = ptr + a->length;
170 for (; n-- > 0; i++, sptr++)
172 int ch = UTF8_GET (ptr, limit);
173 if (i == len)
174 return ch < 0;
175 if (ch != *sptr)
176 return false;
178 return true;
181 /* Count the number of Unicode chars encoded in a given Ut8 string. */
183 _Jv_strLengthUtf8(char* str, int len)
185 register unsigned char* ptr;
186 register unsigned char* limit;
187 int str_length;
189 ptr = (unsigned char*) str;
190 limit = ptr + len;
191 str_length = 0;
192 for (; ptr < limit; str_length++) {
193 if (UTF8_GET (ptr, limit) < 0) {
194 return (-1);
197 return (str_length);
200 /* Calculate a hash value for a string encoded in Utf8 format.
201 * This returns the same hash value as specified or java.lang.String.hashCode.
203 static jint
204 hashUtf8String (char* str, int len)
206 register unsigned char* ptr = (unsigned char*) str;
207 register unsigned char* limit = ptr + len;
208 jint hash = 0;
210 for (; ptr < limit;)
212 int ch = UTF8_GET (ptr, limit);
213 /* Updated specification from
214 http://www.javasoft.com/docs/books/jls/clarify.html. */
215 hash = (31 * hash) + ch;
217 return hash;
220 _Jv_Utf8Const *
221 _Jv_makeUtf8Const (char* s, int len)
223 if (len < 0)
224 len = strlen (s);
225 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
226 if (! m)
227 JvThrow (no_memory);
228 memcpy (m->data, s, len);
229 m->data[len] = 0;
230 m->length = len;
231 m->hash = hashUtf8String (s, len) & 0xFFFF;
232 return (m);
235 _Jv_Utf8Const *
236 _Jv_makeUtf8Const (jstring string)
238 jint hash = string->hashCode ();
239 jint len = _Jv_GetStringUTFLength (string);
241 Utf8Const* m = (Utf8Const*)
242 _Jv_AllocBytesChecked (sizeof(Utf8Const) + len + 1);
244 m->hash = hash;
245 m->length = len;
247 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
248 m->data[len] = 0;
250 return m;
255 #ifdef DEBUG
256 void
257 _Jv_Abort (const char *function, const char *file, int line,
258 const char *message)
259 #else
260 void
261 _Jv_Abort (const char *, const char *, int, const char *message)
262 #endif
264 #ifdef DEBUG
265 fprintf (stderr,
266 "libgcj failure: %s\n in function %s, file %s, line %d\n",
267 message, function, file, line);
268 #else
269 java::io::PrintStream *err = java::lang::System::err;
270 err->print(JvNewStringLatin1 ("libgcj failure: "));
271 err->println(JvNewStringLatin1 (message));
272 err->flush();
273 #endif
274 abort ();
277 static void
278 fail_on_finalization (jobject)
280 JvFail ("object was finalized");
283 void
284 _Jv_GCWatch (jobject obj)
286 _Jv_RegisterFinalizer (obj, fail_on_finalization);
289 void
290 _Jv_ThrowBadArrayIndex(jint bad_index)
292 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
293 (java::lang::String::valueOf(bad_index)));
296 // Allocate some unscanned memory and throw an exception if no memory.
297 void *
298 _Jv_AllocBytesChecked (jsize size)
300 void *r = _Jv_AllocBytes (size);
301 if (! r)
302 _Jv_Throw (no_memory);
303 return r;
306 // Allocate a new object of class C. SIZE is the size of the object
307 // to allocate. You might think this is redundant, but it isn't; some
308 // classes, such as String, aren't of fixed size.
309 jobject
310 _Jv_AllocObject (jclass c, jint size)
312 _Jv_InitClass (c);
314 jobject obj = (jobject) _Jv_AllocObj (size);
315 if (! obj)
316 JvThrow (no_memory);
317 *((_Jv_VTable **) obj) = c->vtable;
319 // If this class has inherited finalize from Object, then don't
320 // bother registering a finalizer. We know that finalize() is the
321 // very first method after the dummy entry. If this turns out to be
322 // unreliable, a more robust implementation can be written. Such an
323 // implementation would look for Object.finalize in Object's method
324 // table at startup, and then use that information to find the
325 // appropriate index in the method vector.
326 if (c->vtable->method[1] != ObjectClass.vtable->method[1])
327 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
329 return obj;
332 // Allocate a new array of Java objects. Each object is of type
333 // `elementClass'. `init' is used to initialize each slot in the
334 // array.
335 jobjectArray
336 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
338 if (count < 0)
339 JvThrow (new java::lang::NegativeArraySizeException);
341 JvAssert (! elementClass->isPrimitive ());
343 jobjectArray obj = NULL;
344 size_t size = (size_t) _Jv_GetArrayElementFromElementType (obj,
345 elementClass);
347 // Check for overflow.
348 if ((size_t) count > (SIZE_T_MAX - size) / sizeof (jobject))
349 JvThrow (no_memory);
351 size += count * sizeof (jobject);
353 // FIXME: second argument should be "current loader" //
354 jclass clas = _Jv_FindArrayClass (elementClass, 0);
356 obj = (jobjectArray) _Jv_AllocArray (size);
357 if (! obj)
358 JvThrow (no_memory);
359 obj->length = count;
360 jobject* ptr = elements(obj);
361 // We know the allocator returns zeroed memory. So don't bother
362 // zeroing it again.
363 if (init)
365 while (--count >= 0)
366 *ptr++ = init;
368 // Set the vtbl last to avoid problems if the GC happens during the
369 // window in this function between the allocation and this
370 // assignment.
371 *((_Jv_VTable **) obj) = clas->vtable;
372 return obj;
375 // Allocate a new array of primitives. ELTYPE is the type of the
376 // element, COUNT is the size of the array.
377 jobject
378 _Jv_NewPrimArray (jclass eltype, jint count)
380 int elsize = eltype->size();
381 if (count < 0)
382 JvThrow (new java::lang::NegativeArraySizeException ());
384 JvAssert (eltype->isPrimitive ());
385 jobject dummy = NULL;
386 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
388 // Check for overflow.
389 if ((size_t) count > (SIZE_T_MAX - size) / elsize)
390 JvThrow (no_memory);
392 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count);
393 if (! arr)
394 JvThrow (no_memory);
395 arr->length = count;
396 // Note that we assume we are given zeroed memory by the allocator.
398 jclass klass = _Jv_FindArrayClass (eltype, 0);
399 // Set the vtbl last to avoid problems if the GC happens during the
400 // window in this function between the allocation and this
401 // assignment.
402 *((_Jv_VTable **) arr) = klass->vtable;
403 return arr;
406 jobject
407 _Jv_NewArray (jint type, jint size)
409 switch (type)
411 case 4: return JvNewBooleanArray (size);
412 case 5: return JvNewCharArray (size);
413 case 6: return JvNewFloatArray (size);
414 case 7: return JvNewDoubleArray (size);
415 case 8: return JvNewByteArray (size);
416 case 9: return JvNewShortArray (size);
417 case 10: return JvNewIntArray (size);
418 case 11: return JvNewLongArray (size);
420 JvFail ("newarray - bad type code");
421 return NULL; // Placate compiler.
424 jobject
425 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
427 JvAssert (type->isArray());
428 jclass element_type = type->getComponentType();
429 jobject result;
430 if (element_type->isPrimitive())
431 result = _Jv_NewPrimArray (element_type, sizes[0]);
432 else
433 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
435 if (dimensions > 1)
437 JvAssert (! element_type->isPrimitive());
438 JvAssert (element_type->isArray());
439 jobject *contents = elements ((jobjectArray) result);
440 for (int i = 0; i < sizes[0]; ++i)
441 contents[i] = _Jv_NewMultiArray (element_type, dimensions - 1,
442 sizes + 1);
445 return result;
448 jobject
449 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
451 va_list args;
452 jint sizes[dimensions];
453 va_start (args, dimensions);
454 for (int i = 0; i < dimensions; ++i)
456 jint size = va_arg (args, jint);
457 sizes[i] = size;
459 va_end (args);
461 return _Jv_NewMultiArray (array_type, dimensions, sizes);
466 class _Jv_PrimClass : public java::lang::Class
468 public:
469 // FIXME: calling convention is weird. If we use the natural types
470 // then the compiler will complain because they aren't Java types.
471 _Jv_PrimClass (jobject cname, jbyte sig, jint len)
473 using namespace java::lang::reflect;
475 // We must initialize every field of the class. We do this in
476 // the same order they are declared in Class.h.
477 next = NULL;
478 name = _Jv_makeUtf8Const ((char *) cname, -1);
479 accflags = Modifier::PUBLIC | Modifier::FINAL;
480 superclass = NULL;
481 constants.size = 0;
482 constants.tags = NULL;
483 constants.data = NULL;
484 methods = NULL;
485 method_count = sig;
486 vtable_method_count = 0;
487 fields = NULL;
488 size_in_bytes = len;
489 field_count = 0;
490 static_field_count = 0;
491 vtable = JV_PRIMITIVE_VTABLE;
492 interfaces = NULL;
493 loader = NULL;
494 interface_count = 0;
495 state = JV_STATE_NOTHING;
496 thread = NULL;
500 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
501 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
503 DECLARE_PRIM_TYPE(byte, 'B', 1);
504 DECLARE_PRIM_TYPE(short, 'S', 2);
505 DECLARE_PRIM_TYPE(int, 'I', 4);
506 DECLARE_PRIM_TYPE(long, 'J', 8);
507 DECLARE_PRIM_TYPE(boolean, 'Z', 1);
508 DECLARE_PRIM_TYPE(char, 'C', 2);
509 DECLARE_PRIM_TYPE(float, 'F', 4);
510 DECLARE_PRIM_TYPE(double, 'D', 8);
511 DECLARE_PRIM_TYPE(void, 'V', 0);
513 jclass
514 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
516 switch (*sig)
518 case 'B':
519 return JvPrimClass (byte);
520 case 'S':
521 return JvPrimClass (short);
522 case 'I':
523 return JvPrimClass (int);
524 case 'J':
525 return JvPrimClass (long);
526 case 'Z':
527 return JvPrimClass (boolean);
528 case 'C':
529 return JvPrimClass (char);
530 case 'F':
531 return JvPrimClass (float);
532 case 'D':
533 return JvPrimClass (double);
534 case 'V':
535 return JvPrimClass (void);
536 case 'L':
538 int i;
539 for (i = 1; sig[i] && sig[i] != ';'; ++i)
541 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
542 return _Jv_FindClass (name, loader);
545 case '[':
546 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig[1], loader),
547 loader);
549 JvFail ("couldn't understand class signature");
550 return NULL; // Placate compiler.
555 JArray<jstring> *
556 JvConvertArgv (int argc, const char **argv)
558 if (argc < 0)
559 argc = 0;
560 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
561 jobject* ptr = elements(ar);
562 for (int i = 0; i < argc; i++)
564 const char *arg = argv[i];
565 // FIXME - should probably use JvNewStringUTF.
566 *ptr++ = JvNewStringLatin1(arg, strlen(arg));
568 return (JArray<jstring>*) ar;
571 // FIXME: These variables are static so that they will be
572 // automatically scanned by the Boehm collector. This is needed
573 // because with qthreads the collector won't scan the initial stack --
574 // it will only scan the qthreads stacks.
576 // Command line arguments.
577 static jobject arg_vec;
579 // The primary threadgroup.
580 static java::lang::ThreadGroup *main_group;
582 // The primary thread.
583 static java::lang::Thread *main_thread;
585 char *
586 _Jv_ThisExecutable (void)
588 return _Jv_execName;
591 void
592 _Jv_ThisExecutable (const char *name)
594 if (name)
596 _Jv_execName = new char[strlen (name) + 1];
597 strcpy (_Jv_execName, name);
601 #ifdef USE_WIN32_SIGNALLING
603 extern "C" int* win32_get_restart_frame (void *);
605 LONG CALLBACK
606 win32_exception_handler (LPEXCEPTION_POINTERS e)
608 int* setjmp_buf;
609 if (e->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
610 setjmp_buf = win32_get_restart_frame (nullp);
611 else if (e->ExceptionRecord->ExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO)
612 setjmp_buf = win32_get_restart_frame (arithexception);
613 else
614 return EXCEPTION_CONTINUE_SEARCH;
616 e->ContextRecord->Ebp = setjmp_buf[0];
617 // FIXME: Why does i386-signal.h increment the PC here, do we need to do it?
618 e->ContextRecord->Eip = setjmp_buf[1];
619 // FIXME: Is this the stack pointer? Do we need it?
620 e->ContextRecord->Esp = setjmp_buf[2];
622 return EXCEPTION_CONTINUE_EXECUTION;
625 #endif
627 static void
628 main_init ()
630 INIT_SEGV;
631 #ifdef HANDLE_FPE
632 INIT_FPE;
633 #else
634 arithexception = new java::lang::ArithmeticException
635 (JvNewStringLatin1 ("/ by zero"));
636 #endif
638 no_memory = new java::lang::OutOfMemoryError;
640 #ifdef USE_LTDL
641 LTDL_SET_PRELOADED_SYMBOLS ();
642 #endif
644 #ifdef USE_WINSOCK
645 // Initialise winsock for networking
646 WSADATA data;
647 if (WSAStartup (MAKEWORD (1, 1), &data))
648 MessageBox (NULL, "Error initialising winsock library.", "Error", MB_OK | MB_ICONEXCLAMATION);
649 #endif /* USE_WINSOCK */
651 #ifdef USE_WIN32_SIGNALLING
652 // Install exception handler
653 SetUnhandledExceptionFilter (win32_exception_handler);
654 #else
655 // We only want this on POSIX systems.
656 struct sigaction act;
657 act.sa_handler = SIG_IGN;
658 sigemptyset (&act.sa_mask);
659 act.sa_flags = 0;
660 sigaction (SIGPIPE, &act, NULL);
661 #endif /* USE_WIN32_SIGNALLING */
663 _Jv_JNI_Init ();
666 #ifndef DISABLE_GETENV_PROPERTIES
668 static char *
669 next_property_key (char *s, size_t *length)
671 size_t l = 0;
673 JvAssert (s);
675 // Skip over whitespace
676 while (isspace (*s))
677 s++;
679 // If we've reached the end, return NULL. Also return NULL if for
680 // some reason we've come across a malformed property string.
681 if (*s == 0
682 || *s == ':'
683 || *s == '=')
684 return NULL;
686 // Determine the length of the property key.
687 while (s[l] != 0
688 && ! isspace (s[l])
689 && s[l] != ':'
690 && s[l] != '=')
692 if (s[l] == '\\'
693 && s[l+1] != 0)
694 l++;
695 l++;
698 *length = l;
700 return s;
703 static char *
704 next_property_value (char *s, size_t *length)
706 size_t l = 0;
708 JvAssert (s);
710 while (isspace (*s))
711 s++;
713 if (*s == ':'
714 || *s == '=')
715 s++;
717 while (isspace (*s))
718 s++;
720 // If we've reached the end, return NULL.
721 if (*s == 0)
722 return NULL;
724 // Determine the length of the property value.
725 while (s[l] != 0
726 && ! isspace (s[l])
727 && s[l] != ':'
728 && s[l] != '=')
730 if (s[l] == '\\'
731 && s[l+1] != 0)
732 l += 2;
733 else
734 l++;
737 *length = l;
739 return s;
742 static void
743 process_gcj_properties ()
745 char *props = getenv("GCJ_PROPERTIES");
746 char *p = props;
747 size_t length;
748 size_t property_count = 0;
750 if (NULL == props)
751 return;
753 // Whip through props quickly in order to count the number of
754 // property values.
755 while (p && (p = next_property_key (p, &length)))
757 // Skip to the end of the key
758 p += length;
760 p = next_property_value (p, &length);
761 if (p)
762 p += length;
764 property_count++;
767 // Allocate an array of property value/key pairs.
768 _Jv_Environment_Properties =
769 (property_pair *) malloc (sizeof(property_pair)
770 * (property_count + 1));
772 // Go through the properties again, initializing _Jv_Properties
773 // along the way.
774 p = props;
775 property_count = 0;
776 while (p && (p = next_property_key (p, &length)))
778 _Jv_Environment_Properties[property_count].key = p;
779 _Jv_Environment_Properties[property_count].key_length = length;
781 // Skip to the end of the key
782 p += length;
784 p = next_property_value (p, &length);
786 _Jv_Environment_Properties[property_count].value = p;
787 _Jv_Environment_Properties[property_count].value_length = length;
789 if (p)
790 p += length;
792 property_count++;
794 memset ((void *) &_Jv_Environment_Properties[property_count],
795 0, sizeof (property_pair));
797 size_t i = 0;
799 // Null terminate the strings.
800 while (_Jv_Environment_Properties[i].key)
802 _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
803 _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
807 #endif // DISABLE_GETENV_PROPERTIES
809 void
810 JvRunMain (jclass klass, int argc, const char **argv)
812 PROCESS_GCJ_PROPERTIES;
814 main_init ();
815 #ifdef HAVE_PROC_SELF_EXE
816 char exec_name[20];
817 sprintf (exec_name, "/proc/%d/exe", getpid ());
818 _Jv_ThisExecutable (exec_name);
819 #else
820 _Jv_ThisExecutable (argv[0]);
821 #endif
823 arg_vec = JvConvertArgv (argc - 1, argv + 1);
824 main_group = new java::lang::ThreadGroup (23);
825 main_thread = new gnu::gcj::runtime::FirstThread (main_group,
826 klass, arg_vec);
828 main_thread->start();
829 _Jv_ThreadWait ();
831 java::lang::Runtime::getRuntime ()->exit (0);
834 void
835 _Jv_RunMain (const char *class_name, int argc, const char **argv)
837 PROCESS_GCJ_PROPERTIES;
839 main_init ();
841 #ifdef HAVE_PROC_SELF_EXE
842 char exec_name[20];
843 sprintf (exec_name, "/proc/%d/exe", getpid ());
844 _Jv_ThisExecutable (exec_name);
845 #endif
847 arg_vec = JvConvertArgv (argc - 1, argv + 1);
848 main_group = new java::lang::ThreadGroup (23);
849 main_thread = new gnu::gcj::runtime::FirstThread (main_group,
850 JvNewStringLatin1 (class_name),
851 arg_vec);
852 main_thread->start();
853 _Jv_ThreadWait ();
855 java::lang::Runtime::getRuntime ()->exit (0);
860 // Parse a string and return a heap size.
861 static size_t
862 parse_heap_size (const char *spec)
864 char *end;
865 unsigned long val = strtoul (spec, &end, 10);
866 if (*end == 'k' || *end == 'K')
867 val *= 1024;
868 else if (*end == 'm' || *end == 'M')
869 val *= 1048576;
870 return (size_t) val;
873 // Set the initial heap size. This might be ignored by the GC layer.
874 // This must be called before _Jv_RunMain.
875 void
876 _Jv_SetInitialHeapSize (const char *arg)
878 size_t size = parse_heap_size (arg);
879 _Jv_GCSetInitialHeapSize (size);
882 // Set the maximum heap size. This might be ignored by the GC layer.
883 // This must be called before _Jv_RunMain.
884 void
885 _Jv_SetMaximumHeapSize (const char *arg)
887 size_t size = parse_heap_size (arg);
888 _Jv_GCSetMaximumHeapSize (size);
893 void *
894 _Jv_Malloc (jsize size)
896 if (size == 0)
897 size = 1;
898 void *ptr = malloc ((size_t) size);
899 if (ptr == NULL)
900 JvThrow (no_memory);
901 return ptr;
904 void *
905 _Jv_Realloc (void *ptr, jsize size)
907 if (size == 0)
908 size = 1;
909 ptr = realloc (ptr, (size_t) size);
910 if (ptr == NULL)
911 JvThrow (no_memory);
912 return ptr;
915 void *
916 _Jv_MallocUnchecked (jsize size)
918 if (size == 0)
919 size = 1;
920 return malloc ((size_t) size);
923 void
924 _Jv_Free (void* ptr)
926 return free (ptr);
931 // In theory, these routines can be #ifdef'd away on machines which
932 // support divide overflow signals. However, we never know if some
933 // code might have been compiled with "-fuse-divide-subroutine", so we
934 // always include them in libgcj.
936 jint
937 _Jv_divI (jint dividend, jint divisor)
939 if (divisor == 0)
940 _Jv_Throw (arithexception);
942 if (dividend == (jint) 0x80000000L && divisor == -1)
943 return dividend;
945 return dividend / divisor;
948 jint
949 _Jv_remI (jint dividend, jint divisor)
951 if (divisor == 0)
952 _Jv_Throw (arithexception);
954 if (dividend == (jint) 0x80000000L && divisor == -1)
955 return 0;
957 return dividend % divisor;
960 jlong
961 _Jv_divJ (jlong dividend, jlong divisor)
963 if (divisor == 0)
964 _Jv_Throw (arithexception);
966 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
967 return dividend;
969 return dividend / divisor;
972 jlong
973 _Jv_remJ (jlong dividend, jlong divisor)
975 if (divisor == 0)
976 _Jv_Throw (arithexception);
978 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
979 return 0;
981 return dividend % divisor;