OSDN Git Service

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