OSDN Git Service

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