OSDN Git Service

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