]> gcc.gnu.org Git - gcc.git/blame - libjava/boehm.cc
re PR java/1586 (linux build problem for libjava - cpplib (?))
[gcc.git] / libjava / boehm.cc
CommitLineData
ee9dd372
TT
1// boehm.cc - interface between libjava and Boehm GC.
2
2ba5f774 3/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
ee9dd372
TT
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
11#include <config.h>
12
13#include <stdio.h>
14
ee9dd372 15#include <jvm.h>
27e934d8
TT
16#include <gcj/cni.h>
17
18#include <java/lang/Class.h>
a52dee2f 19#include <java/lang/reflect/Modifier.h>
58eb6e7c 20#include <java-interp.h>
ee9dd372 21
657ac766
TT
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
ee9dd372
TT
27extern "C"
28{
ee9dd372
TT
29#include <gc_priv.h>
30#include <gc_mark.h>
bf3b8e42 31#include <include/gc_gcj.h>
ee9dd372
TT
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
ee9dd372
TT
52\f
53
54// Nonzero if this module has been initialized.
55static int initialized = 0;
56
bf3b8e42 57#if 0
ee9dd372
TT
58// `kind' index used when allocating Java objects.
59static int obj_kind_x;
60
ee9dd372
TT
61// Freelist used for Java objects.
62static ptr_t *obj_free_list;
bf3b8e42
HB
63#endif /* 0 */
64
65// `kind' index used when allocating Java arrays.
66static int array_kind_x;
ee9dd372
TT
67
68// Freelist used for Java arrays.
69static ptr_t *array_free_list;
70
54c2f04b
AG
71// Lock used to protect access to Boehm's GC_enable/GC_disable functions.
72static _Jv_Mutex_t disable_gc_mutex;
73
ee9dd372
TT
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.
79void *
bf3b8e42 80_Jv_MarkObj (void *addr, void *msp, void *msl, void * /* env */)
ee9dd372
TT
81{
82 mse *mark_stack_ptr = (mse *) msp;
83 mse *mark_stack_limit = (mse *) msl;
84 jobject obj = (jobject) addr;
85
bf3b8e42
HB
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
ee9dd372 90 _Jv_VTable *dt = *(_Jv_VTable **) addr;
bf3b8e42
HB
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))
ee9dd372
TT
98 return mark_stack_ptr;
99 jclass klass = dt->clas;
100
101 // Every object has a sync_info pointer.
4824d1bb
BM
102 ptr_t p = (ptr_t) obj->sync_info;
103 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o1label);
ee9dd372 104 // Mark the object's class.
4824d1bb
BM
105 p = (ptr_t) klass;
106 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label);
ee9dd372 107
1d336a09 108 if (__builtin_expect (klass == &java::lang::Class::class$, false))
ee9dd372 109 {
bf3b8e42
HB
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
ee9dd372
TT
122 jclass c = (jclass) addr;
123
4824d1bb
BM
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);
ee9dd372
TT
128 for (int i = 0; i < c->constants.size; ++i)
129 {
7941ceab 130 /* FIXME: We could make this more precise by using the tags -KKT */
4824d1bb
BM
131 p = (ptr_t) c->constants.data[i].p;
132 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5label);
ee9dd372
TT
133 }
134
58eb6e7c
AG
135#ifdef INTERPRETER
136 if (_Jv_IsInterpretedClass (c))
137 {
4824d1bb
BM
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);
bf3b8e42
HB
142 p = (ptr_t) c->vtable;
143 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5clabel);
58eb6e7c
AG
144 }
145#endif
146
ee9dd372
TT
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.
4824d1bb
BM
150 p = (ptr_t) c->methods;
151 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c6label);
ee9dd372 152
58eb6e7c 153
ee9dd372
TT
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 {
4824d1bb
BM
160 p = (ptr_t) c->methods[i].name;
161 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
ee9dd372 162 cm1label);
4824d1bb
BM
163 p = (ptr_t) c->methods[i].signature;
164 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
ee9dd372 165 cm2label);
58eb6e7c 166
ee9dd372 167 // FIXME: `ncode' entry?
58eb6e7c
AG
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 {
4824d1bb
BM
174 p = (ptr_t) c->methods[i].ncode;
175 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
58eb6e7c
AG
176 cm3label);
177 }
178#endif
ee9dd372
TT
179 }
180 }
181
182 // Mark all the fields.
4824d1bb
BM
183 p = (ptr_t) c->fields;
184 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8label);
ee9dd372
TT
185 for (int i = 0; i < c->field_count; ++i)
186 {
58eb6e7c
AG
187 _Jv_Field* field = &c->fields[i];
188
ee9dd372 189#ifndef COMPACT_FIELDS
4824d1bb
BM
190 p = (ptr_t) field->name;
191 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8alabel);
ee9dd372 192#endif
4824d1bb
BM
193 p = (ptr_t) field->type;
194 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8blabel);
58eb6e7c
AG
195
196 // For the interpreter, we also need to mark the memory
197 // containing static members
a52dee2f 198 if ((field->flags & java::lang::reflect::Modifier::STATIC))
58eb6e7c 199 {
4824d1bb
BM
200 p = (ptr_t) field->u.addr;
201 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8clabel);
58eb6e7c
AG
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;
4824d1bb
BM
210 p = (ptr_t) val;
211 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
58eb6e7c
AG
212 c, c8elabel);
213 }
214 }
ee9dd372
TT
215 }
216
4824d1bb
BM
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);
ee9dd372
TT
221 for (int i = 0; i < c->interface_count; ++i)
222 {
4824d1bb
BM
223 p = (ptr_t) c->interfaces[i];
224 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cClabel);
ee9dd372 225 }
4824d1bb
BM
226 p = (ptr_t) c->loader;
227 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cBlabel);
58eb6e7c
AG
228
229#ifdef INTERPRETER
230 if (_Jv_IsInterpretedClass (c))
231 {
232 _Jv_InterpClass* ic = (_Jv_InterpClass*)c;
233
4824d1bb
BM
234 p = (ptr_t) ic->interpreted_methods;
235 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cElabel);
58eb6e7c
AG
236
237 for (int i = 0; i < c->method_count; i++)
238 {
4824d1bb
BM
239 p = (ptr_t) ic->interpreted_methods[i];
240 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
58eb6e7c
AG
241 cFlabel);
242 }
243
4824d1bb
BM
244 p = (ptr_t) ic->field_initializers;
245 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cGlabel);
58eb6e7c
AG
246
247 }
248#endif
249
ee9dd372
TT
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.
1d336a09 261 while (klass && klass != &java::lang::Object::class$)
ee9dd372
TT
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);
4824d1bb
BM
271 p = (ptr_t) val;
272 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
ee9dd372
TT
273 obj, elabel);
274 }
8a922095 275 field = field->getNextField ();
ee9dd372
TT
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.
287void *
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;
bf3b8e42
HB
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))
ee9dd372
TT
299 return mark_stack_ptr;
300 jclass klass = dt->clas;
301
302 // Every object has a sync_info pointer.
4824d1bb
BM
303 ptr_t p = (ptr_t) array->sync_info;
304 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e1label);
ee9dd372 305 // Mark the object's class.
4824d1bb
BM
306 p = (ptr_t) klass;
307 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label);
ee9dd372
TT
308
309 for (int i = 0; i < JvGetArrayLength (array); ++i)
310 {
311 jobject obj = elements (array)[i];
4824d1bb
BM
312 p = (ptr_t) obj;
313 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e2label);
ee9dd372
TT
314 }
315
316 return mark_stack_ptr;
317}
318
bf3b8e42
HB
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)
326void *
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.
ee9dd372 335void *
bf3b8e42 336_Jv_AllocObj (jsize size, jclass klass)
ee9dd372 337{
bf3b8e42 338 return GC_GCJ_MALLOC (size, klass->vtable);
ee9dd372
TT
339}
340
bf3b8e42
HB
341// Allocate space for a new Java array.
342// Used only for arrays of objects.
ee9dd372 343void *
bf3b8e42 344_Jv_AllocArray (jsize size, jclass klass)
ee9dd372 345{
bf3b8e42
HB
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;
ee9dd372
TT
367}
368
369// Allocate some space that is known to be pointer-free.
370void *
371_Jv_AllocBytes (jsize size)
372{
bf3b8e42 373 void *r = GC_MALLOC_ATOMIC (size);
38b3a2c0
TT
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.
1143c0a0 378 if (__builtin_expect (r != NULL, !NULL))
38b3a2c0
TT
379 memset (r, 0, size);
380 return r;
ee9dd372
TT
381}
382
383static void
384call_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
392void
393_Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *meth)
394{
25fef12b 395 GC_REGISTER_FINALIZER_NO_ORDER (object, call_finalizer, (GC_PTR) meth,
ee9dd372
TT
396 NULL, NULL);
397}
398
399void
400_Jv_RunFinalizers (void)
401{
402 GC_invoke_finalizers ();
403}
404
405void
406_Jv_RunAllFinalizers (void)
407{
408 GC_finalize_all ();
409}
410
411void
412_Jv_RunGC (void)
413{
414 GC_gcollect ();
415}
416
417long
418_Jv_GCTotalMemory (void)
419{
420 return GC_get_heap_size ();
421}
422
ee9dd372
TT
423long
424_Jv_GCFreeMemory (void)
425{
e40217a7 426 return GC_get_free_bytes ();
ee9dd372
TT
427}
428
b8c3c4f0
TT
429void
430_Jv_GCSetInitialHeapSize (size_t size)
431{
432 size_t current = GC_get_heap_size ();
433 if (size > current)
3948f9d0 434 GC_expand_hp (size - current);
b8c3c4f0
TT
435}
436
437void
438_Jv_GCSetMaximumHeapSize (size_t size)
439{
440 GC_set_max_heap_size ((GC_word) size);
441}
442
54c2f04b
AG
443// From boehm's misc.c
444extern "C" void GC_enable();
445extern "C" void GC_disable();
446
447void
448_Jv_DisableGC (void)
449{
450 _Jv_MutexLock (&disable_gc_mutex);
451 GC_disable();
452 _Jv_MutexUnlock (&disable_gc_mutex);
453}
454
455void
456_Jv_EnableGC (void)
457{
458 _Jv_MutexLock (&disable_gc_mutex);
459 GC_enable();
460 _Jv_MutexUnlock (&disable_gc_mutex);
461}
462
ee9dd372
TT
463void
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;
bf3b8e42
HB
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
513void
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;
ee9dd372 529
e40217a7
TT
530 GC_java_finalization = 1;
531
ee9dd372
TT
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
54c2f04b
AG
565 _Jv_MutexInit (&disable_gc_mutex);
566
ee9dd372
TT
567 UNLOCK ();
568 ENABLE_SIGNALS ();
569}
bf3b8e42 570#endif /* 0 */
This page took 0.206739 seconds and 5 git commands to generate.