1 // boehm.cc - interface between libjava and Boehm GC.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
20 #include <java/lang/Class.h>
21 #include <java/lang/reflect/Modifier.h>
22 #include <java-interp.h>
24 // More nastiness: the GC wants to define TRUE and FALSE. We don't
25 // need the Java definitions (themselves a hack), so we undefine them.
29 // We include two autoconf headers. Avoid multiple definition warnings.
32 #undef PACKAGE_TARNAME
33 #undef PACKAGE_VERSION
37 #include <gc_config.h>
39 // Set GC_DEBUG before including gc.h!
40 #ifdef LIBGCJ_GC_DEBUG
46 #include <javaxfc.h> // GC_finalize_all declaration.
48 #ifdef THREAD_LOCAL_ALLOC
49 # define GC_REDIRECT_TO_LOCAL
50 # include <gc_local_alloc.h>
53 // From boehm's misc.c
58 #define MAYBE_MARK(Obj, Top, Limit, Source) \
59 Top=GC_MARK_AND_PUSH((GC_PTR) Obj, Top, Limit, (GC_PTR *) Source)
61 // `kind' index used when allocating Java arrays.
62 static int array_kind_x
;
64 // Freelist used for Java arrays.
65 static void **array_free_list
;
69 // This is called by the GC during the mark phase. It marks a Java
70 // object. We use `void *' arguments and return, and not what the
71 // Boehm GC wants, to avoid pollution in our headers.
73 _Jv_MarkObj (void *addr
, void *msp
, void *msl
, void *env
)
75 struct GC_ms_entry
*mark_stack_ptr
= (struct GC_ms_entry
*)msp
;
76 struct GC_ms_entry
*mark_stack_limit
= (struct GC_ms_entry
*)msl
;
78 if (env
== (void *)1) /* Object allocated with debug allocator. */
79 addr
= (GC_PTR
)GC_USR_PTR_FROM_BASE(addr
);
80 jobject obj
= (jobject
) addr
;
82 _Jv_VTable
*dt
= *(_Jv_VTable
**) addr
;
83 // The object might not yet have its vtable set, or it might
84 // really be an object on the freelist. In either case, the vtable slot
85 // will either be 0, or it will point to a cleared object.
86 // This assumes Java objects have size at least 3 words,
87 // including the header. But this should remain true, since this
88 // should only be used with debugging allocation or with large objects.
89 if (__builtin_expect (! dt
|| !(dt
-> get_finalizer()), false))
90 return mark_stack_ptr
;
91 jclass klass
= dt
->clas
;
95 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, obj
);
97 # ifndef JV_HASH_SYNCHRONIZATION
98 // Every object has a sync_info pointer.
99 p
= (GC_PTR
) obj
->sync_info
;
100 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, obj
);
103 if (__builtin_expect (klass
== &java::lang::Class::class$
, false))
105 // Currently we allocate some of the memory referenced from class objects
106 // as pointerfree memory, and then mark it more intelligently here.
107 // We ensure that the ClassClass mark descriptor forces invocation of
109 // Correctness of this is subtle, but it looks OK to me for now. For the incremental
110 // collector, we need to make sure that the class object is written whenever
111 // any of the subobjects are altered and may need rescanning. This may be tricky
112 // during construction, and this may not be the right way to do this with
113 // incremental collection.
114 // If we overflow the mark stack, we will rescan the class object, so we should
115 // be OK. The same applies if we redo the mark phase because win32 unmapped part
116 // of our root set. - HB
117 jclass c
= (jclass
) addr
;
119 p
= (GC_PTR
) c
->name
;
120 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
121 p
= (GC_PTR
) c
->superclass
;
122 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
124 p
= (GC_PTR
) c
->constants
.tags
;
125 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
126 p
= (GC_PTR
) c
->constants
.data
;
127 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
129 // If the class is an array, then the methods field holds a
130 // pointer to the element class. If the class is primitive,
131 // then the methods field holds a pointer to the array class.
132 p
= (GC_PTR
) c
->methods
;
133 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
135 p
= (GC_PTR
) c
->fields
;
136 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
138 // The vtable might be allocated even for compiled code.
139 p
= (GC_PTR
) c
->vtable
;
140 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
142 p
= (GC_PTR
) c
->interfaces
;
143 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
144 p
= (GC_PTR
) c
->loader
;
145 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
147 // The dispatch tables can be allocated at runtime.
148 p
= (GC_PTR
) c
->ancestors
;
149 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
152 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
154 p
= (GC_PTR
) c
->arrayclass
;
155 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
156 p
= (GC_PTR
) c
->protectionDomain
;
157 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
158 p
= (GC_PTR
) c
->hack_signers
;
159 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
160 p
= (GC_PTR
) c
->aux_info
;
161 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
);
165 // NOTE: each class only holds information about the class
166 // itself. So we must do the marking for the entire inheritance
167 // tree in order to mark all fields. FIXME: what about
168 // interfaces? We skip Object here, because Object only has a
169 // sync_info, and we handled that earlier.
170 // Note: occasionally `klass' can be null. For instance, this
171 // can happen if a GC occurs between the point where an object
172 // is allocated and where the vtbl slot is set.
173 while (klass
&& klass
!= &java::lang::Object::class$
)
175 jfieldID field
= JvGetFirstInstanceField (klass
);
176 jint max
= JvNumInstanceFields (klass
);
178 for (int i
= 0; i
< max
; ++i
)
180 if (JvFieldIsRef (field
))
182 jobject val
= JvGetObjectField (obj
, field
);
184 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, obj
);
186 field
= field
->getNextField ();
188 klass
= klass
->getSuperclass();
192 return mark_stack_ptr
;
195 // This is called by the GC during the mark phase. It marks a Java
196 // array (of objects). We use `void *' arguments and return, and not
197 // what the Boehm GC wants, to avoid pollution in our headers.
199 _Jv_MarkArray (void *addr
, void *msp
, void *msl
, void *env
)
201 struct GC_ms_entry
*mark_stack_ptr
= (struct GC_ms_entry
*)msp
;
202 struct GC_ms_entry
*mark_stack_limit
= (struct GC_ms_entry
*)msl
;
204 if (env
== (void *)1) /* Object allocated with debug allocator. */
205 addr
= (void *)GC_USR_PTR_FROM_BASE(addr
);
206 jobjectArray array
= (jobjectArray
) addr
;
208 _Jv_VTable
*dt
= *(_Jv_VTable
**) addr
;
209 // Assumes size >= 3 words. That's currently true since arrays have
210 // a vtable, sync pointer, and size. If the sync pointer goes away,
211 // we may need to round up the size.
212 if (__builtin_expect (! dt
|| !(dt
-> get_finalizer()), false))
213 return mark_stack_ptr
;
217 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, array
);
219 # ifndef JV_HASH_SYNCHRONIZATION
220 // Every object has a sync_info pointer.
221 p
= (GC_PTR
) array
->sync_info
;
222 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, array
);
225 for (int i
= 0; i
< JvGetArrayLength (array
); ++i
)
227 jobject obj
= elements (array
)[i
];
229 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, array
);
232 return mark_stack_ptr
;
235 // Generate a GC marking descriptor for a class.
237 // We assume that the gcj mark proc has index 0. This is a dubious assumption,
238 // since another one could be registered first. But the compiler also
239 // knows this, so in that case everything else will break, too.
240 #define GCJ_DEFAULT_DESCR GC_MAKE_PROC(GC_GCJ_RESERVED_MARK_PROC_INDEX,0)
243 _Jv_BuildGCDescr(jclass self
)
246 jint bits_per_word
= CHAR_BIT
* sizeof (void *);
248 // Note: for now we only consider a bitmap mark descriptor. We
249 // could also handle the case where the first N fields of a type are
250 // references. However, this is not very likely to be used by many
251 // classes, and it is easier to compute things this way.
253 // The vtable pointer.
254 desc
|= 1ULL << (bits_per_word
- 1);
255 #ifndef JV_HASH_SYNCHRONIZATION
256 // The sync_info field.
257 desc
|= 1ULL << (bits_per_word
- 2);
260 for (jclass klass
= self
; klass
!= NULL
; klass
= klass
->getSuperclass())
262 jfieldID field
= JvGetFirstInstanceField(klass
);
263 int count
= JvNumInstanceFields(klass
);
265 for (int i
= 0; i
< count
; ++i
)
269 unsigned int off
= field
->getOffset();
270 // If we run into a weird situation, we bail.
271 if (off
% sizeof (void *) != 0)
272 return (void *) (GCJ_DEFAULT_DESCR
);
273 off
/= sizeof (void *);
274 // If we find a field outside the range of our bitmap,
275 // fall back to procedure marker. The bottom 2 bits are
277 if (off
>= (unsigned) bits_per_word
- 2)
278 return (void *) (GCJ_DEFAULT_DESCR
);
279 desc
|= 1ULL << (bits_per_word
- off
- 1);
282 field
= field
->getNextField();
286 // For bitmap mark type, bottom bits are 01.
288 // Bogus warning avoidance (on many platforms).
289 return (void *) (unsigned long) desc
;
292 // Allocate some space that is known to be pointer-free.
294 _Jv_AllocBytes (jsize size
)
296 void *r
= GC_MALLOC_ATOMIC (size
);
297 // We have to explicitly zero memory here, as the GC doesn't
298 // guarantee that PTRFREE allocations are zeroed. Note that we
299 // don't have to do this for other allocation types because we set
300 // the `ok_init' flag in the type descriptor.
305 #ifdef LIBGCJ_GC_DEBUG
308 _Jv_AllocObj (jsize size
, jclass klass
)
310 return GC_GCJ_MALLOC (size
, klass
->vtable
);
314 _Jv_AllocPtrFreeObj (jsize size
, jclass klass
)
316 #ifdef JV_HASH_SYNCHRONIZATION
317 void * obj
= GC_MALLOC_ATOMIC(size
);
318 *((_Jv_VTable
**) obj
) = klass
->vtable
;
320 void * obj
= GC_GCJ_MALLOC(size
, klass
->vtable
);
325 #endif /* LIBGCJ_GC_DEBUG */
326 // In the non-debug case, the above two functions are defined
327 // as inline functions in boehm-gc.h. In the debug case we
328 // really want to take advantage of the definitions in gc_gcj.h.
330 // Allocate space for a new Java array.
331 // Used only for arrays of objects.
333 _Jv_AllocArray (jsize size
, jclass klass
)
337 #ifdef LIBGCJ_GC_DEBUG
338 // There isn't much to lose by scanning this conservatively.
339 // If we didn't, the mark proc would have to understand that
340 // it needed to skip the header.
341 obj
= GC_MALLOC(size
);
343 const jsize min_heap_addr
= 16*1024;
344 // A heuristic. If size is less than this value, the size
345 // stored in the array can't possibly be misinterpreted as
346 // a pointer. Thus we lose nothing by scanning the object
347 // completely conservatively, since no misidentification can
350 if (size
< min_heap_addr
)
351 obj
= GC_MALLOC(size
);
353 obj
= GC_generic_malloc (size
, array_kind_x
);
355 *((_Jv_VTable
**) obj
) = klass
->vtable
;
359 /* Allocate space for a new non-Java object, which does not have the usual
360 Java object header but may contain pointers to other GC'ed objects. */
362 _Jv_AllocRawObj (jsize size
)
364 return (void *) GC_MALLOC (size
? size
: 1);
368 call_finalizer (GC_PTR obj
, GC_PTR client_data
)
370 _Jv_FinalizerFunc
*fn
= (_Jv_FinalizerFunc
*) client_data
;
371 jobject jobj
= (jobject
) obj
;
377 _Jv_RegisterFinalizer (void *object
, _Jv_FinalizerFunc
*meth
)
379 GC_REGISTER_FINALIZER_NO_ORDER (object
, call_finalizer
, (GC_PTR
) meth
,
384 _Jv_RunFinalizers (void)
386 GC_invoke_finalizers ();
390 _Jv_RunAllFinalizers (void)
402 _Jv_GCTotalMemory (void)
404 return GC_get_heap_size ();
408 _Jv_GCFreeMemory (void)
410 return GC_get_free_bytes ();
414 _Jv_GCSetInitialHeapSize (size_t size
)
416 size_t current
= GC_get_heap_size ();
418 GC_expand_hp (size
- current
);
422 _Jv_GCSetMaximumHeapSize (size_t size
)
424 GC_set_max_heap_size ((GC_word
) size
);
439 static void * handle_out_of_memory(size_t)
445 gcj_describe_type_fn(void *obj
, char *out_buf
)
447 _Jv_VTable
*dt
= *(_Jv_VTable
**) obj
;
449 if (! dt
/* Shouldn't happen */)
451 strcpy(out_buf
, "GCJ (bad)");
454 jclass klass
= dt
->clas
;
455 if (!klass
/* shouldn't happen */)
457 strcpy(out_buf
, "GCJ (bad)");
460 jstring name
= klass
-> getName();
461 size_t len
= name
-> length();
462 if (len
>= GC_TYPE_DESCR_LEN
) len
= GC_TYPE_DESCR_LEN
- 1;
463 JvGetStringUTFRegion (name
, 0, len
, out_buf
);
472 // Ignore pointers that do not point to the start of an object.
473 GC_all_interior_pointers
= 0;
475 // Configure the collector to use the bitmap marking descriptors that we
476 // stash in the class vtable.
477 // We always use mark proc descriptor 0, since the compiler knows
479 GC_init_gcj_malloc (0, (void *) _Jv_MarkObj
);
481 // Cause an out of memory error to be thrown from the allocators,
482 // instead of returning 0. This is cheaper than checking on allocation.
483 GC_oom_fn
= handle_out_of_memory
;
485 GC_java_finalization
= 1;
487 // We use a different mark procedure for object arrays. This code
488 // configures a different object `kind' for object array allocation and
490 array_free_list
= GC_new_free_list();
491 proc
= GC_new_proc((GC_mark_proc
)_Jv_MarkArray
);
492 array_kind_x
= GC_new_kind(array_free_list
, GC_MAKE_PROC (proc
, 0), 0, 1);
494 // Arrange to have the GC print Java class names in backtraces, etc.
495 GC_register_describe_type_fn(GC_gcj_kind
, gcj_describe_type_fn
);
496 GC_register_describe_type_fn(GC_gcj_debug_kind
, gcj_describe_type_fn
);
499 #ifdef JV_HASH_SYNCHRONIZATION
500 // Allocate an object with a fake vtable pointer, which causes only
501 // the first field (beyond the fake vtable pointer) to be traced.
502 // Eventually this should probably be generalized.
504 static _Jv_VTable trace_one_vtable
= {
506 (void *)(2 * sizeof(void *)),
507 // descriptor; scan 2 words incl. vtable ptr.
508 // Least significant bits must be zero to
509 // identify this as a length descriptor
514 _Jv_AllocTraceOne (jsize size
/* includes vtable slot */)
516 return GC_GCJ_MALLOC (size
, &trace_one_vtable
);
519 // Ditto for two words.
520 // the first field (beyond the fake vtable pointer) to be traced.
521 // Eventually this should probably be generalized.
523 static _Jv_VTable trace_two_vtable
=
526 (void *)(3 * sizeof(void *)),
527 // descriptor; scan 3 words incl. vtable ptr.
532 _Jv_AllocTraceTwo (jsize size
/* includes vtable slot */)
534 return GC_GCJ_MALLOC (size
, &trace_two_vtable
);
537 #endif /* JV_HASH_SYNCHRONIZATION */
540 _Jv_GCInitializeFinalizers (void (*notifier
) (void))
542 GC_finalize_on_demand
= 1;
543 GC_finalizer_notifier
= notifier
;
547 _Jv_GCRegisterDisappearingLink (jobject
*objp
)
549 // This test helps to ensure that we meet a precondition of
550 // GC_general_register_disappearing_link, viz. "Obj must be a
551 // pointer to the first word of an object we allocated."
553 GC_general_register_disappearing_link ((GC_PTR
*) objp
, (GC_PTR
) *objp
);
557 _Jv_GCCanReclaimSoftReference (jobject
)
559 // For now, always reclaim soft references. FIXME.