boehm.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. // boehm.cc - interface between libjava and Boehm GC.
  2. /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
  3. Free Software Foundation
  4. This file is part of libgcj.
  5. This software is copyrighted work licensed under the terms of the
  6. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  7. details. */
  8. #include <config.h>
  9. #include <stdio.h>
  10. #include <limits.h>
  11. #include <jvm.h>
  12. #include <gcj/cni.h>
  13. #include <java/lang/Class.h>
  14. #include <java/lang/reflect/Modifier.h>
  15. #include <java-interp.h>
  16. // More nastiness: the GC wants to define TRUE and FALSE. We don't
  17. // need the Java definitions (themselves a hack), so we undefine them.
  18. #undef TRUE
  19. #undef FALSE
  20. // We include two autoconf headers. Avoid multiple definition warnings.
  21. #undef PACKAGE_NAME
  22. #undef PACKAGE_STRING
  23. #undef PACKAGE_TARNAME
  24. #undef PACKAGE_VERSION
  25. #ifdef HAVE_DLFCN_H
  26. #undef _GNU_SOURCE
  27. #define _GNU_SOURCE
  28. #include <dlfcn.h>
  29. #endif
  30. extern "C"
  31. {
  32. #include <gc_config.h>
  33. // Set GC_DEBUG before including gc.h!
  34. #ifdef LIBGCJ_GC_DEBUG
  35. # define GC_DEBUG
  36. #endif
  37. #include <gc_mark.h>
  38. #include <gc_gcj.h>
  39. #include <javaxfc.h> // GC_finalize_all declaration.
  40. #ifdef THREAD_LOCAL_ALLOC
  41. # define GC_REDIRECT_TO_LOCAL
  42. # include <gc_local_alloc.h>
  43. #endif
  44. // From boehm's misc.c
  45. void GC_enable();
  46. void GC_disable();
  47. };
  48. #define MAYBE_MARK(Obj, Top, Limit, Source) \
  49. Top=GC_MARK_AND_PUSH((GC_PTR) Obj, Top, Limit, (GC_PTR *) Source)
  50. // `kind' index used when allocating Java arrays.
  51. static int array_kind_x;
  52. // Freelist used for Java arrays.
  53. static void **array_free_list;
  54. static int _Jv_GC_has_static_roots (const char *filename, void *, size_t);
  55. // This is called by the GC during the mark phase. It marks a Java
  56. // object. We use `void *' arguments and return, and not what the
  57. // Boehm GC wants, to avoid pollution in our headers.
  58. void *
  59. _Jv_MarkObj (void *addr, void *msp, void *msl, void *env)
  60. {
  61. struct GC_ms_entry *mark_stack_ptr = (struct GC_ms_entry *)msp;
  62. struct GC_ms_entry *mark_stack_limit = (struct GC_ms_entry *)msl;
  63. if (env == (void *)1) /* Object allocated with debug allocator. */
  64. addr = (GC_PTR)GC_USR_PTR_FROM_BASE(addr);
  65. jobject obj = (jobject) addr;
  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. p = (GC_PTR) dt;
  78. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj);
  79. # ifndef JV_HASH_SYNCHRONIZATION
  80. // Every object has a sync_info pointer.
  81. p = (GC_PTR) obj->sync_info;
  82. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj);
  83. # endif
  84. if (__builtin_expect (klass == &java::lang::Class::class$, false))
  85. {
  86. // Currently we allocate some of the memory referenced from class objects
  87. // as pointerfree memory, and then mark it more intelligently here.
  88. // We ensure that the ClassClass mark descriptor forces invocation of
  89. // this procedure.
  90. // Correctness of this is subtle, but it looks OK to me for now. For the incremental
  91. // collector, we need to make sure that the class object is written whenever
  92. // any of the subobjects are altered and may need rescanning. This may be tricky
  93. // during construction, and this may not be the right way to do this with
  94. // incremental collection.
  95. // If we overflow the mark stack, we will rescan the class object, so we should
  96. // be OK. The same applies if we redo the mark phase because win32 unmapped part
  97. // of our root set. - HB
  98. jclass c = (jclass) addr;
  99. p = (GC_PTR) c->name;
  100. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  101. p = (GC_PTR) c->superclass;
  102. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  103. p = (GC_PTR) c->constants.tags;
  104. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  105. p = (GC_PTR) c->constants.data;
  106. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  107. // If the class is an array, then the methods field holds a
  108. // pointer to the element class. If the class is primitive,
  109. // then the methods field holds a pointer to the array class.
  110. p = (GC_PTR) c->methods;
  111. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  112. p = (GC_PTR) c->fields;
  113. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  114. // The vtable might be allocated even for compiled code.
  115. p = (GC_PTR) c->vtable;
  116. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  117. p = (GC_PTR) c->interfaces;
  118. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  119. p = (GC_PTR) c->loader;
  120. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  121. // The dispatch tables can be allocated at runtime.
  122. p = (GC_PTR) c->ancestors;
  123. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  124. p = (GC_PTR) c->idt;
  125. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  126. p = (GC_PTR) c->arrayclass;
  127. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  128. p = (GC_PTR) c->protectionDomain;
  129. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  130. p = (GC_PTR) c->hack_signers;
  131. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  132. p = (GC_PTR) c->aux_info;
  133. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  134. p = (GC_PTR) c->reflection_data;
  135. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  136. // The class chain must be marked for runtime-allocated Classes
  137. // loaded by the bootstrap ClassLoader.
  138. p = (GC_PTR) c->next_or_version;
  139. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
  140. }
  141. else
  142. {
  143. // NOTE: each class only holds information about the class
  144. // itself. So we must do the marking for the entire inheritance
  145. // tree in order to mark all fields. FIXME: what about
  146. // interfaces? We skip Object here, because Object only has a
  147. // sync_info, and we handled that earlier.
  148. // Note: occasionally `klass' can be null. For instance, this
  149. // can happen if a GC occurs between the point where an object
  150. // is allocated and where the vtbl slot is set.
  151. while (klass && klass != &java::lang::Object::class$)
  152. {
  153. jfieldID field = JvGetFirstInstanceField (klass);
  154. jint max = JvNumInstanceFields (klass);
  155. for (int i = 0; i < max; ++i)
  156. {
  157. if (JvFieldIsRef (field))
  158. {
  159. jobject val = JvGetObjectField (obj, field);
  160. p = (GC_PTR) val;
  161. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj);
  162. }
  163. field = field->getNextField ();
  164. }
  165. klass = klass->getSuperclass();
  166. }
  167. }
  168. return mark_stack_ptr;
  169. }
  170. // This is called by the GC during the mark phase. It marks a Java
  171. // array (of objects). We use `void *' arguments and return, and not
  172. // what the Boehm GC wants, to avoid pollution in our headers.
  173. void *
  174. _Jv_MarkArray (void *addr, void *msp, void *msl, void *env)
  175. {
  176. struct GC_ms_entry *mark_stack_ptr = (struct GC_ms_entry *)msp;
  177. struct GC_ms_entry *mark_stack_limit = (struct GC_ms_entry *)msl;
  178. if (env == (void *)1) /* Object allocated with debug allocator. */
  179. addr = (void *)GC_USR_PTR_FROM_BASE(addr);
  180. jobjectArray array = (jobjectArray) addr;
  181. _Jv_VTable *dt = *(_Jv_VTable **) addr;
  182. // Assumes size >= 3 words. That's currently true since arrays have
  183. // a vtable, sync pointer, and size. If the sync pointer goes away,
  184. // we may need to round up the size.
  185. if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
  186. return mark_stack_ptr;
  187. GC_PTR p;
  188. p = (GC_PTR) dt;
  189. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array);
  190. # ifndef JV_HASH_SYNCHRONIZATION
  191. // Every object has a sync_info pointer.
  192. p = (GC_PTR) array->sync_info;
  193. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array);
  194. # endif
  195. for (int i = 0; i < JvGetArrayLength (array); ++i)
  196. {
  197. jobject obj = elements (array)[i];
  198. p = (GC_PTR) obj;
  199. MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array);
  200. }
  201. return mark_stack_ptr;
  202. }
  203. // Generate a GC marking descriptor for a class.
  204. //
  205. // We assume that the gcj mark proc has index 0. This is a dubious assumption,
  206. // since another one could be registered first. But the compiler also
  207. // knows this, so in that case everything else will break, too.
  208. #define GCJ_DEFAULT_DESCR GC_MAKE_PROC(GC_GCJ_RESERVED_MARK_PROC_INDEX,0)
  209. void *
  210. _Jv_BuildGCDescr(jclass self)
  211. {
  212. jlong desc = 0;
  213. jint bits_per_word = CHAR_BIT * sizeof (void *);
  214. // Note: for now we only consider a bitmap mark descriptor. We
  215. // could also handle the case where the first N fields of a type are
  216. // references. However, this is not very likely to be used by many
  217. // classes, and it is easier to compute things this way.
  218. // The vtable pointer.
  219. desc |= 1ULL << (bits_per_word - 1);
  220. #ifndef JV_HASH_SYNCHRONIZATION
  221. // The sync_info field.
  222. desc |= 1ULL << (bits_per_word - 2);
  223. #endif
  224. for (jclass klass = self; klass != NULL; klass = klass->getSuperclass())
  225. {
  226. jfieldID field = JvGetFirstInstanceField(klass);
  227. int count = JvNumInstanceFields(klass);
  228. for (int i = 0; i < count; ++i)
  229. {
  230. if (field->isRef())
  231. {
  232. unsigned int off = field->getOffset();
  233. // If we run into a weird situation, we bail.
  234. if (off % sizeof (void *) != 0)
  235. return (void *) (GCJ_DEFAULT_DESCR);
  236. off /= sizeof (void *);
  237. // If we find a field outside the range of our bitmap,
  238. // fall back to procedure marker. The bottom 2 bits are
  239. // reserved.
  240. if (off >= (unsigned) bits_per_word - 2)
  241. return (void *) (GCJ_DEFAULT_DESCR);
  242. desc |= 1ULL << (bits_per_word - off - 1);
  243. }
  244. field = field->getNextField();
  245. }
  246. }
  247. // For bitmap mark type, bottom bits are 01.
  248. desc |= 1;
  249. // Bogus warning avoidance (on many platforms).
  250. return (void *) (unsigned long) desc;
  251. }
  252. // Allocate some space that is known to be pointer-free.
  253. void *
  254. _Jv_AllocBytes (jsize size)
  255. {
  256. void *r = GC_MALLOC_ATOMIC (size);
  257. // We have to explicitly zero memory here, as the GC doesn't
  258. // guarantee that PTRFREE allocations are zeroed. Note that we
  259. // don't have to do this for other allocation types because we set
  260. // the `ok_init' flag in the type descriptor.
  261. memset (r, 0, size);
  262. return r;
  263. }
  264. #ifdef LIBGCJ_GC_DEBUG
  265. void *
  266. _Jv_AllocObj (jsize size, jclass klass)
  267. {
  268. return GC_GCJ_MALLOC (size, klass->vtable);
  269. }
  270. void *
  271. _Jv_AllocPtrFreeObj (jsize size, jclass klass)
  272. {
  273. #ifdef JV_HASH_SYNCHRONIZATION
  274. void * obj = GC_MALLOC_ATOMIC(size);
  275. *((_Jv_VTable **) obj) = klass->vtable;
  276. #else
  277. void * obj = GC_GCJ_MALLOC(size, klass->vtable);
  278. #endif
  279. return obj;
  280. }
  281. #endif /* LIBGCJ_GC_DEBUG */
  282. // In the non-debug case, the above two functions are defined
  283. // as inline functions in boehm-gc.h. In the debug case we
  284. // really want to take advantage of the definitions in gc_gcj.h.
  285. // Allocate space for a new Java array.
  286. // Used only for arrays of objects.
  287. void *
  288. _Jv_AllocArray (jsize size, jclass klass)
  289. {
  290. void *obj;
  291. #ifdef LIBGCJ_GC_DEBUG
  292. // There isn't much to lose by scanning this conservatively.
  293. // If we didn't, the mark proc would have to understand that
  294. // it needed to skip the header.
  295. obj = GC_MALLOC(size);
  296. #else
  297. const jsize min_heap_addr = 16*1024;
  298. // A heuristic. If size is less than this value, the size
  299. // stored in the array can't possibly be misinterpreted as
  300. // a pointer. Thus we lose nothing by scanning the object
  301. // completely conservatively, since no misidentification can
  302. // take place.
  303. if (size < min_heap_addr)
  304. obj = GC_MALLOC(size);
  305. else
  306. obj = GC_generic_malloc (size, array_kind_x);
  307. #endif
  308. *((_Jv_VTable **) obj) = klass->vtable;
  309. return obj;
  310. }
  311. /* Allocate space for a new non-Java object, which does not have the usual
  312. Java object header but may contain pointers to other GC'ed objects. */
  313. void *
  314. _Jv_AllocRawObj (jsize size)
  315. {
  316. return (void *) GC_MALLOC (size ? size : 1);
  317. }
  318. #ifdef INTERPRETER
  319. typedef _Jv_ClosureList *closure_list_pointer;
  320. /* Release closures in a _Jv_ClosureList. */
  321. static void
  322. finalize_closure_list (GC_PTR obj, GC_PTR)
  323. {
  324. _Jv_ClosureList **clpp = (_Jv_ClosureList **)obj;
  325. _Jv_ClosureList::releaseClosures (clpp);
  326. }
  327. /* Allocate a double-indirect pointer to a _Jv_ClosureList that will
  328. get garbage-collected after this double-indirect pointer becomes
  329. unreachable by any other objects, including finalizable ones. */
  330. _Jv_ClosureList **
  331. _Jv_ClosureListFinalizer ()
  332. {
  333. _Jv_ClosureList **clpp;
  334. clpp = (_Jv_ClosureList **)_Jv_AllocBytes (sizeof (*clpp));
  335. GC_REGISTER_FINALIZER_UNREACHABLE (clpp, finalize_closure_list,
  336. NULL, NULL, NULL);
  337. return clpp;
  338. }
  339. #endif // INTERPRETER
  340. static void
  341. call_finalizer (GC_PTR obj, GC_PTR client_data)
  342. {
  343. _Jv_FinalizerFunc *fn = (_Jv_FinalizerFunc *) client_data;
  344. jobject jobj = (jobject) obj;
  345. (*fn) (jobj);
  346. }
  347. void
  348. _Jv_RegisterFinalizer (void *object, _Jv_FinalizerFunc *meth)
  349. {
  350. GC_REGISTER_FINALIZER_NO_ORDER (object, call_finalizer, (GC_PTR) meth,
  351. NULL, NULL);
  352. }
  353. void
  354. _Jv_RunFinalizers (void)
  355. {
  356. GC_invoke_finalizers ();
  357. }
  358. void
  359. _Jv_RunAllFinalizers (void)
  360. {
  361. GC_finalize_all ();
  362. }
  363. void
  364. _Jv_RunGC (void)
  365. {
  366. GC_gcollect ();
  367. }
  368. long
  369. _Jv_GCTotalMemory (void)
  370. {
  371. return GC_get_heap_size ();
  372. }
  373. long
  374. _Jv_GCFreeMemory (void)
  375. {
  376. return GC_get_free_bytes ();
  377. }
  378. void
  379. _Jv_GCSetInitialHeapSize (size_t size)
  380. {
  381. size_t current = GC_get_heap_size ();
  382. if (size > current)
  383. GC_expand_hp (size - current);
  384. }
  385. void
  386. _Jv_GCSetMaximumHeapSize (size_t size)
  387. {
  388. GC_set_max_heap_size ((GC_word) size);
  389. }
  390. int
  391. _Jv_SetGCFreeSpaceDivisor (int div)
  392. {
  393. return (int)GC_set_free_space_divisor ((GC_word)div);
  394. }
  395. void
  396. _Jv_DisableGC (void)
  397. {
  398. GC_disable();
  399. }
  400. void
  401. _Jv_EnableGC (void)
  402. {
  403. GC_enable();
  404. }
  405. static void * handle_out_of_memory(size_t)
  406. {
  407. _Jv_ThrowNoMemory();
  408. }
  409. static void
  410. gcj_describe_type_fn(void *obj, char *out_buf)
  411. {
  412. _Jv_VTable *dt = *(_Jv_VTable **) obj;
  413. if (! dt /* Shouldn't happen */)
  414. {
  415. strcpy(out_buf, "GCJ (bad)");
  416. return;
  417. }
  418. jclass klass = dt->clas;
  419. if (!klass /* shouldn't happen */)
  420. {
  421. strcpy(out_buf, "GCJ (bad)");
  422. return;
  423. }
  424. jstring name = klass -> getName();
  425. size_t len = name -> length();
  426. if (len >= GC_TYPE_DESCR_LEN) len = GC_TYPE_DESCR_LEN - 1;
  427. JvGetStringUTFRegion (name, 0, len, out_buf);
  428. out_buf[len] = '\0';
  429. }
  430. void
  431. _Jv_InitGC (void)
  432. {
  433. int proc;
  434. static bool gc_initialized;
  435. if (gc_initialized)
  436. return;
  437. gc_initialized = 1;
  438. // Ignore pointers that do not point to the start of an object.
  439. GC_all_interior_pointers = 0;
  440. #if defined (HAVE_DLFCN_H) && defined (HAVE_DLADDR)
  441. // Tell the collector to ask us before scanning DSOs.
  442. GC_register_has_static_roots_callback (_Jv_GC_has_static_roots);
  443. #endif
  444. // Configure the collector to use the bitmap marking descriptors that we
  445. // stash in the class vtable.
  446. // We always use mark proc descriptor 0, since the compiler knows
  447. // about it.
  448. GC_init_gcj_malloc (0, (void *) _Jv_MarkObj);
  449. // Cause an out of memory error to be thrown from the allocators,
  450. // instead of returning 0. This is cheaper than checking on allocation.
  451. GC_oom_fn = handle_out_of_memory;
  452. GC_java_finalization = 1;
  453. // We use a different mark procedure for object arrays. This code
  454. // configures a different object `kind' for object array allocation and
  455. // marking.
  456. array_free_list = GC_new_free_list();
  457. proc = GC_new_proc((GC_mark_proc)_Jv_MarkArray);
  458. array_kind_x = GC_new_kind(array_free_list, GC_MAKE_PROC (proc, 0), 0, 1);
  459. // Arrange to have the GC print Java class names in backtraces, etc.
  460. GC_register_describe_type_fn(GC_gcj_kind, gcj_describe_type_fn);
  461. GC_register_describe_type_fn(GC_gcj_debug_kind, gcj_describe_type_fn);
  462. }
  463. #ifdef JV_HASH_SYNCHRONIZATION
  464. // Allocate an object with a fake vtable pointer, which causes only
  465. // the first field (beyond the fake vtable pointer) to be traced.
  466. // Eventually this should probably be generalized.
  467. static _Jv_VTable trace_one_vtable = {
  468. 0, // class pointer
  469. (void *)(2 * sizeof(void *)),
  470. // descriptor; scan 2 words incl. vtable ptr.
  471. // Least significant bits must be zero to
  472. // identify this as a length descriptor
  473. {0} // First method
  474. };
  475. void *
  476. _Jv_AllocTraceOne (jsize size /* includes vtable slot */)
  477. {
  478. return GC_GCJ_MALLOC (size, &trace_one_vtable);
  479. }
  480. // Ditto for two words.
  481. // the first field (beyond the fake vtable pointer) to be traced.
  482. // Eventually this should probably be generalized.
  483. static _Jv_VTable trace_two_vtable =
  484. {
  485. 0, // class pointer
  486. (void *)(3 * sizeof(void *)),
  487. // descriptor; scan 3 words incl. vtable ptr.
  488. {0} // First method
  489. };
  490. void *
  491. _Jv_AllocTraceTwo (jsize size /* includes vtable slot */)
  492. {
  493. return GC_GCJ_MALLOC (size, &trace_two_vtable);
  494. }
  495. #endif /* JV_HASH_SYNCHRONIZATION */
  496. void
  497. _Jv_GCInitializeFinalizers (void (*notifier) (void))
  498. {
  499. GC_finalize_on_demand = 1;
  500. GC_finalizer_notifier = notifier;
  501. }
  502. void
  503. _Jv_GCRegisterDisappearingLink (jobject *objp)
  504. {
  505. // This test helps to ensure that we meet a precondition of
  506. // GC_general_register_disappearing_link, viz. "Obj must be a
  507. // pointer to the first word of an object we allocated."
  508. if (GC_base(*objp))
  509. GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp);
  510. }
  511. jboolean
  512. _Jv_GCCanReclaimSoftReference (jobject)
  513. {
  514. // For now, always reclaim soft references. FIXME.
  515. return true;
  516. }
  517. #if defined (HAVE_DLFCN_H) && defined (HAVE_DLADDR)
  518. // We keep a store of the filenames of DSOs that need to be
  519. // conservatively scanned by the garbage collector. During collection
  520. // the gc calls _Jv_GC_has_static_roots() to see if the data segment
  521. // of a DSO should be scanned.
  522. typedef struct filename_node
  523. {
  524. char *name;
  525. struct filename_node *link;
  526. } filename_node;
  527. #define FILENAME_STORE_SIZE 17
  528. static filename_node *filename_store[FILENAME_STORE_SIZE];
  529. // Find a filename in filename_store.
  530. static filename_node **
  531. find_file (const char *filename)
  532. {
  533. int index = strlen (filename) % FILENAME_STORE_SIZE;
  534. filename_node **node = &filename_store[index];
  535. while (*node)
  536. {
  537. if (strcmp ((*node)->name, filename) == 0)
  538. return node;
  539. node = &(*node)->link;
  540. }
  541. return node;
  542. }
  543. // Print the store of filenames of DSOs that need collection.
  544. void
  545. _Jv_print_gc_store (void)
  546. {
  547. for (int i = 0; i < FILENAME_STORE_SIZE; i++)
  548. {
  549. filename_node *node = filename_store[i];
  550. while (node)
  551. {
  552. fprintf (stderr, "%s\n", node->name);
  553. node = node->link;
  554. }
  555. }
  556. }
  557. // Create a new node in the store of libraries to collect.
  558. static filename_node *
  559. new_node (const char *filename)
  560. {
  561. filename_node *node = (filename_node*)_Jv_Malloc (sizeof (filename_node));
  562. node->name = (char *)_Jv_Malloc (strlen (filename) + 1);
  563. node->link = NULL;
  564. strcpy (node->name, filename);
  565. return node;
  566. }
  567. // Nonzero if the gc should scan this lib.
  568. static int
  569. _Jv_GC_has_static_roots (const char *filename, void *, size_t)
  570. {
  571. if (filename == NULL || strlen (filename) == 0)
  572. // No filename; better safe than sorry.
  573. return 1;
  574. filename_node **node = find_file (filename);
  575. if (*node)
  576. return 1;
  577. return 0;
  578. }
  579. #endif
  580. // Register the DSO that contains p for collection.
  581. void
  582. _Jv_RegisterLibForGc (const void *p __attribute__ ((__unused__)))
  583. {
  584. #if defined (HAVE_DLFCN_H) && defined (HAVE_DLADDR)
  585. Dl_info info;
  586. if (dladdr (const_cast<void *>(p), &info) != 0)
  587. {
  588. filename_node **node = find_file (info.dli_fname);
  589. if (! *node)
  590. *node = new_node (info.dli_fname);
  591. }
  592. #endif
  593. }
  594. void
  595. _Jv_SuspendThread (_Jv_Thread_t *thread)
  596. {
  597. #if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
  598. && !defined(GC_WIN32_THREADS) && !defined(GC_DARWIN_THREADS)
  599. GC_suspend_thread (_Jv_GetPlatformThreadID (thread));
  600. #endif
  601. }
  602. void
  603. _Jv_ResumeThread (_Jv_Thread_t *thread)
  604. {
  605. #if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
  606. && !defined(GC_WIN32_THREADS) && !defined(GC_DARWIN_THREADS)
  607. GC_resume_thread (_Jv_GetPlatformThreadID (thread));
  608. #endif
  609. }
  610. int
  611. _Jv_IsThreadSuspended (_Jv_Thread_t *thread)
  612. {
  613. #if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
  614. && !defined(GC_WIN32_THREADS) && !defined(GC_DARWIN_THREADS)
  615. return GC_is_thread_suspended (_Jv_GetPlatformThreadID (thread));
  616. #else
  617. return 0;
  618. #endif
  619. }
  620. void
  621. _Jv_GCAttachThread ()
  622. {
  623. // The registration interface is only defined on posixy systems and
  624. // only actually works if pthread_getattr_np is defined.
  625. // FIXME: until gc7 it is simpler to disable this on solaris.
  626. #if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(GC_SOLARIS_THREADS) \
  627. && !defined(GC_WIN32_THREADS)
  628. GC_register_my_thread ();
  629. #endif
  630. }
  631. void
  632. _Jv_GCDetachThread ()
  633. {
  634. #if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(GC_SOLARIS_THREADS) \
  635. && !defined(GC_WIN32_THREADS)
  636. GC_unregister_my_thread ();
  637. #endif
  638. }