877d97c9d9c8eed1c255d739193e54f81cb05626
[gcc.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
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
302 // Every object has a sync_info pointer.
303 ptr_t p = (ptr_t) array->sync_info;
304 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e1label);
305 // Mark the object's class.
306 p = (ptr_t) klass;
307 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label);
308
309 for (int i = 0; i < JvGetArrayLength (array); ++i)
310 {
311 jobject obj = elements (array)[i];
312 p = (ptr_t) obj;
313 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e2label);
314 }
315
316 return mark_stack_ptr;
317 }
318
319 // Return GC descriptor for interpreted class
320 #ifdef INTERPRETER
321
322 // We assume that the gcj mark proc has index 0. This is a dubious assumption,
323 // since another one could be registered first. But the compiler also
324 // knows this, so in that case everything else will break, too.
325 #define GCJ_DEFAULT_DESCR MAKE_PROC(GCJ_RESERVED_MARK_PROC_INDEX,0)
326 void *
327 _Jv_BuildGCDescr(jclass klass)
328 {
329 /* FIXME: We should really look at the class and build the descriptor. */
330 return (void *)(GCJ_DEFAULT_DESCR);
331 }
332 #endif
333
334 // Allocate space for a new Java object.
335 void *
336 _Jv_AllocObj (jsize size, jclass klass)
337 {
338 return GC_GCJ_MALLOC (size, klass->vtable);
339 }
340
341 // Allocate space for a new Java array.
342 // Used only for arrays of objects.
343 void *
344 _Jv_AllocArray (jsize size, jclass klass)
345 {
346 void *obj;
347 const jsize min_heap_addr = 16*1024;
348 // A heuristic. If size is less than this value, the size
349 // stored in the array can't possibly be misinterpreted as
350 // a pointer. Thus we lose nothing by scanning the object
351 // completely conservatively, since no misidentification can
352 // take place.
353
354 #ifdef GC_DEBUG
355 // There isn't much to lose by scanning this conservatively.
356 // If we didn't, the mark proc would have to understand that
357 // it needed to skip the header.
358 obj = GC_MALLOC(size);
359 #else
360 if (size < min_heap_addr)
361 obj = GC_MALLOC(size);
362 else
363 obj = GC_GENERIC_MALLOC (size, array_kind_x);
364 #endif
365 *((_Jv_VTable **) obj) = klass->vtable;
366 return obj;
367 }
368
369 // Allocate some space that is known to be pointer-free.
370 void *
371 _Jv_AllocBytes (jsize size)
372 {
373 void *r = GC_MALLOC_ATOMIC (size);
374 // We have to explicitly zero memory here, as the GC doesn't
375 // guarantee that PTRFREE allocations are zeroed. Note that we
376 // don't have to do this for other allocation types because we set
377 // the `ok_init' flag in the type descriptor.
378 if (__builtin_expect (r != NULL, !NULL))
379 memset (r, 0, size);
380 return r;
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 void
464 _Jv_InitGC (void)
465 {
466 int proc;
467 DCL_LOCK_STATE;
468
469 DISABLE_SIGNALS ();
470 LOCK ();
471
472 if (initialized)
473 {
474 UNLOCK ();
475 ENABLE_SIGNALS ();
476 return;
477 }
478 initialized = 1;
479 UNLOCK ();
480
481 // Configure the collector to use the bitmap marking descriptors that we
482 // stash in the class vtable.
483 GC_init_gcj_malloc (0, (void *) _Jv_MarkObj);
484
485 LOCK ();
486 GC_java_finalization = 1;
487
488 // We use a different mark procedure for object arrays. This code
489 // configures a different object `kind' for object array allocation and
490 // marking. FIXME: see above.
491 array_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
492 * sizeof (ptr_t),
493 PTRFREE);
494 memset (array_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
495
496 proc = GC_n_mark_procs++;
497 GC_mark_procs[proc] = (mark_proc) _Jv_MarkArray;
498
499 array_kind_x = GC_n_kinds++;
500 GC_obj_kinds[array_kind_x].ok_freelist = array_free_list;
501 GC_obj_kinds[array_kind_x].ok_reclaim_list = 0;
502 GC_obj_kinds[array_kind_x].ok_descriptor = MAKE_PROC (proc, 0);
503 GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE;
504 GC_obj_kinds[array_kind_x].ok_init = TRUE;
505
506 _Jv_MutexInit (&disable_gc_mutex);
507
508 UNLOCK ();
509 ENABLE_SIGNALS ();
510 }
511
512 #if 0
513 void
514 _Jv_InitGC (void)
515 {
516 int proc;
517 DCL_LOCK_STATE;
518
519 DISABLE_SIGNALS ();
520 LOCK ();
521
522 if (initialized)
523 {
524 UNLOCK ();
525 ENABLE_SIGNALS ();
526 return;
527 }
528 initialized = 1;
529
530 GC_java_finalization = 1;
531
532 // Set up state for marking and allocation of Java objects.
533 obj_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
534 * sizeof (ptr_t),
535 PTRFREE);
536 memset (obj_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
537
538 proc = GC_n_mark_procs++;
539 GC_mark_procs[proc] = (mark_proc) _Jv_MarkObj;
540
541 obj_kind_x = GC_n_kinds++;
542 GC_obj_kinds[obj_kind_x].ok_freelist = obj_free_list;
543 GC_obj_kinds[obj_kind_x].ok_reclaim_list = 0;
544 GC_obj_kinds[obj_kind_x].ok_descriptor = MAKE_PROC (proc, 0);
545 GC_obj_kinds[obj_kind_x].ok_relocate_descr = FALSE;
546 GC_obj_kinds[obj_kind_x].ok_init = TRUE;
547
548 // Set up state for marking and allocation of arrays of Java
549 // objects.
550 array_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
551 * sizeof (ptr_t),
552 PTRFREE);
553 memset (array_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
554
555 proc = GC_n_mark_procs++;
556 GC_mark_procs[proc] = (mark_proc) _Jv_MarkArray;
557
558 array_kind_x = GC_n_kinds++;
559 GC_obj_kinds[array_kind_x].ok_freelist = array_free_list;
560 GC_obj_kinds[array_kind_x].ok_reclaim_list = 0;
561 GC_obj_kinds[array_kind_x].ok_descriptor = MAKE_PROC (proc, 0);
562 GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE;
563 GC_obj_kinds[array_kind_x].ok_init = TRUE;
564
565 _Jv_MutexInit (&disable_gc_mutex);
566
567 UNLOCK ();
568 ENABLE_SIGNALS ();
569 }
570 #endif /* 0 */