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