| 1 | // boehm.cc - interface between libjava and Boehm GC.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2000, 2001 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 | #include <stdio.h>
|
|---|
| 14 |
|
|---|
| 15 | #include <jvm.h>
|
|---|
| 16 | #include <gcj/cni.h>
|
|---|
| 17 |
|
|---|
| 18 | #include <java/lang/Class.h>
|
|---|
| 19 | #include <java/lang/reflect/Modifier.h>
|
|---|
| 20 | #include <java-interp.h>
|
|---|
| 21 |
|
|---|
| 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 |
|
|---|
| 27 | extern "C"
|
|---|
| 28 | {
|
|---|
| 29 | #include <private/gc_pmark.h>
|
|---|
| 30 | #include <gc_gcj.h>
|
|---|
| 31 |
|
|---|
| 32 | #ifdef THREAD_LOCAL_ALLOC
|
|---|
| 33 | # define GC_REDIRECT_TO_LOCAL
|
|---|
| 34 | # include <gc_local_alloc.h>
|
|---|
| 35 | #endif
|
|---|
| 36 |
|
|---|
| 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);
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | // We must check for plausibility ourselves.
|
|---|
| 43 | #define MAYBE_MARK(Obj, Top, Limit, Source, Exit) \
|
|---|
| 44 | Top=GC_MARK_AND_PUSH((GC_PTR)Obj, Top, Limit, (GC_PTR *)Source)
|
|---|
| 45 |
|
|---|
| 46 | // `kind' index used when allocating Java arrays.
|
|---|
| 47 | static int array_kind_x;
|
|---|
| 48 |
|
|---|
| 49 | // Freelist used for Java arrays.
|
|---|
| 50 | static ptr_t *array_free_list;
|
|---|
| 51 |
|
|---|
| 52 | // Lock used to protect access to Boehm's GC_enable/GC_disable functions.
|
|---|
| 53 | static _Jv_Mutex_t disable_gc_mutex;
|
|---|
| 54 |
|
|---|
| 55 | |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 | // This is called by the GC during the mark phase. It marks a Java
|
|---|
| 59 | // object. We use `void *' arguments and return, and not what the
|
|---|
| 60 | // Boehm GC wants, to avoid pollution in our headers.
|
|---|
| 61 | void *
|
|---|
| 62 | _Jv_MarkObj (void *addr, void *msp, void *msl, void * /* env */)
|
|---|
| 63 | {
|
|---|
| 64 | mse *mark_stack_ptr = (mse *) msp;
|
|---|
| 65 | mse *mark_stack_limit = (mse *) msl;
|
|---|
| 66 | jobject obj = (jobject) addr;
|
|---|
| 67 |
|
|---|
| 68 | // FIXME: if env is 1, this object was allocated through the debug
|
|---|
| 69 | // interface, and addr points to the beginning of the debug header.
|
|---|
| 70 | // In that case, we should really add the size of the header to addr.
|
|---|
| 71 |
|
|---|
| 72 | _Jv_VTable *dt = *(_Jv_VTable **) addr;
|
|---|
| 73 | // The object might not yet have its vtable set, or it might
|
|---|
| 74 | // really be an object on the freelist. In either case, the vtable slot
|
|---|
| 75 | // will either be 0, or it will point to a cleared object.
|
|---|
| 76 | // This assumes Java objects have size at least 3 words,
|
|---|
| 77 | // including the header. But this should remain true, since this
|
|---|
| 78 | // should only be used with debugging allocation or with large objects.
|
|---|
| 79 | if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
|
|---|
| 80 | return mark_stack_ptr;
|
|---|
| 81 | jclass klass = dt->clas;
|
|---|
| 82 | ptr_t p;
|
|---|
| 83 |
|
|---|
| 84 | # ifndef JV_HASH_SYNCHRONIZATION
|
|---|
| 85 | // Every object has a sync_info pointer.
|
|---|
| 86 | p = (ptr_t) obj->sync_info;
|
|---|
| 87 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o1label);
|
|---|
| 88 | # endif
|
|---|
| 89 | // Mark the object's class.
|
|---|
| 90 | p = (ptr_t) klass;
|
|---|
| 91 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label);
|
|---|
| 92 |
|
|---|
| 93 | if (__builtin_expect (klass == &java::lang::Class::class$, false))
|
|---|
| 94 | {
|
|---|
| 95 | // Currently we allocate some of the memory referenced from class objects
|
|---|
| 96 | // as pointerfree memory, and then mark it more intelligently here.
|
|---|
| 97 | // We ensure that the ClassClass mark descriptor forces invocation of
|
|---|
| 98 | // this procedure.
|
|---|
| 99 | // Correctness of this is subtle, but it looks OK to me for now. For the incremental
|
|---|
| 100 | // collector, we need to make sure that the class object is written whenever
|
|---|
| 101 | // any of the subobjects are altered and may need rescanning. This may be tricky
|
|---|
| 102 | // during construction, and this may not be the right way to do this with
|
|---|
| 103 | // incremental collection.
|
|---|
| 104 | // If we overflow the mark stack, we will rescan the class object, so we should
|
|---|
| 105 | // be OK. The same applies if we redo the mark phase because win32 unmapped part
|
|---|
| 106 | // of our root set. - HB
|
|---|
| 107 | jclass c = (jclass) addr;
|
|---|
| 108 |
|
|---|
| 109 | p = (ptr_t) c->name;
|
|---|
| 110 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c3label);
|
|---|
| 111 | p = (ptr_t) c->superclass;
|
|---|
| 112 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c4label);
|
|---|
| 113 | for (int i = 0; i < c->constants.size; ++i)
|
|---|
| 114 | {
|
|---|
| 115 | /* FIXME: We could make this more precise by using the tags -KKT */
|
|---|
| 116 | p = (ptr_t) c->constants.data[i].p;
|
|---|
| 117 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5label);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | #ifdef INTERPRETER
|
|---|
| 121 | if (_Jv_IsInterpretedClass (c))
|
|---|
| 122 | {
|
|---|
| 123 | p = (ptr_t) c->constants.tags;
|
|---|
| 124 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5alabel);
|
|---|
| 125 | p = (ptr_t) c->constants.data;
|
|---|
| 126 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5blabel);
|
|---|
| 127 | p = (ptr_t) c->vtable;
|
|---|
| 128 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5clabel);
|
|---|
| 129 | }
|
|---|
| 130 | #endif
|
|---|
| 131 |
|
|---|
| 132 | // If the class is an array, then the methods field holds a
|
|---|
| 133 | // pointer to the element class. If the class is primitive,
|
|---|
| 134 | // then the methods field holds a pointer to the array class.
|
|---|
| 135 | p = (ptr_t) c->methods;
|
|---|
| 136 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c6label);
|
|---|
| 137 |
|
|---|
| 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 = (ptr_t) c->methods[i].name;
|
|---|
| 146 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
|
|---|
| 147 | cm1label);
|
|---|
| 148 | p = (ptr_t) c->methods[i].signature;
|
|---|
| 149 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
|
|---|
| 150 | cm2label);
|
|---|
| 151 |
|
|---|
| 152 | // FIXME: `ncode' entry?
|
|---|
| 153 |
|
|---|
| 154 | #ifdef INTERPRETER
|
|---|
| 155 | // The interpreter installs a heap-allocated
|
|---|
| 156 | // trampoline here, so we'll mark it.
|
|---|
| 157 | if (_Jv_IsInterpretedClass (c))
|
|---|
| 158 | {
|
|---|
| 159 | p = (ptr_t) c->methods[i].ncode;
|
|---|
| 160 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
|
|---|
| 161 | cm3label);
|
|---|
| 162 | }
|
|---|
| 163 | #endif
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | // Mark all the fields.
|
|---|
| 168 | p = (ptr_t) c->fields;
|
|---|
| 169 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8label);
|
|---|
| 170 | for (int i = 0; i < c->field_count; ++i)
|
|---|
| 171 | {
|
|---|
| 172 | _Jv_Field* field = &c->fields[i];
|
|---|
| 173 |
|
|---|
| 174 | #ifndef COMPACT_FIELDS
|
|---|
| 175 | p = (ptr_t) field->name;
|
|---|
| 176 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8alabel);
|
|---|
| 177 | #endif
|
|---|
| 178 | p = (ptr_t) field->type;
|
|---|
| 179 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8blabel);
|
|---|
| 180 |
|
|---|
| 181 | // For the interpreter, we also need to mark the memory
|
|---|
| 182 | // containing static members
|
|---|
| 183 | if ((field->flags & java::lang::reflect::Modifier::STATIC))
|
|---|
| 184 | {
|
|---|
| 185 | p = (ptr_t) field->u.addr;
|
|---|
| 186 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8clabel);
|
|---|
| 187 |
|
|---|
| 188 | // also, if the static member is a reference,
|
|---|
| 189 | // mark also the value pointed to. We check for isResolved
|
|---|
| 190 | // since marking can happen before memory is allocated for
|
|---|
| 191 | // static members.
|
|---|
| 192 | if (JvFieldIsRef (field) && field->isResolved())
|
|---|
| 193 | {
|
|---|
| 194 | jobject val = *(jobject*) field->u.addr;
|
|---|
| 195 | p = (ptr_t) val;
|
|---|
| 196 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
|
|---|
| 197 | c, c8elabel);
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | p = (ptr_t) c->vtable;
|
|---|
| 203 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c9label);
|
|---|
| 204 | p = (ptr_t) c->interfaces;
|
|---|
| 205 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cAlabel);
|
|---|
| 206 | for (int i = 0; i < c->interface_count; ++i)
|
|---|
| 207 | {
|
|---|
| 208 | p = (ptr_t) c->interfaces[i];
|
|---|
| 209 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cClabel);
|
|---|
| 210 | }
|
|---|
| 211 | p = (ptr_t) c->loader;
|
|---|
| 212 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cBlabel);
|
|---|
| 213 | p = (ptr_t) c->arrayclass;
|
|---|
| 214 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cDlabel);
|
|---|
| 215 |
|
|---|
| 216 | #ifdef INTERPRETER
|
|---|
| 217 | if (_Jv_IsInterpretedClass (c))
|
|---|
| 218 | {
|
|---|
| 219 | _Jv_InterpClass* ic = (_Jv_InterpClass*)c;
|
|---|
| 220 |
|
|---|
| 221 | p = (ptr_t) ic->interpreted_methods;
|
|---|
| 222 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cElabel);
|
|---|
| 223 |
|
|---|
| 224 | for (int i = 0; i < c->method_count; i++)
|
|---|
| 225 | {
|
|---|
| 226 | p = (ptr_t) ic->interpreted_methods[i];
|
|---|
| 227 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
|
|---|
| 228 | cFlabel);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | p = (ptr_t) ic->field_initializers;
|
|---|
| 232 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cGlabel);
|
|---|
| 233 |
|
|---|
| 234 | }
|
|---|
| 235 | #endif
|
|---|
| 236 |
|
|---|
| 237 | }
|
|---|
| 238 | else
|
|---|
| 239 | {
|
|---|
| 240 | // NOTE: each class only holds information about the class
|
|---|
| 241 | // itself. So we must do the marking for the entire inheritance
|
|---|
| 242 | // tree in order to mark all fields. FIXME: what about
|
|---|
| 243 | // interfaces? We skip Object here, because Object only has a
|
|---|
| 244 | // sync_info, and we handled that earlier.
|
|---|
| 245 | // Note: occasionally `klass' can be null. For instance, this
|
|---|
| 246 | // can happen if a GC occurs between the point where an object
|
|---|
| 247 | // is allocated and where the vtbl slot is set.
|
|---|
| 248 | while (klass && klass != &java::lang::Object::class$)
|
|---|
| 249 | {
|
|---|
| 250 | jfieldID field = JvGetFirstInstanceField (klass);
|
|---|
| 251 | jint max = JvNumInstanceFields (klass);
|
|---|
| 252 |
|
|---|
| 253 | for (int i = 0; i < max; ++i)
|
|---|
| 254 | {
|
|---|
| 255 | if (JvFieldIsRef (field))
|
|---|
| 256 | {
|
|---|
| 257 | jobject val = JvGetObjectField (obj, field);
|
|---|
| 258 | p = (ptr_t) val;
|
|---|
| 259 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
|
|---|
| 260 | obj, elabel);
|
|---|
| 261 | }
|
|---|
| 262 | field = field->getNextField ();
|
|---|
| 263 | }
|
|---|
| 264 | klass = klass->getSuperclass();
|
|---|
| 265 | }
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | return mark_stack_ptr;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | // This is called by the GC during the mark phase. It marks a Java
|
|---|
| 272 | // array (of objects). We use `void *' arguments and return, and not
|
|---|
| 273 | // what the Boehm GC wants, to avoid pollution in our headers.
|
|---|
| 274 | void *
|
|---|
| 275 | _Jv_MarkArray (void *addr, void *msp, void *msl, void * /*env*/)
|
|---|
| 276 | {
|
|---|
| 277 | mse *mark_stack_ptr = (mse *) msp;
|
|---|
| 278 | mse *mark_stack_limit = (mse *) msl;
|
|---|
| 279 | jobjectArray array = (jobjectArray) addr;
|
|---|
| 280 |
|
|---|
| 281 | _Jv_VTable *dt = *(_Jv_VTable **) addr;
|
|---|
| 282 | // Assumes size >= 3 words. That's currently true since arrays have
|
|---|
| 283 | // a vtable, sync pointer, and size. If the sync pointer goes away,
|
|---|
| 284 | // we may need to round up the size.
|
|---|
| 285 | if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
|
|---|
| 286 | return mark_stack_ptr;
|
|---|
| 287 | jclass klass = dt->clas;
|
|---|
| 288 | ptr_t p;
|
|---|
| 289 |
|
|---|
| 290 | # ifndef JV_HASH_SYNCHRONIZATION
|
|---|
| 291 | // Every object has a sync_info pointer.
|
|---|
| 292 | p = (ptr_t) array->sync_info;
|
|---|
| 293 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e1label);
|
|---|
| 294 | # endif
|
|---|
| 295 | // Mark the object's class.
|
|---|
| 296 | p = (ptr_t) klass;
|
|---|
| 297 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, &(dt -> clas), o2label);
|
|---|
| 298 |
|
|---|
| 299 | for (int i = 0; i < JvGetArrayLength (array); ++i)
|
|---|
| 300 | {
|
|---|
| 301 | jobject obj = elements (array)[i];
|
|---|
| 302 | p = (ptr_t) obj;
|
|---|
| 303 | MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e2label);
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | return mark_stack_ptr;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | // Generate a GC marking descriptor for a class.
|
|---|
| 310 | //
|
|---|
| 311 | // We assume that the gcj mark proc has index 0. This is a dubious assumption,
|
|---|
| 312 | // since another one could be registered first. But the compiler also
|
|---|
| 313 | // knows this, so in that case everything else will break, too.
|
|---|
| 314 | #define GCJ_DEFAULT_DESCR GC_MAKE_PROC(GC_GCJ_RESERVED_MARK_PROC_INDEX,0)
|
|---|
| 315 | void *
|
|---|
| 316 | _Jv_BuildGCDescr(jclass)
|
|---|
| 317 | {
|
|---|
| 318 | /* FIXME: We should really look at the class and build the descriptor. */
|
|---|
| 319 | return (void *)(GCJ_DEFAULT_DESCR);
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | // Allocate some space that is known to be pointer-free.
|
|---|
| 323 | void *
|
|---|
| 324 | _Jv_AllocBytes (jsize size)
|
|---|
| 325 | {
|
|---|
| 326 | void *r = GC_MALLOC_ATOMIC (size);
|
|---|
| 327 | // We have to explicitly zero memory here, as the GC doesn't
|
|---|
| 328 | // guarantee that PTRFREE allocations are zeroed. Note that we
|
|---|
| 329 | // don't have to do this for other allocation types because we set
|
|---|
| 330 | // the `ok_init' flag in the type descriptor.
|
|---|
| 331 | memset (r, 0, size);
|
|---|
| 332 | return r;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | // Allocate space for a new Java array.
|
|---|
| 336 | // Used only for arrays of objects.
|
|---|
| 337 | void *
|
|---|
| 338 | _Jv_AllocArray (jsize size, jclass klass)
|
|---|
| 339 | {
|
|---|
| 340 | void *obj;
|
|---|
| 341 | const jsize min_heap_addr = 16*1024;
|
|---|
| 342 | // A heuristic. If size is less than this value, the size
|
|---|
| 343 | // stored in the array can't possibly be misinterpreted as
|
|---|
| 344 | // a pointer. Thus we lose nothing by scanning the object
|
|---|
| 345 | // completely conservatively, since no misidentification can
|
|---|
| 346 | // take place.
|
|---|
| 347 |
|
|---|
| 348 | #ifdef GC_DEBUG
|
|---|
| 349 | // There isn't much to lose by scanning this conservatively.
|
|---|
| 350 | // If we didn't, the mark proc would have to understand that
|
|---|
| 351 | // it needed to skip the header.
|
|---|
| 352 | obj = GC_MALLOC(size);
|
|---|
| 353 | #else
|
|---|
| 354 | if (size < min_heap_addr)
|
|---|
| 355 | obj = GC_MALLOC(size);
|
|---|
| 356 | else
|
|---|
| 357 | obj = GC_generic_malloc (size, array_kind_x);
|
|---|
| 358 | #endif
|
|---|
| 359 | *((_Jv_VTable **) obj) = klass->vtable;
|
|---|
| 360 | return obj;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | /* Allocate space for a new non-Java object, which does not have the usual
|
|---|
| 364 | Java object header but may contain pointers to other GC'ed objects. */
|
|---|
| 365 | void *
|
|---|
| 366 | _Jv_AllocRawObj (jsize size)
|
|---|
| 367 | {
|
|---|
| 368 | return (void *) GC_MALLOC (size);
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | static void
|
|---|
| 372 | call_finalizer (GC_PTR obj, GC_PTR client_data)
|
|---|
| 373 | {
|
|---|
| 374 | _Jv_FinalizerFunc *fn = (_Jv_FinalizerFunc *) client_data;
|
|---|
| 375 | jobject jobj = (jobject) obj;
|
|---|
| 376 |
|
|---|
| 377 | (*fn) (jobj);
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | void
|
|---|
| 381 | _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *meth)
|
|---|
| 382 | {
|
|---|
| 383 | GC_REGISTER_FINALIZER_NO_ORDER (object, call_finalizer, (GC_PTR) meth,
|
|---|
| 384 | NULL, NULL);
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | void
|
|---|
| 388 | _Jv_RunFinalizers (void)
|
|---|
| 389 | {
|
|---|
| 390 | GC_invoke_finalizers ();
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | void
|
|---|
| 394 | _Jv_RunAllFinalizers (void)
|
|---|
| 395 | {
|
|---|
| 396 | GC_finalize_all ();
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | void
|
|---|
| 400 | _Jv_RunGC (void)
|
|---|
| 401 | {
|
|---|
| 402 | GC_gcollect ();
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | long
|
|---|
| 406 | _Jv_GCTotalMemory (void)
|
|---|
| 407 | {
|
|---|
| 408 | return GC_get_heap_size ();
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | long
|
|---|
| 412 | _Jv_GCFreeMemory (void)
|
|---|
| 413 | {
|
|---|
| 414 | return GC_get_free_bytes ();
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | void
|
|---|
| 418 | _Jv_GCSetInitialHeapSize (size_t size)
|
|---|
| 419 | {
|
|---|
| 420 | size_t current = GC_get_heap_size ();
|
|---|
| 421 | if (size > current)
|
|---|
| 422 | GC_expand_hp (size - current);
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | void
|
|---|
| 426 | _Jv_GCSetMaximumHeapSize (size_t size)
|
|---|
| 427 | {
|
|---|
| 428 | GC_set_max_heap_size ((GC_word) size);
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | // From boehm's misc.c
|
|---|
| 432 | extern "C" void GC_enable();
|
|---|
| 433 | extern "C" void GC_disable();
|
|---|
| 434 |
|
|---|
| 435 | void
|
|---|
| 436 | _Jv_DisableGC (void)
|
|---|
| 437 | {
|
|---|
| 438 | _Jv_MutexLock (&disable_gc_mutex);
|
|---|
| 439 | GC_disable();
|
|---|
| 440 | _Jv_MutexUnlock (&disable_gc_mutex);
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | void
|
|---|
| 444 | _Jv_EnableGC (void)
|
|---|
| 445 | {
|
|---|
| 446 | _Jv_MutexLock (&disable_gc_mutex);
|
|---|
| 447 | GC_enable();
|
|---|
| 448 | _Jv_MutexUnlock (&disable_gc_mutex);
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | static void * handle_out_of_memory(size_t)
|
|---|
| 452 | {
|
|---|
| 453 | _Jv_ThrowNoMemory();
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | void
|
|---|
| 457 | _Jv_InitGC (void)
|
|---|
| 458 | {
|
|---|
| 459 | int proc;
|
|---|
| 460 |
|
|---|
| 461 | // Ignore pointers that do not point to the start of an object.
|
|---|
| 462 | GC_all_interior_pointers = 0;
|
|---|
| 463 |
|
|---|
| 464 | // Configure the collector to use the bitmap marking descriptors that we
|
|---|
| 465 | // stash in the class vtable.
|
|---|
| 466 | GC_init_gcj_malloc (0, (void *) _Jv_MarkObj);
|
|---|
| 467 |
|
|---|
| 468 | // Cause an out of memory error to be thrown from the allocators,
|
|---|
| 469 | // instead of returning 0. This is cheaper than checking on allocation.
|
|---|
| 470 | GC_oom_fn = handle_out_of_memory;
|
|---|
| 471 |
|
|---|
| 472 | GC_java_finalization = 1;
|
|---|
| 473 |
|
|---|
| 474 | // We use a different mark procedure for object arrays. This code
|
|---|
| 475 | // configures a different object `kind' for object array allocation and
|
|---|
| 476 | // marking. FIXME: see above.
|
|---|
| 477 | array_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
|
|---|
| 478 | * sizeof (ptr_t),
|
|---|
| 479 | PTRFREE);
|
|---|
| 480 | memset (array_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
|
|---|
| 481 |
|
|---|
| 482 | proc = GC_n_mark_procs++;
|
|---|
| 483 | GC_mark_procs[proc] = (GC_mark_proc) _Jv_MarkArray;
|
|---|
| 484 |
|
|---|
| 485 | array_kind_x = GC_n_kinds++;
|
|---|
| 486 | GC_obj_kinds[array_kind_x].ok_freelist = array_free_list;
|
|---|
| 487 | GC_obj_kinds[array_kind_x].ok_reclaim_list = 0;
|
|---|
| 488 | GC_obj_kinds[array_kind_x].ok_descriptor = GC_MAKE_PROC (proc, 0);
|
|---|
| 489 | GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE;
|
|---|
| 490 | GC_obj_kinds[array_kind_x].ok_init = TRUE;
|
|---|
| 491 |
|
|---|
| 492 | _Jv_MutexInit (&disable_gc_mutex);
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | #ifdef JV_HASH_SYNCHRONIZATION
|
|---|
| 496 | // Allocate an object with a fake vtable pointer, which causes only
|
|---|
| 497 | // the first field (beyond the fake vtable pointer) to be traced.
|
|---|
| 498 | // Eventually this should probably be generalized.
|
|---|
| 499 |
|
|---|
| 500 | static _Jv_VTable trace_one_vtable = {
|
|---|
| 501 | 0, // class pointer
|
|---|
| 502 | (void *)(2 * sizeof(void *)),
|
|---|
| 503 | // descriptor; scan 2 words incl. vtable ptr.
|
|---|
| 504 | // Least significant bits must be zero to
|
|---|
| 505 | // identify this as a length descriptor
|
|---|
| 506 | {0} // First method
|
|---|
| 507 | };
|
|---|
| 508 |
|
|---|
| 509 | void *
|
|---|
| 510 | _Jv_AllocTraceOne (jsize size /* includes vtable slot */)
|
|---|
| 511 | {
|
|---|
| 512 | return GC_GCJ_MALLOC (size, &trace_one_vtable);
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | // Ditto for two words.
|
|---|
| 516 | // the first field (beyond the fake vtable pointer) to be traced.
|
|---|
| 517 | // Eventually this should probably be generalized.
|
|---|
| 518 |
|
|---|
| 519 | static _Jv_VTable trace_two_vtable =
|
|---|
| 520 | {
|
|---|
| 521 | 0, // class pointer
|
|---|
| 522 | (void *)(3 * sizeof(void *)),
|
|---|
| 523 | // descriptor; scan 3 words incl. vtable ptr.
|
|---|
| 524 | {0} // First method
|
|---|
| 525 | };
|
|---|
| 526 |
|
|---|
| 527 | void *
|
|---|
| 528 | _Jv_AllocTraceTwo (jsize size /* includes vtable slot */)
|
|---|
| 529 | {
|
|---|
| 530 | return GC_GCJ_MALLOC (size, &trace_two_vtable);
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | #endif /* JV_HASH_SYNCHRONIZATION */
|
|---|
| 534 |
|
|---|
| 535 | void
|
|---|
| 536 | _Jv_GCInitializeFinalizers (void (*notifier) (void))
|
|---|
| 537 | {
|
|---|
| 538 | GC_finalize_on_demand = 1;
|
|---|
| 539 | GC_finalizer_notifier = notifier;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | void
|
|---|
| 543 | _Jv_GCRegisterDisappearingLink (jobject *objp)
|
|---|
| 544 | {
|
|---|
| 545 | GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp);
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | jboolean
|
|---|
| 549 | _Jv_GCCanReclaimSoftReference (jobject)
|
|---|
| 550 | {
|
|---|
| 551 | // For now, always reclaim soft references. FIXME.
|
|---|
| 552 | return true;
|
|---|
| 553 | }
|
|---|