1 // boehm.cc - interface between libjava and Boehm GC.
3 /* Copyright (C) 1998, 1999, 2000 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.
31 #include <include/gc_gcj.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
;
101 // Every object has a sync_info pointer.
102 ptr_t p
= (ptr_t
) obj
->sync_info
;
103 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, obj
, o1label
);
104 // Mark the object's class.
106 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, obj
, o2label
);
108 if (__builtin_expect (klass
== &java::lang::Class::class$
, false))
110 // Currently we allocate some of the memory referenced from class objects
111 // as pointerfree memory, and then mark it more intelligently here.
112 // We ensure that the ClassClass mark descriptor forces invocation of
114 // Correctness of this is subtle, but it looks OK to me for now. For the incremental
115 // collector, we need to make sure that the class object is written whenever
116 // any of the subobjects are altered and may need rescanning. This may be tricky
117 // during construction, and this may not be the right way to do this with
118 // incremental collection.
119 // If we overflow the mark stack, we will rescan the class object, so we should
120 // be OK. The same applies if we redo the mark phase because win32 unmapped part
121 // of our root set. - HB
122 jclass c
= (jclass
) addr
;
125 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c3label
);
126 p
= (ptr_t
) c
->superclass
;
127 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c4label
);
128 for (int i
= 0; i
< c
->constants
.size
; ++i
)
130 /* FIXME: We could make this more precise by using the tags -KKT */
131 p
= (ptr_t
) c
->constants
.data
[i
].p
;
132 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c5label
);
136 if (_Jv_IsInterpretedClass (c
))
138 p
= (ptr_t
) c
->constants
.tags
;
139 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c5alabel
);
140 p
= (ptr_t
) c
->constants
.data
;
141 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c5blabel
);
142 p
= (ptr_t
) c
->vtable
;
143 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c5clabel
);
147 // If the class is an array, then the methods field holds a
148 // pointer to the element class. If the class is primitive,
149 // then the methods field holds a pointer to the array class.
150 p
= (ptr_t
) c
->methods
;
151 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c6label
);
154 if (! c
->isArray() && ! c
->isPrimitive())
156 // Scan each method in the cases where `methods' really
157 // points to a methods structure.
158 for (int i
= 0; i
< c
->method_count
; ++i
)
160 p
= (ptr_t
) c
->methods
[i
].name
;
161 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
,
163 p
= (ptr_t
) c
->methods
[i
].signature
;
164 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
,
167 // FIXME: `ncode' entry?
170 // The interpreter installs a heap-allocated
171 // trampoline here, so we'll mark it.
172 if (_Jv_IsInterpretedClass (c
))
174 p
= (ptr_t
) c
->methods
[i
].ncode
;
175 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
,
182 // Mark all the fields.
183 p
= (ptr_t
) c
->fields
;
184 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c8label
);
185 for (int i
= 0; i
< c
->field_count
; ++i
)
187 _Jv_Field
* field
= &c
->fields
[i
];
189 #ifndef COMPACT_FIELDS
190 p
= (ptr_t
) field
->name
;
191 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c8alabel
);
193 p
= (ptr_t
) field
->type
;
194 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c8blabel
);
196 // For the interpreter, we also need to mark the memory
197 // containing static members
198 if ((field
->flags
& java::lang::reflect::Modifier::STATIC
))
200 p
= (ptr_t
) field
->u
.addr
;
201 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c8clabel
);
203 // also, if the static member is a reference,
204 // mark also the value pointed to. We check for isResolved
205 // since marking can happen before memory is allocated for
207 if (JvFieldIsRef (field
) && field
->isResolved())
209 jobject val
= *(jobject
*) field
->u
.addr
;
211 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
,
217 p
= (ptr_t
) c
->vtable
;
218 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, c9label
);
219 p
= (ptr_t
) c
->interfaces
;
220 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, cAlabel
);
221 for (int i
= 0; i
< c
->interface_count
; ++i
)
223 p
= (ptr_t
) c
->interfaces
[i
];
224 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, cClabel
);
226 p
= (ptr_t
) c
->loader
;
227 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, c
, cBlabel
);
230 if (_Jv_IsInterpretedClass (c
))
232 _Jv_InterpClass
* ic
= (_Jv_InterpClass
*)c
;
234 p
= (ptr_t
) ic
->interpreted_methods
;
235 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, ic
, cElabel
);
237 for (int i
= 0; i
< c
->method_count
; i
++)
239 p
= (ptr_t
) ic
->interpreted_methods
[i
];
240 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, ic
, \
244 p
= (ptr_t
) ic
->field_initializers
;
245 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, ic
, cGlabel
);
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
);
272 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
,
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.
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
;
302 // Every object has a sync_info pointer.
303 ptr_t p
= (ptr_t
) array
->sync_info
;
304 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, array
, e1label
);
305 // Mark the object's class.
307 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, obj
, o2label
);
309 for (int i
= 0; i
< JvGetArrayLength (array
); ++i
)
311 jobject obj
= elements (array
)[i
];
313 MAYBE_MARK (p
, mark_stack_ptr
, mark_stack_limit
, array
, e2label
);
316 return mark_stack_ptr
;
319 // Return GC descriptor for interpreted class
322 // We assume that the gcj mark proc has index 0. This is a dubious assumption,
323 // since another one could be registered first. But the compiler also
324 // knows this, so in that case everything else will break, too.
325 #define GCJ_DEFAULT_DESCR MAKE_PROC(GCJ_RESERVED_MARK_PROC_INDEX,0)
327 _Jv_BuildGCDescr(jclass klass
)
329 /* FIXME: We should really look at the class and build the descriptor. */
330 return (void *)(GCJ_DEFAULT_DESCR
);
334 // Allocate space for a new Java object.
336 _Jv_AllocObj (jsize size
, jclass klass
)
338 return GC_GCJ_MALLOC (size
, klass
->vtable
);
341 // Allocate space for a new Java array.
342 // Used only for arrays of objects.
344 _Jv_AllocArray (jsize size
, jclass klass
)
347 const jsize min_heap_addr
= 16*1024;
348 // A heuristic. If size is less than this value, the size
349 // stored in the array can't possibly be misinterpreted as
350 // a pointer. Thus we lose nothing by scanning the object
351 // completely conservatively, since no misidentification can
355 // There isn't much to lose by scanning this conservatively.
356 // If we didn't, the mark proc would have to understand that
357 // it needed to skip the header.
358 obj
= GC_MALLOC(size
);
360 if (size
< min_heap_addr
)
361 obj
= GC_MALLOC(size
);
363 obj
= GC_GENERIC_MALLOC (size
, array_kind_x
);
365 *((_Jv_VTable
**) obj
) = klass
->vtable
;
369 // Allocate some space that is known to be pointer-free.
371 _Jv_AllocBytes (jsize size
)
373 void *r
= GC_MALLOC_ATOMIC (size
);
374 // We have to explicitly zero memory here, as the GC doesn't
375 // guarantee that PTRFREE allocations are zeroed. Note that we
376 // don't have to do this for other allocation types because we set
377 // the `ok_init' flag in the type descriptor.
378 if (__builtin_expect (r
!= NULL
, !NULL
))
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
);
481 // Configure the collector to use the bitmap marking descriptors that we
482 // stash in the class vtable.
483 GC_init_gcj_malloc (0, (void *) _Jv_MarkObj
);
486 GC_java_finalization
= 1;
488 // We use a different mark procedure for object arrays. This code
489 // configures a different object `kind' for object array allocation and
490 // marking. FIXME: see above.
491 array_free_list
= (ptr_t
*) GC_generic_malloc_inner ((MAXOBJSZ
+ 1)
494 memset (array_free_list
, 0, (MAXOBJSZ
+ 1) * sizeof (ptr_t
));
496 proc
= GC_n_mark_procs
++;
497 GC_mark_procs
[proc
] = (mark_proc
) _Jv_MarkArray
;
499 array_kind_x
= GC_n_kinds
++;
500 GC_obj_kinds
[array_kind_x
].ok_freelist
= array_free_list
;
501 GC_obj_kinds
[array_kind_x
].ok_reclaim_list
= 0;
502 GC_obj_kinds
[array_kind_x
].ok_descriptor
= MAKE_PROC (proc
, 0);
503 GC_obj_kinds
[array_kind_x
].ok_relocate_descr
= FALSE
;
504 GC_obj_kinds
[array_kind_x
].ok_init
= TRUE
;
506 _Jv_MutexInit (&disable_gc_mutex
);
530 GC_java_finalization
= 1;
532 // Set up state for marking and allocation of Java objects.
533 obj_free_list
= (ptr_t
*) GC_generic_malloc_inner ((MAXOBJSZ
+ 1)
536 memset (obj_free_list
, 0, (MAXOBJSZ
+ 1) * sizeof (ptr_t
));
538 proc
= GC_n_mark_procs
++;
539 GC_mark_procs
[proc
] = (mark_proc
) _Jv_MarkObj
;
541 obj_kind_x
= GC_n_kinds
++;
542 GC_obj_kinds
[obj_kind_x
].ok_freelist
= obj_free_list
;
543 GC_obj_kinds
[obj_kind_x
].ok_reclaim_list
= 0;
544 GC_obj_kinds
[obj_kind_x
].ok_descriptor
= MAKE_PROC (proc
, 0);
545 GC_obj_kinds
[obj_kind_x
].ok_relocate_descr
= FALSE
;
546 GC_obj_kinds
[obj_kind_x
].ok_init
= TRUE
;
548 // Set up state for marking and allocation of arrays of Java
550 array_free_list
= (ptr_t
*) GC_generic_malloc_inner ((MAXOBJSZ
+ 1)
553 memset (array_free_list
, 0, (MAXOBJSZ
+ 1) * sizeof (ptr_t
));
555 proc
= GC_n_mark_procs
++;
556 GC_mark_procs
[proc
] = (mark_proc
) _Jv_MarkArray
;
558 array_kind_x
= GC_n_kinds
++;
559 GC_obj_kinds
[array_kind_x
].ok_freelist
= array_free_list
;
560 GC_obj_kinds
[array_kind_x
].ok_reclaim_list
= 0;
561 GC_obj_kinds
[array_kind_x
].ok_descriptor
= MAKE_PROC (proc
, 0);
562 GC_obj_kinds
[array_kind_x
].ok_relocate_descr
= FALSE
;
563 GC_obj_kinds
[array_kind_x
].ok_init
= TRUE
;
565 _Jv_MutexInit (&disable_gc_mutex
);