]> gcc.gnu.org Git - gcc.git/blame - libjava/boehm.cc
unwind-c.c: Define NO_SIZE_OF_ENCODED_VALUE.
[gcc.git] / libjava / boehm.cc
CommitLineData
ee9dd372
TT
1// boehm.cc - interface between libjava and Boehm GC.
2
7ea082bb 3/* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation
ee9dd372
TT
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
11#include <config.h>
12
13#include <stdio.h>
14
ee9dd372 15#include <jvm.h>
27e934d8
TT
16#include <gcj/cni.h>
17
18#include <java/lang/Class.h>
a52dee2f 19#include <java/lang/reflect/Modifier.h>
58eb6e7c 20#include <java-interp.h>
ee9dd372 21
657ac766
TT
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
26
ee9dd372
TT
27extern "C"
28{
1f4eb17d
BM
29#include <private/gc_pmark.h>
30#include <gc_gcj.h>
ee9dd372 31
eed56669
HB
32#ifdef THREAD_LOCAL_ALLOC
33# define GC_REDIRECT_TO_LOCAL
34# include <gc_local_alloc.h>
35#endif
36
ee9dd372
TT
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);
7125beea 40};
ee9dd372 41
ee9dd372 42#define MAYBE_MARK(Obj, Top, Limit, Source, Exit) \
eed56669 43 Top=GC_MARK_AND_PUSH((GC_PTR)Obj, Top, Limit, (GC_PTR *)Source)
ee9dd372 44
bf3b8e42
HB
45// `kind' index used when allocating Java arrays.
46static int array_kind_x;
ee9dd372
TT
47
48// Freelist used for Java arrays.
49static ptr_t *array_free_list;
50
54c2f04b
AG
51// Lock used to protect access to Boehm's GC_enable/GC_disable functions.
52static _Jv_Mutex_t disable_gc_mutex;
53
ee9dd372
TT
54\f
55
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.
59void *
bf3b8e42 60_Jv_MarkObj (void *addr, void *msp, void *msl, void * /* env */)
ee9dd372
TT
61{
62 mse *mark_stack_ptr = (mse *) msp;
63 mse *mark_stack_limit = (mse *) msl;
64 jobject obj = (jobject) addr;
65
bf3b8e42
HB
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.
69
ee9dd372 70 _Jv_VTable *dt = *(_Jv_VTable **) addr;
bf3b8e42
HB
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))
ee9dd372
TT
78 return mark_stack_ptr;
79 jclass klass = dt->clas;
3610e0d5 80 ptr_t p;
ee9dd372 81
3610e0d5
TT
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
ee9dd372 87 // Mark the object's class.
4824d1bb
BM
88 p = (ptr_t) klass;
89 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label);
ee9dd372 90
1d336a09 91 if (__builtin_expect (klass == &java::lang::Class::class$, false))
ee9dd372 92 {
bf3b8e42
HB
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
ee9dd372
TT
105 jclass c = (jclass) addr;
106
4824d1bb
BM
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);
ee9dd372
TT
111 for (int i = 0; i < c->constants.size; ++i)
112 {
7941ceab 113 /* FIXME: We could make this more precise by using the tags -KKT */
4824d1bb
BM
114 p = (ptr_t) c->constants.data[i].p;
115 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5label);
ee9dd372
TT
116 }
117
58eb6e7c
AG
118#ifdef INTERPRETER
119 if (_Jv_IsInterpretedClass (c))
120 {
4824d1bb
BM
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);
bf3b8e42
HB
125 p = (ptr_t) c->vtable;
126 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5clabel);
58eb6e7c
AG
127 }
128#endif
129
ee9dd372
TT
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.
4824d1bb
BM
133 p = (ptr_t) c->methods;
134 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c6label);
ee9dd372 135
7ea082bb
TT
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;
58eb6e7c 142
ee9dd372
TT
143 if (! c->isArray() && ! c->isPrimitive())
144 {
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)
148 {
4824d1bb
BM
149 p = (ptr_t) c->methods[i].name;
150 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
ee9dd372 151 cm1label);
4824d1bb
BM
152 p = (ptr_t) c->methods[i].signature;
153 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
ee9dd372 154 cm2label);
ee9dd372
TT
155 }
156 }
157
158 // Mark all the fields.
4824d1bb
BM
159 p = (ptr_t) c->fields;
160 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8label);
ee9dd372
TT
161 for (int i = 0; i < c->field_count; ++i)
162 {
58eb6e7c
AG
163 _Jv_Field* field = &c->fields[i];
164
ee9dd372 165#ifndef COMPACT_FIELDS
4824d1bb
BM
166 p = (ptr_t) field->name;
167 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8alabel);
ee9dd372 168#endif
4824d1bb
BM
169 p = (ptr_t) field->type;
170 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8blabel);
58eb6e7c
AG
171
172 // For the interpreter, we also need to mark the memory
173 // containing static members
a52dee2f 174 if ((field->flags & java::lang::reflect::Modifier::STATIC))
58eb6e7c 175 {
4824d1bb
BM
176 p = (ptr_t) field->u.addr;
177 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8clabel);
58eb6e7c
AG
178
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())
184 {
185 jobject val = *(jobject*) field->u.addr;
4824d1bb
BM
186 p = (ptr_t) val;
187 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
58eb6e7c
AG
188 c, c8elabel);
189 }
190 }
ee9dd372
TT
191 }
192
4824d1bb
BM
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);
ee9dd372
TT
197 for (int i = 0; i < c->interface_count; ++i)
198 {
4824d1bb
BM
199 p = (ptr_t) c->interfaces[i];
200 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cClabel);
ee9dd372 201 }
4824d1bb
BM
202 p = (ptr_t) c->loader;
203 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cBlabel);
5bb11b2e
BM
204 p = (ptr_t) c->arrayclass;
205 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cDlabel);
7125beea
AG
206 p = (ptr_t) c->protectionDomain;
207 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cPlabel);
58eb6e7c
AG
208
209#ifdef INTERPRETER
210 if (_Jv_IsInterpretedClass (c))
211 {
b4d0051b 212 _Jv_InterpClass* ic = (_Jv_InterpClass*) c;
58eb6e7c 213
4824d1bb
BM
214 p = (ptr_t) ic->interpreted_methods;
215 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cElabel);
58eb6e7c
AG
216
217 for (int i = 0; i < c->method_count; i++)
218 {
4824d1bb
BM
219 p = (ptr_t) ic->interpreted_methods[i];
220 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
58eb6e7c 221 cFlabel);
b4d0051b
TT
222
223 // Mark the direct-threaded code.
224 if ((c->methods[i].accflags
225 & java::lang::reflect::Modifier::NATIVE) == 0)
226 {
227 _Jv_InterpMethod *im
228 = (_Jv_InterpMethod *) ic->interpreted_methods[i];
229 if (im)
230 {
231 p = (ptr_t) im->prepared;
232 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
233 cFlabel);
234 }
235 }
236
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);
58eb6e7c
AG
242 }
243
4824d1bb
BM
244 p = (ptr_t) ic->field_initializers;
245 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cGlabel);
58eb6e7c
AG
246
247 }
248#endif
249
ee9dd372
TT
250 }
251 else
252 {
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.
1d336a09 261 while (klass && klass != &java::lang::Object::class$)
ee9dd372
TT
262 {
263 jfieldID field = JvGetFirstInstanceField (klass);
264 jint max = JvNumInstanceFields (klass);
265
266 for (int i = 0; i < max; ++i)
267 {
268 if (JvFieldIsRef (field))
269 {
270 jobject val = JvGetObjectField (obj, field);
4824d1bb
BM
271 p = (ptr_t) val;
272 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
ee9dd372
TT
273 obj, elabel);
274 }
8a922095 275 field = field->getNextField ();
ee9dd372
TT
276 }
277 klass = klass->getSuperclass();
278 }
279 }
280
281 return mark_stack_ptr;
282}
283
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.
287void *
288_Jv_MarkArray (void *addr, void *msp, void *msl, void * /*env*/)
289{
290 mse *mark_stack_ptr = (mse *) msp;
291 mse *mark_stack_limit = (mse *) msl;
292 jobjectArray array = (jobjectArray) addr;
293
294 _Jv_VTable *dt = *(_Jv_VTable **) addr;
bf3b8e42
HB
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))
ee9dd372
TT
299 return mark_stack_ptr;
300 jclass klass = dt->clas;
3610e0d5 301 ptr_t p;
ee9dd372 302
3610e0d5
TT
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
ee9dd372 308 // Mark the object's class.
4824d1bb 309 p = (ptr_t) klass;
eed56669 310 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, &(dt -> clas), o2label);
ee9dd372
TT
311
312 for (int i = 0; i < JvGetArrayLength (array); ++i)
313 {
314 jobject obj = elements (array)[i];
4824d1bb
BM
315 p = (ptr_t) obj;
316 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e2label);
ee9dd372
TT
317 }
318
319 return mark_stack_ptr;
320}
321
619c238c
BM
322// Generate a GC marking descriptor for a class.
323//
bf3b8e42
HB
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.
1f4eb17d 327#define GCJ_DEFAULT_DESCR GC_MAKE_PROC(GC_GCJ_RESERVED_MARK_PROC_INDEX,0)
bf3b8e42 328void *
3610e0d5 329_Jv_BuildGCDescr(jclass)
bf3b8e42
HB
330{
331 /* FIXME: We should really look at the class and build the descriptor. */
332 return (void *)(GCJ_DEFAULT_DESCR);
333}
bf3b8e42 334
3610e0d5 335// Allocate some space that is known to be pointer-free.
ee9dd372 336void *
3610e0d5 337_Jv_AllocBytes (jsize size)
ee9dd372 338{
3610e0d5
TT
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;
ee9dd372
TT
346}
347
bf3b8e42
HB
348// Allocate space for a new Java array.
349// Used only for arrays of objects.
ee9dd372 350void *
bf3b8e42 351_Jv_AllocArray (jsize size, jclass klass)
ee9dd372 352{
bf3b8e42
HB
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.
360
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
eed56669 370 obj = GC_generic_malloc (size, array_kind_x);
bf3b8e42
HB
371#endif
372 *((_Jv_VTable **) obj) = klass->vtable;
373 return obj;
ee9dd372
TT
374}
375
7866a458
BM
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. */
378void *
379_Jv_AllocRawObj (jsize size)
380{
381 return (void *) GC_MALLOC (size);
382}
383
ee9dd372
TT
384static void
385call_finalizer (GC_PTR obj, GC_PTR client_data)
386{
387 _Jv_FinalizerFunc *fn = (_Jv_FinalizerFunc *) client_data;
388 jobject jobj = (jobject) obj;
389
390 (*fn) (jobj);
391}
392
393void
394_Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *meth)
395{
25fef12b 396 GC_REGISTER_FINALIZER_NO_ORDER (object, call_finalizer, (GC_PTR) meth,
ee9dd372
TT
397 NULL, NULL);
398}
399
400void
401_Jv_RunFinalizers (void)
402{
403 GC_invoke_finalizers ();
404}
405
406void
407_Jv_RunAllFinalizers (void)
408{
409 GC_finalize_all ();
410}
411
412void
413_Jv_RunGC (void)
414{
415 GC_gcollect ();
416}
417
418long
419_Jv_GCTotalMemory (void)
420{
421 return GC_get_heap_size ();
422}
423
ee9dd372
TT
424long
425_Jv_GCFreeMemory (void)
426{
e40217a7 427 return GC_get_free_bytes ();
ee9dd372
TT
428}
429
b8c3c4f0
TT
430void
431_Jv_GCSetInitialHeapSize (size_t size)
432{
433 size_t current = GC_get_heap_size ();
434 if (size > current)
3948f9d0 435 GC_expand_hp (size - current);
b8c3c4f0
TT
436}
437
438void
439_Jv_GCSetMaximumHeapSize (size_t size)
440{
441 GC_set_max_heap_size ((GC_word) size);
442}
443
54c2f04b
AG
444// From boehm's misc.c
445extern "C" void GC_enable();
446extern "C" void GC_disable();
447
448void
449_Jv_DisableGC (void)
450{
451 _Jv_MutexLock (&disable_gc_mutex);
452 GC_disable();
453 _Jv_MutexUnlock (&disable_gc_mutex);
454}
455
456void
457_Jv_EnableGC (void)
458{
459 _Jv_MutexLock (&disable_gc_mutex);
460 GC_enable();
461 _Jv_MutexUnlock (&disable_gc_mutex);
462}
463
3610e0d5
TT
464static void * handle_out_of_memory(size_t)
465{
466 _Jv_ThrowNoMemory();
467}
468
ee9dd372
TT
469void
470_Jv_InitGC (void)
471{
472 int proc;
ee9dd372 473
88c38cc1
BM
474 // Ignore pointers that do not point to the start of an object.
475 GC_all_interior_pointers = 0;
bf3b8e42
HB
476
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);
480
3610e0d5
TT
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;
484
bf3b8e42
HB
485 GC_java_finalization = 1;
486
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));
494
495 proc = GC_n_mark_procs++;
1f4eb17d 496 GC_mark_procs[proc] = (GC_mark_proc) _Jv_MarkArray;
bf3b8e42
HB
497
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;
1f4eb17d 501 GC_obj_kinds[array_kind_x].ok_descriptor = GC_MAKE_PROC (proc, 0);
bf3b8e42
HB
502 GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE;
503 GC_obj_kinds[array_kind_x].ok_init = TRUE;
504
505 _Jv_MutexInit (&disable_gc_mutex);
bf3b8e42
HB
506}
507
3610e0d5
TT
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.
512
513static _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
3a8da024 518 // identify this as a length descriptor
3610e0d5
TT
519 {0} // First method
520};
521
522void *
523_Jv_AllocTraceOne (jsize size /* includes vtable slot */)
524{
525 return GC_GCJ_MALLOC (size, &trace_one_vtable);
526}
527
3a8da024
HB
528// Ditto for two words.
529// the first field (beyond the fake vtable pointer) to be traced.
530// Eventually this should probably be generalized.
531
532static _Jv_VTable trace_two_vtable =
533{
534 0, // class pointer
535 (void *)(3 * sizeof(void *)),
536 // descriptor; scan 3 words incl. vtable ptr.
537 {0} // First method
538};
539
540void *
541_Jv_AllocTraceTwo (jsize size /* includes vtable slot */)
542{
543 return GC_GCJ_MALLOC (size, &trace_two_vtable);
544}
545
3610e0d5
TT
546#endif /* JV_HASH_SYNCHRONIZATION */
547
eeedeb5a
TT
548void
549_Jv_GCInitializeFinalizers (void (*notifier) (void))
550{
551 GC_finalize_on_demand = 1;
552 GC_finalizer_notifier = notifier;
553}
554
2b3d3db6
TT
555void
556_Jv_GCRegisterDisappearingLink (jobject *objp)
557{
558 GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp);
559}
560
561jboolean
96a1ceb3 562_Jv_GCCanReclaimSoftReference (jobject)
2b3d3db6
TT
563{
564 // For now, always reclaim soft references. FIXME.
565 return true;
566}
This page took 0.365495 seconds and 5 git commands to generate.