* doc/invoke.texi (Optimize Options): Replace "it's" with "its".
[official-gcc.git] / libjava / boehm.cc
blob5006059dabd92925fe1f17841d052595a5b9084b
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 #define MAYBE_MARK(Obj, Top, Limit, Source, Exit) \
43 Top=GC_MARK_AND_PUSH((GC_PTR)Obj, Top, Limit, (GC_PTR *)Source)
45 // `kind' index used when allocating Java arrays.
46 static int array_kind_x;
48 // Freelist used for Java arrays.
49 static ptr_t *array_free_list;
51 // Lock used to protect access to Boehm's GC_enable/GC_disable functions.
52 static _Jv_Mutex_t disable_gc_mutex;
56 // This is called by the GC during the mark phase. It marks a Java
57 // object. We use `void *' arguments and return, and not what the
58 // Boehm GC wants, to avoid pollution in our headers.
59 void *
60 _Jv_MarkObj (void *addr, void *msp, void *msl, void * /* env */)
62 mse *mark_stack_ptr = (mse *) msp;
63 mse *mark_stack_limit = (mse *) msl;
64 jobject obj = (jobject) addr;
66 // FIXME: if env is 1, this object was allocated through the debug
67 // interface, and addr points to the beginning of the debug header.
68 // In that case, we should really add the size of the header to addr.
70 _Jv_VTable *dt = *(_Jv_VTable **) addr;
71 // The object might not yet have its vtable set, or it might
72 // really be an object on the freelist. In either case, the vtable slot
73 // will either be 0, or it will point to a cleared object.
74 // This assumes Java objects have size at least 3 words,
75 // including the header. But this should remain true, since this
76 // should only be used with debugging allocation or with large objects.
77 if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
78 return mark_stack_ptr;
79 jclass klass = dt->clas;
80 ptr_t p;
82 # ifndef JV_HASH_SYNCHRONIZATION
83 // Every object has a sync_info pointer.
84 p = (ptr_t) obj->sync_info;
85 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o1label);
86 # endif
87 // Mark the object's class.
88 p = (ptr_t) klass;
89 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label);
91 if (__builtin_expect (klass == &java::lang::Class::class$, false))
93 // Currently we allocate some of the memory referenced from class objects
94 // as pointerfree memory, and then mark it more intelligently here.
95 // We ensure that the ClassClass mark descriptor forces invocation of
96 // this procedure.
97 // Correctness of this is subtle, but it looks OK to me for now. For the incremental
98 // collector, we need to make sure that the class object is written whenever
99 // any of the subobjects are altered and may need rescanning. This may be tricky
100 // during construction, and this may not be the right way to do this with
101 // incremental collection.
102 // If we overflow the mark stack, we will rescan the class object, so we should
103 // be OK. The same applies if we redo the mark phase because win32 unmapped part
104 // of our root set. - HB
105 jclass c = (jclass) addr;
107 p = (ptr_t) c->name;
108 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c3label);
109 p = (ptr_t) c->superclass;
110 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c4label);
111 for (int i = 0; i < c->constants.size; ++i)
113 /* FIXME: We could make this more precise by using the tags -KKT */
114 p = (ptr_t) c->constants.data[i].p;
115 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5label);
118 #ifdef INTERPRETER
119 if (_Jv_IsInterpretedClass (c))
121 p = (ptr_t) c->constants.tags;
122 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5alabel);
123 p = (ptr_t) c->constants.data;
124 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5blabel);
125 p = (ptr_t) c->vtable;
126 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5clabel);
128 #endif
130 // If the class is an array, then the methods field holds a
131 // pointer to the element class. If the class is primitive,
132 // then the methods field holds a pointer to the array class.
133 p = (ptr_t) c->methods;
134 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c6label);
136 // The vtable might have been set, but the rest of the class
137 // could still be uninitialized. If this is the case, then
138 // c.isArray will SEGV. We check for this, and if it is the
139 // case we just return.
140 if (__builtin_expect (c->name == NULL, false))
141 return mark_stack_ptr;
143 if (! c->isArray() && ! c->isPrimitive())
145 // Scan each method in the cases where `methods' really
146 // points to a methods structure.
147 for (int i = 0; i < c->method_count; ++i)
149 p = (ptr_t) c->methods[i].name;
150 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
151 cm1label);
152 p = (ptr_t) c->methods[i].signature;
153 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
154 cm2label);
158 // Mark all the fields.
159 p = (ptr_t) c->fields;
160 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8label);
161 for (int i = 0; i < c->field_count; ++i)
163 _Jv_Field* field = &c->fields[i];
165 #ifndef COMPACT_FIELDS
166 p = (ptr_t) field->name;
167 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8alabel);
168 #endif
169 p = (ptr_t) field->type;
170 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8blabel);
172 // For the interpreter, we also need to mark the memory
173 // containing static members
174 if ((field->flags & java::lang::reflect::Modifier::STATIC))
176 p = (ptr_t) field->u.addr;
177 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8clabel);
179 // also, if the static member is a reference,
180 // mark also the value pointed to. We check for isResolved
181 // since marking can happen before memory is allocated for
182 // static members.
183 if (JvFieldIsRef (field) && field->isResolved())
185 jobject val = *(jobject*) field->u.addr;
186 p = (ptr_t) val;
187 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
188 c, c8elabel);
193 p = (ptr_t) c->vtable;
194 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c9label);
195 p = (ptr_t) c->interfaces;
196 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cAlabel);
197 for (int i = 0; i < c->interface_count; ++i)
199 p = (ptr_t) c->interfaces[i];
200 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cClabel);
202 p = (ptr_t) c->loader;
203 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cBlabel);
204 p = (ptr_t) c->arrayclass;
205 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cDlabel);
206 p = (ptr_t) c->protectionDomain;
207 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cPlabel);
209 #ifdef INTERPRETER
210 if (_Jv_IsInterpretedClass (c))
212 _Jv_InterpClass* ic = (_Jv_InterpClass*) c;
214 p = (ptr_t) ic->interpreted_methods;
215 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cElabel);
217 for (int i = 0; i < c->method_count; i++)
219 p = (ptr_t) ic->interpreted_methods[i];
220 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
221 cFlabel);
223 // Mark the direct-threaded code.
224 if ((c->methods[i].accflags
225 & java::lang::reflect::Modifier::NATIVE) == 0)
227 _Jv_InterpMethod *im
228 = (_Jv_InterpMethod *) ic->interpreted_methods[i];
229 if (im)
231 p = (ptr_t) im->prepared;
232 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
233 cFlabel);
237 // The interpreter installs a heap-allocated trampoline
238 // here, so we'll mark it.
239 p = (ptr_t) c->methods[i].ncode;
240 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
241 cm3label);
244 p = (ptr_t) ic->field_initializers;
245 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cGlabel);
248 #endif
251 else
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.
261 while (klass && klass != &java::lang::Object::class$)
263 jfieldID field = JvGetFirstInstanceField (klass);
264 jint max = JvNumInstanceFields (klass);
266 for (int i = 0; i < max; ++i)
268 if (JvFieldIsRef (field))
270 jobject val = JvGetObjectField (obj, field);
271 p = (ptr_t) val;
272 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
273 obj, elabel);
275 field = field->getNextField ();
277 klass = klass->getSuperclass();
281 return mark_stack_ptr;
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.
287 void *
288 _Jv_MarkArray (void *addr, void *msp, void *msl, void * /*env*/)
290 mse *mark_stack_ptr = (mse *) msp;
291 mse *mark_stack_limit = (mse *) msl;
292 jobjectArray array = (jobjectArray) addr;
294 _Jv_VTable *dt = *(_Jv_VTable **) addr;
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))
299 return mark_stack_ptr;
300 jclass klass = dt->clas;
301 ptr_t p;
303 # ifndef JV_HASH_SYNCHRONIZATION
304 // Every object has a sync_info pointer.
305 p = (ptr_t) array->sync_info;
306 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e1label);
307 # endif
308 // Mark the object's class.
309 p = (ptr_t) klass;
310 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, &(dt -> clas), o2label);
312 for (int i = 0; i < JvGetArrayLength (array); ++i)
314 jobject obj = elements (array)[i];
315 p = (ptr_t) obj;
316 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e2label);
319 return mark_stack_ptr;
322 // Generate a GC marking descriptor for a class.
324 // We assume that the gcj mark proc has index 0. This is a dubious assumption,
325 // since another one could be registered first. But the compiler also
326 // knows this, so in that case everything else will break, too.
327 #define GCJ_DEFAULT_DESCR GC_MAKE_PROC(GC_GCJ_RESERVED_MARK_PROC_INDEX,0)
328 void *
329 _Jv_BuildGCDescr(jclass)
331 /* FIXME: We should really look at the class and build the descriptor. */
332 return (void *)(GCJ_DEFAULT_DESCR);
335 // Allocate some space that is known to be pointer-free.
336 void *
337 _Jv_AllocBytes (jsize size)
339 void *r = GC_MALLOC_ATOMIC (size);
340 // We have to explicitly zero memory here, as the GC doesn't
341 // guarantee that PTRFREE allocations are zeroed. Note that we
342 // don't have to do this for other allocation types because we set
343 // the `ok_init' flag in the type descriptor.
344 memset (r, 0, size);
345 return r;
348 // Allocate space for a new Java array.
349 // Used only for arrays of objects.
350 void *
351 _Jv_AllocArray (jsize size, jclass klass)
353 void *obj;
354 const jsize min_heap_addr = 16*1024;
355 // A heuristic. If size is less than this value, the size
356 // stored in the array can't possibly be misinterpreted as
357 // a pointer. Thus we lose nothing by scanning the object
358 // completely conservatively, since no misidentification can
359 // take place.
361 #ifdef GC_DEBUG
362 // There isn't much to lose by scanning this conservatively.
363 // If we didn't, the mark proc would have to understand that
364 // it needed to skip the header.
365 obj = GC_MALLOC(size);
366 #else
367 if (size < min_heap_addr)
368 obj = GC_MALLOC(size);
369 else
370 obj = GC_generic_malloc (size, array_kind_x);
371 #endif
372 *((_Jv_VTable **) obj) = klass->vtable;
373 return obj;
376 /* Allocate space for a new non-Java object, which does not have the usual
377 Java object header but may contain pointers to other GC'ed objects. */
378 void *
379 _Jv_AllocRawObj (jsize size)
381 return (void *) GC_MALLOC (size);
384 static void
385 call_finalizer (GC_PTR obj, GC_PTR client_data)
387 _Jv_FinalizerFunc *fn = (_Jv_FinalizerFunc *) client_data;
388 jobject jobj = (jobject) obj;
390 (*fn) (jobj);
393 void
394 _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *meth)
396 GC_REGISTER_FINALIZER_NO_ORDER (object, call_finalizer, (GC_PTR) meth,
397 NULL, NULL);
400 void
401 _Jv_RunFinalizers (void)
403 GC_invoke_finalizers ();
406 void
407 _Jv_RunAllFinalizers (void)
409 GC_finalize_all ();
412 void
413 _Jv_RunGC (void)
415 GC_gcollect ();
418 long
419 _Jv_GCTotalMemory (void)
421 return GC_get_heap_size ();
424 long
425 _Jv_GCFreeMemory (void)
427 return GC_get_free_bytes ();
430 void
431 _Jv_GCSetInitialHeapSize (size_t size)
433 size_t current = GC_get_heap_size ();
434 if (size > current)
435 GC_expand_hp (size - current);
438 void
439 _Jv_GCSetMaximumHeapSize (size_t size)
441 GC_set_max_heap_size ((GC_word) size);
444 // From boehm's misc.c
445 extern "C" void GC_enable();
446 extern "C" void GC_disable();
448 void
449 _Jv_DisableGC (void)
451 _Jv_MutexLock (&disable_gc_mutex);
452 GC_disable();
453 _Jv_MutexUnlock (&disable_gc_mutex);
456 void
457 _Jv_EnableGC (void)
459 _Jv_MutexLock (&disable_gc_mutex);
460 GC_enable();
461 _Jv_MutexUnlock (&disable_gc_mutex);
464 static void * handle_out_of_memory(size_t)
466 _Jv_ThrowNoMemory();
469 void
470 _Jv_InitGC (void)
472 int proc;
474 // Ignore pointers that do not point to the start of an object.
475 GC_all_interior_pointers = 0;
477 // Configure the collector to use the bitmap marking descriptors that we
478 // stash in the class vtable.
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. FIXME: see above.
490 array_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
491 * sizeof (ptr_t),
492 PTRFREE);
493 memset (array_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
495 proc = GC_n_mark_procs++;
496 GC_mark_procs[proc] = (GC_mark_proc) _Jv_MarkArray;
498 array_kind_x = GC_n_kinds++;
499 GC_obj_kinds[array_kind_x].ok_freelist = array_free_list;
500 GC_obj_kinds[array_kind_x].ok_reclaim_list = 0;
501 GC_obj_kinds[array_kind_x].ok_descriptor = GC_MAKE_PROC (proc, 0);
502 GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE;
503 GC_obj_kinds[array_kind_x].ok_init = TRUE;
505 _Jv_MutexInit (&disable_gc_mutex);
508 #ifdef JV_HASH_SYNCHRONIZATION
509 // Allocate an object with a fake vtable pointer, which causes only
510 // the first field (beyond the fake vtable pointer) to be traced.
511 // Eventually this should probably be generalized.
513 static _Jv_VTable trace_one_vtable = {
514 0, // class pointer
515 (void *)(2 * sizeof(void *)),
516 // descriptor; scan 2 words incl. vtable ptr.
517 // Least significant bits must be zero to
518 // identify this as a length descriptor
519 {0} // First method
522 void *
523 _Jv_AllocTraceOne (jsize size /* includes vtable slot */)
525 return GC_GCJ_MALLOC (size, &trace_one_vtable);
528 // Ditto for two words.
529 // the first field (beyond the fake vtable pointer) to be traced.
530 // Eventually this should probably be generalized.
532 static _Jv_VTable trace_two_vtable =
534 0, // class pointer
535 (void *)(3 * sizeof(void *)),
536 // descriptor; scan 3 words incl. vtable ptr.
537 {0} // First method
540 void *
541 _Jv_AllocTraceTwo (jsize size /* includes vtable slot */)
543 return GC_GCJ_MALLOC (size, &trace_two_vtable);
546 #endif /* JV_HASH_SYNCHRONIZATION */
548 void
549 _Jv_GCInitializeFinalizers (void (*notifier) (void))
551 GC_finalize_on_demand = 1;
552 GC_finalizer_notifier = notifier;
555 void
556 _Jv_GCRegisterDisappearingLink (jobject *objp)
558 GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp);
561 jboolean
562 _Jv_GCCanReclaimSoftReference (jobject)
564 // For now, always reclaim soft references. FIXME.
565 return true;