Merge from mainline
[official-gcc.git] / libjava / boehm.cc
blob7066e286b4960394f535f34ede81c4650f5460f3
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
10 details. */
12 #include <config.h>
14 #include <stdio.h>
15 #include <limits.h>
17 #include <jvm.h>
18 #include <gcj/cni.h>
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.
26 #undef TRUE
27 #undef FALSE
29 // We include two autoconf headers. Avoid multiple definition warnings.
30 #undef PACKAGE_NAME
31 #undef PACKAGE_STRING
32 #undef PACKAGE_TARNAME
33 #undef PACKAGE_VERSION
35 extern "C"
37 #include <gc_config.h>
39 // Set GC_DEBUG before including gc.h!
40 #ifdef LIBGCJ_GC_DEBUG
41 # define GC_DEBUG
42 #endif
44 #include <gc_mark.h>
45 #include <gc_gcj.h>
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>
51 #endif
53 // From boehm's misc.c
54 void GC_enable();
55 void GC_disable();
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.
72 void *
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;
92 GC_PTR p;
94 p = (GC_PTR) dt;
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);
101 # endif
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
108 // this procedure.
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);
151 p = (GC_PTR) c->idt;
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);
163 else
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);
183 p = (GC_PTR) val;
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.
198 void *
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;
214 GC_PTR p;
216 p = (GC_PTR) dt;
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);
223 # endif
225 for (int i = 0; i < JvGetArrayLength (array); ++i)
227 jobject obj = elements (array)[i];
228 p = (GC_PTR) obj;
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)
242 void *
243 _Jv_BuildGCDescr(jclass self)
245 jlong desc = 0;
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);
258 #endif
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)
267 if (field->isRef())
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
276 // reserved.
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.
287 desc |= 1;
288 // Bogus warning avoidance (on many platforms).
289 return (void *) (unsigned long) desc;
292 // Allocate some space that is known to be pointer-free.
293 void *
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.
301 memset (r, 0, size);
302 return r;
305 #ifdef LIBGCJ_GC_DEBUG
307 void *
308 _Jv_AllocObj (jsize size, jclass klass)
310 return GC_GCJ_MALLOC (size, klass->vtable);
313 void *
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;
319 #else
320 void * obj = GC_GCJ_MALLOC(size, klass->vtable);
321 #endif
322 return obj;
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.
332 void *
333 _Jv_AllocArray (jsize size, jclass klass)
335 void *obj;
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);
342 #else
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
348 // take place.
350 if (size < min_heap_addr)
351 obj = GC_MALLOC(size);
352 else
353 obj = GC_generic_malloc (size, array_kind_x);
354 #endif
355 *((_Jv_VTable **) obj) = klass->vtable;
356 return obj;
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. */
361 void *
362 _Jv_AllocRawObj (jsize size)
364 return (void *) GC_MALLOC (size ? size : 1);
367 static void
368 call_finalizer (GC_PTR obj, GC_PTR client_data)
370 _Jv_FinalizerFunc *fn = (_Jv_FinalizerFunc *) client_data;
371 jobject jobj = (jobject) obj;
373 (*fn) (jobj);
376 void
377 _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *meth)
379 GC_REGISTER_FINALIZER_NO_ORDER (object, call_finalizer, (GC_PTR) meth,
380 NULL, NULL);
383 void
384 _Jv_RunFinalizers (void)
386 GC_invoke_finalizers ();
389 void
390 _Jv_RunAllFinalizers (void)
392 GC_finalize_all ();
395 void
396 _Jv_RunGC (void)
398 GC_gcollect ();
401 long
402 _Jv_GCTotalMemory (void)
404 return GC_get_heap_size ();
407 long
408 _Jv_GCFreeMemory (void)
410 return GC_get_free_bytes ();
413 void
414 _Jv_GCSetInitialHeapSize (size_t size)
416 size_t current = GC_get_heap_size ();
417 if (size > current)
418 GC_expand_hp (size - current);
421 void
422 _Jv_GCSetMaximumHeapSize (size_t size)
424 GC_set_max_heap_size ((GC_word) size);
427 void
428 _Jv_DisableGC (void)
430 GC_disable();
433 void
434 _Jv_EnableGC (void)
436 GC_enable();
439 static void * handle_out_of_memory(size_t)
441 _Jv_ThrowNoMemory();
444 static void
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)");
452 return;
454 jclass klass = dt->clas;
455 if (!klass /* shouldn't happen */)
457 strcpy(out_buf, "GCJ (bad)");
458 return;
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);
464 out_buf[len] = '\0';
467 void
468 _Jv_InitGC (void)
470 int proc;
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
478 // about it.
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
489 // marking.
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 = {
505 0, // class pointer
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
510 {0} // First method
513 void *
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 =
525 0, // class pointer
526 (void *)(3 * sizeof(void *)),
527 // descriptor; scan 3 words incl. vtable ptr.
528 {0} // First method
531 void *
532 _Jv_AllocTraceTwo (jsize size /* includes vtable slot */)
534 return GC_GCJ_MALLOC (size, &trace_two_vtable);
537 #endif /* JV_HASH_SYNCHRONIZATION */
539 void
540 _Jv_GCInitializeFinalizers (void (*notifier) (void))
542 GC_finalize_on_demand = 1;
543 GC_finalizer_notifier = notifier;
546 void
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."
552 if (GC_base(*objp))
553 GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp);
556 jboolean
557 _Jv_GCCanReclaimSoftReference (jobject)
559 // For now, always reclaim soft references. FIXME.
560 return true;