finalize.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*
  2. * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3. * Copyright (c) 1991-1996 by Xerox Corporation. All rights reserved.
  4. * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
  5. * Copyright (C) 2007 Free Software Foundation, Inc
  6. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  7. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  8. *
  9. * Permission is hereby granted to use or copy this program
  10. * for any purpose, provided the above notices are retained on all copies.
  11. * Permission to modify the code and to distribute modified code is granted,
  12. * provided the above notices are retained, and a notice that the code was
  13. * modified is included with the above copyright notice.
  14. */
  15. #include "private/gc_pmark.h"
  16. #ifndef GC_NO_FINALIZATION
  17. /* Type of mark procedure used for marking from finalizable object. */
  18. /* This procedure normally does not mark the object, only its */
  19. /* descendants. */
  20. typedef void (* finalization_mark_proc)(ptr_t /* finalizable_obj_ptr */);
  21. #define HASH3(addr,size,log_size) \
  22. ((((word)(addr) >> 3) ^ ((word)(addr) >> (3 + (log_size)))) \
  23. & ((size) - 1))
  24. #define HASH2(addr,log_size) HASH3(addr, 1 << (log_size), log_size)
  25. struct hash_chain_entry {
  26. word hidden_key;
  27. struct hash_chain_entry * next;
  28. };
  29. struct disappearing_link {
  30. struct hash_chain_entry prolog;
  31. # define dl_hidden_link prolog.hidden_key
  32. /* Field to be cleared. */
  33. # define dl_next(x) (struct disappearing_link *)((x) -> prolog.next)
  34. # define dl_set_next(x, y) \
  35. (void)((x)->prolog.next = (struct hash_chain_entry *)(y))
  36. word dl_hidden_obj; /* Pointer to object base */
  37. };
  38. struct dl_hashtbl_s {
  39. struct disappearing_link **head;
  40. signed_word log_size;
  41. word entries;
  42. };
  43. STATIC struct dl_hashtbl_s GC_dl_hashtbl = {
  44. /* head */ NULL, /* log_size */ -1, /* entries */ 0 };
  45. #ifndef GC_LONG_REFS_NOT_NEEDED
  46. STATIC struct dl_hashtbl_s GC_ll_hashtbl = { NULL, -1, 0 };
  47. #endif
  48. STATIC struct finalizable_object {
  49. struct hash_chain_entry prolog;
  50. # define fo_hidden_base prolog.hidden_key
  51. /* Pointer to object base. */
  52. /* No longer hidden once object */
  53. /* is on finalize_now queue. */
  54. # define fo_next(x) (struct finalizable_object *)((x) -> prolog.next)
  55. # define fo_set_next(x,y) ((x)->prolog.next = (struct hash_chain_entry *)(y))
  56. GC_finalization_proc fo_fn; /* Finalizer. */
  57. ptr_t fo_client_data;
  58. word fo_object_size; /* In bytes. */
  59. finalization_mark_proc fo_mark_proc; /* Mark-through procedure */
  60. } **GC_fo_head = 0;
  61. STATIC struct finalizable_object * GC_finalize_now = 0;
  62. /* List of objects that should be finalized now. */
  63. static signed_word log_fo_table_size = -1;
  64. GC_INNER void GC_push_finalizer_structures(void)
  65. {
  66. GC_ASSERT((word)&GC_dl_hashtbl.head % sizeof(word) == 0);
  67. GC_ASSERT((word)&GC_fo_head % sizeof(word) == 0);
  68. GC_ASSERT((word)&GC_finalize_now % sizeof(word) == 0);
  69. # ifndef GC_LONG_REFS_NOT_NEEDED
  70. GC_ASSERT((word)&GC_ll_hashtbl.head % sizeof(word) == 0);
  71. GC_push_all((ptr_t)(&GC_ll_hashtbl.head),
  72. (ptr_t)(&GC_ll_hashtbl.head) + sizeof(word));
  73. # endif
  74. GC_push_all((ptr_t)(&GC_dl_hashtbl.head),
  75. (ptr_t)(&GC_dl_hashtbl.head) + sizeof(word));
  76. GC_push_all((ptr_t)(&GC_fo_head), (ptr_t)(&GC_fo_head) + sizeof(word));
  77. GC_push_all((ptr_t)(&GC_finalize_now),
  78. (ptr_t)(&GC_finalize_now) + sizeof(word));
  79. }
  80. /* Double the size of a hash table. *size_ptr is the log of its current */
  81. /* size. May be a no-op. */
  82. /* *table is a pointer to an array of hash headers. If we succeed, we */
  83. /* update both *table and *log_size_ptr. Lock is held. */
  84. STATIC void GC_grow_table(struct hash_chain_entry ***table,
  85. signed_word *log_size_ptr)
  86. {
  87. register word i;
  88. register struct hash_chain_entry *p;
  89. signed_word log_old_size = *log_size_ptr;
  90. signed_word log_new_size = log_old_size + 1;
  91. word old_size = ((log_old_size == -1)? 0: (1 << log_old_size));
  92. word new_size = (word)1 << log_new_size;
  93. /* FIXME: Power of 2 size often gets rounded up to one more page. */
  94. struct hash_chain_entry **new_table = (struct hash_chain_entry **)
  95. GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
  96. (size_t)new_size * sizeof(struct hash_chain_entry *), NORMAL);
  97. if (new_table == 0) {
  98. if (*table == 0) {
  99. ABORT("Insufficient space for initial table allocation");
  100. } else {
  101. return;
  102. }
  103. }
  104. for (i = 0; i < old_size; i++) {
  105. p = (*table)[i];
  106. while (p != 0) {
  107. ptr_t real_key = GC_REVEAL_POINTER(p -> hidden_key);
  108. struct hash_chain_entry *next = p -> next;
  109. size_t new_hash = HASH3(real_key, new_size, log_new_size);
  110. p -> next = new_table[new_hash];
  111. new_table[new_hash] = p;
  112. p = next;
  113. }
  114. }
  115. *log_size_ptr = log_new_size;
  116. *table = new_table;
  117. }
  118. GC_API int GC_CALL GC_register_disappearing_link(void * * link)
  119. {
  120. ptr_t base;
  121. base = (ptr_t)GC_base(link);
  122. if (base == 0)
  123. ABORT("Bad arg to GC_register_disappearing_link");
  124. return(GC_general_register_disappearing_link(link, base));
  125. }
  126. STATIC int GC_register_disappearing_link_inner(
  127. struct dl_hashtbl_s *dl_hashtbl, void **link,
  128. const void *obj)
  129. {
  130. struct disappearing_link *curr_dl;
  131. size_t index;
  132. struct disappearing_link * new_dl;
  133. DCL_LOCK_STATE;
  134. LOCK();
  135. GC_ASSERT(obj != NULL && GC_base_C(obj) == obj);
  136. if (dl_hashtbl -> log_size == -1
  137. || dl_hashtbl -> entries > ((word)1 << dl_hashtbl -> log_size)) {
  138. GC_grow_table((struct hash_chain_entry ***)&dl_hashtbl -> head,
  139. &dl_hashtbl -> log_size);
  140. GC_COND_LOG_PRINTF("Grew dl table to %u entries\n",
  141. 1 << (unsigned)dl_hashtbl -> log_size);
  142. }
  143. index = HASH2(link, dl_hashtbl -> log_size);
  144. for (curr_dl = dl_hashtbl -> head[index]; curr_dl != 0;
  145. curr_dl = dl_next(curr_dl)) {
  146. if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
  147. curr_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
  148. UNLOCK();
  149. return GC_DUPLICATE;
  150. }
  151. }
  152. new_dl = (struct disappearing_link *)
  153. GC_INTERNAL_MALLOC(sizeof(struct disappearing_link),NORMAL);
  154. if (0 == new_dl) {
  155. GC_oom_func oom_fn = GC_oom_fn;
  156. UNLOCK();
  157. new_dl = (struct disappearing_link *)
  158. (*oom_fn)(sizeof(struct disappearing_link));
  159. if (0 == new_dl) {
  160. return GC_NO_MEMORY;
  161. }
  162. /* It's not likely we'll make it here, but ... */
  163. LOCK();
  164. /* Recalculate index since the table may grow. */
  165. index = HASH2(link, dl_hashtbl -> log_size);
  166. /* Check again that our disappearing link not in the table. */
  167. for (curr_dl = dl_hashtbl -> head[index]; curr_dl != 0;
  168. curr_dl = dl_next(curr_dl)) {
  169. if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
  170. curr_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
  171. UNLOCK();
  172. # ifndef DBG_HDRS_ALL
  173. /* Free unused new_dl returned by GC_oom_fn() */
  174. GC_free((void *)new_dl);
  175. # endif
  176. return GC_DUPLICATE;
  177. }
  178. }
  179. }
  180. new_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
  181. new_dl -> dl_hidden_link = GC_HIDE_POINTER(link);
  182. dl_set_next(new_dl, dl_hashtbl -> head[index]);
  183. dl_hashtbl -> head[index] = new_dl;
  184. dl_hashtbl -> entries++;
  185. UNLOCK();
  186. return GC_SUCCESS;
  187. }
  188. GC_API int GC_CALL GC_general_register_disappearing_link(void * * link,
  189. const void * obj)
  190. {
  191. if (((word)link & (ALIGNMENT-1)) != 0 || NULL == link)
  192. ABORT("Bad arg to GC_general_register_disappearing_link");
  193. return GC_register_disappearing_link_inner(&GC_dl_hashtbl, link, obj);
  194. }
  195. #ifdef DBG_HDRS_ALL
  196. # define FREE_DL_ENTRY(curr_dl) dl_set_next(curr_dl, NULL)
  197. #else
  198. # define FREE_DL_ENTRY(curr_dl) GC_free(curr_dl)
  199. #endif
  200. /* Unregisters given link and returns the link entry to free. */
  201. /* Assume the lock is held. */
  202. GC_INLINE struct disappearing_link *GC_unregister_disappearing_link_inner(
  203. struct dl_hashtbl_s *dl_hashtbl, void **link)
  204. {
  205. struct disappearing_link *curr_dl;
  206. struct disappearing_link *prev_dl = NULL;
  207. size_t index = HASH2(link, dl_hashtbl->log_size);
  208. for (curr_dl = dl_hashtbl -> head[index]; curr_dl;
  209. curr_dl = dl_next(curr_dl)) {
  210. if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
  211. /* Remove found entry from the table. */
  212. if (NULL == prev_dl) {
  213. dl_hashtbl -> head[index] = dl_next(curr_dl);
  214. } else {
  215. dl_set_next(prev_dl, dl_next(curr_dl));
  216. }
  217. dl_hashtbl -> entries--;
  218. break;
  219. }
  220. prev_dl = curr_dl;
  221. }
  222. return curr_dl;
  223. }
  224. GC_API int GC_CALL GC_unregister_disappearing_link(void * * link)
  225. {
  226. struct disappearing_link *curr_dl;
  227. DCL_LOCK_STATE;
  228. if (((word)link & (ALIGNMENT-1)) != 0) return(0); /* Nothing to do. */
  229. LOCK();
  230. curr_dl = GC_unregister_disappearing_link_inner(&GC_dl_hashtbl, link);
  231. UNLOCK();
  232. if (NULL == curr_dl) return 0;
  233. FREE_DL_ENTRY(curr_dl);
  234. return 1;
  235. }
  236. #ifndef GC_LONG_REFS_NOT_NEEDED
  237. GC_API int GC_CALL GC_register_long_link(void * * link, const void * obj)
  238. {
  239. if (((word)link & (ALIGNMENT-1)) != 0 || NULL == link)
  240. ABORT("Bad arg to GC_register_long_link");
  241. return GC_register_disappearing_link_inner(&GC_ll_hashtbl, link, obj);
  242. }
  243. GC_API int GC_CALL GC_unregister_long_link(void * * link)
  244. {
  245. struct disappearing_link *curr_dl;
  246. DCL_LOCK_STATE;
  247. if (((word)link & (ALIGNMENT-1)) != 0) return(0); /* Nothing to do. */
  248. LOCK();
  249. curr_dl = GC_unregister_disappearing_link_inner(&GC_ll_hashtbl, link);
  250. UNLOCK();
  251. if (NULL == curr_dl) return 0;
  252. FREE_DL_ENTRY(curr_dl);
  253. return 1;
  254. }
  255. #endif /* !GC_LONG_REFS_NOT_NEEDED */
  256. #ifndef GC_MOVE_DISAPPEARING_LINK_NOT_NEEDED
  257. /* Moves a link. Assume the lock is held. */
  258. STATIC int GC_move_disappearing_link_inner(
  259. struct dl_hashtbl_s *dl_hashtbl,
  260. void **link, void **new_link)
  261. {
  262. struct disappearing_link *curr_dl, *prev_dl, *new_dl;
  263. size_t curr_index, new_index;
  264. word curr_hidden_link;
  265. word new_hidden_link;
  266. /* Find current link. */
  267. curr_index = HASH2(link, dl_hashtbl -> log_size);
  268. curr_hidden_link = GC_HIDE_POINTER(link);
  269. prev_dl = NULL;
  270. for (curr_dl = dl_hashtbl -> head[curr_index]; curr_dl;
  271. curr_dl = dl_next(curr_dl)) {
  272. if (curr_dl -> dl_hidden_link == curr_hidden_link)
  273. break;
  274. prev_dl = curr_dl;
  275. }
  276. if (NULL == curr_dl) {
  277. return GC_NOT_FOUND;
  278. }
  279. if (link == new_link) {
  280. return GC_SUCCESS; /* Nothing to do. */
  281. }
  282. /* link found; now check new_link not present. */
  283. new_index = HASH2(new_link, dl_hashtbl -> log_size);
  284. new_hidden_link = GC_HIDE_POINTER(new_link);
  285. for (new_dl = dl_hashtbl -> head[new_index]; new_dl;
  286. new_dl = dl_next(new_dl)) {
  287. if (new_dl -> dl_hidden_link == new_hidden_link) {
  288. /* Target already registered; bail. */
  289. return GC_DUPLICATE;
  290. }
  291. }
  292. /* Remove from old, add to new, update link. */
  293. if (NULL == prev_dl) {
  294. dl_hashtbl -> head[curr_index] = dl_next(curr_dl);
  295. } else {
  296. dl_set_next(prev_dl, dl_next(curr_dl));
  297. }
  298. curr_dl -> dl_hidden_link = new_hidden_link;
  299. dl_set_next(curr_dl, dl_hashtbl -> head[new_index]);
  300. dl_hashtbl -> head[new_index] = curr_dl;
  301. return GC_SUCCESS;
  302. }
  303. GC_API int GC_CALL GC_move_disappearing_link(void **link, void **new_link)
  304. {
  305. int result;
  306. DCL_LOCK_STATE;
  307. if (((word)new_link & (ALIGNMENT-1)) != 0 || new_link == NULL)
  308. ABORT("Bad new_link arg to GC_move_disappearing_link");
  309. if (((word)link & (ALIGNMENT-1)) != 0)
  310. return GC_NOT_FOUND; /* Nothing to do. */
  311. LOCK();
  312. result = GC_move_disappearing_link_inner(&GC_dl_hashtbl, link, new_link);
  313. UNLOCK();
  314. return result;
  315. }
  316. # ifndef GC_LONG_REFS_NOT_NEEDED
  317. GC_API int GC_CALL GC_move_long_link(void **link, void **new_link)
  318. {
  319. int result;
  320. DCL_LOCK_STATE;
  321. if (((word)new_link & (ALIGNMENT-1)) != 0 || new_link == NULL)
  322. ABORT("Bad new_link arg to GC_move_disappearing_link");
  323. if (((word)link & (ALIGNMENT-1)) != 0)
  324. return GC_NOT_FOUND; /* Nothing to do. */
  325. LOCK();
  326. result = GC_move_disappearing_link_inner(&GC_ll_hashtbl, link, new_link);
  327. UNLOCK();
  328. return result;
  329. }
  330. # endif /* !GC_LONG_REFS_NOT_NEEDED */
  331. #endif /* !GC_MOVE_DISAPPEARING_LINK_NOT_NEEDED */
  332. /* Possible finalization_marker procedures. Note that mark stack */
  333. /* overflow is handled by the caller, and is not a disaster. */
  334. STATIC void GC_normal_finalize_mark_proc(ptr_t p)
  335. {
  336. hdr * hhdr = HDR(p);
  337. PUSH_OBJ(p, hhdr, GC_mark_stack_top,
  338. &(GC_mark_stack[GC_mark_stack_size]));
  339. }
  340. /* This only pays very partial attention to the mark descriptor. */
  341. /* It does the right thing for normal and atomic objects, and treats */
  342. /* most others as normal. */
  343. STATIC void GC_ignore_self_finalize_mark_proc(ptr_t p)
  344. {
  345. hdr * hhdr = HDR(p);
  346. word descr = hhdr -> hb_descr;
  347. ptr_t q;
  348. word r;
  349. ptr_t scan_limit;
  350. ptr_t target_limit = p + hhdr -> hb_sz - 1;
  351. if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
  352. scan_limit = p + descr - sizeof(word);
  353. } else {
  354. scan_limit = target_limit + 1 - sizeof(word);
  355. }
  356. for (q = p; (word)q <= (word)scan_limit; q += ALIGNMENT) {
  357. r = *(word *)q;
  358. if (r < (word)p || r > (word)target_limit) {
  359. GC_PUSH_ONE_HEAP(r, q, GC_mark_stack_top);
  360. }
  361. }
  362. }
  363. STATIC void GC_null_finalize_mark_proc(ptr_t p GC_ATTR_UNUSED) {}
  364. /* Possible finalization_marker procedures. Note that mark stack */
  365. /* overflow is handled by the caller, and is not a disaster. */
  366. /* GC_unreachable_finalize_mark_proc is an alias for normal marking, */
  367. /* but it is explicitly tested for, and triggers different */
  368. /* behavior. Objects registered in this way are not finalized */
  369. /* if they are reachable by other finalizable objects, even if those */
  370. /* other objects specify no ordering. */
  371. STATIC void GC_unreachable_finalize_mark_proc(ptr_t p)
  372. {
  373. GC_normal_finalize_mark_proc(p);
  374. }
  375. /* Register a finalization function. See gc.h for details. */
  376. /* The last parameter is a procedure that determines */
  377. /* marking for finalization ordering. Any objects marked */
  378. /* by that procedure will be guaranteed to not have been */
  379. /* finalized when this finalizer is invoked. */
  380. STATIC void GC_register_finalizer_inner(void * obj,
  381. GC_finalization_proc fn, void *cd,
  382. GC_finalization_proc *ofn, void **ocd,
  383. finalization_mark_proc mp)
  384. {
  385. ptr_t base;
  386. struct finalizable_object * curr_fo, * prev_fo;
  387. size_t index;
  388. struct finalizable_object *new_fo = 0;
  389. hdr *hhdr = NULL; /* initialized to prevent warning. */
  390. GC_oom_func oom_fn;
  391. DCL_LOCK_STATE;
  392. LOCK();
  393. if (log_fo_table_size == -1
  394. || GC_fo_entries > ((word)1 << log_fo_table_size)) {
  395. GC_grow_table((struct hash_chain_entry ***)&GC_fo_head,
  396. &log_fo_table_size);
  397. GC_COND_LOG_PRINTF("Grew fo table to %u entries\n",
  398. 1 << (unsigned)log_fo_table_size);
  399. }
  400. /* in the THREADS case we hold allocation lock. */
  401. base = (ptr_t)obj;
  402. for (;;) {
  403. index = HASH2(base, log_fo_table_size);
  404. prev_fo = 0;
  405. curr_fo = GC_fo_head[index];
  406. while (curr_fo != 0) {
  407. GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
  408. if (curr_fo -> fo_hidden_base == GC_HIDE_POINTER(base)) {
  409. /* Interruption by a signal in the middle of this */
  410. /* should be safe. The client may see only *ocd */
  411. /* updated, but we'll declare that to be his problem. */
  412. if (ocd) *ocd = (void *) (curr_fo -> fo_client_data);
  413. if (ofn) *ofn = curr_fo -> fo_fn;
  414. /* Delete the structure for base. */
  415. if (prev_fo == 0) {
  416. GC_fo_head[index] = fo_next(curr_fo);
  417. } else {
  418. fo_set_next(prev_fo, fo_next(curr_fo));
  419. }
  420. if (fn == 0) {
  421. GC_fo_entries--;
  422. /* May not happen if we get a signal. But a high */
  423. /* estimate will only make the table larger than */
  424. /* necessary. */
  425. # if !defined(THREADS) && !defined(DBG_HDRS_ALL)
  426. GC_free((void *)curr_fo);
  427. # endif
  428. } else {
  429. curr_fo -> fo_fn = fn;
  430. curr_fo -> fo_client_data = (ptr_t)cd;
  431. curr_fo -> fo_mark_proc = mp;
  432. /* Reinsert it. We deleted it first to maintain */
  433. /* consistency in the event of a signal. */
  434. if (prev_fo == 0) {
  435. GC_fo_head[index] = curr_fo;
  436. } else {
  437. fo_set_next(prev_fo, curr_fo);
  438. }
  439. }
  440. UNLOCK();
  441. # ifndef DBG_HDRS_ALL
  442. if (EXPECT(new_fo != 0, FALSE)) {
  443. /* Free unused new_fo returned by GC_oom_fn() */
  444. GC_free((void *)new_fo);
  445. }
  446. # endif
  447. return;
  448. }
  449. prev_fo = curr_fo;
  450. curr_fo = fo_next(curr_fo);
  451. }
  452. if (EXPECT(new_fo != 0, FALSE)) {
  453. /* new_fo is returned by GC_oom_fn(), so fn != 0 and hhdr != 0. */
  454. break;
  455. }
  456. if (fn == 0) {
  457. if (ocd) *ocd = 0;
  458. if (ofn) *ofn = 0;
  459. UNLOCK();
  460. return;
  461. }
  462. GET_HDR(base, hhdr);
  463. if (EXPECT(0 == hhdr, FALSE)) {
  464. /* We won't collect it, hence finalizer wouldn't be run. */
  465. if (ocd) *ocd = 0;
  466. if (ofn) *ofn = 0;
  467. UNLOCK();
  468. return;
  469. }
  470. new_fo = (struct finalizable_object *)
  471. GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
  472. if (EXPECT(new_fo != 0, TRUE))
  473. break;
  474. oom_fn = GC_oom_fn;
  475. UNLOCK();
  476. new_fo = (struct finalizable_object *)
  477. (*oom_fn)(sizeof(struct finalizable_object));
  478. if (0 == new_fo) {
  479. /* No enough memory. *ocd and *ofn remains unchanged. */
  480. return;
  481. }
  482. /* It's not likely we'll make it here, but ... */
  483. LOCK();
  484. /* Recalculate index since the table may grow and */
  485. /* check again that our finalizer is not in the table. */
  486. }
  487. GC_ASSERT(GC_size(new_fo) >= sizeof(struct finalizable_object));
  488. if (ocd) *ocd = 0;
  489. if (ofn) *ofn = 0;
  490. new_fo -> fo_hidden_base = GC_HIDE_POINTER(base);
  491. new_fo -> fo_fn = fn;
  492. new_fo -> fo_client_data = (ptr_t)cd;
  493. new_fo -> fo_object_size = hhdr -> hb_sz;
  494. new_fo -> fo_mark_proc = mp;
  495. fo_set_next(new_fo, GC_fo_head[index]);
  496. GC_fo_entries++;
  497. GC_fo_head[index] = new_fo;
  498. UNLOCK();
  499. }
  500. GC_API void GC_CALL GC_register_finalizer(void * obj,
  501. GC_finalization_proc fn, void * cd,
  502. GC_finalization_proc *ofn, void ** ocd)
  503. {
  504. GC_register_finalizer_inner(obj, fn, cd, ofn,
  505. ocd, GC_normal_finalize_mark_proc);
  506. }
  507. GC_API void GC_CALL GC_register_finalizer_ignore_self(void * obj,
  508. GC_finalization_proc fn, void * cd,
  509. GC_finalization_proc *ofn, void ** ocd)
  510. {
  511. GC_register_finalizer_inner(obj, fn, cd, ofn,
  512. ocd, GC_ignore_self_finalize_mark_proc);
  513. }
  514. GC_API void GC_CALL GC_register_finalizer_no_order(void * obj,
  515. GC_finalization_proc fn, void * cd,
  516. GC_finalization_proc *ofn, void ** ocd)
  517. {
  518. GC_register_finalizer_inner(obj, fn, cd, ofn,
  519. ocd, GC_null_finalize_mark_proc);
  520. }
  521. static GC_bool need_unreachable_finalization = FALSE;
  522. /* Avoid the work if this isn't used. */
  523. GC_API void GC_CALL GC_register_finalizer_unreachable(void * obj,
  524. GC_finalization_proc fn, void * cd,
  525. GC_finalization_proc *ofn, void ** ocd)
  526. {
  527. need_unreachable_finalization = TRUE;
  528. GC_ASSERT(GC_java_finalization);
  529. GC_register_finalizer_inner(obj, fn, cd, ofn,
  530. ocd, GC_unreachable_finalize_mark_proc);
  531. }
  532. #ifndef NO_DEBUGGING
  533. STATIC void GC_dump_finalization_links(
  534. const struct dl_hashtbl_s *dl_hashtbl)
  535. {
  536. struct disappearing_link *curr_dl;
  537. ptr_t real_ptr, real_link;
  538. size_t dl_size = dl_hashtbl->log_size == -1 ? 0 :
  539. 1 << dl_hashtbl->log_size;
  540. size_t i;
  541. for (i = 0; i < dl_size; i++) {
  542. for (curr_dl = dl_hashtbl -> head[i]; curr_dl != 0;
  543. curr_dl = dl_next(curr_dl)) {
  544. real_ptr = GC_REVEAL_POINTER(curr_dl -> dl_hidden_obj);
  545. real_link = GC_REVEAL_POINTER(curr_dl -> dl_hidden_link);
  546. GC_printf("Object: %p, link: %p\n", real_ptr, real_link);
  547. }
  548. }
  549. }
  550. void GC_dump_finalization(void)
  551. {
  552. struct finalizable_object * curr_fo;
  553. size_t fo_size = log_fo_table_size == -1 ? 0 : 1 << log_fo_table_size;
  554. ptr_t real_ptr;
  555. size_t i;
  556. GC_printf("Disappearing (short) links:\n");
  557. GC_dump_finalization_links(&GC_dl_hashtbl);
  558. # ifndef GC_LONG_REFS_NOT_NEEDED
  559. GC_printf("Disappearing long links:\n");
  560. GC_dump_finalization_links(&GC_ll_hashtbl);
  561. # endif
  562. GC_printf("Finalizers:\n");
  563. for (i = 0; i < fo_size; i++) {
  564. for (curr_fo = GC_fo_head[i]; curr_fo != 0;
  565. curr_fo = fo_next(curr_fo)) {
  566. real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
  567. GC_printf("Finalizable object: %p\n", real_ptr);
  568. }
  569. }
  570. }
  571. #endif /* !NO_DEBUGGING */
  572. #ifndef SMALL_CONFIG
  573. STATIC word GC_old_dl_entries = 0; /* for stats printing */
  574. # ifndef GC_LONG_REFS_NOT_NEEDED
  575. STATIC word GC_old_ll_entries = 0;
  576. # endif
  577. #endif /* !SMALL_CONFIG */
  578. #ifndef THREADS
  579. /* Global variables to minimize the level of recursion when a client */
  580. /* finalizer allocates memory. */
  581. STATIC int GC_finalizer_nested = 0;
  582. /* Only the lowest byte is used, the rest is */
  583. /* padding for proper global data alignment */
  584. /* required for some compilers (like Watcom). */
  585. STATIC unsigned GC_finalizer_skipped = 0;
  586. /* Checks and updates the level of finalizers recursion. */
  587. /* Returns NULL if GC_invoke_finalizers() should not be called by the */
  588. /* collector (to minimize the risk of a deep finalizers recursion), */
  589. /* otherwise returns a pointer to GC_finalizer_nested. */
  590. STATIC unsigned char *GC_check_finalizer_nested(void)
  591. {
  592. unsigned nesting_level = *(unsigned char *)&GC_finalizer_nested;
  593. if (nesting_level) {
  594. /* We are inside another GC_invoke_finalizers(). */
  595. /* Skip some implicitly-called GC_invoke_finalizers() */
  596. /* depending on the nesting (recursion) level. */
  597. if (++GC_finalizer_skipped < (1U << nesting_level)) return NULL;
  598. GC_finalizer_skipped = 0;
  599. }
  600. *(char *)&GC_finalizer_nested = (char)(nesting_level + 1);
  601. return (unsigned char *)&GC_finalizer_nested;
  602. }
  603. #endif /* THREADS */
  604. #define ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr_dl, prev_dl) \
  605. { \
  606. size_t i; \
  607. size_t dl_size = dl_hashtbl->log_size == -1 ? 0 : \
  608. 1 << dl_hashtbl->log_size; \
  609. for (i = 0; i < dl_size; i++) { \
  610. curr_dl = dl_hashtbl -> head[i]; \
  611. prev_dl = NULL; \
  612. while (curr_dl) {
  613. #define ITERATE_DL_HASHTBL_END(curr_dl, prev_dl) \
  614. prev_dl = curr_dl; \
  615. curr_dl = dl_next(curr_dl); \
  616. } \
  617. } \
  618. }
  619. #define DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr_dl, prev_dl, next_dl) \
  620. { \
  621. next_dl = dl_next(curr_dl); \
  622. if (NULL == prev_dl) { \
  623. dl_hashtbl -> head[i] = next_dl; \
  624. } else { \
  625. dl_set_next(prev_dl, next_dl); \
  626. } \
  627. GC_clear_mark_bit(curr_dl); \
  628. dl_hashtbl -> entries--; \
  629. curr_dl = next_dl; \
  630. continue; \
  631. }
  632. GC_INLINE void GC_make_disappearing_links_disappear(
  633. struct dl_hashtbl_s* dl_hashtbl)
  634. {
  635. struct disappearing_link *curr, *prev, *next;
  636. ptr_t real_ptr, real_link;
  637. ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr, prev)
  638. real_ptr = GC_REVEAL_POINTER(curr -> dl_hidden_obj);
  639. real_link = GC_REVEAL_POINTER(curr -> dl_hidden_link);
  640. if (!GC_is_marked(real_ptr)) {
  641. *(word *)real_link = 0;
  642. GC_clear_mark_bit(curr);
  643. DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr, prev, next);
  644. }
  645. ITERATE_DL_HASHTBL_END(curr, prev)
  646. }
  647. GC_INLINE void GC_remove_dangling_disappearing_links(
  648. struct dl_hashtbl_s* dl_hashtbl)
  649. {
  650. struct disappearing_link *curr, *prev, *next;
  651. ptr_t real_link;
  652. ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr, prev)
  653. real_link = GC_base(GC_REVEAL_POINTER(curr -> dl_hidden_link));
  654. if (NULL != real_link && !GC_is_marked(real_link)) {
  655. GC_clear_mark_bit(curr);
  656. DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr, prev, next);
  657. }
  658. ITERATE_DL_HASHTBL_END(curr, prev)
  659. }
  660. /* Called with held lock (but the world is running). */
  661. /* Cause disappearing links to disappear and unreachable objects to be */
  662. /* enqueued for finalization. */
  663. GC_INNER void GC_finalize(void)
  664. {
  665. struct finalizable_object * curr_fo, * prev_fo, * next_fo;
  666. ptr_t real_ptr;
  667. size_t i;
  668. size_t fo_size = log_fo_table_size == -1 ? 0 : 1 << log_fo_table_size;
  669. # ifndef SMALL_CONFIG
  670. /* Save current GC_[dl/ll]_entries value for stats printing */
  671. GC_old_dl_entries = GC_dl_hashtbl.entries;
  672. # ifndef GC_LONG_REFS_NOT_NEEDED
  673. GC_old_ll_entries = GC_ll_hashtbl.entries;
  674. # endif
  675. # endif
  676. GC_make_disappearing_links_disappear(&GC_dl_hashtbl);
  677. /* Mark all objects reachable via chains of 1 or more pointers */
  678. /* from finalizable objects. */
  679. GC_ASSERT(GC_mark_state == MS_NONE);
  680. for (i = 0; i < fo_size; i++) {
  681. for (curr_fo = GC_fo_head[i]; curr_fo != 0;
  682. curr_fo = fo_next(curr_fo)) {
  683. GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
  684. real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
  685. if (!GC_is_marked(real_ptr)) {
  686. GC_MARKED_FOR_FINALIZATION(real_ptr);
  687. GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
  688. if (GC_is_marked(real_ptr)) {
  689. WARN("Finalization cycle involving %p\n", real_ptr);
  690. }
  691. }
  692. }
  693. }
  694. /* Enqueue for finalization all objects that are still */
  695. /* unreachable. */
  696. GC_bytes_finalized = 0;
  697. for (i = 0; i < fo_size; i++) {
  698. curr_fo = GC_fo_head[i];
  699. prev_fo = 0;
  700. while (curr_fo != 0) {
  701. real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
  702. if (!GC_is_marked(real_ptr)) {
  703. if (!GC_java_finalization) {
  704. GC_set_mark_bit(real_ptr);
  705. }
  706. /* Delete from hash table */
  707. next_fo = fo_next(curr_fo);
  708. if (prev_fo == 0) {
  709. GC_fo_head[i] = next_fo;
  710. } else {
  711. fo_set_next(prev_fo, next_fo);
  712. }
  713. GC_fo_entries--;
  714. /* Add to list of objects awaiting finalization. */
  715. fo_set_next(curr_fo, GC_finalize_now);
  716. GC_finalize_now = curr_fo;
  717. /* unhide object pointer so any future collections will */
  718. /* see it. */
  719. curr_fo -> fo_hidden_base =
  720. (word)GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
  721. GC_bytes_finalized +=
  722. curr_fo -> fo_object_size
  723. + sizeof(struct finalizable_object);
  724. GC_ASSERT(GC_is_marked(GC_base(curr_fo)));
  725. curr_fo = next_fo;
  726. } else {
  727. prev_fo = curr_fo;
  728. curr_fo = fo_next(curr_fo);
  729. }
  730. }
  731. }
  732. if (GC_java_finalization) {
  733. /* make sure we mark everything reachable from objects finalized
  734. using the no_order mark_proc */
  735. for (curr_fo = GC_finalize_now;
  736. curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
  737. real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
  738. if (!GC_is_marked(real_ptr)) {
  739. if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
  740. GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
  741. }
  742. if (curr_fo -> fo_mark_proc != GC_unreachable_finalize_mark_proc) {
  743. GC_set_mark_bit(real_ptr);
  744. }
  745. }
  746. }
  747. /* now revive finalize-when-unreachable objects reachable from
  748. other finalizable objects */
  749. if (need_unreachable_finalization) {
  750. curr_fo = GC_finalize_now;
  751. prev_fo = 0;
  752. while (curr_fo != 0) {
  753. next_fo = fo_next(curr_fo);
  754. if (curr_fo -> fo_mark_proc == GC_unreachable_finalize_mark_proc) {
  755. real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
  756. if (!GC_is_marked(real_ptr)) {
  757. GC_set_mark_bit(real_ptr);
  758. } else {
  759. if (prev_fo == 0)
  760. GC_finalize_now = next_fo;
  761. else
  762. fo_set_next(prev_fo, next_fo);
  763. curr_fo -> fo_hidden_base =
  764. GC_HIDE_POINTER(curr_fo -> fo_hidden_base);
  765. GC_bytes_finalized -=
  766. curr_fo->fo_object_size + sizeof(struct finalizable_object);
  767. i = HASH2(real_ptr, log_fo_table_size);
  768. fo_set_next (curr_fo, GC_fo_head[i]);
  769. GC_fo_entries++;
  770. GC_fo_head[i] = curr_fo;
  771. curr_fo = prev_fo;
  772. }
  773. }
  774. prev_fo = curr_fo;
  775. curr_fo = next_fo;
  776. }
  777. }
  778. }
  779. GC_remove_dangling_disappearing_links(&GC_dl_hashtbl);
  780. # ifndef GC_LONG_REFS_NOT_NEEDED
  781. GC_make_disappearing_links_disappear(&GC_ll_hashtbl);
  782. GC_remove_dangling_disappearing_links(&GC_ll_hashtbl);
  783. # endif
  784. if (GC_fail_count) {
  785. /* Don't prevent running finalizers if there has been an allocation */
  786. /* failure recently. */
  787. # ifdef THREADS
  788. GC_reset_finalizer_nested();
  789. # else
  790. GC_finalizer_nested = 0;
  791. # endif
  792. }
  793. }
  794. #ifndef JAVA_FINALIZATION_NOT_NEEDED
  795. /* Enqueue all remaining finalizers to be run - Assumes lock is held. */
  796. STATIC void GC_enqueue_all_finalizers(void)
  797. {
  798. struct finalizable_object * curr_fo, * prev_fo, * next_fo;
  799. ptr_t real_ptr;
  800. register int i;
  801. int fo_size;
  802. fo_size = log_fo_table_size == -1 ? 0 : 1 << log_fo_table_size;
  803. GC_bytes_finalized = 0;
  804. for (i = 0; i < fo_size; i++) {
  805. curr_fo = GC_fo_head[i];
  806. prev_fo = 0;
  807. while (curr_fo != 0) {
  808. real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
  809. GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
  810. GC_set_mark_bit(real_ptr);
  811. /* Delete from hash table */
  812. next_fo = fo_next(curr_fo);
  813. if (prev_fo == 0) {
  814. GC_fo_head[i] = next_fo;
  815. } else {
  816. fo_set_next(prev_fo, next_fo);
  817. }
  818. GC_fo_entries--;
  819. /* Add to list of objects awaiting finalization. */
  820. fo_set_next(curr_fo, GC_finalize_now);
  821. GC_finalize_now = curr_fo;
  822. /* unhide object pointer so any future collections will */
  823. /* see it. */
  824. curr_fo -> fo_hidden_base =
  825. (word)GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
  826. GC_bytes_finalized +=
  827. curr_fo -> fo_object_size + sizeof(struct finalizable_object);
  828. curr_fo = next_fo;
  829. }
  830. }
  831. }
  832. /* Invoke all remaining finalizers that haven't yet been run.
  833. * This is needed for strict compliance with the Java standard,
  834. * which can make the runtime guarantee that all finalizers are run.
  835. * Unfortunately, the Java standard implies we have to keep running
  836. * finalizers until there are no more left, a potential infinite loop.
  837. * YUCK.
  838. * Note that this is even more dangerous than the usual Java
  839. * finalizers, in that objects reachable from static variables
  840. * may have been finalized when these finalizers are run.
  841. * Finalizers run at this point must be prepared to deal with a
  842. * mostly broken world.
  843. * This routine is externally callable, so is called without
  844. * the allocation lock.
  845. */
  846. GC_API void GC_CALL GC_finalize_all(void)
  847. {
  848. DCL_LOCK_STATE;
  849. LOCK();
  850. while (GC_fo_entries > 0) {
  851. GC_enqueue_all_finalizers();
  852. UNLOCK();
  853. GC_invoke_finalizers();
  854. /* Running the finalizers in this thread is arguably not a good */
  855. /* idea when we should be notifying another thread to run them. */
  856. /* But otherwise we don't have a great way to wait for them to */
  857. /* run. */
  858. LOCK();
  859. }
  860. UNLOCK();
  861. }
  862. #endif /* !JAVA_FINALIZATION_NOT_NEEDED */
  863. /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
  864. /* finalizers can only be called from some kind of "safe state" and */
  865. /* getting into that safe state is expensive.) */
  866. GC_API int GC_CALL GC_should_invoke_finalizers(void)
  867. {
  868. return GC_finalize_now != 0;
  869. }
  870. /* Invoke finalizers for all objects that are ready to be finalized. */
  871. /* Should be called without allocation lock. */
  872. GC_API int GC_CALL GC_invoke_finalizers(void)
  873. {
  874. struct finalizable_object * curr_fo;
  875. int count = 0;
  876. word bytes_freed_before = 0; /* initialized to prevent warning. */
  877. DCL_LOCK_STATE;
  878. while (GC_finalize_now != 0) {
  879. # ifdef THREADS
  880. LOCK();
  881. # endif
  882. if (count == 0) {
  883. bytes_freed_before = GC_bytes_freed;
  884. /* Don't do this outside, since we need the lock. */
  885. }
  886. curr_fo = GC_finalize_now;
  887. # ifdef THREADS
  888. if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
  889. UNLOCK();
  890. if (curr_fo == 0) break;
  891. # else
  892. GC_finalize_now = fo_next(curr_fo);
  893. # endif
  894. fo_set_next(curr_fo, 0);
  895. (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
  896. curr_fo -> fo_client_data);
  897. curr_fo -> fo_client_data = 0;
  898. ++count;
  899. # ifdef UNDEFINED
  900. /* This is probably a bad idea. It throws off accounting if */
  901. /* nearly all objects are finalizable. O.w. it shouldn't */
  902. /* matter. */
  903. GC_free((void *)curr_fo);
  904. # endif
  905. }
  906. /* bytes_freed_before is initialized whenever count != 0 */
  907. if (count != 0 && bytes_freed_before != GC_bytes_freed) {
  908. LOCK();
  909. GC_finalizer_bytes_freed += (GC_bytes_freed - bytes_freed_before);
  910. UNLOCK();
  911. }
  912. return count;
  913. }
  914. static GC_word last_finalizer_notification = 0;
  915. GC_INNER void GC_notify_or_invoke_finalizers(void)
  916. {
  917. GC_finalizer_notifier_proc notifier_fn = 0;
  918. # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
  919. static word last_back_trace_gc_no = 1; /* Skip first one. */
  920. # endif
  921. DCL_LOCK_STATE;
  922. # if defined(THREADS) && !defined(KEEP_BACK_PTRS) \
  923. && !defined(MAKE_BACK_GRAPH)
  924. /* Quick check (while unlocked) for an empty finalization queue. */
  925. if (GC_finalize_now == 0) return;
  926. # endif
  927. LOCK();
  928. /* This is a convenient place to generate backtraces if appropriate, */
  929. /* since that code is not callable with the allocation lock. */
  930. # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
  931. if (GC_gc_no > last_back_trace_gc_no) {
  932. # ifdef KEEP_BACK_PTRS
  933. long i;
  934. /* Stops when GC_gc_no wraps; that's OK. */
  935. last_back_trace_gc_no = (word)(-1); /* disable others. */
  936. for (i = 0; i < GC_backtraces; ++i) {
  937. /* FIXME: This tolerates concurrent heap mutation, */
  938. /* which may cause occasional mysterious results. */
  939. /* We need to release the GC lock, since GC_print_callers */
  940. /* acquires it. It probably shouldn't. */
  941. UNLOCK();
  942. GC_generate_random_backtrace_no_gc();
  943. LOCK();
  944. }
  945. last_back_trace_gc_no = GC_gc_no;
  946. # endif
  947. # ifdef MAKE_BACK_GRAPH
  948. if (GC_print_back_height) {
  949. UNLOCK();
  950. GC_print_back_graph_stats();
  951. LOCK();
  952. }
  953. # endif
  954. }
  955. # endif
  956. if (GC_finalize_now == 0) {
  957. UNLOCK();
  958. return;
  959. }
  960. if (!GC_finalize_on_demand) {
  961. unsigned char *pnested = GC_check_finalizer_nested();
  962. UNLOCK();
  963. /* Skip GC_invoke_finalizers() if nested */
  964. if (pnested != NULL) {
  965. (void) GC_invoke_finalizers();
  966. *pnested = 0; /* Reset since no more finalizers. */
  967. # ifndef THREADS
  968. GC_ASSERT(GC_finalize_now == 0);
  969. # endif /* Otherwise GC can run concurrently and add more */
  970. }
  971. return;
  972. }
  973. /* These variables require synchronization to avoid data races. */
  974. if (last_finalizer_notification != GC_gc_no) {
  975. last_finalizer_notification = GC_gc_no;
  976. notifier_fn = GC_finalizer_notifier;
  977. }
  978. UNLOCK();
  979. if (notifier_fn != 0)
  980. (*notifier_fn)(); /* Invoke the notifier */
  981. }
  982. #ifndef SMALL_CONFIG
  983. # ifndef GC_LONG_REFS_NOT_NEEDED
  984. # define IF_LONG_REFS_PRESENT_ELSE(x,y) (x)
  985. # else
  986. # define IF_LONG_REFS_PRESENT_ELSE(x,y) (y)
  987. # endif
  988. GC_INNER void GC_print_finalization_stats(void)
  989. {
  990. struct finalizable_object *fo;
  991. unsigned long ready = 0;
  992. GC_log_printf("%lu finalization entries;"
  993. " %lu/%lu short/long disappearing links alive\n",
  994. (unsigned long)GC_fo_entries,
  995. (unsigned long)GC_dl_hashtbl.entries,
  996. (unsigned long)IF_LONG_REFS_PRESENT_ELSE(
  997. GC_ll_hashtbl.entries, 0));
  998. for (fo = GC_finalize_now; 0 != fo; fo = fo_next(fo))
  999. ++ready;
  1000. GC_log_printf("%lu finalization-ready objects;"
  1001. " %ld/%ld short/long links cleared\n",
  1002. ready,
  1003. (long)GC_old_dl_entries - (long)GC_dl_hashtbl.entries,
  1004. (long)IF_LONG_REFS_PRESENT_ELSE(
  1005. GC_old_ll_entries - GC_ll_hashtbl.entries, 0));
  1006. }
  1007. #endif /* !SMALL_CONFIG */
  1008. #endif /* !GC_NO_FINALIZATION */