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