1 // boehm.cc - interface between libjava and Boehm GC.
3 /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
18 #include <java/lang/Class.h>
19 #include <java/lang/reflect/Modifier.h>
20 #include <java-interp.h>
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.
29 #include <private/gc_priv.h>
30 #include <private/gc_pmark.h>
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
);
38 // FIXME: this should probably be defined in some GC header.
40 # define GC_GENERIC_MALLOC(Size, Type) \
41 GC_debug_generic_malloc (Size, Type, GC_EXTRAS)
43 # define GC_GENERIC_MALLOC(Size, Type) GC_generic_malloc (Size, Type)
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)
54 // Nonzero if this module has been initialized.
55 static int initialized
= 0;
58 // `kind' index used when allocating Java objects.
59 static int obj_kind_x
;
61 // Freelist used for Java objects.
62 static ptr_t
*obj_free_list
;
65 // `kind' index used when allocating Java arrays.
66 static int array_kind_x
;
68 // Freelist used for Java arrays.
69 static ptr_t
*array_free_list
;
71 // Lock used to protect access to Boehm's GC_enable/GC_disable functions.
72 static _Jv_Mutex_t disable_gc_mutex
;
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.
80 _Jv_MarkObj (void *addr
, void *msp
, void *msl
, void * /* env */)
82 mse
*mark_stack_ptr
= (mse
*) msp
;
83 mse
*mark_stack_limit
= (mse
*) msl
;
84 jobject obj
= (jobject
) addr
;
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.
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
;
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
);
107 // Mark the object's class.
109 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, obj
, o2label
);
111 if (__builtin_expect (klass
== &java::lang::Class::class$
, false))
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
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
;
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
)
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
);
139 if (_Jv_IsInterpretedClass (c
))
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
);
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
);
157 if (! c
->isArray() && ! c
->isPrimitive())
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
)
163 p
= (ptr_t
) c
->methods
[i
].name
;
164 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
,
166 p
= (ptr_t
) c
->methods
[i
].signature
;
167 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
,
170 // FIXME: `ncode' entry?
173 // The interpreter installs a heap-allocated
174 // trampoline here, so we'll mark it.
175 if (_Jv_IsInterpretedClass (c
))
177 p
= (ptr_t
) c
->methods
[i
].ncode
;
178 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
,
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
)
190 _Jv_Field
* field
= &c
->fields
[i
];
192 #ifndef COMPACT_FIELDS
193 p
= (ptr_t
) field
->name
;
194 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c8alabel
);
196 p
= (ptr_t
) field
->type
;
197 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c8blabel
);
199 // For the interpreter, we also need to mark the memory
200 // containing static members
201 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
))
203 p
= (ptr_t
) field
->u
.addr
;
204 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c8clabel
);
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
210 if (JvFieldIsRef (field
) && field
->isResolved())
212 jobject val
= *(jobject
*) field
->u
.addr
;
214 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
,
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
)
226 p
= (ptr_t
) c
->interfaces
[i
];
227 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, cClabel
);
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
);
235 if (_Jv_IsInterpretedClass (c
))
237 _Jv_InterpClass
* ic
= (_Jv_InterpClass
*)c
;
239 p
= (ptr_t
) ic
->interpreted_methods
;
240 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, ic
, cElabel
);
242 for (int i
= 0; i
< c
->method_count
; i
++)
244 p
= (ptr_t
) ic
->interpreted_methods
[i
];
245 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, ic
, \
249 p
= (ptr_t
) ic
->field_initializers
;
250 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, ic
, cGlabel
);
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$
)
268 jfieldID field
= JvGetFirstInstanceField (klass
);
269 jint max
= JvNumInstanceFields (klass
);
271 for (int i
= 0; i
< max
; ++i
)
273 if (JvFieldIsRef (field
))
275 jobject val
= JvGetObjectField (obj
, field
);
277 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
,
280 field
= field
->getNextField ();
282 klass
= klass
->getSuperclass();
286 return mark_stack_ptr
;
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.
293 _Jv_MarkArray (void *addr
, void *msp
, void *msl
, void * /*env*/)
295 mse
*mark_stack_ptr
= (mse
*) msp
;
296 mse
*mark_stack_limit
= (mse
*) msl
;
297 jobjectArray array
= (jobjectArray
) addr
;
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
;
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
);
313 // Mark the object's class.
315 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, obj
, o2label
);
317 for (int i
= 0; i
< JvGetArrayLength (array
); ++i
)
319 jobject obj
= elements (array
)[i
];
321 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, array
, e2label
);
324 return mark_stack_ptr
;
327 // Return GC descriptor for interpreted class
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)
335 _Jv_BuildGCDescr(jclass
)
337 /* FIXME: We should really look at the class and build the descriptor. */
338 return (void *)(GCJ_DEFAULT_DESCR
);
342 // Allocate some space that is known to be pointer-free.
344 _Jv_AllocBytes (jsize size
)
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.
355 // Allocate space for a new Java array.
356 // Used only for arrays of objects.
358 _Jv_AllocArray (jsize size
, jclass klass
)
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
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
);
374 if (size
< min_heap_addr
)
375 obj
= GC_MALLOC(size
);
377 obj
= GC_GENERIC_MALLOC (size
, array_kind_x
);
379 *((_Jv_VTable
**) obj
) = klass
->vtable
;
384 call_finalizer (GC_PTR obj
, GC_PTR client_data
)
386 _Jv_FinalizerFunc
*fn
= (_Jv_FinalizerFunc
*) client_data
;
387 jobject jobj
= (jobject
) obj
;
393 _Jv_RegisterFinalizer (void *object
, _Jv_FinalizerFunc
*meth
)
395 GC_REGISTER_FINALIZER_NO_ORDER (object
, call_finalizer
, (GC_PTR
) meth
,
400 _Jv_RunFinalizers (void)
402 GC_invoke_finalizers ();
406 _Jv_RunAllFinalizers (void)
418 _Jv_GCTotalMemory (void)
420 return GC_get_heap_size ();
424 _Jv_GCFreeMemory (void)
426 return GC_get_free_bytes ();
430 _Jv_GCSetInitialHeapSize (size_t size
)
432 size_t current
= GC_get_heap_size ();
434 GC_expand_hp (size
- current
);
438 _Jv_GCSetMaximumHeapSize (size_t size
)
440 GC_set_max_heap_size ((GC_word
) size
);
443 // From boehm's misc.c
444 extern "C" void GC_enable();
445 extern "C" void GC_disable();
450 _Jv_MutexLock (&disable_gc_mutex
);
452 _Jv_MutexUnlock (&disable_gc_mutex
);
458 _Jv_MutexLock (&disable_gc_mutex
);
460 _Jv_MutexUnlock (&disable_gc_mutex
);
463 static void * handle_out_of_memory(size_t)
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
);
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
;
495 GC_java_finalization
= 1;
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)
503 memset (array_free_list
, 0, (MAXOBJSZ
+ 1) * sizeof (ptr_t
));
505 proc
= GC_n_mark_procs
++;
506 GC_mark_procs
[proc
] = (GC_mark_proc
) _Jv_MarkArray
;
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
;
515 _Jv_MutexInit (&disable_gc_mutex
);
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.
526 static _Jv_VTable trace_one_vtable
= {
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
536 _Jv_AllocTraceOne (jsize size
/* includes vtable slot */)
538 return GC_GCJ_MALLOC (size
, &trace_one_vtable
);
541 #endif /* JV_HASH_SYNCHRONIZATION */
561 GC_java_finalization
= 1;
563 // Set up state for marking and allocation of Java objects.
564 obj_free_list
= (ptr_t
*) GC_generic_malloc_inner ((MAXOBJSZ
+ 1)
567 memset (obj_free_list
, 0, (MAXOBJSZ
+ 1) * sizeof (ptr_t
));
569 proc
= GC_n_mark_procs
++;
570 GC_mark_procs
[proc
] = (GC_mark_proc
) _Jv_MarkObj
;
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
;
579 // Set up state for marking and allocation of arrays of Java
581 array_free_list
= (ptr_t
*) GC_generic_malloc_inner ((MAXOBJSZ
+ 1)
584 memset (array_free_list
, 0, (MAXOBJSZ
+ 1) * sizeof (ptr_t
));
586 proc
= GC_n_mark_procs
++;
587 GC_mark_procs
[proc
] = (GC_mark_proc
) _Jv_MarkArray
;
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
;
596 _Jv_MutexInit (&disable_gc_mutex
);