]>
Commit | Line | Data |
---|---|---|
ee9dd372 TT |
1 | // boehm.cc - interface between libjava and Boehm GC. |
2 | ||
3610e0d5 | 3 | /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation |
ee9dd372 TT |
4 | |
5 | This file is part of libgcj. | |
6 | ||
7 | This software is copyrighted work licensed under the terms of the | |
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for | |
9 | details. */ | |
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 |
27 | extern "C" |
28 | { | |
1f4eb17d BM |
29 | #include <private/gc_priv.h> |
30 | #include <private/gc_pmark.h> | |
31 | #include <gc_gcj.h> | |
ee9dd372 | 32 | |
eed56669 HB |
33 | #ifdef THREAD_LOCAL_ALLOC |
34 | # define GC_REDIRECT_TO_LOCAL | |
35 | # include <gc_local_alloc.h> | |
36 | #endif | |
37 | ||
ee9dd372 TT |
38 | // These aren't declared in any Boehm GC header. |
39 | void GC_finalize_all (void); | |
40 | ptr_t GC_debug_generic_malloc (size_t size, int k, GC_EXTRA_PARAMS); | |
41 | }; | |
42 | ||
ee9dd372 TT |
43 | // We must check for plausibility ourselves. |
44 | #define MAYBE_MARK(Obj, Top, Limit, Source, Exit) \ | |
eed56669 | 45 | Top=GC_MARK_AND_PUSH((GC_PTR)Obj, Top, Limit, (GC_PTR *)Source) |
ee9dd372 | 46 | |
ee9dd372 TT |
47 | \f |
48 | ||
49 | // Nonzero if this module has been initialized. | |
50 | static int initialized = 0; | |
51 | ||
bf3b8e42 | 52 | #if 0 |
ee9dd372 TT |
53 | // `kind' index used when allocating Java objects. |
54 | static int obj_kind_x; | |
55 | ||
ee9dd372 TT |
56 | // Freelist used for Java objects. |
57 | static ptr_t *obj_free_list; | |
bf3b8e42 HB |
58 | #endif /* 0 */ |
59 | ||
60 | // `kind' index used when allocating Java arrays. | |
61 | static int array_kind_x; | |
ee9dd372 TT |
62 | |
63 | // Freelist used for Java arrays. | |
64 | static ptr_t *array_free_list; | |
65 | ||
54c2f04b AG |
66 | // Lock used to protect access to Boehm's GC_enable/GC_disable functions. |
67 | static _Jv_Mutex_t disable_gc_mutex; | |
68 | ||
ee9dd372 TT |
69 | \f |
70 | ||
71 | // This is called by the GC during the mark phase. It marks a Java | |
72 | // object. We use `void *' arguments and return, and not what the | |
73 | // Boehm GC wants, to avoid pollution in our headers. | |
74 | void * | |
bf3b8e42 | 75 | _Jv_MarkObj (void *addr, void *msp, void *msl, void * /* env */) |
ee9dd372 TT |
76 | { |
77 | mse *mark_stack_ptr = (mse *) msp; | |
78 | mse *mark_stack_limit = (mse *) msl; | |
79 | jobject obj = (jobject) addr; | |
80 | ||
bf3b8e42 HB |
81 | // FIXME: if env is 1, this object was allocated through the debug |
82 | // interface, and addr points to the beginning of the debug header. | |
83 | // In that case, we should really add the size of the header to addr. | |
84 | ||
ee9dd372 | 85 | _Jv_VTable *dt = *(_Jv_VTable **) addr; |
bf3b8e42 HB |
86 | // The object might not yet have its vtable set, or it might |
87 | // really be an object on the freelist. In either case, the vtable slot | |
88 | // will either be 0, or it will point to a cleared object. | |
89 | // This assumes Java objects have size at least 3 words, | |
90 | // including the header. But this should remain true, since this | |
91 | // should only be used with debugging allocation or with large objects. | |
92 | if (__builtin_expect (! dt || !(dt -> get_finalizer()), false)) | |
ee9dd372 TT |
93 | return mark_stack_ptr; |
94 | jclass klass = dt->clas; | |
3610e0d5 | 95 | ptr_t p; |
ee9dd372 | 96 | |
3610e0d5 TT |
97 | # ifndef JV_HASH_SYNCHRONIZATION |
98 | // Every object has a sync_info pointer. | |
99 | p = (ptr_t) obj->sync_info; | |
100 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o1label); | |
101 | # endif | |
ee9dd372 | 102 | // Mark the object's class. |
4824d1bb BM |
103 | p = (ptr_t) klass; |
104 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label); | |
ee9dd372 | 105 | |
1d336a09 | 106 | if (__builtin_expect (klass == &java::lang::Class::class$, false)) |
ee9dd372 | 107 | { |
bf3b8e42 HB |
108 | // Currently we allocate some of the memory referenced from class objects |
109 | // as pointerfree memory, and then mark it more intelligently here. | |
110 | // We ensure that the ClassClass mark descriptor forces invocation of | |
111 | // this procedure. | |
112 | // Correctness of this is subtle, but it looks OK to me for now. For the incremental | |
113 | // collector, we need to make sure that the class object is written whenever | |
114 | // any of the subobjects are altered and may need rescanning. This may be tricky | |
115 | // during construction, and this may not be the right way to do this with | |
116 | // incremental collection. | |
117 | // If we overflow the mark stack, we will rescan the class object, so we should | |
118 | // be OK. The same applies if we redo the mark phase because win32 unmapped part | |
119 | // of our root set. - HB | |
ee9dd372 TT |
120 | jclass c = (jclass) addr; |
121 | ||
4824d1bb BM |
122 | p = (ptr_t) c->name; |
123 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c3label); | |
124 | p = (ptr_t) c->superclass; | |
125 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c4label); | |
ee9dd372 TT |
126 | for (int i = 0; i < c->constants.size; ++i) |
127 | { | |
7941ceab | 128 | /* FIXME: We could make this more precise by using the tags -KKT */ |
4824d1bb BM |
129 | p = (ptr_t) c->constants.data[i].p; |
130 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5label); | |
ee9dd372 TT |
131 | } |
132 | ||
58eb6e7c AG |
133 | #ifdef INTERPRETER |
134 | if (_Jv_IsInterpretedClass (c)) | |
135 | { | |
4824d1bb BM |
136 | p = (ptr_t) c->constants.tags; |
137 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5alabel); | |
138 | p = (ptr_t) c->constants.data; | |
139 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5blabel); | |
bf3b8e42 HB |
140 | p = (ptr_t) c->vtable; |
141 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5clabel); | |
58eb6e7c AG |
142 | } |
143 | #endif | |
144 | ||
ee9dd372 TT |
145 | // If the class is an array, then the methods field holds a |
146 | // pointer to the element class. If the class is primitive, | |
147 | // then the methods field holds a pointer to the array class. | |
4824d1bb BM |
148 | p = (ptr_t) c->methods; |
149 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c6label); | |
ee9dd372 | 150 | |
58eb6e7c | 151 | |
ee9dd372 TT |
152 | if (! c->isArray() && ! c->isPrimitive()) |
153 | { | |
154 | // Scan each method in the cases where `methods' really | |
155 | // points to a methods structure. | |
156 | for (int i = 0; i < c->method_count; ++i) | |
157 | { | |
4824d1bb BM |
158 | p = (ptr_t) c->methods[i].name; |
159 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, | |
ee9dd372 | 160 | cm1label); |
4824d1bb BM |
161 | p = (ptr_t) c->methods[i].signature; |
162 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, | |
ee9dd372 | 163 | cm2label); |
58eb6e7c | 164 | |
ee9dd372 | 165 | // FIXME: `ncode' entry? |
58eb6e7c AG |
166 | |
167 | #ifdef INTERPRETER | |
168 | // The interpreter installs a heap-allocated | |
169 | // trampoline here, so we'll mark it. | |
170 | if (_Jv_IsInterpretedClass (c)) | |
171 | { | |
4824d1bb BM |
172 | p = (ptr_t) c->methods[i].ncode; |
173 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, | |
58eb6e7c AG |
174 | cm3label); |
175 | } | |
176 | #endif | |
ee9dd372 TT |
177 | } |
178 | } | |
179 | ||
180 | // Mark all the fields. | |
4824d1bb BM |
181 | p = (ptr_t) c->fields; |
182 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8label); | |
ee9dd372 TT |
183 | for (int i = 0; i < c->field_count; ++i) |
184 | { | |
58eb6e7c AG |
185 | _Jv_Field* field = &c->fields[i]; |
186 | ||
ee9dd372 | 187 | #ifndef COMPACT_FIELDS |
4824d1bb BM |
188 | p = (ptr_t) field->name; |
189 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8alabel); | |
ee9dd372 | 190 | #endif |
4824d1bb BM |
191 | p = (ptr_t) field->type; |
192 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8blabel); | |
58eb6e7c AG |
193 | |
194 | // For the interpreter, we also need to mark the memory | |
195 | // containing static members | |
a52dee2f | 196 | if ((field->flags & java::lang::reflect::Modifier::STATIC)) |
58eb6e7c | 197 | { |
4824d1bb BM |
198 | p = (ptr_t) field->u.addr; |
199 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8clabel); | |
58eb6e7c AG |
200 | |
201 | // also, if the static member is a reference, | |
202 | // mark also the value pointed to. We check for isResolved | |
203 | // since marking can happen before memory is allocated for | |
204 | // static members. | |
205 | if (JvFieldIsRef (field) && field->isResolved()) | |
206 | { | |
207 | jobject val = *(jobject*) field->u.addr; | |
4824d1bb BM |
208 | p = (ptr_t) val; |
209 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, | |
58eb6e7c AG |
210 | c, c8elabel); |
211 | } | |
212 | } | |
ee9dd372 TT |
213 | } |
214 | ||
4824d1bb BM |
215 | p = (ptr_t) c->vtable; |
216 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c9label); | |
217 | p = (ptr_t) c->interfaces; | |
218 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cAlabel); | |
ee9dd372 TT |
219 | for (int i = 0; i < c->interface_count; ++i) |
220 | { | |
4824d1bb BM |
221 | p = (ptr_t) c->interfaces[i]; |
222 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cClabel); | |
ee9dd372 | 223 | } |
4824d1bb BM |
224 | p = (ptr_t) c->loader; |
225 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cBlabel); | |
5bb11b2e BM |
226 | p = (ptr_t) c->arrayclass; |
227 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cDlabel); | |
58eb6e7c AG |
228 | |
229 | #ifdef INTERPRETER | |
230 | if (_Jv_IsInterpretedClass (c)) | |
231 | { | |
232 | _Jv_InterpClass* ic = (_Jv_InterpClass*)c; | |
233 | ||
4824d1bb BM |
234 | p = (ptr_t) ic->interpreted_methods; |
235 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cElabel); | |
58eb6e7c AG |
236 | |
237 | for (int i = 0; i < c->method_count; i++) | |
238 | { | |
4824d1bb BM |
239 | p = (ptr_t) ic->interpreted_methods[i]; |
240 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \ | |
58eb6e7c AG |
241 | cFlabel); |
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. | |
287 | void * | |
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 | ||
bf3b8e42 HB |
322 | // Return GC descriptor for interpreted class |
323 | #ifdef INTERPRETER | |
324 | ||
325 | // We assume that the gcj mark proc has index 0. This is a dubious assumption, | |
326 | // since another one could be registered first. But the compiler also | |
327 | // knows this, so in that case everything else will break, too. | |
1f4eb17d | 328 | #define GCJ_DEFAULT_DESCR GC_MAKE_PROC(GC_GCJ_RESERVED_MARK_PROC_INDEX,0) |
bf3b8e42 | 329 | void * |
3610e0d5 | 330 | _Jv_BuildGCDescr(jclass) |
bf3b8e42 HB |
331 | { |
332 | /* FIXME: We should really look at the class and build the descriptor. */ | |
333 | return (void *)(GCJ_DEFAULT_DESCR); | |
334 | } | |
335 | #endif | |
336 | ||
3610e0d5 | 337 | // Allocate some space that is known to be pointer-free. |
ee9dd372 | 338 | void * |
3610e0d5 | 339 | _Jv_AllocBytes (jsize size) |
ee9dd372 | 340 | { |
3610e0d5 TT |
341 | void *r = GC_MALLOC_ATOMIC (size); |
342 | // We have to explicitly zero memory here, as the GC doesn't | |
343 | // guarantee that PTRFREE allocations are zeroed. Note that we | |
344 | // don't have to do this for other allocation types because we set | |
345 | // the `ok_init' flag in the type descriptor. | |
346 | memset (r, 0, size); | |
347 | return r; | |
ee9dd372 TT |
348 | } |
349 | ||
bf3b8e42 HB |
350 | // Allocate space for a new Java array. |
351 | // Used only for arrays of objects. | |
ee9dd372 | 352 | void * |
bf3b8e42 | 353 | _Jv_AllocArray (jsize size, jclass klass) |
ee9dd372 | 354 | { |
bf3b8e42 HB |
355 | void *obj; |
356 | const jsize min_heap_addr = 16*1024; | |
357 | // A heuristic. If size is less than this value, the size | |
358 | // stored in the array can't possibly be misinterpreted as | |
359 | // a pointer. Thus we lose nothing by scanning the object | |
360 | // completely conservatively, since no misidentification can | |
361 | // take place. | |
362 | ||
363 | #ifdef GC_DEBUG | |
364 | // There isn't much to lose by scanning this conservatively. | |
365 | // If we didn't, the mark proc would have to understand that | |
366 | // it needed to skip the header. | |
367 | obj = GC_MALLOC(size); | |
368 | #else | |
369 | if (size < min_heap_addr) | |
370 | obj = GC_MALLOC(size); | |
371 | else | |
eed56669 | 372 | obj = GC_generic_malloc (size, array_kind_x); |
bf3b8e42 HB |
373 | #endif |
374 | *((_Jv_VTable **) obj) = klass->vtable; | |
375 | return obj; | |
ee9dd372 TT |
376 | } |
377 | ||
7866a458 BM |
378 | /* Allocate space for a new non-Java object, which does not have the usual |
379 | Java object header but may contain pointers to other GC'ed objects. */ | |
380 | void * | |
381 | _Jv_AllocRawObj (jsize size) | |
382 | { | |
383 | return (void *) GC_MALLOC (size); | |
384 | } | |
385 | ||
ee9dd372 TT |
386 | static void |
387 | call_finalizer (GC_PTR obj, GC_PTR client_data) | |
388 | { | |
389 | _Jv_FinalizerFunc *fn = (_Jv_FinalizerFunc *) client_data; | |
390 | jobject jobj = (jobject) obj; | |
391 | ||
392 | (*fn) (jobj); | |
393 | } | |
394 | ||
395 | void | |
396 | _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *meth) | |
397 | { | |
25fef12b | 398 | GC_REGISTER_FINALIZER_NO_ORDER (object, call_finalizer, (GC_PTR) meth, |
ee9dd372 TT |
399 | NULL, NULL); |
400 | } | |
401 | ||
402 | void | |
403 | _Jv_RunFinalizers (void) | |
404 | { | |
405 | GC_invoke_finalizers (); | |
406 | } | |
407 | ||
408 | void | |
409 | _Jv_RunAllFinalizers (void) | |
410 | { | |
411 | GC_finalize_all (); | |
412 | } | |
413 | ||
414 | void | |
415 | _Jv_RunGC (void) | |
416 | { | |
417 | GC_gcollect (); | |
418 | } | |
419 | ||
420 | long | |
421 | _Jv_GCTotalMemory (void) | |
422 | { | |
423 | return GC_get_heap_size (); | |
424 | } | |
425 | ||
ee9dd372 TT |
426 | long |
427 | _Jv_GCFreeMemory (void) | |
428 | { | |
e40217a7 | 429 | return GC_get_free_bytes (); |
ee9dd372 TT |
430 | } |
431 | ||
b8c3c4f0 TT |
432 | void |
433 | _Jv_GCSetInitialHeapSize (size_t size) | |
434 | { | |
435 | size_t current = GC_get_heap_size (); | |
436 | if (size > current) | |
3948f9d0 | 437 | GC_expand_hp (size - current); |
b8c3c4f0 TT |
438 | } |
439 | ||
440 | void | |
441 | _Jv_GCSetMaximumHeapSize (size_t size) | |
442 | { | |
443 | GC_set_max_heap_size ((GC_word) size); | |
444 | } | |
445 | ||
54c2f04b AG |
446 | // From boehm's misc.c |
447 | extern "C" void GC_enable(); | |
448 | extern "C" void GC_disable(); | |
449 | ||
450 | void | |
451 | _Jv_DisableGC (void) | |
452 | { | |
453 | _Jv_MutexLock (&disable_gc_mutex); | |
454 | GC_disable(); | |
455 | _Jv_MutexUnlock (&disable_gc_mutex); | |
456 | } | |
457 | ||
458 | void | |
459 | _Jv_EnableGC (void) | |
460 | { | |
461 | _Jv_MutexLock (&disable_gc_mutex); | |
462 | GC_enable(); | |
463 | _Jv_MutexUnlock (&disable_gc_mutex); | |
464 | } | |
465 | ||
3610e0d5 TT |
466 | static void * handle_out_of_memory(size_t) |
467 | { | |
468 | _Jv_ThrowNoMemory(); | |
469 | } | |
470 | ||
ee9dd372 TT |
471 | void |
472 | _Jv_InitGC (void) | |
473 | { | |
474 | int proc; | |
475 | DCL_LOCK_STATE; | |
476 | ||
477 | DISABLE_SIGNALS (); | |
478 | LOCK (); | |
479 | ||
480 | if (initialized) | |
481 | { | |
482 | UNLOCK (); | |
483 | ENABLE_SIGNALS (); | |
484 | return; | |
485 | } | |
486 | initialized = 1; | |
bf3b8e42 HB |
487 | UNLOCK (); |
488 | ||
489 | // Configure the collector to use the bitmap marking descriptors that we | |
490 | // stash in the class vtable. | |
491 | GC_init_gcj_malloc (0, (void *) _Jv_MarkObj); | |
492 | ||
3610e0d5 TT |
493 | // Cause an out of memory error to be thrown from the allocators, |
494 | // instead of returning 0. This is cheaper than checking on allocation. | |
495 | GC_oom_fn = handle_out_of_memory; | |
496 | ||
bf3b8e42 HB |
497 | LOCK (); |
498 | GC_java_finalization = 1; | |
499 | ||
500 | // We use a different mark procedure for object arrays. This code | |
501 | // configures a different object `kind' for object array allocation and | |
502 | // marking. FIXME: see above. | |
503 | array_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1) | |
504 | * sizeof (ptr_t), | |
505 | PTRFREE); | |
506 | memset (array_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t)); | |
507 | ||
508 | proc = GC_n_mark_procs++; | |
1f4eb17d | 509 | GC_mark_procs[proc] = (GC_mark_proc) _Jv_MarkArray; |
bf3b8e42 HB |
510 | |
511 | array_kind_x = GC_n_kinds++; | |
512 | GC_obj_kinds[array_kind_x].ok_freelist = array_free_list; | |
513 | GC_obj_kinds[array_kind_x].ok_reclaim_list = 0; | |
1f4eb17d | 514 | GC_obj_kinds[array_kind_x].ok_descriptor = GC_MAKE_PROC (proc, 0); |
bf3b8e42 HB |
515 | GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE; |
516 | GC_obj_kinds[array_kind_x].ok_init = TRUE; | |
517 | ||
518 | _Jv_MutexInit (&disable_gc_mutex); | |
519 | ||
520 | UNLOCK (); | |
521 | ENABLE_SIGNALS (); | |
522 | } | |
523 | ||
3610e0d5 TT |
524 | #ifdef JV_HASH_SYNCHRONIZATION |
525 | // Allocate an object with a fake vtable pointer, which causes only | |
526 | // the first field (beyond the fake vtable pointer) to be traced. | |
527 | // Eventually this should probably be generalized. | |
528 | ||
529 | static _Jv_VTable trace_one_vtable = { | |
530 | 0, // class pointer | |
531 | (void *)(2 * sizeof(void *)), | |
532 | // descriptor; scan 2 words incl. vtable ptr. | |
533 | // Least significant bits must be zero to | |
534 | // identify this as a lenght descriptor | |
535 | {0} // First method | |
536 | }; | |
537 | ||
538 | void * | |
539 | _Jv_AllocTraceOne (jsize size /* includes vtable slot */) | |
540 | { | |
541 | return GC_GCJ_MALLOC (size, &trace_one_vtable); | |
542 | } | |
543 | ||
544 | #endif /* JV_HASH_SYNCHRONIZATION */ | |
545 | ||
eeedeb5a TT |
546 | void |
547 | _Jv_GCInitializeFinalizers (void (*notifier) (void)) | |
548 | { | |
549 | GC_finalize_on_demand = 1; | |
550 | GC_finalizer_notifier = notifier; | |
551 | } | |
552 | ||
2b3d3db6 TT |
553 | void |
554 | _Jv_GCRegisterDisappearingLink (jobject *objp) | |
555 | { | |
556 | GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp); | |
557 | } | |
558 | ||
559 | jboolean | |
560 | _Jv_GCCanReclaimSoftReference (jobject obj) | |
561 | { | |
562 | // For now, always reclaim soft references. FIXME. | |
563 | return true; | |
564 | } | |
565 | ||
bf3b8e42 HB |
566 | #if 0 |
567 | void | |
568 | _Jv_InitGC (void) | |
569 | { | |
570 | int proc; | |
571 | DCL_LOCK_STATE; | |
572 | ||
573 | DISABLE_SIGNALS (); | |
574 | LOCK (); | |
575 | ||
576 | if (initialized) | |
577 | { | |
578 | UNLOCK (); | |
579 | ENABLE_SIGNALS (); | |
580 | return; | |
581 | } | |
582 | initialized = 1; | |
ee9dd372 | 583 | |
e40217a7 TT |
584 | GC_java_finalization = 1; |
585 | ||
ee9dd372 TT |
586 | // Set up state for marking and allocation of Java objects. |
587 | obj_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1) | |
588 | * sizeof (ptr_t), | |
589 | PTRFREE); | |
590 | memset (obj_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t)); | |
591 | ||
592 | proc = GC_n_mark_procs++; | |
1f4eb17d | 593 | GC_mark_procs[proc] = (GC_mark_proc) _Jv_MarkObj; |
ee9dd372 TT |
594 | |
595 | obj_kind_x = GC_n_kinds++; | |
596 | GC_obj_kinds[obj_kind_x].ok_freelist = obj_free_list; | |
597 | GC_obj_kinds[obj_kind_x].ok_reclaim_list = 0; | |
1f4eb17d | 598 | GC_obj_kinds[obj_kind_x].ok_descriptor = GC_MAKE_PROC (proc, 0); |
ee9dd372 TT |
599 | GC_obj_kinds[obj_kind_x].ok_relocate_descr = FALSE; |
600 | GC_obj_kinds[obj_kind_x].ok_init = TRUE; | |
601 | ||
602 | // Set up state for marking and allocation of arrays of Java | |
603 | // objects. | |
604 | array_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1) | |
605 | * sizeof (ptr_t), | |
606 | PTRFREE); | |
607 | memset (array_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t)); | |
608 | ||
609 | proc = GC_n_mark_procs++; | |
1f4eb17d | 610 | GC_mark_procs[proc] = (GC_mark_proc) _Jv_MarkArray; |
ee9dd372 TT |
611 | |
612 | array_kind_x = GC_n_kinds++; | |
613 | GC_obj_kinds[array_kind_x].ok_freelist = array_free_list; | |
614 | GC_obj_kinds[array_kind_x].ok_reclaim_list = 0; | |
1f4eb17d | 615 | GC_obj_kinds[array_kind_x].ok_descriptor = GC_MAKE_PROC (proc, 0); |
ee9dd372 TT |
616 | GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE; |
617 | GC_obj_kinds[array_kind_x].ok_init = TRUE; | |
618 | ||
54c2f04b AG |
619 | _Jv_MutexInit (&disable_gc_mutex); |
620 | ||
ee9dd372 TT |
621 | UNLOCK (); |
622 | ENABLE_SIGNALS (); | |
623 | } | |
bf3b8e42 | 624 | #endif /* 0 */ |