(__ev_set_acc_u64): Use __ev_create_u64 to convert uint64_t into __ev64_opaque__.
[official-gcc.git] / libjava / boehm.cc
blob466c9223ac81f422bf7c2ad2b9c8bc966f963583
1 // boehm.cc - interface between libjava and Boehm GC.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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
9 details. */
11 #include <config.h>
13 #include <stdio.h>
15 #include <jvm.h>
16 #include <gcj/cni.h>
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.
24 #undef TRUE
25 #undef FALSE
27 extern "C"
29 #include <private/gc_pmark.h>
30 #include <gc_gcj.h>
32 #ifdef THREAD_LOCAL_ALLOC
33 # define GC_REDIRECT_TO_LOCAL
34 # include <gc_local_alloc.h>
35 #endif
37 // These aren't declared in any Boehm GC header.
38 void GC_finalize_all (void);
39 ptr_t GC_debug_generic_malloc (size_t size, int k, GC_EXTRA_PARAMS);
42 // We must check for plausibility ourselves.
43 #define MAYBE_MARK(Obj, Top, Limit, Source, Exit) \
44 Top=GC_MARK_AND_PUSH((GC_PTR)Obj, Top, Limit, (GC_PTR *)Source)
46 // `kind' index used when allocating Java arrays.
47 static int array_kind_x;
49 // Freelist used for Java arrays.
50 static ptr_t *array_free_list;
52 // Lock used to protect access to Boehm's GC_enable/GC_disable functions.
53 static _Jv_Mutex_t disable_gc_mutex;
57 // This is called by the GC during the mark phase. It marks a Java
58 // object. We use `void *' arguments and return, and not what the
59 // Boehm GC wants, to avoid pollution in our headers.
60 void *
61 _Jv_MarkObj (void *addr, void *msp, void *msl, void * /* env */)
63 mse *mark_stack_ptr = (mse *) msp;
64 mse *mark_stack_limit = (mse *) msl;
65 jobject obj = (jobject) addr;
67 // FIXME: if env is 1, this object was allocated through the debug
68 // interface, and addr points to the beginning of the debug header.
69 // In that case, we should really add the size of the header to addr.
71 _Jv_VTable *dt = *(_Jv_VTable **) addr;
72 // The object might not yet have its vtable set, or it might
73 // really be an object on the freelist. In either case, the vtable slot
74 // will either be 0, or it will point to a cleared object.
75 // This assumes Java objects have size at least 3 words,
76 // including the header. But this should remain true, since this
77 // should only be used with debugging allocation or with large objects.
78 if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
79 return mark_stack_ptr;
80 jclass klass = dt->clas;
81 ptr_t p;
83 # ifndef JV_HASH_SYNCHRONIZATION
84 // Every object has a sync_info pointer.
85 p = (ptr_t) obj->sync_info;
86 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o1label);
87 # endif
88 // Mark the object's class.
89 p = (ptr_t) klass;
90 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label);
92 if (__builtin_expect (klass == &java::lang::Class::class$, false))
94 // Currently we allocate some of the memory referenced from class objects
95 // as pointerfree memory, and then mark it more intelligently here.
96 // We ensure that the ClassClass mark descriptor forces invocation of
97 // this procedure.
98 // Correctness of this is subtle, but it looks OK to me for now. For the incremental
99 // collector, we need to make sure that the class object is written whenever
100 // any of the subobjects are altered and may need rescanning. This may be tricky
101 // during construction, and this may not be the right way to do this with
102 // incremental collection.
103 // If we overflow the mark stack, we will rescan the class object, so we should
104 // be OK. The same applies if we redo the mark phase because win32 unmapped part
105 // of our root set. - HB
106 jclass c = (jclass) addr;
108 p = (ptr_t) c->name;
109 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c3label);
110 p = (ptr_t) c->superclass;
111 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c4label);
112 for (int i = 0; i < c->constants.size; ++i)
114 /* FIXME: We could make this more precise by using the tags -KKT */
115 p = (ptr_t) c->constants.data[i].p;
116 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5label);
119 #ifdef INTERPRETER
120 if (_Jv_IsInterpretedClass (c))
122 p = (ptr_t) c->constants.tags;
123 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5alabel);
124 p = (ptr_t) c->constants.data;
125 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5blabel);
126 p = (ptr_t) c->vtable;
127 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5clabel);
129 #endif
131 // If the class is an array, then the methods field holds a
132 // pointer to the element class. If the class is primitive,
133 // then the methods field holds a pointer to the array class.
134 p = (ptr_t) c->methods;
135 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c6label);
137 // The vtable might have been set, but the rest of the class
138 // could still be uninitialized. If this is the case, then
139 // c.isArray will SEGV. We check for this, and if it is the
140 // case we just return.
141 if (__builtin_expect (c->name == NULL, false))
142 return mark_stack_ptr;
144 if (! c->isArray() && ! c->isPrimitive())
146 // Scan each method in the cases where `methods' really
147 // points to a methods structure.
148 for (int i = 0; i < c->method_count; ++i)
150 p = (ptr_t) c->methods[i].name;
151 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
152 cm1label);
153 p = (ptr_t) c->methods[i].signature;
154 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
155 cm2label);
157 // FIXME: `ncode' entry?
159 #ifdef INTERPRETER
160 // The interpreter installs a heap-allocated
161 // trampoline here, so we'll mark it.
162 if (_Jv_IsInterpretedClass (c))
164 p = (ptr_t) c->methods[i].ncode;
165 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
166 cm3label);
168 #endif
172 // Mark all the fields.
173 p = (ptr_t) c->fields;
174 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8label);
175 for (int i = 0; i < c->field_count; ++i)
177 _Jv_Field* field = &c->fields[i];
179 #ifndef COMPACT_FIELDS
180 p = (ptr_t) field->name;
181 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8alabel);
182 #endif
183 p = (ptr_t) field->type;
184 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8blabel);
186 // For the interpreter, we also need to mark the memory
187 // containing static members
188 if ((field->flags & java::lang::reflect::Modifier::STATIC))
190 p = (ptr_t) field->u.addr;
191 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8clabel);
193 // also, if the static member is a reference,
194 // mark also the value pointed to. We check for isResolved
195 // since marking can happen before memory is allocated for
196 // static members.
197 if (JvFieldIsRef (field) && field->isResolved())
199 jobject val = *(jobject*) field->u.addr;
200 p = (ptr_t) val;
201 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
202 c, c8elabel);
207 p = (ptr_t) c->vtable;
208 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c9label);
209 p = (ptr_t) c->interfaces;
210 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cAlabel);
211 for (int i = 0; i < c->interface_count; ++i)
213 p = (ptr_t) c->interfaces[i];
214 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cClabel);
216 p = (ptr_t) c->loader;
217 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cBlabel);
218 p = (ptr_t) c->arrayclass;
219 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cDlabel);
221 #ifdef INTERPRETER
222 if (_Jv_IsInterpretedClass (c))
224 _Jv_InterpClass* ic = (_Jv_InterpClass*)c;
226 p = (ptr_t) ic->interpreted_methods;
227 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cElabel);
229 for (int i = 0; i < c->method_count; i++)
231 p = (ptr_t) ic->interpreted_methods[i];
232 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
233 cFlabel);
236 p = (ptr_t) ic->field_initializers;
237 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cGlabel);
240 #endif
243 else
245 // NOTE: each class only holds information about the class
246 // itself. So we must do the marking for the entire inheritance
247 // tree in order to mark all fields. FIXME: what about
248 // interfaces? We skip Object here, because Object only has a
249 // sync_info, and we handled that earlier.
250 // Note: occasionally `klass' can be null. For instance, this
251 // can happen if a GC occurs between the point where an object
252 // is allocated and where the vtbl slot is set.
253 while (klass && klass != &java::lang::Object::class$)
255 jfieldID field = JvGetFirstInstanceField (klass);
256 jint max = JvNumInstanceFields (klass);
258 for (int i = 0; i < max; ++i)
260 if (JvFieldIsRef (field))
262 jobject val = JvGetObjectField (obj, field);
263 p = (ptr_t) val;
264 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
265 obj, elabel);
267 field = field->getNextField ();
269 klass = klass->getSuperclass();
273 return mark_stack_ptr;
276 // This is called by the GC during the mark phase. It marks a Java
277 // array (of objects). We use `void *' arguments and return, and not
278 // what the Boehm GC wants, to avoid pollution in our headers.
279 void *
280 _Jv_MarkArray (void *addr, void *msp, void *msl, void * /*env*/)
282 mse *mark_stack_ptr = (mse *) msp;
283 mse *mark_stack_limit = (mse *) msl;
284 jobjectArray array = (jobjectArray) addr;
286 _Jv_VTable *dt = *(_Jv_VTable **) addr;
287 // Assumes size >= 3 words. That's currently true since arrays have
288 // a vtable, sync pointer, and size. If the sync pointer goes away,
289 // we may need to round up the size.
290 if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
291 return mark_stack_ptr;
292 jclass klass = dt->clas;
293 ptr_t p;
295 # ifndef JV_HASH_SYNCHRONIZATION
296 // Every object has a sync_info pointer.
297 p = (ptr_t) array->sync_info;
298 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e1label);
299 # endif
300 // Mark the object's class.
301 p = (ptr_t) klass;
302 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, &(dt -> clas), o2label);
304 for (int i = 0; i < JvGetArrayLength (array); ++i)
306 jobject obj = elements (array)[i];
307 p = (ptr_t) obj;
308 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e2label);
311 return mark_stack_ptr;
314 // Generate a GC marking descriptor for a class.
316 // We assume that the gcj mark proc has index 0. This is a dubious assumption,
317 // since another one could be registered first. But the compiler also
318 // knows this, so in that case everything else will break, too.
319 #define GCJ_DEFAULT_DESCR GC_MAKE_PROC(GC_GCJ_RESERVED_MARK_PROC_INDEX,0)
320 void *
321 _Jv_BuildGCDescr(jclass)
323 /* FIXME: We should really look at the class and build the descriptor. */
324 return (void *)(GCJ_DEFAULT_DESCR);
327 // Allocate some space that is known to be pointer-free.
328 void *
329 _Jv_AllocBytes (jsize size)
331 void *r = GC_MALLOC_ATOMIC (size);
332 // We have to explicitly zero memory here, as the GC doesn't
333 // guarantee that PTRFREE allocations are zeroed. Note that we
334 // don't have to do this for other allocation types because we set
335 // the `ok_init' flag in the type descriptor.
336 memset (r, 0, size);
337 return r;
340 // Allocate space for a new Java array.
341 // Used only for arrays of objects.
342 void *
343 _Jv_AllocArray (jsize size, jclass klass)
345 void *obj;
346 const jsize min_heap_addr = 16*1024;
347 // A heuristic. If size is less than this value, the size
348 // stored in the array can't possibly be misinterpreted as
349 // a pointer. Thus we lose nothing by scanning the object
350 // completely conservatively, since no misidentification can
351 // take place.
353 #ifdef GC_DEBUG
354 // There isn't much to lose by scanning this conservatively.
355 // If we didn't, the mark proc would have to understand that
356 // it needed to skip the header.
357 obj = GC_MALLOC(size);
358 #else
359 if (size < min_heap_addr)
360 obj = GC_MALLOC(size);
361 else
362 obj = GC_generic_malloc (size, array_kind_x);
363 #endif
364 *((_Jv_VTable **) obj) = klass->vtable;
365 return obj;
368 /* Allocate space for a new non-Java object, which does not have the usual
369 Java object header but may contain pointers to other GC'ed objects. */
370 void *
371 _Jv_AllocRawObj (jsize size)
373 return (void *) GC_MALLOC (size);
376 static void
377 call_finalizer (GC_PTR obj, GC_PTR client_data)
379 _Jv_FinalizerFunc *fn = (_Jv_FinalizerFunc *) client_data;
380 jobject jobj = (jobject) obj;
382 (*fn) (jobj);
385 void
386 _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *meth)
388 GC_REGISTER_FINALIZER_NO_ORDER (object, call_finalizer, (GC_PTR) meth,
389 NULL, NULL);
392 void
393 _Jv_RunFinalizers (void)
395 GC_invoke_finalizers ();
398 void
399 _Jv_RunAllFinalizers (void)
401 GC_finalize_all ();
404 void
405 _Jv_RunGC (void)
407 GC_gcollect ();
410 long
411 _Jv_GCTotalMemory (void)
413 return GC_get_heap_size ();
416 long
417 _Jv_GCFreeMemory (void)
419 return GC_get_free_bytes ();
422 void
423 _Jv_GCSetInitialHeapSize (size_t size)
425 size_t current = GC_get_heap_size ();
426 if (size > current)
427 GC_expand_hp (size - current);
430 void
431 _Jv_GCSetMaximumHeapSize (size_t size)
433 GC_set_max_heap_size ((GC_word) size);
436 // From boehm's misc.c
437 extern "C" void GC_enable();
438 extern "C" void GC_disable();
440 void
441 _Jv_DisableGC (void)
443 _Jv_MutexLock (&disable_gc_mutex);
444 GC_disable();
445 _Jv_MutexUnlock (&disable_gc_mutex);
448 void
449 _Jv_EnableGC (void)
451 _Jv_MutexLock (&disable_gc_mutex);
452 GC_enable();
453 _Jv_MutexUnlock (&disable_gc_mutex);
456 static void * handle_out_of_memory(size_t)
458 _Jv_ThrowNoMemory();
461 void
462 _Jv_InitGC (void)
464 int proc;
466 // Ignore pointers that do not point to the start of an object.
467 GC_all_interior_pointers = 0;
469 // Configure the collector to use the bitmap marking descriptors that we
470 // stash in the class vtable.
471 GC_init_gcj_malloc (0, (void *) _Jv_MarkObj);
473 // Cause an out of memory error to be thrown from the allocators,
474 // instead of returning 0. This is cheaper than checking on allocation.
475 GC_oom_fn = handle_out_of_memory;
477 GC_java_finalization = 1;
479 // We use a different mark procedure for object arrays. This code
480 // configures a different object `kind' for object array allocation and
481 // marking. FIXME: see above.
482 array_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
483 * sizeof (ptr_t),
484 PTRFREE);
485 memset (array_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
487 proc = GC_n_mark_procs++;
488 GC_mark_procs[proc] = (GC_mark_proc) _Jv_MarkArray;
490 array_kind_x = GC_n_kinds++;
491 GC_obj_kinds[array_kind_x].ok_freelist = array_free_list;
492 GC_obj_kinds[array_kind_x].ok_reclaim_list = 0;
493 GC_obj_kinds[array_kind_x].ok_descriptor = GC_MAKE_PROC (proc, 0);
494 GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE;
495 GC_obj_kinds[array_kind_x].ok_init = TRUE;
497 _Jv_MutexInit (&disable_gc_mutex);
500 #ifdef JV_HASH_SYNCHRONIZATION
501 // Allocate an object with a fake vtable pointer, which causes only
502 // the first field (beyond the fake vtable pointer) to be traced.
503 // Eventually this should probably be generalized.
505 static _Jv_VTable trace_one_vtable = {
506 0, // class pointer
507 (void *)(2 * sizeof(void *)),
508 // descriptor; scan 2 words incl. vtable ptr.
509 // Least significant bits must be zero to
510 // identify this as a length descriptor
511 {0} // First method
514 void *
515 _Jv_AllocTraceOne (jsize size /* includes vtable slot */)
517 return GC_GCJ_MALLOC (size, &trace_one_vtable);
520 // Ditto for two words.
521 // the first field (beyond the fake vtable pointer) to be traced.
522 // Eventually this should probably be generalized.
524 static _Jv_VTable trace_two_vtable =
526 0, // class pointer
527 (void *)(3 * sizeof(void *)),
528 // descriptor; scan 3 words incl. vtable ptr.
529 {0} // First method
532 void *
533 _Jv_AllocTraceTwo (jsize size /* includes vtable slot */)
535 return GC_GCJ_MALLOC (size, &trace_two_vtable);
538 #endif /* JV_HASH_SYNCHRONIZATION */
540 void
541 _Jv_GCInitializeFinalizers (void (*notifier) (void))
543 GC_finalize_on_demand = 1;
544 GC_finalizer_notifier = notifier;
547 void
548 _Jv_GCRegisterDisappearingLink (jobject *objp)
550 GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp);
553 jboolean
554 _Jv_GCCanReclaimSoftReference (jobject)
556 // For now, always reclaim soft references. FIXME.
557 return true;