natObject.cc (notify): Throw message with IllegalMonitorStateException.
[gcc.git] / libjava / prims.cc
1 // prims.cc - Code for core of runtime environment.
2
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 #include <config.h>
12
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <signal.h>
18
19 #pragma implementation "gcj/array.h"
20
21 #include <gcj/cni.h>
22 #include <jvm.h>
23 #include <java-signal.h>
24 #include <java-threads.h>
25
26 #ifndef DISABLE_GETENV_PROPERTIES
27 #include <ctype.h>
28 #include <java-props.h>
29 #define PROCESS_GCJ_PROPERTIES process_gcj_properties()
30 #else
31 #define PROCESS_GCJ_PROPERTIES
32 #endif // DISABLE_GETENV_PROPERTIES
33
34 #include <java/lang/Class.h>
35 #include <java/lang/Runtime.h>
36 #include <java/lang/String.h>
37 #include <java/lang/Thread.h>
38 #include <java/lang/ThreadGroup.h>
39 #include <java/lang/FirstThread.h>
40 #include <java/lang/ArrayIndexOutOfBoundsException.h>
41 #include <java/lang/ArithmeticException.h>
42 #include <java/lang/ClassFormatError.h>
43 #include <java/lang/ClassCastException.h>
44 #include <java/lang/NegativeArraySizeException.h>
45 #include <java/lang/NullPointerException.h>
46 #include <java/lang/OutOfMemoryError.h>
47 #include <java/lang/ArrayStoreException.h>
48 #include <java/lang/System.h>
49 #include <java/lang/reflect/Modifier.h>
50 #include <java/io/PrintStream.h>
51
52 #ifdef USE_LTDL
53 #include <ltdl.h>
54 #endif
55
56 #define ObjectClass _CL_Q34java4lang6Object
57 extern java::lang::Class ObjectClass;
58
59 // We allocate a single OutOfMemoryError exception which we keep
60 // around for use if we run out of memory.
61 static java::lang::OutOfMemoryError *no_memory;
62
63 // Largest representable size_t.
64 #define SIZE_T_MAX ((size_t) (~ (size_t) 0))
65
66 // Properties set at compile time.
67 const char **_Jv_Compiler_Properties;
68
69 #ifndef DISABLE_GETENV_PROPERTIES
70 // Property key/value pairs.
71 property_pair *_Jv_Environment_Properties;
72 #endif
73 \f
74
75 #ifdef HANDLE_SEGV
76 static java::lang::NullPointerException *nullp;
77 SIGNAL_HANDLER (catch_segv)
78 {
79 MAKE_THROW_FRAME;
80 _Jv_Throw (nullp);
81 }
82 #endif
83
84 static java::lang::ArithmeticException *arithexception;
85
86 #ifdef HANDLE_FPE
87 SIGNAL_HANDLER (catch_fpe)
88 {
89 #ifdef HANDLE_DIVIDE_OVERFLOW
90 HANDLE_DIVIDE_OVERFLOW;
91 #else
92 MAKE_THROW_FRAME;
93 #endif
94 _Jv_Throw (arithexception);
95 }
96 #endif
97
98 \f
99
100 jboolean
101 _Jv_equalUtf8Consts (Utf8Const* a, Utf8Const *b)
102 {
103 register int len;
104 register _Jv_ushort *aptr, *bptr;
105 if (a == b)
106 return true;
107 if (a->hash != b->hash)
108 return false;
109 len = a->length;
110 if (b->length != len)
111 return false;
112 aptr = (_Jv_ushort *)a->data;
113 bptr = (_Jv_ushort *)b->data;
114 len = (len + 1) >> 1;
115 while (--len >= 0)
116 if (*aptr++ != *bptr++)
117 return false;
118 return true;
119 }
120
121 /* True iff A is equal to STR.
122 HASH is STR->hashCode().
123 */
124
125 jboolean
126 _Jv_equal (Utf8Const* a, jstring str, jint hash)
127 {
128 if (a->hash != (_Jv_ushort) hash)
129 return false;
130 jint len = str->length();
131 jint i = 0;
132 jchar *sptr = _Jv_GetStringChars (str);
133 register unsigned char* ptr = (unsigned char*) a->data;
134 register unsigned char* limit = ptr + a->length;
135 for (;; i++, sptr++)
136 {
137 int ch = UTF8_GET (ptr, limit);
138 if (i == len)
139 return ch < 0;
140 if (ch != *sptr)
141 return false;
142 }
143 return true;
144 }
145
146 /* Count the number of Unicode chars encoded in a given Ut8 string. */
147 int
148 _Jv_strLengthUtf8(char* str, int len)
149 {
150 register unsigned char* ptr;
151 register unsigned char* limit;
152 int str_length;
153
154 ptr = (unsigned char*) str;
155 limit = ptr + len;
156 str_length = 0;
157 for (; ptr < limit; str_length++) {
158 if (UTF8_GET (ptr, limit) < 0) {
159 return (-1);
160 }
161 }
162 return (str_length);
163 }
164
165 /* Calculate a hash value for a string encoded in Utf8 format.
166 * This returns the same hash value as specified or java.lang.String.hashCode.
167 */
168 static jint
169 hashUtf8String (char* str, int len)
170 {
171 register unsigned char* ptr = (unsigned char*) str;
172 register unsigned char* limit = ptr + len;
173 jint hash = 0;
174
175 for (; ptr < limit;)
176 {
177 int ch = UTF8_GET (ptr, limit);
178 /* Updated specification from
179 http://www.javasoft.com/docs/books/jls/clarify.html. */
180 hash = (31 * hash) + ch;
181 }
182 return hash;
183 }
184
185 _Jv_Utf8Const *
186 _Jv_makeUtf8Const (char* s, int len)
187 {
188 if (len < 0)
189 len = strlen (s);
190 Utf8Const* m = (Utf8Const*) _Jv_AllocBytes (sizeof(Utf8Const) + len + 1);
191 if (! m)
192 JvThrow (no_memory);
193 memcpy (m->data, s, len);
194 m->data[len] = 0;
195 m->length = len;
196 m->hash = hashUtf8String (s, len) & 0xFFFF;
197 return (m);
198 }
199
200 _Jv_Utf8Const *
201 _Jv_makeUtf8Const (jstring string)
202 {
203 jint hash = string->hashCode ();
204 jint len = _Jv_GetStringUTFLength (string);
205
206 Utf8Const* m = (Utf8Const*)
207 _Jv_AllocBytesChecked (sizeof(Utf8Const) + len + 1);
208
209 m->hash = hash;
210 m->length = len;
211
212 _Jv_GetStringUTFRegion (string, 0, string->length (), m->data);
213 m->data[len] = 0;
214
215 return m;
216 }
217
218 \f
219
220 #ifdef DEBUG
221 void
222 _Jv_Abort (const char *function, const char *file, int line,
223 const char *message)
224 #else
225 void
226 _Jv_Abort (const char *, const char *, int, const char *message)
227 #endif
228 {
229 #ifdef DEBUG
230 fprintf (stderr,
231 "libgcj failure: %s\n in function %s, file %s, line %d\n",
232 message, function, file, line);
233 #else
234 java::io::PrintStream *err = java::lang::System::err;
235 err->print(JvNewStringLatin1 ("libgcj failure: "));
236 err->println(JvNewStringLatin1 (message));
237 err->flush();
238 #endif
239 abort ();
240 }
241
242 static void
243 fail_on_finalization (jobject)
244 {
245 JvFail ("object was finalized");
246 }
247
248 void
249 _Jv_GCWatch (jobject obj)
250 {
251 _Jv_RegisterFinalizer (obj, fail_on_finalization);
252 }
253
254 void
255 _Jv_ThrowBadArrayIndex(jint bad_index)
256 {
257 JvThrow (new java::lang::ArrayIndexOutOfBoundsException
258 (java::lang::String::valueOf(bad_index)));
259 }
260
261 void*
262 _Jv_CheckCast (jclass c, jobject obj)
263 {
264 if (obj != NULL && ! c->isAssignableFrom(obj->getClass()))
265 JvThrow (new java::lang::ClassCastException);
266 return obj;
267 }
268
269 void
270 _Jv_CheckArrayStore (jobject arr, jobject obj)
271 {
272 if (obj)
273 {
274 JvAssert (arr != NULL);
275 jclass arr_class = arr->getClass();
276 JvAssert (arr_class->isArray());
277 jclass elt_class = arr_class->getComponentType();
278 jclass obj_class = obj->getClass();
279 if (! elt_class->isAssignableFrom(obj_class))
280 JvThrow (new java::lang::ArrayStoreException);
281 }
282 }
283
284 \f
285
286 // Allocate some unscanned memory and throw an exception if no memory.
287 void *
288 _Jv_AllocBytesChecked (jsize size)
289 {
290 void *r = _Jv_AllocBytes (size);
291 if (! r)
292 _Jv_Throw (no_memory);
293 return r;
294 }
295
296 // Allocate a new object of class C. SIZE is the size of the object
297 // to allocate. You might think this is redundant, but it isn't; some
298 // classes, such as String, aren't of fixed size.
299 jobject
300 _Jv_AllocObject (jclass c, jint size)
301 {
302 _Jv_InitClass (c);
303
304 jobject obj = (jobject) _Jv_AllocObj (size);
305 if (! obj)
306 JvThrow (no_memory);
307 *((_Jv_VTable **) obj) = c->vtable;
308
309 // If this class has inherited finalize from Object, then don't
310 // bother registering a finalizer. We know that finalize() is the
311 // very first method after the dummy entry. If this turns out to be
312 // unreliable, a more robust implementation can be written. Such an
313 // implementation would look for Object.finalize in Object's method
314 // table at startup, and then use that information to find the
315 // appropriate index in the method vector.
316 if (c->vtable->method[1] != ObjectClass.vtable->method[1])
317 _Jv_RegisterFinalizer (obj, _Jv_FinalizeObject);
318
319 return obj;
320 }
321
322 // Allocate a new array of Java objects. Each object is of type
323 // `elementClass'. `init' is used to initialize each slot in the
324 // array.
325 jobjectArray
326 _Jv_NewObjectArray (jsize count, jclass elementClass, jobject init)
327 {
328 if (count < 0)
329 JvThrow (new java::lang::NegativeArraySizeException);
330
331 JvAssert (! elementClass->isPrimitive ());
332
333 jobjectArray obj = NULL;
334 size_t size = (size_t) _Jv_GetArrayElementFromElementType (obj,
335 elementClass);
336
337 // Check for overflow.
338 if ((size_t) count > (SIZE_T_MAX - size) / sizeof (jobject))
339 JvThrow (no_memory);
340
341 size += count * sizeof (jobject);
342
343 // FIXME: second argument should be "current loader" //
344 jclass clas = _Jv_FindArrayClass (elementClass, 0);
345
346 obj = (jobjectArray) _Jv_AllocArray (size);
347 if (! obj)
348 JvThrow (no_memory);
349 obj->length = count;
350 jobject* ptr = elements(obj);
351 // We know the allocator returns zeroed memory. So don't bother
352 // zeroing it again.
353 if (init)
354 {
355 while (--count >= 0)
356 *ptr++ = init;
357 }
358 // Set the vtbl last to avoid problems if the GC happens during the
359 // window in this function between the allocation and this
360 // assignment.
361 *((_Jv_VTable **) obj) = clas->vtable;
362 return obj;
363 }
364
365 // Allocate a new array of primitives. ELTYPE is the type of the
366 // element, COUNT is the size of the array.
367 jobject
368 _Jv_NewPrimArray (jclass eltype, jint count)
369 {
370 int elsize = eltype->size();
371 if (count < 0)
372 JvThrow (new java::lang::NegativeArraySizeException ());
373
374 JvAssert (eltype->isPrimitive ());
375 jobject dummy = NULL;
376 size_t size = (size_t) _Jv_GetArrayElementFromElementType (dummy, eltype);
377
378 // Check for overflow.
379 if ((size_t) count > (SIZE_T_MAX - size) / elsize)
380 JvThrow (no_memory);
381
382 __JArray *arr = (__JArray*) _Jv_AllocObj (size + elsize * count);
383 if (! arr)
384 JvThrow (no_memory);
385 arr->length = count;
386 // Note that we assume we are given zeroed memory by the allocator.
387
388 jclass klass = _Jv_FindArrayClass (eltype, 0);
389 // Set the vtbl last to avoid problems if the GC happens during the
390 // window in this function between the allocation and this
391 // assignment.
392 *((_Jv_VTable **) arr) = klass->vtable;
393 return arr;
394 }
395
396 jcharArray
397 JvNewCharArray (jint length)
398 {
399 return (jcharArray) _Jv_NewPrimArray (JvPrimClass (char), length);
400 }
401
402 jbooleanArray
403 JvNewBooleanArray (jint length)
404 {
405 return (jbooleanArray) _Jv_NewPrimArray (JvPrimClass (boolean), length);
406 }
407
408 jbyteArray
409 JvNewByteArray (jint length)
410 {
411 return (jbyteArray) _Jv_NewPrimArray (JvPrimClass (byte), length);
412 }
413
414 jshortArray
415 JvNewShortArray (jint length)
416 {
417 return (jshortArray) _Jv_NewPrimArray (JvPrimClass (short), length);
418 }
419
420 jintArray
421 JvNewIntArray (jint length)
422 {
423 return (jintArray) _Jv_NewPrimArray (JvPrimClass (int), length);
424 }
425
426 jlongArray
427 JvNewLongArray (jint length)
428 {
429 return (jlongArray) _Jv_NewPrimArray (JvPrimClass (long), length);
430 }
431
432 jfloatArray
433 JvNewFloatArray (jint length)
434 {
435 return (jfloatArray) _Jv_NewPrimArray (JvPrimClass (float), length);
436 }
437
438 jdoubleArray
439 JvNewDoubleArray (jint length)
440 {
441 return (jdoubleArray) _Jv_NewPrimArray (JvPrimClass (double), length);
442 }
443
444 jobject
445 _Jv_NewArray (jint type, jint size)
446 {
447 switch (type)
448 {
449 case 4: return JvNewBooleanArray (size);
450 case 5: return JvNewCharArray (size);
451 case 6: return JvNewFloatArray (size);
452 case 7: return JvNewDoubleArray (size);
453 case 8: return JvNewByteArray (size);
454 case 9: return JvNewShortArray (size);
455 case 10: return JvNewIntArray (size);
456 case 11: return JvNewLongArray (size);
457 }
458 JvFail ("newarray - bad type code");
459 return NULL; // Placate compiler.
460 }
461
462 jobject
463 _Jv_NewMultiArray (jclass type, jint dimensions, jint *sizes)
464 {
465 JvAssert (type->isArray());
466 jclass element_type = type->getComponentType();
467 jobject result;
468 if (element_type->isPrimitive())
469 result = _Jv_NewPrimArray (element_type, sizes[0]);
470 else
471 result = _Jv_NewObjectArray (sizes[0], element_type, NULL);
472
473 if (dimensions > 1)
474 {
475 JvAssert (! element_type->isPrimitive());
476 JvAssert (element_type->isArray());
477 jobject *contents = elements ((jobjectArray) result);
478 for (int i = 0; i < sizes[0]; ++i)
479 contents[i] = _Jv_NewMultiArray (element_type, dimensions - 1,
480 sizes + 1);
481 }
482
483 return result;
484 }
485
486 jobject
487 _Jv_NewMultiArray (jclass array_type, jint dimensions, ...)
488 {
489 va_list args;
490 jint sizes[dimensions];
491 va_start (args, dimensions);
492 for (int i = 0; i < dimensions; ++i)
493 {
494 jint size = va_arg (args, jint);
495 sizes[i] = size;
496 }
497 va_end (args);
498
499 return _Jv_NewMultiArray (array_type, dimensions, sizes);
500 }
501
502 \f
503
504 class _Jv_PrimClass : public java::lang::Class
505 {
506 public:
507 // FIXME: calling convention is weird. If we use the natural types
508 // then the compiler will complain because they aren't Java types.
509 _Jv_PrimClass (jobject cname, jbyte sig, jint len)
510 {
511 using namespace java::lang::reflect;
512
513 // We must initialize every field of the class. We do this in
514 // the same order they are declared in Class.h.
515 next = NULL;
516 name = _Jv_makeUtf8Const ((char *) cname, -1);
517 accflags = Modifier::PUBLIC | Modifier::FINAL;
518 superclass = NULL;
519 constants.size = 0;
520 constants.tags = NULL;
521 constants.data = NULL;
522 methods = NULL;
523 method_count = sig;
524 vtable_method_count = 0;
525 fields = NULL;
526 size_in_bytes = len;
527 field_count = 0;
528 static_field_count = 0;
529 vtable = JV_PRIMITIVE_VTABLE;
530 interfaces = NULL;
531 loader = NULL;
532 interface_count = 0;
533 state = 0; // FIXME.
534 thread = NULL;
535 }
536 };
537
538 #define DECLARE_PRIM_TYPE(NAME, SIG, LEN) \
539 _Jv_PrimClass _Jv_##NAME##Class((jobject) #NAME, (jbyte) SIG, (jint) LEN)
540
541 DECLARE_PRIM_TYPE(byte, 'B', 1);
542 DECLARE_PRIM_TYPE(short, 'S', 2);
543 DECLARE_PRIM_TYPE(int, 'I', 4);
544 DECLARE_PRIM_TYPE(long, 'J', 8);
545 DECLARE_PRIM_TYPE(boolean, 'Z', 1);
546 DECLARE_PRIM_TYPE(char, 'C', 2);
547 DECLARE_PRIM_TYPE(float, 'F', 4);
548 DECLARE_PRIM_TYPE(double, 'D', 8);
549 DECLARE_PRIM_TYPE(void, 'V', 0);
550
551 jclass
552 _Jv_FindClassFromSignature (char *sig, java::lang::ClassLoader *loader)
553 {
554 switch (*sig)
555 {
556 case 'B':
557 return JvPrimClass (byte);
558 case 'S':
559 return JvPrimClass (short);
560 case 'I':
561 return JvPrimClass (int);
562 case 'J':
563 return JvPrimClass (long);
564 case 'Z':
565 return JvPrimClass (boolean);
566 case 'C':
567 return JvPrimClass (char);
568 case 'F':
569 return JvPrimClass (float);
570 case 'D':
571 return JvPrimClass (double);
572 case 'V':
573 return JvPrimClass (void);
574 case 'L':
575 {
576 int i;
577 for (i = 1; sig[i] && sig[i] != ';'; ++i)
578 ;
579 _Jv_Utf8Const *name = _Jv_makeUtf8Const (&sig[1], i - 1);
580 return _Jv_FindClass (name, loader);
581
582 }
583 case '[':
584 return _Jv_FindArrayClass (_Jv_FindClassFromSignature (&sig[1], loader),
585 loader);
586 }
587 JvFail ("couldn't understand class signature");
588 return NULL; // Placate compiler.
589 }
590
591 \f
592
593 JArray<jstring> *
594 JvConvertArgv (int argc, const char **argv)
595 {
596 if (argc < 0)
597 argc = 0;
598 jobjectArray ar = JvNewObjectArray(argc, &StringClass, NULL);
599 jobject* ptr = elements(ar);
600 for (int i = 0; i < argc; i++)
601 {
602 const char *arg = argv[i];
603 // FIXME - should probably use JvNewStringUTF.
604 *ptr++ = JvNewStringLatin1(arg, strlen(arg));
605 }
606 return (JArray<jstring>*) ar;
607 }
608
609 // FIXME: These variables are static so that they will be
610 // automatically scanned by the Boehm collector. This is needed
611 // because with qthreads the collector won't scan the initial stack --
612 // it will only scan the qthreads stacks.
613
614 // Command line arguments.
615 static jobject arg_vec;
616
617 // The primary threadgroup.
618 static java::lang::ThreadGroup *main_group;
619
620 // The primary thread.
621 static java::lang::Thread *main_thread;
622
623 static void
624 main_init (void)
625 {
626 INIT_SEGV;
627 #ifdef HANDLE_FPE
628 INIT_FPE;
629 #else
630 arithexception = new java::lang::ArithmeticException
631 (JvNewStringLatin1 ("/ by zero"));
632 #endif
633
634 no_memory = new java::lang::OutOfMemoryError;
635
636 #ifdef USE_LTDL
637 LTDL_SET_PRELOADED_SYMBOLS ();
638 #endif
639
640 // FIXME: we only want this on POSIX systems.
641 struct sigaction act;
642 act.sa_handler = SIG_IGN;
643 sigemptyset (&act.sa_mask);
644 act.sa_flags = 0;
645 sigaction (SIGPIPE, &act, NULL);
646 }
647
648 #ifndef DISABLE_GETENV_PROPERTIES
649
650 static char *
651 next_property_key (char *s, size_t *length)
652 {
653 size_t l = 0;
654
655 JvAssert (s);
656
657 // Skip over whitespace
658 while (isspace (*s))
659 s++;
660
661 // If we've reached the end, return NULL. Also return NULL if for
662 // some reason we've come across a malformed property string.
663 if (*s == 0
664 || *s == ':'
665 || *s == '=')
666 return NULL;
667
668 // Determine the length of the property key.
669 while (s[l] != 0
670 && ! isspace (s[l])
671 && s[l] != ':'
672 && s[l] != '=')
673 {
674 if (s[l] == '\\'
675 && s[l+1] != 0)
676 l++;
677 l++;
678 }
679
680 *length = l;
681
682 return s;
683 }
684
685 static char *
686 next_property_value (char *s, size_t *length)
687 {
688 size_t l = 0;
689
690 JvAssert (s);
691
692 while (isspace (*s))
693 s++;
694
695 if (*s == ':'
696 || *s == '=')
697 s++;
698
699 while (isspace (*s))
700 s++;
701
702 // If we've reached the end, return NULL.
703 if (*s == 0)
704 return NULL;
705
706 // Determine the length of the property value.
707 while (s[l] != 0
708 && ! isspace (s[l])
709 && s[l] != ':'
710 && s[l] != '=')
711 {
712 if (s[l] == '\\'
713 && s[l+1] != 0)
714 l += 2;
715 else
716 l++;
717 }
718
719 *length = l;
720
721 return s;
722 }
723
724 static void
725 process_gcj_properties ()
726 {
727 char *props = getenv("GCJ_PROPERTIES");
728 char *p = props;
729 size_t length;
730 size_t property_count = 0;
731
732 if (NULL == props)
733 return;
734
735 // Whip through props quickly in order to count the number of
736 // property values.
737 while (p && (p = next_property_key (p, &length)))
738 {
739 // Skip to the end of the key
740 p += length;
741
742 p = next_property_value (p, &length);
743 if (p)
744 p += length;
745
746 property_count++;
747 }
748
749 // Allocate an array of property value/key pairs.
750 _Jv_Environment_Properties =
751 (property_pair *) malloc (sizeof(property_pair)
752 * (property_count + 1));
753
754 // Go through the properties again, initializing _Jv_Properties
755 // along the way.
756 p = props;
757 property_count = 0;
758 while (p && (p = next_property_key (p, &length)))
759 {
760 _Jv_Environment_Properties[property_count].key = p;
761 _Jv_Environment_Properties[property_count].key_length = length;
762
763 // Skip to the end of the key
764 p += length;
765
766 p = next_property_value (p, &length);
767
768 _Jv_Environment_Properties[property_count].value = p;
769 _Jv_Environment_Properties[property_count].value_length = length;
770
771 if (p)
772 p += length;
773
774 property_count++;
775 }
776 memset ((void *) &_Jv_Environment_Properties[property_count],
777 0, sizeof (property_pair));
778 {
779 size_t i = 0;
780
781 // Null terminate the strings.
782 while (_Jv_Environment_Properties[i].key)
783 {
784 _Jv_Environment_Properties[i].key[_Jv_Environment_Properties[i].key_length] = 0;
785 _Jv_Environment_Properties[i++].value[_Jv_Environment_Properties[i].value_length] = 0;
786 }
787 }
788 }
789 #endif // DISABLE_GETENV_PROPERTIES
790
791 void
792 JvRunMain (jclass klass, int argc, const char **argv)
793 {
794 PROCESS_GCJ_PROPERTIES;
795
796 main_init ();
797
798 arg_vec = JvConvertArgv (argc - 1, argv + 1);
799 main_group = new java::lang::ThreadGroup (23);
800 main_thread = new java::lang::FirstThread (main_group, klass, arg_vec);
801
802 main_thread->start();
803 _Jv_ThreadWait ();
804
805 java::lang::Runtime::getRuntime ()->exit (0);
806 }
807
808 void
809 _Jv_RunMain (const char *class_name, int argc, const char **argv)
810 {
811 PROCESS_GCJ_PROPERTIES;
812
813 main_init ();
814
815 arg_vec = JvConvertArgv (argc - 1, argv + 1);
816 main_group = new java::lang::ThreadGroup (23);
817 main_thread = new java::lang::FirstThread (main_group,
818 JvNewStringLatin1 (class_name),
819 arg_vec);
820 main_thread->start();
821 _Jv_ThreadWait ();
822
823 java::lang::Runtime::getRuntime ()->exit (0);
824 }
825
826 \f
827
828 // Parse a string and return a heap size.
829 static size_t
830 parse_heap_size (const char *spec)
831 {
832 char *end;
833 unsigned long val = strtoul (spec, &end, 10);
834 if (*end == 'k' || *end == 'K')
835 val *= 1024;
836 else if (*end == 'm' || *end == 'M')
837 val *= 1048576;
838 return (size_t) val;
839 }
840
841 // Set the initial heap size. This might be ignored by the GC layer.
842 // This must be called before _Jv_RunMain.
843 void
844 _Jv_SetInitialHeapSize (const char *arg)
845 {
846 size_t size = parse_heap_size (arg);
847 _Jv_GCSetInitialHeapSize (size);
848 }
849
850 // Set the maximum heap size. This might be ignored by the GC layer.
851 // This must be called before _Jv_RunMain.
852 void
853 _Jv_SetMaximumHeapSize (const char *arg)
854 {
855 size_t size = parse_heap_size (arg);
856 _Jv_GCSetMaximumHeapSize (size);
857 }
858
859 \f
860
861 void *
862 _Jv_Malloc (jsize size)
863 {
864 if (size == 0)
865 size = 1;
866 void *ptr = malloc ((size_t) size);
867 if (ptr == NULL)
868 JvThrow (no_memory);
869 return ptr;
870 }
871
872 void
873 _Jv_Free (void* ptr)
874 {
875 return free (ptr);
876 }
877
878 \f
879
880 // In theory, these routines can be #ifdef'd away on machines which
881 // support divide overflow signals. However, we never know if some
882 // code might have been compiled with "-fuse-divide-subroutine", so we
883 // always include them in libgcj.
884
885 jint
886 _Jv_divI (jint dividend, jint divisor)
887 {
888 if (divisor == 0)
889 _Jv_Throw (arithexception);
890
891 if (dividend == (jint) 0x80000000L && divisor == -1)
892 return dividend;
893
894 return dividend / divisor;
895 }
896
897 jint
898 _Jv_remI (jint dividend, jint divisor)
899 {
900 if (divisor == 0)
901 _Jv_Throw (arithexception);
902
903 if (dividend == (jint) 0x80000000L && divisor == -1)
904 return 0;
905
906 return dividend % divisor;
907 }
908
909 jlong
910 _Jv_divJ (jlong dividend, jlong divisor)
911 {
912 if (divisor == 0)
913 _Jv_Throw (arithexception);
914
915 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
916 return dividend;
917
918 return dividend / divisor;
919 }
920
921 jlong
922 _Jv_remJ (jlong dividend, jlong divisor)
923 {
924 if (divisor == 0)
925 _Jv_Throw (arithexception);
926
927 if (dividend == (jlong) 0x8000000000000000LL && divisor == -1)
928 return 0;
929
930 return dividend % divisor;
931 }