]> gcc.gnu.org Git - gcc.git/blob - libjava/boehm.cc
InetAddress.java: Reformatted.
[gcc.git] / libjava / boehm.cc
1 // boehm.cc - interface between libjava and Boehm GC.
2
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
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 extern "C"
14 {
15 #include <gc_config.h>
16
17 // Set GC_DEBUG before including gc.h!
18 #ifdef LIBGCJ_GC_DEBUG
19 # define GC_DEBUG
20 #endif
21
22 #include <gc_mark.h>
23 #include <gc_gcj.h>
24 #include <javaxfc.h> // GC_finalize_all declaration.
25
26 #ifdef THREAD_LOCAL_ALLOC
27 # define GC_REDIRECT_TO_LOCAL
28 # include <gc_local_alloc.h>
29 #endif
30 };
31
32 #include <stdio.h>
33 #include <limits.h>
34
35 #include <jvm.h>
36 #include <gcj/cni.h>
37
38 #include <java/lang/Class.h>
39 #include <java/lang/reflect/Modifier.h>
40 #include <java-interp.h>
41
42 #define MAYBE_MARK(Obj, Top, Limit, Source, Exit) \
43 Top=GC_MARK_AND_PUSH((GC_PTR)Obj, Top, Limit, (GC_PTR *)Source)
44
45 // `kind' index used when allocating Java arrays.
46 static int array_kind_x;
47
48 // Freelist used for Java arrays.
49 static void * *array_free_list;
50
51 \f
52
53 // This is called by the GC during the mark phase. It marks a Java
54 // object. We use `void *' arguments and return, and not what the
55 // Boehm GC wants, to avoid pollution in our headers.
56 void *
57 _Jv_MarkObj (void *addr, void *msp, void *msl, void * env)
58 {
59 struct GC_ms_entry *mark_stack_ptr = (struct GC_ms_entry *)msp;
60 struct GC_ms_entry *mark_stack_limit = (struct GC_ms_entry *)msl;
61
62 if (env == (void *)1) /* Object allocated with debug allocator. */
63 addr = (GC_PTR)GC_USR_PTR_FROM_BASE(addr);
64 jobject obj = (jobject) addr;
65
66 _Jv_VTable *dt = *(_Jv_VTable **) addr;
67 // The object might not yet have its vtable set, or it might
68 // really be an object on the freelist. In either case, the vtable slot
69 // will either be 0, or it will point to a cleared object.
70 // This assumes Java objects have size at least 3 words,
71 // including the header. But this should remain true, since this
72 // should only be used with debugging allocation or with large objects.
73 if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
74 return mark_stack_ptr;
75 jclass klass = dt->clas;
76 GC_PTR p;
77
78 # ifndef JV_HASH_SYNCHRONIZATION
79 // Every object has a sync_info pointer.
80 p = (GC_PTR) obj->sync_info;
81 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o1label);
82 # endif
83 // Mark the object's class.
84 p = (GC_PTR) klass;
85 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label);
86
87 if (__builtin_expect (klass == &java::lang::Class::class$, false))
88 {
89 // Currently we allocate some of the memory referenced from class objects
90 // as pointerfree memory, and then mark it more intelligently here.
91 // We ensure that the ClassClass mark descriptor forces invocation of
92 // this procedure.
93 // Correctness of this is subtle, but it looks OK to me for now. For the incremental
94 // collector, we need to make sure that the class object is written whenever
95 // any of the subobjects are altered and may need rescanning. This may be tricky
96 // during construction, and this may not be the right way to do this with
97 // incremental collection.
98 // If we overflow the mark stack, we will rescan the class object, so we should
99 // be OK. The same applies if we redo the mark phase because win32 unmapped part
100 // of our root set. - HB
101 jclass c = (jclass) addr;
102
103 p = (GC_PTR) c->name;
104 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c3label);
105 p = (GC_PTR) c->superclass;
106 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c4label);
107 for (int i = 0; i < c->constants.size; ++i)
108 {
109 /* FIXME: We could make this more precise by using the tags -KKT */
110 p = (GC_PTR) c->constants.data[i].p;
111 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5label);
112 }
113
114 #ifdef INTERPRETER
115 if (_Jv_IsInterpretedClass (c))
116 {
117 p = (GC_PTR) c->constants.tags;
118 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5alabel);
119 p = (GC_PTR) c->constants.data;
120 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5blabel);
121 p = (GC_PTR) c->vtable;
122 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5clabel);
123 }
124 #endif
125
126 // If the class is an array, then the methods field holds a
127 // pointer to the element class. If the class is primitive,
128 // then the methods field holds a pointer to the array class.
129 p = (GC_PTR) c->methods;
130 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c6label);
131
132 // The vtable might have been set, but the rest of the class
133 // could still be uninitialized. If this is the case, then
134 // c.isArray will SEGV. We check for this, and if it is the
135 // case we just return.
136 if (__builtin_expect (c->name == NULL, false))
137 return mark_stack_ptr;
138
139 if (! c->isArray() && ! c->isPrimitive())
140 {
141 // Scan each method in the cases where `methods' really
142 // points to a methods structure.
143 for (int i = 0; i < c->method_count; ++i)
144 {
145 p = (GC_PTR) c->methods[i].name;
146 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
147 cm1label);
148 p = (GC_PTR) c->methods[i].signature;
149 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
150 cm2label);
151 }
152 }
153
154 // Mark all the fields.
155 p = (GC_PTR) c->fields;
156 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8label);
157 for (int i = 0; i < c->field_count; ++i)
158 {
159 _Jv_Field* field = &c->fields[i];
160
161 #ifndef COMPACT_FIELDS
162 p = (GC_PTR) field->name;
163 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8alabel);
164 #endif
165 p = (GC_PTR) field->type;
166 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8blabel);
167
168 // For the interpreter, we also need to mark the memory
169 // containing static members
170 if ((field->flags & java::lang::reflect::Modifier::STATIC))
171 {
172 p = (GC_PTR) field->u.addr;
173 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8clabel);
174
175 // also, if the static member is a reference,
176 // mark also the value pointed to. We check for isResolved
177 // since marking can happen before memory is allocated for
178 // static members.
179 if (JvFieldIsRef (field) && field->isResolved())
180 {
181 jobject val = *(jobject*) field->u.addr;
182 p = (GC_PTR) val;
183 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
184 c, c8elabel);
185 }
186 }
187 }
188
189 p = (GC_PTR) c->vtable;
190 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c9label);
191 p = (GC_PTR) c->interfaces;
192 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cAlabel);
193 for (int i = 0; i < c->interface_count; ++i)
194 {
195 p = (GC_PTR) c->interfaces[i];
196 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cClabel);
197 }
198 p = (GC_PTR) c->loader;
199 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cBlabel);
200 p = (GC_PTR) c->arrayclass;
201 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cDlabel);
202 p = (GC_PTR) c->protectionDomain;
203 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cPlabel);
204 p = (GC_PTR) c->hack_signers;
205 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cSlabel);
206 p = (GC_PTR) c->aux_info;
207 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cTlabel);
208
209 #ifdef INTERPRETER
210 if (_Jv_IsInterpretedClass (c))
211 {
212 _Jv_InterpClass* ic = (_Jv_InterpClass*) c->aux_info;
213
214 p = (GC_PTR) ic->interpreted_methods;
215 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cElabel);
216
217 for (int i = 0; i < c->method_count; i++)
218 {
219 p = (GC_PTR) ic->interpreted_methods[i];
220 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
221 cFlabel);
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 = (GC_PTR) 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 = (GC_PTR) c->methods[i].ncode;
240 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
241 cm3label);
242 }
243
244 p = (GC_PTR) ic->field_initializers;
245 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cGlabel);
246
247 }
248 #endif
249
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.
261 while (klass && klass != &java::lang::Object::class$)
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);
271 p = (GC_PTR) val;
272 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
273 obj, elabel);
274 }
275 field = field->getNextField ();
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 struct GC_ms_entry *mark_stack_ptr = (struct GC_ms_entry *)msp;
291 struct GC_ms_entry *mark_stack_limit = (struct GC_ms_entry *)msl;
292
293 if (env == (void *)1) /* Object allocated with debug allocator. */
294 addr = (void *)GC_USR_PTR_FROM_BASE(addr);
295 jobjectArray array = (jobjectArray) addr;
296
297 _Jv_VTable *dt = *(_Jv_VTable **) addr;
298 // Assumes size >= 3 words. That's currently true since arrays have
299 // a vtable, sync pointer, and size. If the sync pointer goes away,
300 // we may need to round up the size.
301 if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
302 return mark_stack_ptr;
303 jclass klass = dt->clas;
304 GC_PTR p;
305
306 # ifndef JV_HASH_SYNCHRONIZATION
307 // Every object has a sync_info pointer.
308 p = (GC_PTR) array->sync_info;
309 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e1label);
310 # endif
311 // Mark the object's class.
312 p = (GC_PTR) klass;
313 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, &(dt -> clas), o2label);
314
315 for (int i = 0; i < JvGetArrayLength (array); ++i)
316 {
317 jobject obj = elements (array)[i];
318 p = (GC_PTR) obj;
319 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e2label);
320 }
321
322 return mark_stack_ptr;
323 }
324
325 // Generate a GC marking descriptor for a class.
326 //
327 // We assume that the gcj mark proc has index 0. This is a dubious assumption,
328 // since another one could be registered first. But the compiler also
329 // knows this, so in that case everything else will break, too.
330 #define GCJ_DEFAULT_DESCR GC_MAKE_PROC(GC_GCJ_RESERVED_MARK_PROC_INDEX,0)
331
332 void *
333 _Jv_BuildGCDescr(jclass self)
334 {
335 jlong desc = 0;
336 jint bits_per_word = CHAR_BIT * sizeof (void *);
337
338 // Note: for now we only consider a bitmap mark descriptor. We
339 // could also handle the case where the first N fields of a type are
340 // references. However, this is not very likely to be used by many
341 // classes, and it is easier to compute things this way.
342
343 // The vtable pointer.
344 desc |= 1ULL << (bits_per_word - 1);
345 #ifndef JV_HASH_SYNCHRONIZATION
346 // The sync_info field.
347 desc |= 1ULL << (bits_per_word - 2);
348 #endif
349
350 for (jclass klass = self; klass != NULL; klass = klass->getSuperclass())
351 {
352 jfieldID field = JvGetFirstInstanceField(klass);
353 int count = JvNumInstanceFields(klass);
354
355 for (int i = 0; i < count; ++i)
356 {
357 if (field->isRef())
358 {
359 unsigned int off = field->getOffset();
360 // If we run into a weird situation, we bail.
361 if (off % sizeof (void *) != 0)
362 return (void *) (GCJ_DEFAULT_DESCR);
363 off /= sizeof (void *);
364 // If we find a field outside the range of our bitmap,
365 // fall back to procedure marker. The bottom 2 bits are
366 // reserved.
367 if (off >= (unsigned)bits_per_word - 2)
368 return (void *) (GCJ_DEFAULT_DESCR);
369 desc |= 1ULL << (bits_per_word - off - 1);
370 }
371
372 field = field->getNextField();
373 }
374 }
375
376 // For bitmap mark type, bottom bits are 01.
377 desc |= 1;
378 // Bogus warning avoidance (on many platforms).
379 return (void *) (unsigned long) desc;
380 }
381
382 // Allocate some space that is known to be pointer-free.
383 void *
384 _Jv_AllocBytes (jsize size)
385 {
386 void *r = GC_MALLOC_ATOMIC (size);
387 // We have to explicitly zero memory here, as the GC doesn't
388 // guarantee that PTRFREE allocations are zeroed. Note that we
389 // don't have to do this for other allocation types because we set
390 // the `ok_init' flag in the type descriptor.
391 memset (r, 0, size);
392 return r;
393 }
394
395 #ifdef LIBGCJ_GC_DEBUG
396
397 void *
398 _Jv_AllocObj (jsize size, jclass klass)
399 {
400 return GC_GCJ_MALLOC (size, klass->vtable);
401 }
402
403 void *
404 _Jv_AllocPtrFreeObj (jsize size, jclass klass)
405 {
406 #ifdef JV_HASH_SYNCHRONIZATION
407 void * obj = GC_MALLOC_ATOMIC(size);
408 *((_Jv_VTable **) obj) = klass->vtable;
409 #else
410 void * obj = GC_GCJ_MALLOC(size, klass->vtable);
411 #endif
412 return obj;
413 }
414
415 #endif /* LIBGCJ_GC_DEBUG */
416 // In the non-debug case, the above two functions are defined
417 // as inline functions in boehm-gc.h. In the debug case we
418 // really want to take advantage of the definitions in gc_gcj.h.
419
420 // Allocate space for a new Java array.
421 // Used only for arrays of objects.
422 void *
423 _Jv_AllocArray (jsize size, jclass klass)
424 {
425 void *obj;
426
427 #ifdef LIBGCJ_GC_DEBUG
428 // There isn't much to lose by scanning this conservatively.
429 // If we didn't, the mark proc would have to understand that
430 // it needed to skip the header.
431 obj = GC_MALLOC(size);
432 #else
433 const jsize min_heap_addr = 16*1024;
434 // A heuristic. If size is less than this value, the size
435 // stored in the array can't possibly be misinterpreted as
436 // a pointer. Thus we lose nothing by scanning the object
437 // completely conservatively, since no misidentification can
438 // take place.
439
440 if (size < min_heap_addr)
441 obj = GC_MALLOC(size);
442 else
443 obj = GC_generic_malloc (size, array_kind_x);
444 #endif
445 *((_Jv_VTable **) obj) = klass->vtable;
446 return obj;
447 }
448
449 /* Allocate space for a new non-Java object, which does not have the usual
450 Java object header but may contain pointers to other GC'ed objects. */
451 void *
452 _Jv_AllocRawObj (jsize size)
453 {
454 return (void *) GC_MALLOC (size);
455 }
456
457 static void
458 call_finalizer (GC_PTR obj, GC_PTR client_data)
459 {
460 _Jv_FinalizerFunc *fn = (_Jv_FinalizerFunc *) client_data;
461 jobject jobj = (jobject) obj;
462
463 (*fn) (jobj);
464 }
465
466 void
467 _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *meth)
468 {
469 GC_REGISTER_FINALIZER_NO_ORDER (object, call_finalizer, (GC_PTR) meth,
470 NULL, NULL);
471 }
472
473 void
474 _Jv_RunFinalizers (void)
475 {
476 GC_invoke_finalizers ();
477 }
478
479 void
480 _Jv_RunAllFinalizers (void)
481 {
482 GC_finalize_all ();
483 }
484
485 void
486 _Jv_RunGC (void)
487 {
488 GC_gcollect ();
489 }
490
491 long
492 _Jv_GCTotalMemory (void)
493 {
494 return GC_get_heap_size ();
495 }
496
497 long
498 _Jv_GCFreeMemory (void)
499 {
500 return GC_get_free_bytes ();
501 }
502
503 void
504 _Jv_GCSetInitialHeapSize (size_t size)
505 {
506 size_t current = GC_get_heap_size ();
507 if (size > current)
508 GC_expand_hp (size - current);
509 }
510
511 void
512 _Jv_GCSetMaximumHeapSize (size_t size)
513 {
514 GC_set_max_heap_size ((GC_word) size);
515 }
516
517 // From boehm's misc.c
518 extern "C" void GC_enable();
519 extern "C" void GC_disable();
520
521 void
522 _Jv_DisableGC (void)
523 {
524 GC_disable();
525 }
526
527 void
528 _Jv_EnableGC (void)
529 {
530 GC_enable();
531 }
532
533 static void * handle_out_of_memory(size_t)
534 {
535 _Jv_ThrowNoMemory();
536 }
537
538 static void
539 gcj_describe_type_fn(void *obj, char *out_buf)
540 {
541 _Jv_VTable *dt = *(_Jv_VTable **) obj;
542
543 if (! dt /* Shouldn't happen */)
544 {
545 strcpy(out_buf, "GCJ (bad)");
546 return;
547 }
548 jclass klass = dt->clas;
549 if (!klass /* shouldn't happen */)
550 {
551 strcpy(out_buf, "GCJ (bad)");
552 return;
553 }
554 jstring name = klass -> getName();
555 size_t len = name -> length();
556 if (len >= GC_TYPE_DESCR_LEN) len = GC_TYPE_DESCR_LEN - 1;
557 JvGetStringUTFRegion (name, 0, len, out_buf);
558 out_buf[len] = '\0';
559 }
560
561 void
562 _Jv_InitGC (void)
563 {
564 int proc;
565
566 // Ignore pointers that do not point to the start of an object.
567 GC_all_interior_pointers = 0;
568
569 // Configure the collector to use the bitmap marking descriptors that we
570 // stash in the class vtable.
571 // We always use mark proc descriptor 0, since the compiler knows
572 // about it.
573 GC_init_gcj_malloc (0, (void *) _Jv_MarkObj);
574
575 // Cause an out of memory error to be thrown from the allocators,
576 // instead of returning 0. This is cheaper than checking on allocation.
577 GC_oom_fn = handle_out_of_memory;
578
579 GC_java_finalization = 1;
580
581 // We use a different mark procedure for object arrays. This code
582 // configures a different object `kind' for object array allocation and
583 // marking.
584 array_free_list = GC_new_free_list();
585 proc = GC_new_proc((GC_mark_proc)_Jv_MarkArray);
586 array_kind_x = GC_new_kind(array_free_list, GC_MAKE_PROC (proc, 0), 0, 1);
587
588 /* Arrange to have the GC print Java class names in backtraces, etc. */
589 GC_register_describe_type_fn(GC_gcj_kind, gcj_describe_type_fn);
590 GC_register_describe_type_fn(GC_gcj_debug_kind, gcj_describe_type_fn);
591 }
592
593 #ifdef JV_HASH_SYNCHRONIZATION
594 // Allocate an object with a fake vtable pointer, which causes only
595 // the first field (beyond the fake vtable pointer) to be traced.
596 // Eventually this should probably be generalized.
597
598 static _Jv_VTable trace_one_vtable = {
599 0, // class pointer
600 (void *)(2 * sizeof(void *)),
601 // descriptor; scan 2 words incl. vtable ptr.
602 // Least significant bits must be zero to
603 // identify this as a length descriptor
604 {0} // First method
605 };
606
607 void *
608 _Jv_AllocTraceOne (jsize size /* includes vtable slot */)
609 {
610 return GC_GCJ_MALLOC (size, &trace_one_vtable);
611 }
612
613 // Ditto for two words.
614 // the first field (beyond the fake vtable pointer) to be traced.
615 // Eventually this should probably be generalized.
616
617 static _Jv_VTable trace_two_vtable =
618 {
619 0, // class pointer
620 (void *)(3 * sizeof(void *)),
621 // descriptor; scan 3 words incl. vtable ptr.
622 {0} // First method
623 };
624
625 void *
626 _Jv_AllocTraceTwo (jsize size /* includes vtable slot */)
627 {
628 return GC_GCJ_MALLOC (size, &trace_two_vtable);
629 }
630
631 #endif /* JV_HASH_SYNCHRONIZATION */
632
633 void
634 _Jv_GCInitializeFinalizers (void (*notifier) (void))
635 {
636 GC_finalize_on_demand = 1;
637 GC_finalizer_notifier = notifier;
638 }
639
640 void
641 _Jv_GCRegisterDisappearingLink (jobject *objp)
642 {
643 GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp);
644 }
645
646 jboolean
647 _Jv_GCCanReclaimSoftReference (jobject)
648 {
649 // For now, always reclaim soft references. FIXME.
650 return true;
651 }
This page took 0.064378 seconds and 5 git commands to generate.