OSDN Git Service

* gnu/gcj/convert/Convert.java (version): Use java.vm.name
[pf3gnuchains/gcc-fork.git] / libjava / boehm.cc
1 // boehm.cc - interface between libjava and Boehm GC.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001  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 #include <stdio.h>
14
15 #include <jvm.h>
16 #include <gcj/cni.h>
17
18 #include <java/lang/Class.h>
19 #include <java/lang/reflect/Modifier.h>
20 #include <java-interp.h>
21
22 // More nastiness: the GC wants to define TRUE and FALSE.  We don't
23 // need the Java definitions (themselves a hack), so we undefine them.
24 #undef TRUE
25 #undef FALSE
26
27 extern "C"
28 {
29 #include <private/gc_priv.h>
30 #include <private/gc_pmark.h>
31 #include <gc_gcj.h>
32
33 #ifdef THREAD_LOCAL_ALLOC
34 # define GC_REDIRECT_TO_LOCAL
35 # include <gc_local_alloc.h>
36 #endif
37
38   // These aren't declared in any Boehm GC header.
39   void GC_finalize_all (void);
40   ptr_t GC_debug_generic_malloc (size_t size, int k, GC_EXTRA_PARAMS);
41 };
42
43 // We must check for plausibility ourselves.
44 #define MAYBE_MARK(Obj, Top, Limit, Source, Exit)  \
45         Top=GC_MARK_AND_PUSH((GC_PTR)Obj, Top, Limit, (GC_PTR *)Source)
46
47 \f
48
49 // Nonzero if this module has been initialized.
50 static int initialized = 0;
51
52 #if 0
53 // `kind' index used when allocating Java objects.
54 static int obj_kind_x;
55
56 // Freelist used for Java objects.
57 static ptr_t *obj_free_list;
58 #endif /* 0 */
59
60 // `kind' index used when allocating Java arrays.
61 static int array_kind_x;
62
63 // Freelist used for Java arrays.
64 static ptr_t *array_free_list;
65
66 // Lock used to protect access to Boehm's GC_enable/GC_disable functions.
67 static _Jv_Mutex_t disable_gc_mutex;
68
69 \f
70
71 // This is called by the GC during the mark phase.  It marks a Java
72 // object.  We use `void *' arguments and return, and not what the
73 // Boehm GC wants, to avoid pollution in our headers.
74 void *
75 _Jv_MarkObj (void *addr, void *msp, void *msl, void * /* env */)
76 {
77   mse *mark_stack_ptr = (mse *) msp;
78   mse *mark_stack_limit = (mse *) msl;
79   jobject obj = (jobject) addr;
80
81   // FIXME: if env is 1, this object was allocated through the debug
82   // interface, and addr points to the beginning of the debug header.
83   // In that case, we should really add the size of the header to addr.
84
85   _Jv_VTable *dt = *(_Jv_VTable **) addr;
86   // The object might not yet have its vtable set, or it might
87   // really be an object on the freelist.  In either case, the vtable slot
88   // will either be 0, or it will point to a cleared object.
89   // This assumes Java objects have size at least 3 words,
90   // including the header.   But this should remain true, since this
91   // should only be used with debugging allocation or with large objects.
92   if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
93     return mark_stack_ptr;
94   jclass klass = dt->clas;
95   ptr_t p;
96
97 # ifndef JV_HASH_SYNCHRONIZATION
98     // Every object has a sync_info pointer.
99     p = (ptr_t) obj->sync_info;
100     MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o1label);
101 # endif
102   // Mark the object's class.
103   p = (ptr_t) klass;
104   MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label);
105
106   if (__builtin_expect (klass == &java::lang::Class::class$, false))
107     {
108       // Currently we allocate some of the memory referenced from class objects
109       // as pointerfree memory, and then mark it more intelligently here.
110       // We ensure that the ClassClass mark descriptor forces invocation of
111       // this procedure.
112       // Correctness of this is subtle, but it looks OK to me for now.  For the incremental
113       // collector, we need to make sure that the class object is written whenever
114       // any of the subobjects are altered and may need rescanning.  This may be tricky
115       // during construction, and this may not be the right way to do this with
116       // incremental collection.
117       // If we overflow the mark stack, we will rescan the class object, so we should
118       // be OK.  The same applies if we redo the mark phase because win32 unmapped part
119       // of our root set.               - HB
120       jclass c = (jclass) addr;
121
122       p = (ptr_t) c->name;
123       MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c3label);
124       p = (ptr_t) c->superclass;
125       MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c4label);
126       for (int i = 0; i < c->constants.size; ++i)
127         {
128           /* FIXME: We could make this more precise by using the tags -KKT */
129           p = (ptr_t) c->constants.data[i].p;
130           MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5label);
131         }
132
133 #ifdef INTERPRETER
134       if (_Jv_IsInterpretedClass (c))
135         {
136           p = (ptr_t) c->constants.tags;
137           MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5alabel);
138           p = (ptr_t) c->constants.data;
139           MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5blabel);
140           p = (ptr_t) c->vtable;
141           MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5clabel);
142         }
143 #endif
144
145       // If the class is an array, then the methods field holds a
146       // pointer to the element class.  If the class is primitive,
147       // then the methods field holds a pointer to the array class.
148       p = (ptr_t) c->methods;
149       MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c6label);
150
151
152       if (! c->isArray() && ! c->isPrimitive())
153         {
154           // Scan each method in the cases where `methods' really
155           // points to a methods structure.
156           for (int i = 0; i < c->method_count; ++i)
157             {
158               p = (ptr_t) c->methods[i].name;
159               MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
160                              cm1label);
161               p = (ptr_t) c->methods[i].signature;
162               MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
163                              cm2label);
164
165               // FIXME: `ncode' entry?
166
167 #ifdef INTERPRETER
168               // The interpreter installs a heap-allocated
169               // trampoline here, so we'll mark it. 
170               if (_Jv_IsInterpretedClass (c))
171                   {
172                       p = (ptr_t) c->methods[i].ncode;
173                       MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
174                                   cm3label);
175                   }
176 #endif
177             }
178         }
179
180       // Mark all the fields.
181       p = (ptr_t) c->fields;
182       MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8label);
183       for (int i = 0; i < c->field_count; ++i)
184         {
185           _Jv_Field* field = &c->fields[i];
186
187 #ifndef COMPACT_FIELDS
188           p = (ptr_t) field->name;
189           MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8alabel);
190 #endif
191           p = (ptr_t) field->type;
192           MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8blabel);
193
194           // For the interpreter, we also need to mark the memory
195           // containing static members
196           if ((field->flags & java::lang::reflect::Modifier::STATIC))
197             {
198               p = (ptr_t) field->u.addr;
199               MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8clabel);
200
201               // also, if the static member is a reference,
202               // mark also the value pointed to.  We check for isResolved
203               // since marking can happen before memory is allocated for
204               // static members.
205               if (JvFieldIsRef (field) && field->isResolved()) 
206                 {
207                   jobject val = *(jobject*) field->u.addr;
208                   p = (ptr_t) val;
209                   MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
210                               c, c8elabel);
211                 }
212             }
213         }
214
215       p = (ptr_t) c->vtable;
216       MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c9label);
217       p = (ptr_t) c->interfaces;
218       MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cAlabel);
219       for (int i = 0; i < c->interface_count; ++i)
220         {
221           p = (ptr_t) c->interfaces[i];
222           MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cClabel);
223         }
224       p = (ptr_t) c->loader;
225       MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cBlabel);
226       p = (ptr_t) c->arrayclass;
227       MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cDlabel);
228
229 #ifdef INTERPRETER
230       if (_Jv_IsInterpretedClass (c))
231         {
232           _Jv_InterpClass* ic = (_Jv_InterpClass*)c;
233
234           p = (ptr_t) ic->interpreted_methods;
235           MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cElabel);
236
237           for (int i = 0; i < c->method_count; i++)
238             {
239               p = (ptr_t) ic->interpreted_methods[i];
240               MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
241                           cFlabel);
242             }
243
244           p = (ptr_t) ic->field_initializers;
245           MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cGlabel);
246           
247         }
248 #endif
249
250     }
251   else
252     {
253       // NOTE: each class only holds information about the class
254       // itself.  So we must do the marking for the entire inheritance
255       // tree in order to mark all fields.  FIXME: what about
256       // interfaces?  We skip Object here, because Object only has a
257       // sync_info, and we handled that earlier.
258       // Note: occasionally `klass' can be null.  For instance, this
259       // can happen if a GC occurs between the point where an object
260       // is allocated and where the vtbl slot is set.
261       while (klass && klass != &java::lang::Object::class$)
262         {
263           jfieldID field = JvGetFirstInstanceField (klass);
264           jint max = JvNumInstanceFields (klass);
265
266           for (int i = 0; i < max; ++i)
267             {
268               if (JvFieldIsRef (field))
269                 {
270                   jobject val = JvGetObjectField (obj, field);
271                   p = (ptr_t) val;
272                   MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
273                               obj, elabel);
274                 }
275               field = field->getNextField ();
276             }
277           klass = klass->getSuperclass();
278         }
279     }
280
281   return mark_stack_ptr;
282 }
283
284 // This is called by the GC during the mark phase.  It marks a Java
285 // array (of objects).  We use `void *' arguments and return, and not
286 // what the Boehm GC wants, to avoid pollution in our headers.
287 void *
288 _Jv_MarkArray (void *addr, void *msp, void *msl, void * /*env*/)
289 {
290   mse *mark_stack_ptr = (mse *) msp;
291   mse *mark_stack_limit = (mse *) msl;
292   jobjectArray array = (jobjectArray) addr;
293
294   _Jv_VTable *dt = *(_Jv_VTable **) addr;
295   // Assumes size >= 3 words.  That's currently true since arrays have
296   // a vtable, sync pointer, and size.  If the sync pointer goes away,
297   // we may need to round up the size.
298   if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
299     return mark_stack_ptr;
300   jclass klass = dt->clas;
301   ptr_t p;
302
303 # ifndef JV_HASH_SYNCHRONIZATION
304     // Every object has a sync_info pointer.
305     p = (ptr_t) array->sync_info;
306     MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e1label);
307 # endif
308   // Mark the object's class.
309   p = (ptr_t) klass;
310   MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, &(dt -> clas), o2label);
311
312   for (int i = 0; i < JvGetArrayLength (array); ++i)
313     {
314       jobject obj = elements (array)[i];
315       p = (ptr_t) obj;
316       MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e2label);
317     }
318
319   return mark_stack_ptr;
320 }
321
322 // Generate a GC marking descriptor for a class.
323 //
324 // We assume that the gcj mark proc has index 0.  This is a dubious assumption,
325 // since another one could be registered first.  But the compiler also
326 // knows this, so in that case everything else will break, too.
327 #define GCJ_DEFAULT_DESCR GC_MAKE_PROC(GC_GCJ_RESERVED_MARK_PROC_INDEX,0)
328 void *
329 _Jv_BuildGCDescr(jclass)
330 {
331   /* FIXME: We should really look at the class and build the descriptor. */
332   return (void *)(GCJ_DEFAULT_DESCR);
333 }
334
335 // Allocate some space that is known to be pointer-free.
336 void *
337 _Jv_AllocBytes (jsize size)
338 {
339   void *r = GC_MALLOC_ATOMIC (size);
340   // We have to explicitly zero memory here, as the GC doesn't
341   // guarantee that PTRFREE allocations are zeroed.  Note that we
342   // don't have to do this for other allocation types because we set
343   // the `ok_init' flag in the type descriptor.
344   memset (r, 0, size);
345   return r;
346 }
347
348 // Allocate space for a new Java array.
349 // Used only for arrays of objects.
350 void *
351 _Jv_AllocArray (jsize size, jclass klass)
352 {
353   void *obj;
354   const jsize min_heap_addr = 16*1024;
355   // A heuristic.  If size is less than this value, the size
356   // stored in the array can't possibly be misinterpreted as
357   // a pointer.   Thus we lose nothing by scanning the object
358   // completely conservatively, since no misidentification can
359   // take place.
360   
361 #ifdef GC_DEBUG
362   // There isn't much to lose by scanning this conservatively.
363   // If we didn't, the mark proc would have to understand that
364   // it needed to skip the header.
365   obj = GC_MALLOC(size);
366 #else
367   if (size < min_heap_addr) 
368     obj = GC_MALLOC(size);
369   else 
370     obj = GC_generic_malloc (size, array_kind_x);
371 #endif
372   *((_Jv_VTable **) obj) = klass->vtable;
373   return obj;
374 }
375
376 /* Allocate space for a new non-Java object, which does not have the usual 
377    Java object header but may contain pointers to other GC'ed objects. */
378 void *
379 _Jv_AllocRawObj (jsize size)
380 {
381   return (void *) GC_MALLOC (size);
382 }
383
384 static void
385 call_finalizer (GC_PTR obj, GC_PTR client_data)
386 {
387   _Jv_FinalizerFunc *fn = (_Jv_FinalizerFunc *) client_data;
388   jobject jobj = (jobject) obj;
389
390   (*fn) (jobj);
391 }
392
393 void
394 _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *meth)
395 {
396   GC_REGISTER_FINALIZER_NO_ORDER (object, call_finalizer, (GC_PTR) meth,
397                                   NULL, NULL);
398 }
399
400 void
401 _Jv_RunFinalizers (void)
402 {
403   GC_invoke_finalizers ();
404 }
405
406 void
407 _Jv_RunAllFinalizers (void)
408 {
409   GC_finalize_all ();
410 }
411
412 void
413 _Jv_RunGC (void)
414 {
415   GC_gcollect ();
416 }
417
418 long
419 _Jv_GCTotalMemory (void)
420 {
421   return GC_get_heap_size ();
422 }
423
424 long
425 _Jv_GCFreeMemory (void)
426 {
427   return GC_get_free_bytes ();
428 }
429
430 void
431 _Jv_GCSetInitialHeapSize (size_t size)
432 {
433   size_t current = GC_get_heap_size ();
434   if (size > current)
435     GC_expand_hp (size - current);
436 }
437
438 void
439 _Jv_GCSetMaximumHeapSize (size_t size)
440 {
441   GC_set_max_heap_size ((GC_word) size);
442 }
443
444 // From boehm's misc.c 
445 extern "C" void GC_enable();
446 extern "C" void GC_disable();
447
448 void
449 _Jv_DisableGC (void)
450 {
451   _Jv_MutexLock (&disable_gc_mutex); 
452   GC_disable();
453   _Jv_MutexUnlock (&disable_gc_mutex); 
454 }
455
456 void
457 _Jv_EnableGC (void)
458 {
459   _Jv_MutexLock (&disable_gc_mutex); 
460   GC_enable();
461   _Jv_MutexUnlock (&disable_gc_mutex); 
462 }
463
464 static void * handle_out_of_memory(size_t)
465 {
466   _Jv_ThrowNoMemory();
467 }
468
469 void
470 _Jv_InitGC (void)
471 {
472   int proc;
473   DCL_LOCK_STATE;
474
475   DISABLE_SIGNALS ();
476   LOCK ();
477
478   if (initialized)
479     {
480       UNLOCK ();
481       ENABLE_SIGNALS ();
482       return;
483     }
484   initialized = 1;
485   UNLOCK ();
486
487   // Configure the collector to use the bitmap marking descriptors that we
488   // stash in the class vtable.
489   GC_init_gcj_malloc (0, (void *) _Jv_MarkObj);  
490
491   // Cause an out of memory error to be thrown from the allocators,
492   // instead of returning 0.  This is cheaper than checking on allocation.
493   GC_oom_fn = handle_out_of_memory;
494
495   LOCK ();
496   GC_java_finalization = 1;
497
498   // We use a different mark procedure for object arrays. This code 
499   // configures a different object `kind' for object array allocation and
500   // marking. FIXME: see above.
501   array_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
502                                                        * sizeof (ptr_t),
503                                                        PTRFREE);
504   memset (array_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
505
506   proc = GC_n_mark_procs++;
507   GC_mark_procs[proc] = (GC_mark_proc) _Jv_MarkArray;
508
509   array_kind_x = GC_n_kinds++;
510   GC_obj_kinds[array_kind_x].ok_freelist = array_free_list;
511   GC_obj_kinds[array_kind_x].ok_reclaim_list = 0;
512   GC_obj_kinds[array_kind_x].ok_descriptor = GC_MAKE_PROC (proc, 0);
513   GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE;
514   GC_obj_kinds[array_kind_x].ok_init = TRUE;
515
516   _Jv_MutexInit (&disable_gc_mutex);
517
518   UNLOCK ();
519   ENABLE_SIGNALS ();
520 }
521
522 #ifdef JV_HASH_SYNCHRONIZATION
523 // Allocate an object with a fake vtable pointer, which causes only
524 // the first field (beyond the fake vtable pointer) to be traced.
525 // Eventually this should probably be generalized.
526
527 static _Jv_VTable trace_one_vtable = {
528     0,                  // class pointer
529     (void *)(2 * sizeof(void *)),
530                         // descriptor; scan 2 words incl. vtable ptr.
531                         // Least significant bits must be zero to
532                         // identify this as a length descriptor
533     {0}                 // First method
534 };
535
536 void *
537 _Jv_AllocTraceOne (jsize size /* includes vtable slot */) 
538 {
539   return GC_GCJ_MALLOC (size, &trace_one_vtable);
540 }
541
542 // Ditto for two words.
543 // the first field (beyond the fake vtable pointer) to be traced.
544 // Eventually this should probably be generalized.
545
546 static _Jv_VTable trace_two_vtable =
547 {
548   0,                    // class pointer
549   (void *)(3 * sizeof(void *)),
550                         // descriptor; scan 3 words incl. vtable ptr.
551   {0}                   // First method
552 };
553
554 void *
555 _Jv_AllocTraceTwo (jsize size /* includes vtable slot */) 
556 {
557   return GC_GCJ_MALLOC (size, &trace_two_vtable);
558 }
559
560 #endif /* JV_HASH_SYNCHRONIZATION */
561
562 void
563 _Jv_GCInitializeFinalizers (void (*notifier) (void))
564 {
565   GC_finalize_on_demand = 1;
566   GC_finalizer_notifier = notifier;
567 }
568
569 void
570 _Jv_GCRegisterDisappearingLink (jobject *objp)
571 {
572   GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp);
573 }
574
575 jboolean
576 _Jv_GCCanReclaimSoftReference (jobject)
577 {
578   // For now, always reclaim soft references.  FIXME.
579   return true;
580 }
581
582 #if 0
583 void
584 _Jv_InitGC (void)
585 {
586   int proc;
587   DCL_LOCK_STATE;
588
589   DISABLE_SIGNALS ();
590   LOCK ();
591
592   if (initialized)
593    {
594      UNLOCK ();
595      ENABLE_SIGNALS ();
596      return;
597    }
598   initialized = 1;
599
600   GC_java_finalization = 1;
601
602   // Set up state for marking and allocation of Java objects.
603   obj_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
604                                                      * sizeof (ptr_t),
605                                                      PTRFREE);
606   memset (obj_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
607
608   proc = GC_n_mark_procs++;
609   GC_mark_procs[proc] = (GC_mark_proc) _Jv_MarkObj;
610
611   obj_kind_x = GC_n_kinds++;
612   GC_obj_kinds[obj_kind_x].ok_freelist = obj_free_list;
613   GC_obj_kinds[obj_kind_x].ok_reclaim_list = 0;
614   GC_obj_kinds[obj_kind_x].ok_descriptor = GC_MAKE_PROC (proc, 0);
615   GC_obj_kinds[obj_kind_x].ok_relocate_descr = FALSE;
616   GC_obj_kinds[obj_kind_x].ok_init = TRUE;
617
618   // Set up state for marking and allocation of arrays of Java
619   // objects.
620   array_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
621                                                        * sizeof (ptr_t),
622                                                        PTRFREE);
623   memset (array_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
624
625   proc = GC_n_mark_procs++;
626   GC_mark_procs[proc] = (GC_mark_proc) _Jv_MarkArray;
627
628   array_kind_x = GC_n_kinds++;
629   GC_obj_kinds[array_kind_x].ok_freelist = array_free_list;
630   GC_obj_kinds[array_kind_x].ok_reclaim_list = 0;
631   GC_obj_kinds[array_kind_x].ok_descriptor = GC_MAKE_PROC (proc, 0);
632   GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE;
633   GC_obj_kinds[array_kind_x].ok_init = TRUE;
634
635   _Jv_MutexInit (&disable_gc_mutex);
636
637   UNLOCK ();
638   ENABLE_SIGNALS ();
639 }
640 #endif /* 0 */