finalize.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. # include "javaxfc.h" /* to get GC_finalize_all() as extern "C" */
  18. /* Type of mark procedure used for marking from finalizable object. */
  19. /* This procedure normally does not mark the object, only its */
  20. /* descendants. */
  21. typedef void (* finalization_mark_proc)(ptr_t /* finalizable_obj_ptr */);
  22. #define HASH3(addr,size,log_size) \
  23. ((((word)(addr) >> 3) ^ ((word)(addr) >> (3 + (log_size)))) \
  24. & ((size) - 1))
  25. #define HASH2(addr,log_size) HASH3(addr, (word)1 << (log_size), log_size)
  26. struct hash_chain_entry {
  27. word hidden_key;
  28. struct hash_chain_entry * next;
  29. };
  30. struct disappearing_link {
  31. struct hash_chain_entry prolog;
  32. # define dl_hidden_link prolog.hidden_key
  33. /* Field to be cleared. */
  34. # define dl_next(x) (struct disappearing_link *)((x) -> prolog.next)
  35. # define dl_set_next(x, y) \
  36. (void)((x)->prolog.next = (struct hash_chain_entry *)(y))
  37. word dl_hidden_obj; /* Pointer to object base */
  38. };
  39. struct dl_hashtbl_s {
  40. struct disappearing_link **head;
  41. signed_word log_size;
  42. word entries;
  43. };
  44. STATIC struct dl_hashtbl_s GC_dl_hashtbl = {
  45. /* head */ NULL, /* log_size */ -1, /* entries */ 0 };
  46. #ifndef GC_LONG_REFS_NOT_NEEDED
  47. STATIC struct dl_hashtbl_s GC_ll_hashtbl = { NULL, -1, 0 };
  48. #endif
  49. struct finalizable_object {
  50. struct hash_chain_entry prolog;
  51. # define fo_hidden_base prolog.hidden_key
  52. /* Pointer to object base. */
  53. /* No longer hidden once object */
  54. /* is on finalize_now queue. */
  55. # define fo_next(x) (struct finalizable_object *)((x) -> prolog.next)
  56. # define fo_set_next(x,y) ((x)->prolog.next = (struct hash_chain_entry *)(y))
  57. GC_finalization_proc fo_fn; /* Finalizer. */
  58. ptr_t fo_client_data;
  59. word fo_object_size; /* In bytes. */
  60. finalization_mark_proc fo_mark_proc; /* Mark-through procedure */
  61. };
  62. static signed_word log_fo_table_size = -1;
  63. STATIC struct fnlz_roots_s {
  64. struct finalizable_object **fo_head;
  65. /* List of objects that should be finalized now: */
  66. struct finalizable_object *finalize_now;
  67. } GC_fnlz_roots = { NULL, NULL };
  68. void GC_clear_finalizable_object_table()
  69. {
  70. log_fo_table_size = -1;
  71. GC_fnlz_roots.fo_head = NULL;
  72. GC_fnlz_roots.finalize_now = NULL;
  73. }
  74. #ifdef AO_HAVE_store
  75. /* Update finalize_now atomically as GC_should_invoke_finalizers does */
  76. /* not acquire the allocation lock. */
  77. # define SET_FINALIZE_NOW(fo) \
  78. AO_store((volatile AO_t *)&GC_fnlz_roots.finalize_now, (AO_t)(fo))
  79. #else
  80. # define SET_FINALIZE_NOW(fo) (void)(GC_fnlz_roots.finalize_now = (fo))
  81. #endif /* !THREADS */
  82. GC_API void GC_CALL GC_push_finalizer_structures(void)
  83. {
  84. GC_ASSERT((word)(&GC_dl_hashtbl.head) % sizeof(word) == 0);
  85. GC_ASSERT((word)(&GC_fnlz_roots) % sizeof(word) == 0);
  86. # ifndef GC_LONG_REFS_NOT_NEEDED
  87. GC_ASSERT((word)(&GC_ll_hashtbl.head) % sizeof(word) == 0);
  88. GC_PUSH_ALL_SYM(GC_ll_hashtbl.head);
  89. # endif
  90. GC_PUSH_ALL_SYM(GC_dl_hashtbl.head);
  91. GC_PUSH_ALL_SYM(GC_fnlz_roots);
  92. }
  93. /* Threshold of log_size to initiate full collection before growing */
  94. /* a hash table. */
  95. #ifndef GC_ON_GROW_LOG_SIZE_MIN
  96. # define GC_ON_GROW_LOG_SIZE_MIN CPP_LOG_HBLKSIZE
  97. #endif
  98. /* Double the size of a hash table. *log_size_ptr is the log of its */
  99. /* current size. May be a no-op. */
  100. /* *table is a pointer to an array of hash headers. If we succeed, we */
  101. /* update both *table and *log_size_ptr. Lock is held. */
  102. STATIC void GC_grow_table(struct hash_chain_entry ***table,
  103. signed_word *log_size_ptr, word *entries_ptr)
  104. {
  105. word i;
  106. struct hash_chain_entry *p;
  107. signed_word log_old_size = *log_size_ptr;
  108. signed_word log_new_size = log_old_size + 1;
  109. word old_size = log_old_size == -1 ? 0 : (word)1 << log_old_size;
  110. word new_size = (word)1 << log_new_size;
  111. /* FIXME: Power of 2 size often gets rounded up to one more page. */
  112. struct hash_chain_entry **new_table;
  113. GC_ASSERT(I_HOLD_LOCK());
  114. /* Avoid growing the table in case of at least 25% of entries can */
  115. /* be deleted by enforcing a collection. Ignored for small tables. */
  116. /* UNITY: we always skip this optimization, as we want to */
  117. /* avoid triggering a full GC whenever possible. */
  118. #ifdef UNITY_ENABLE_GC_ON_GROW_LOG_SIZE_MIN
  119. if (log_old_size >= GC_ON_GROW_LOG_SIZE_MIN && !GC_incremental) {
  120. IF_CANCEL(int cancel_state;)
  121. DISABLE_CANCEL(cancel_state);
  122. (void)GC_try_to_collect_inner(GC_never_stop_func);
  123. RESTORE_CANCEL(cancel_state);
  124. /* GC_finalize might decrease entries value. */
  125. if (*entries_ptr < ((word)1 << log_old_size) - (*entries_ptr >> 2))
  126. return;
  127. }
  128. #endif
  129. new_table = (struct hash_chain_entry **)
  130. GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
  131. (size_t)new_size * sizeof(struct hash_chain_entry *),
  132. NORMAL);
  133. if (new_table == 0) {
  134. if (*table == 0) {
  135. ABORT("Insufficient space for initial table allocation");
  136. } else {
  137. return;
  138. }
  139. }
  140. for (i = 0; i < old_size; i++) {
  141. p = (*table)[i];
  142. while (p != 0) {
  143. ptr_t real_key = (ptr_t)GC_REVEAL_POINTER(p->hidden_key);
  144. struct hash_chain_entry *next = p -> next;
  145. size_t new_hash = HASH3(real_key, new_size, log_new_size);
  146. p -> next = new_table[new_hash];
  147. GC_dirty(p);
  148. new_table[new_hash] = p;
  149. p = next;
  150. }
  151. }
  152. *log_size_ptr = log_new_size;
  153. *table = new_table;
  154. GC_dirty(new_table); /* entire object */
  155. }
  156. GC_API int GC_CALL GC_register_disappearing_link(void * * link)
  157. {
  158. ptr_t base;
  159. base = (ptr_t)GC_base(link);
  160. if (base == 0)
  161. ABORT("Bad arg to GC_register_disappearing_link");
  162. return(GC_general_register_disappearing_link(link, base));
  163. }
  164. STATIC int GC_register_disappearing_link_inner(
  165. struct dl_hashtbl_s *dl_hashtbl, void **link,
  166. const void *obj, const char *tbl_log_name)
  167. {
  168. struct disappearing_link *curr_dl;
  169. size_t index;
  170. struct disappearing_link * new_dl;
  171. DCL_LOCK_STATE;
  172. if (EXPECT(GC_find_leak, FALSE)) return GC_UNIMPLEMENTED;
  173. LOCK();
  174. GC_ASSERT(obj != NULL && GC_base_C(obj) == obj);
  175. if (dl_hashtbl -> log_size == -1
  176. || dl_hashtbl -> entries > ((word)1 << dl_hashtbl -> log_size)) {
  177. GC_grow_table((struct hash_chain_entry ***)&dl_hashtbl -> head,
  178. &dl_hashtbl -> log_size, &dl_hashtbl -> entries);
  179. # ifdef LINT2
  180. if (dl_hashtbl->log_size < 0) ABORT("log_size is negative");
  181. # endif
  182. GC_COND_LOG_PRINTF("Grew %s table to %u entries\n", tbl_log_name,
  183. 1 << (unsigned)dl_hashtbl -> log_size);
  184. }
  185. index = HASH2(link, dl_hashtbl -> log_size);
  186. for (curr_dl = dl_hashtbl -> head[index]; curr_dl != 0;
  187. curr_dl = dl_next(curr_dl)) {
  188. if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
  189. curr_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
  190. UNLOCK();
  191. return GC_DUPLICATE;
  192. }
  193. }
  194. new_dl = (struct disappearing_link *)
  195. GC_INTERNAL_MALLOC(sizeof(struct disappearing_link),NORMAL);
  196. if (0 == new_dl) {
  197. GC_oom_func oom_fn = GC_oom_fn;
  198. UNLOCK();
  199. new_dl = (struct disappearing_link *)
  200. (*oom_fn)(sizeof(struct disappearing_link));
  201. if (0 == new_dl) {
  202. return GC_NO_MEMORY;
  203. }
  204. /* It's not likely we'll make it here, but ... */
  205. LOCK();
  206. /* Recalculate index since the table may grow. */
  207. index = HASH2(link, dl_hashtbl -> log_size);
  208. /* Check again that our disappearing link not in the table. */
  209. for (curr_dl = dl_hashtbl -> head[index]; curr_dl != 0;
  210. curr_dl = dl_next(curr_dl)) {
  211. if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
  212. curr_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
  213. UNLOCK();
  214. # ifndef DBG_HDRS_ALL
  215. /* Free unused new_dl returned by GC_oom_fn() */
  216. GC_free((void *)new_dl);
  217. # endif
  218. return GC_DUPLICATE;
  219. }
  220. }
  221. }
  222. new_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
  223. new_dl -> dl_hidden_link = GC_HIDE_POINTER(link);
  224. dl_set_next(new_dl, dl_hashtbl -> head[index]);
  225. dl_hashtbl -> head[index] = new_dl;
  226. dl_hashtbl -> entries++;
  227. GC_dirty(dl_hashtbl->head + index);
  228. UNLOCK();
  229. GC_dirty(new_dl);
  230. return GC_SUCCESS;
  231. }
  232. GC_API int GC_CALL GC_general_register_disappearing_link(void * * link,
  233. const void * obj)
  234. {
  235. if (((word)link & (ALIGNMENT-1)) != 0 || !NONNULL_ARG_NOT_NULL(link))
  236. ABORT("Bad arg to GC_general_register_disappearing_link");
  237. return GC_register_disappearing_link_inner(&GC_dl_hashtbl, link, obj,
  238. "dl");
  239. }
  240. #ifdef DBG_HDRS_ALL
  241. # define FREE_DL_ENTRY(curr_dl) dl_set_next(curr_dl, NULL)
  242. #else
  243. # define FREE_DL_ENTRY(curr_dl) GC_free(curr_dl)
  244. #endif
  245. /* Unregisters given link and returns the link entry to free. */
  246. GC_INLINE struct disappearing_link *GC_unregister_disappearing_link_inner(
  247. struct dl_hashtbl_s *dl_hashtbl, void **link)
  248. {
  249. struct disappearing_link *curr_dl;
  250. struct disappearing_link *prev_dl = NULL;
  251. size_t index;
  252. GC_ASSERT(I_HOLD_LOCK());
  253. if (dl_hashtbl->log_size == -1)
  254. return NULL; /* prevent integer shift by a negative amount */
  255. index = HASH2(link, dl_hashtbl->log_size);
  256. for (curr_dl = dl_hashtbl -> head[index]; curr_dl;
  257. curr_dl = dl_next(curr_dl)) {
  258. if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
  259. /* Remove found entry from the table. */
  260. if (NULL == prev_dl) {
  261. dl_hashtbl -> head[index] = dl_next(curr_dl);
  262. GC_dirty(dl_hashtbl->head + index);
  263. } else {
  264. dl_set_next(prev_dl, dl_next(curr_dl));
  265. GC_dirty(prev_dl);
  266. }
  267. dl_hashtbl -> entries--;
  268. break;
  269. }
  270. prev_dl = curr_dl;
  271. }
  272. return curr_dl;
  273. }
  274. GC_API int GC_CALL GC_unregister_disappearing_link(void * * link)
  275. {
  276. struct disappearing_link *curr_dl;
  277. DCL_LOCK_STATE;
  278. if (((word)link & (ALIGNMENT-1)) != 0) return(0); /* Nothing to do. */
  279. LOCK();
  280. curr_dl = GC_unregister_disappearing_link_inner(&GC_dl_hashtbl, link);
  281. UNLOCK();
  282. if (NULL == curr_dl) return 0;
  283. FREE_DL_ENTRY(curr_dl);
  284. return 1;
  285. }
  286. /* Toggle-ref support. */
  287. #ifndef GC_TOGGLE_REFS_NOT_NEEDED
  288. typedef union {
  289. /* Lowest bit is used to distinguish between choices. */
  290. void *strong_ref;
  291. GC_hidden_pointer weak_ref;
  292. } GCToggleRef;
  293. STATIC GC_toggleref_func GC_toggleref_callback = 0;
  294. STATIC GCToggleRef *GC_toggleref_arr = NULL;
  295. STATIC int GC_toggleref_array_size = 0;
  296. STATIC int GC_toggleref_array_capacity = 0;
  297. GC_INNER void GC_process_togglerefs(void)
  298. {
  299. int i;
  300. int new_size = 0;
  301. GC_bool needs_barrier = FALSE;
  302. GC_ASSERT(I_HOLD_LOCK());
  303. for (i = 0; i < GC_toggleref_array_size; ++i) {
  304. GCToggleRef r = GC_toggleref_arr[i];
  305. void *obj = r.strong_ref;
  306. if (((word)obj & 1) != 0) {
  307. obj = GC_REVEAL_POINTER(r.weak_ref);
  308. }
  309. if (NULL == obj) {
  310. continue;
  311. }
  312. switch (GC_toggleref_callback(obj)) {
  313. case GC_TOGGLE_REF_DROP:
  314. break;
  315. case GC_TOGGLE_REF_STRONG:
  316. GC_toggleref_arr[new_size++].strong_ref = obj;
  317. needs_barrier = TRUE;
  318. break;
  319. case GC_TOGGLE_REF_WEAK:
  320. GC_toggleref_arr[new_size++].weak_ref = GC_HIDE_POINTER(obj);
  321. break;
  322. default:
  323. ABORT("Bad toggle-ref status returned by callback");
  324. }
  325. }
  326. if (new_size < GC_toggleref_array_size) {
  327. BZERO(&GC_toggleref_arr[new_size],
  328. (GC_toggleref_array_size - new_size) * sizeof(GCToggleRef));
  329. GC_toggleref_array_size = new_size;
  330. }
  331. if (needs_barrier)
  332. GC_dirty(GC_toggleref_arr); /* entire object */
  333. }
  334. STATIC void GC_normal_finalize_mark_proc(ptr_t);
  335. static void push_and_mark_object(void *p)
  336. {
  337. GC_normal_finalize_mark_proc((ptr_t)p);
  338. while (!GC_mark_stack_empty()) {
  339. MARK_FROM_MARK_STACK();
  340. }
  341. GC_set_mark_bit(p);
  342. if (GC_mark_state != MS_NONE) {
  343. while (!GC_mark_some(0)) {
  344. /* Empty. */
  345. }
  346. }
  347. }
  348. STATIC void GC_mark_togglerefs(void)
  349. {
  350. int i;
  351. if (NULL == GC_toggleref_arr)
  352. return;
  353. /* TODO: Hide GC_toggleref_arr to avoid its marking from roots. */
  354. GC_set_mark_bit(GC_toggleref_arr);
  355. for (i = 0; i < GC_toggleref_array_size; ++i) {
  356. void *obj = GC_toggleref_arr[i].strong_ref;
  357. if (obj != NULL && ((word)obj & 1) == 0) {
  358. push_and_mark_object(obj);
  359. }
  360. }
  361. }
  362. STATIC void GC_clear_togglerefs(void)
  363. {
  364. int i;
  365. for (i = 0; i < GC_toggleref_array_size; ++i) {
  366. if ((GC_toggleref_arr[i].weak_ref & 1) != 0) {
  367. if (!GC_is_marked(GC_REVEAL_POINTER(GC_toggleref_arr[i].weak_ref))) {
  368. GC_toggleref_arr[i].weak_ref = 0;
  369. } else {
  370. /* No need to copy, BDWGC is a non-moving collector. */
  371. }
  372. }
  373. }
  374. }
  375. GC_API void GC_CALL GC_set_toggleref_func(GC_toggleref_func fn)
  376. {
  377. DCL_LOCK_STATE;
  378. LOCK();
  379. GC_toggleref_callback = fn;
  380. UNLOCK();
  381. }
  382. GC_API GC_toggleref_func GC_CALL GC_get_toggleref_func(void)
  383. {
  384. GC_toggleref_func fn;
  385. DCL_LOCK_STATE;
  386. LOCK();
  387. fn = GC_toggleref_callback;
  388. UNLOCK();
  389. return fn;
  390. }
  391. static GC_bool ensure_toggleref_capacity(int capacity_inc)
  392. {
  393. GC_ASSERT(capacity_inc >= 0);
  394. GC_ASSERT(I_HOLD_LOCK());
  395. if (NULL == GC_toggleref_arr) {
  396. GC_toggleref_array_capacity = 32; /* initial capacity */
  397. GC_toggleref_arr = (GCToggleRef *)GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
  398. GC_toggleref_array_capacity * sizeof(GCToggleRef),
  399. NORMAL);
  400. if (NULL == GC_toggleref_arr)
  401. return FALSE;
  402. }
  403. if ((unsigned)GC_toggleref_array_size + (unsigned)capacity_inc
  404. >= (unsigned)GC_toggleref_array_capacity) {
  405. GCToggleRef *new_array;
  406. while ((unsigned)GC_toggleref_array_capacity
  407. < (unsigned)GC_toggleref_array_size + (unsigned)capacity_inc) {
  408. GC_toggleref_array_capacity *= 2;
  409. if (GC_toggleref_array_capacity < 0) /* overflow */
  410. return FALSE;
  411. }
  412. new_array = (GCToggleRef *)GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
  413. GC_toggleref_array_capacity * sizeof(GCToggleRef),
  414. NORMAL);
  415. if (NULL == new_array)
  416. return FALSE;
  417. if (EXPECT(GC_toggleref_array_size > 0, TRUE))
  418. BCOPY(GC_toggleref_arr, new_array,
  419. GC_toggleref_array_size * sizeof(GCToggleRef));
  420. GC_INTERNAL_FREE(GC_toggleref_arr);
  421. GC_toggleref_arr = new_array;
  422. }
  423. return TRUE;
  424. }
  425. GC_API int GC_CALL GC_toggleref_add(void *obj, int is_strong_ref)
  426. {
  427. int res = GC_SUCCESS;
  428. DCL_LOCK_STATE;
  429. GC_ASSERT(NONNULL_ARG_NOT_NULL(obj));
  430. LOCK();
  431. if (GC_toggleref_callback != 0) {
  432. if (!ensure_toggleref_capacity(1)) {
  433. res = GC_NO_MEMORY;
  434. } else {
  435. GC_toggleref_arr[GC_toggleref_array_size].strong_ref =
  436. is_strong_ref ? obj : (void *)GC_HIDE_POINTER(obj);
  437. if (is_strong_ref)
  438. GC_dirty(GC_toggleref_arr + GC_toggleref_array_size);
  439. GC_toggleref_array_size++;
  440. }
  441. }
  442. UNLOCK();
  443. return res;
  444. }
  445. #endif /* !GC_TOGGLE_REFS_NOT_NEEDED */
  446. /* Finalizer callback support. */
  447. STATIC GC_await_finalize_proc GC_object_finalized_proc = 0;
  448. GC_API void GC_CALL GC_set_await_finalize_proc(GC_await_finalize_proc fn)
  449. {
  450. DCL_LOCK_STATE;
  451. LOCK();
  452. GC_object_finalized_proc = fn;
  453. UNLOCK();
  454. }
  455. GC_API GC_await_finalize_proc GC_CALL GC_get_await_finalize_proc(void)
  456. {
  457. GC_await_finalize_proc fn;
  458. DCL_LOCK_STATE;
  459. LOCK();
  460. fn = GC_object_finalized_proc;
  461. UNLOCK();
  462. return fn;
  463. }
  464. #ifndef GC_LONG_REFS_NOT_NEEDED
  465. GC_API int GC_CALL GC_register_long_link(void * * link, const void * obj)
  466. {
  467. if (((word)link & (ALIGNMENT-1)) != 0 || !NONNULL_ARG_NOT_NULL(link))
  468. ABORT("Bad arg to GC_register_long_link");
  469. return GC_register_disappearing_link_inner(&GC_ll_hashtbl, link, obj,
  470. "long dl");
  471. }
  472. GC_API int GC_CALL GC_unregister_long_link(void * * link)
  473. {
  474. struct disappearing_link *curr_dl;
  475. DCL_LOCK_STATE;
  476. if (((word)link & (ALIGNMENT-1)) != 0) return(0); /* Nothing to do. */
  477. LOCK();
  478. curr_dl = GC_unregister_disappearing_link_inner(&GC_ll_hashtbl, link);
  479. UNLOCK();
  480. if (NULL == curr_dl) return 0;
  481. FREE_DL_ENTRY(curr_dl);
  482. return 1;
  483. }
  484. #endif /* !GC_LONG_REFS_NOT_NEEDED */
  485. #ifndef GC_MOVE_DISAPPEARING_LINK_NOT_NEEDED
  486. /* Moves a link. Assume the lock is held. */
  487. STATIC int GC_move_disappearing_link_inner(
  488. struct dl_hashtbl_s *dl_hashtbl,
  489. void **link, void **new_link)
  490. {
  491. struct disappearing_link *curr_dl, *prev_dl, *new_dl;
  492. size_t curr_index, new_index;
  493. word curr_hidden_link;
  494. word new_hidden_link;
  495. GC_ASSERT(I_HOLD_LOCK());
  496. if (dl_hashtbl->log_size == -1)
  497. return GC_NOT_FOUND; /* prevent integer shift by a negative amount */
  498. /* Find current link. */
  499. curr_index = HASH2(link, dl_hashtbl -> log_size);
  500. curr_hidden_link = GC_HIDE_POINTER(link);
  501. prev_dl = NULL;
  502. for (curr_dl = dl_hashtbl -> head[curr_index]; curr_dl;
  503. curr_dl = dl_next(curr_dl)) {
  504. if (curr_dl -> dl_hidden_link == curr_hidden_link)
  505. break;
  506. prev_dl = curr_dl;
  507. }
  508. if (NULL == curr_dl) {
  509. return GC_NOT_FOUND;
  510. }
  511. if (link == new_link) {
  512. return GC_SUCCESS; /* Nothing to do. */
  513. }
  514. /* link found; now check new_link not present. */
  515. new_index = HASH2(new_link, dl_hashtbl -> log_size);
  516. new_hidden_link = GC_HIDE_POINTER(new_link);
  517. for (new_dl = dl_hashtbl -> head[new_index]; new_dl;
  518. new_dl = dl_next(new_dl)) {
  519. if (new_dl -> dl_hidden_link == new_hidden_link) {
  520. /* Target already registered; bail. */
  521. return GC_DUPLICATE;
  522. }
  523. }
  524. /* Remove from old, add to new, update link. */
  525. if (NULL == prev_dl) {
  526. dl_hashtbl -> head[curr_index] = dl_next(curr_dl);
  527. } else {
  528. dl_set_next(prev_dl, dl_next(curr_dl));
  529. GC_dirty(prev_dl);
  530. }
  531. curr_dl -> dl_hidden_link = new_hidden_link;
  532. dl_set_next(curr_dl, dl_hashtbl -> head[new_index]);
  533. dl_hashtbl -> head[new_index] = curr_dl;
  534. GC_dirty(curr_dl);
  535. GC_dirty(dl_hashtbl->head); /* entire object */
  536. return GC_SUCCESS;
  537. }
  538. GC_API int GC_CALL GC_move_disappearing_link(void **link, void **new_link)
  539. {
  540. int result;
  541. DCL_LOCK_STATE;
  542. if (((word)new_link & (ALIGNMENT-1)) != 0
  543. || !NONNULL_ARG_NOT_NULL(new_link))
  544. ABORT("Bad new_link arg to GC_move_disappearing_link");
  545. if (((word)link & (ALIGNMENT-1)) != 0)
  546. return GC_NOT_FOUND; /* Nothing to do. */
  547. LOCK();
  548. result = GC_move_disappearing_link_inner(&GC_dl_hashtbl, link, new_link);
  549. UNLOCK();
  550. return result;
  551. }
  552. # ifndef GC_LONG_REFS_NOT_NEEDED
  553. GC_API int GC_CALL GC_move_long_link(void **link, void **new_link)
  554. {
  555. int result;
  556. DCL_LOCK_STATE;
  557. if (((word)new_link & (ALIGNMENT-1)) != 0
  558. || !NONNULL_ARG_NOT_NULL(new_link))
  559. ABORT("Bad new_link arg to GC_move_long_link");
  560. if (((word)link & (ALIGNMENT-1)) != 0)
  561. return GC_NOT_FOUND; /* Nothing to do. */
  562. LOCK();
  563. result = GC_move_disappearing_link_inner(&GC_ll_hashtbl, link, new_link);
  564. UNLOCK();
  565. return result;
  566. }
  567. # endif /* !GC_LONG_REFS_NOT_NEEDED */
  568. #endif /* !GC_MOVE_DISAPPEARING_LINK_NOT_NEEDED */
  569. /* Possible finalization_marker procedures. Note that mark stack */
  570. /* overflow is handled by the caller, and is not a disaster. */
  571. STATIC void GC_normal_finalize_mark_proc(ptr_t p)
  572. {
  573. hdr * hhdr = HDR(p);
  574. PUSH_OBJ(p, hhdr, GC_mark_stack_top,
  575. &(GC_mark_stack[GC_mark_stack_size]));
  576. }
  577. /* This only pays very partial attention to the mark descriptor. */
  578. /* It does the right thing for normal and atomic objects, and treats */
  579. /* most others as normal. */
  580. STATIC void GC_ignore_self_finalize_mark_proc(ptr_t p)
  581. {
  582. hdr * hhdr = HDR(p);
  583. word descr = hhdr -> hb_descr;
  584. ptr_t q;
  585. ptr_t scan_limit;
  586. ptr_t target_limit = p + hhdr -> hb_sz - 1;
  587. if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
  588. scan_limit = p + descr - sizeof(word);
  589. } else {
  590. scan_limit = target_limit + 1 - sizeof(word);
  591. }
  592. for (q = p; (word)q <= (word)scan_limit; q += ALIGNMENT) {
  593. word r = *(word *)q;
  594. if (r < (word)p || r > (word)target_limit) {
  595. GC_PUSH_ONE_HEAP(r, q, GC_mark_stack_top);
  596. }
  597. }
  598. }
  599. STATIC void GC_null_finalize_mark_proc(ptr_t p GC_ATTR_UNUSED) {}
  600. /* Possible finalization_marker procedures. Note that mark stack */
  601. /* overflow is handled by the caller, and is not a disaster. */
  602. /* GC_unreachable_finalize_mark_proc is an alias for normal marking, */
  603. /* but it is explicitly tested for, and triggers different */
  604. /* behavior. Objects registered in this way are not finalized */
  605. /* if they are reachable by other finalizable objects, even if those */
  606. /* other objects specify no ordering. */
  607. STATIC void GC_unreachable_finalize_mark_proc(ptr_t p)
  608. {
  609. GC_normal_finalize_mark_proc(p);
  610. }
  611. /* Register a finalization function. See gc.h for details. */
  612. /* The last parameter is a procedure that determines */
  613. /* marking for finalization ordering. Any objects marked */
  614. /* by that procedure will be guaranteed to not have been */
  615. /* finalized when this finalizer is invoked. */
  616. STATIC void GC_register_finalizer_inner(void * obj,
  617. GC_finalization_proc fn, void *cd,
  618. GC_finalization_proc *ofn, void **ocd,
  619. finalization_mark_proc mp)
  620. {
  621. struct finalizable_object * curr_fo;
  622. size_t index;
  623. struct finalizable_object *new_fo = 0;
  624. hdr *hhdr = NULL; /* initialized to prevent warning. */
  625. DCL_LOCK_STATE;
  626. if (EXPECT(GC_find_leak, FALSE)) return;
  627. LOCK();
  628. if (log_fo_table_size == -1
  629. || GC_fo_entries > ((word)1 << log_fo_table_size)) {
  630. GC_grow_table((struct hash_chain_entry ***)&GC_fnlz_roots.fo_head,
  631. &log_fo_table_size, &GC_fo_entries);
  632. # ifdef LINT2
  633. if (log_fo_table_size < 0) ABORT("log_size is negative");
  634. # endif
  635. GC_COND_LOG_PRINTF("Grew fo table to %u entries\n",
  636. 1 << (unsigned)log_fo_table_size);
  637. }
  638. /* in the THREADS case we hold allocation lock. */
  639. for (;;) {
  640. struct finalizable_object *prev_fo = NULL;
  641. GC_oom_func oom_fn;
  642. index = HASH2(obj, log_fo_table_size);
  643. curr_fo = GC_fnlz_roots.fo_head[index];
  644. while (curr_fo != 0) {
  645. GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
  646. if (curr_fo -> fo_hidden_base == GC_HIDE_POINTER(obj)) {
  647. /* Interruption by a signal in the middle of this */
  648. /* should be safe. The client may see only *ocd */
  649. /* updated, but we'll declare that to be his problem. */
  650. if (ocd) *ocd = (void *) (curr_fo -> fo_client_data);
  651. if (ofn) *ofn = curr_fo -> fo_fn;
  652. /* Delete the structure for obj. */
  653. if (prev_fo == 0) {
  654. GC_fnlz_roots.fo_head[index] = fo_next(curr_fo);
  655. } else {
  656. fo_set_next(prev_fo, fo_next(curr_fo));
  657. GC_dirty(prev_fo);
  658. }
  659. if (fn == 0) {
  660. GC_fo_entries--;
  661. /* May not happen if we get a signal. But a high */
  662. /* estimate will only make the table larger than */
  663. /* necessary. */
  664. # if !defined(THREADS) && !defined(DBG_HDRS_ALL)
  665. GC_free((void *)curr_fo);
  666. # endif
  667. } else {
  668. curr_fo -> fo_fn = fn;
  669. curr_fo -> fo_client_data = (ptr_t)cd;
  670. curr_fo -> fo_mark_proc = mp;
  671. GC_dirty(curr_fo);
  672. /* Reinsert it. We deleted it first to maintain */
  673. /* consistency in the event of a signal. */
  674. if (prev_fo == 0) {
  675. GC_fnlz_roots.fo_head[index] = curr_fo;
  676. } else {
  677. fo_set_next(prev_fo, curr_fo);
  678. GC_dirty(prev_fo);
  679. }
  680. }
  681. if (NULL == prev_fo)
  682. GC_dirty(GC_fnlz_roots.fo_head + index);
  683. UNLOCK();
  684. # ifndef DBG_HDRS_ALL
  685. if (EXPECT(new_fo != 0, FALSE)) {
  686. /* Free unused new_fo returned by GC_oom_fn() */
  687. GC_free((void *)new_fo);
  688. }
  689. # endif
  690. return;
  691. }
  692. prev_fo = curr_fo;
  693. curr_fo = fo_next(curr_fo);
  694. }
  695. if (EXPECT(new_fo != 0, FALSE)) {
  696. /* new_fo is returned by GC_oom_fn(). */
  697. GC_ASSERT(fn != 0);
  698. # ifdef LINT2
  699. if (NULL == hhdr) ABORT("Bad hhdr in GC_register_finalizer_inner");
  700. # endif
  701. break;
  702. }
  703. if (fn == 0) {
  704. if (ocd) *ocd = 0;
  705. if (ofn) *ofn = 0;
  706. UNLOCK();
  707. return;
  708. }
  709. GET_HDR(obj, hhdr);
  710. if (EXPECT(0 == hhdr, FALSE)) {
  711. /* We won't collect it, hence finalizer wouldn't be run. */
  712. if (ocd) *ocd = 0;
  713. if (ofn) *ofn = 0;
  714. UNLOCK();
  715. return;
  716. }
  717. new_fo = (struct finalizable_object *)
  718. GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
  719. if (EXPECT(new_fo != 0, TRUE))
  720. break;
  721. oom_fn = GC_oom_fn;
  722. UNLOCK();
  723. new_fo = (struct finalizable_object *)
  724. (*oom_fn)(sizeof(struct finalizable_object));
  725. if (0 == new_fo) {
  726. /* No enough memory. *ocd and *ofn remains unchanged. */
  727. return;
  728. }
  729. /* It's not likely we'll make it here, but ... */
  730. LOCK();
  731. /* Recalculate index since the table may grow and */
  732. /* check again that our finalizer is not in the table. */
  733. }
  734. GC_ASSERT(GC_size(new_fo) >= sizeof(struct finalizable_object));
  735. if (ocd) *ocd = 0;
  736. if (ofn) *ofn = 0;
  737. new_fo -> fo_hidden_base = GC_HIDE_POINTER(obj);
  738. new_fo -> fo_fn = fn;
  739. new_fo -> fo_client_data = (ptr_t)cd;
  740. new_fo -> fo_object_size = hhdr -> hb_sz;
  741. new_fo -> fo_mark_proc = mp;
  742. fo_set_next(new_fo, GC_fnlz_roots.fo_head[index]);
  743. GC_fo_entries++;
  744. GC_fnlz_roots.fo_head[index] = new_fo;
  745. GC_dirty(GC_fnlz_roots.fo_head + index);
  746. UNLOCK();
  747. GC_dirty(new_fo);
  748. }
  749. GC_API void GC_CALL GC_register_finalizer(void * obj,
  750. GC_finalization_proc fn, void * cd,
  751. GC_finalization_proc *ofn, void ** ocd)
  752. {
  753. GC_register_finalizer_inner(obj, fn, cd, ofn,
  754. ocd, GC_normal_finalize_mark_proc);
  755. }
  756. GC_API void GC_CALL GC_register_finalizer_ignore_self(void * obj,
  757. GC_finalization_proc fn, void * cd,
  758. GC_finalization_proc *ofn, void ** ocd)
  759. {
  760. GC_register_finalizer_inner(obj, fn, cd, ofn,
  761. ocd, GC_ignore_self_finalize_mark_proc);
  762. }
  763. GC_API void GC_CALL GC_register_finalizer_no_order(void * obj,
  764. GC_finalization_proc fn, void * cd,
  765. GC_finalization_proc *ofn, void ** ocd)
  766. {
  767. GC_register_finalizer_inner(obj, fn, cd, ofn,
  768. ocd, GC_null_finalize_mark_proc);
  769. }
  770. static GC_bool need_unreachable_finalization = FALSE;
  771. /* Avoid the work if this isn't used. */
  772. GC_API void GC_CALL GC_register_finalizer_unreachable(void * obj,
  773. GC_finalization_proc fn, void * cd,
  774. GC_finalization_proc *ofn, void ** ocd)
  775. {
  776. need_unreachable_finalization = TRUE;
  777. GC_ASSERT(GC_java_finalization);
  778. GC_register_finalizer_inner(obj, fn, cd, ofn,
  779. ocd, GC_unreachable_finalize_mark_proc);
  780. }
  781. #ifndef NO_DEBUGGING
  782. STATIC void GC_dump_finalization_links(
  783. const struct dl_hashtbl_s *dl_hashtbl)
  784. {
  785. size_t dl_size = dl_hashtbl->log_size == -1 ? 0 :
  786. (size_t)1 << dl_hashtbl->log_size;
  787. size_t i;
  788. for (i = 0; i < dl_size; i++) {
  789. struct disappearing_link *curr_dl;
  790. for (curr_dl = dl_hashtbl -> head[i]; curr_dl != 0;
  791. curr_dl = dl_next(curr_dl)) {
  792. ptr_t real_ptr = (ptr_t)GC_REVEAL_POINTER(curr_dl->dl_hidden_obj);
  793. ptr_t real_link = (ptr_t)GC_REVEAL_POINTER(curr_dl->dl_hidden_link);
  794. GC_printf("Object: %p, link: %p\n",
  795. (void *)real_ptr, (void *)real_link);
  796. }
  797. }
  798. }
  799. GC_API void GC_CALL GC_dump_finalization(void)
  800. {
  801. struct finalizable_object * curr_fo;
  802. size_t fo_size = log_fo_table_size == -1 ? 0 :
  803. (size_t)1 << log_fo_table_size;
  804. size_t i;
  805. GC_printf("Disappearing (short) links:\n");
  806. GC_dump_finalization_links(&GC_dl_hashtbl);
  807. # ifndef GC_LONG_REFS_NOT_NEEDED
  808. GC_printf("Disappearing long links:\n");
  809. GC_dump_finalization_links(&GC_ll_hashtbl);
  810. # endif
  811. GC_printf("Finalizers:\n");
  812. for (i = 0; i < fo_size; i++) {
  813. for (curr_fo = GC_fnlz_roots.fo_head[i];
  814. curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
  815. ptr_t real_ptr = (ptr_t)GC_REVEAL_POINTER(curr_fo->fo_hidden_base);
  816. GC_printf("Finalizable object: %p\n", (void *)real_ptr);
  817. }
  818. }
  819. }
  820. #endif /* !NO_DEBUGGING */
  821. #ifndef SMALL_CONFIG
  822. STATIC word GC_old_dl_entries = 0; /* for stats printing */
  823. # ifndef GC_LONG_REFS_NOT_NEEDED
  824. STATIC word GC_old_ll_entries = 0;
  825. # endif
  826. #endif /* !SMALL_CONFIG */
  827. #ifndef THREADS
  828. /* Global variables to minimize the level of recursion when a client */
  829. /* finalizer allocates memory. */
  830. STATIC int GC_finalizer_nested = 0;
  831. /* Only the lowest byte is used, the rest is */
  832. /* padding for proper global data alignment */
  833. /* required for some compilers (like Watcom). */
  834. STATIC unsigned GC_finalizer_skipped = 0;
  835. /* Checks and updates the level of finalizers recursion. */
  836. /* Returns NULL if GC_invoke_finalizers() should not be called by the */
  837. /* collector (to minimize the risk of a deep finalizers recursion), */
  838. /* otherwise returns a pointer to GC_finalizer_nested. */
  839. STATIC unsigned char *GC_check_finalizer_nested(void)
  840. {
  841. unsigned nesting_level = *(unsigned char *)&GC_finalizer_nested;
  842. if (nesting_level) {
  843. /* We are inside another GC_invoke_finalizers(). */
  844. /* Skip some implicitly-called GC_invoke_finalizers() */
  845. /* depending on the nesting (recursion) level. */
  846. if (++GC_finalizer_skipped < (1U << nesting_level)) return NULL;
  847. GC_finalizer_skipped = 0;
  848. }
  849. *(char *)&GC_finalizer_nested = (char)(nesting_level + 1);
  850. return (unsigned char *)&GC_finalizer_nested;
  851. }
  852. #endif /* THREADS */
  853. #define ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr_dl, prev_dl) \
  854. { \
  855. size_t i; \
  856. size_t dl_size = dl_hashtbl->log_size == -1 ? 0 : \
  857. (size_t)1 << dl_hashtbl->log_size; \
  858. GC_bool needs_barrier = FALSE; \
  859. GC_ASSERT(I_HOLD_LOCK()); \
  860. for (i = 0; i < dl_size; i++) { \
  861. struct disappearing_link *prev_dl = NULL; \
  862. curr_dl = dl_hashtbl -> head[i]; \
  863. while (curr_dl) {
  864. #define ITERATE_DL_HASHTBL_END(curr_dl, prev_dl) \
  865. prev_dl = curr_dl; \
  866. curr_dl = dl_next(curr_dl); \
  867. } \
  868. } \
  869. if (needs_barrier) \
  870. GC_dirty(dl_hashtbl -> head); /* entire object */ \
  871. }
  872. #define DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr_dl, prev_dl, next_dl) \
  873. { \
  874. next_dl = dl_next(curr_dl); \
  875. if (NULL == prev_dl) { \
  876. dl_hashtbl -> head[i] = next_dl; \
  877. needs_barrier = TRUE; \
  878. } else { \
  879. dl_set_next(prev_dl, next_dl); \
  880. GC_dirty(prev_dl); \
  881. } \
  882. GC_clear_mark_bit(curr_dl); \
  883. dl_hashtbl -> entries--; \
  884. curr_dl = next_dl; \
  885. continue; \
  886. }
  887. GC_INLINE void GC_make_disappearing_links_disappear(
  888. struct dl_hashtbl_s* dl_hashtbl)
  889. {
  890. struct disappearing_link *curr, *next;
  891. ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr, prev)
  892. ptr_t real_ptr = (ptr_t)GC_REVEAL_POINTER(curr->dl_hidden_obj);
  893. ptr_t real_link = (ptr_t)GC_REVEAL_POINTER(curr->dl_hidden_link);
  894. if (!GC_is_marked(real_ptr)) {
  895. *(word *)real_link = 0;
  896. GC_clear_mark_bit(curr);
  897. DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr, prev, next);
  898. }
  899. ITERATE_DL_HASHTBL_END(curr, prev)
  900. }
  901. GC_INLINE void GC_remove_dangling_disappearing_links(
  902. struct dl_hashtbl_s* dl_hashtbl)
  903. {
  904. struct disappearing_link *curr, *next;
  905. ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr, prev)
  906. ptr_t real_link =
  907. (ptr_t)GC_base(GC_REVEAL_POINTER(curr->dl_hidden_link));
  908. if (NULL != real_link && !GC_is_marked(real_link)) {
  909. GC_clear_mark_bit(curr);
  910. DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr, prev, next);
  911. }
  912. ITERATE_DL_HASHTBL_END(curr, prev)
  913. }
  914. /* Called with held lock (but the world is running). */
  915. /* Cause disappearing links to disappear and unreachable objects to be */
  916. /* enqueued for finalization. */
  917. GC_INNER void GC_finalize(void)
  918. {
  919. struct finalizable_object * curr_fo, * prev_fo, * next_fo;
  920. ptr_t real_ptr;
  921. size_t i;
  922. size_t fo_size = log_fo_table_size == -1 ? 0 :
  923. (size_t)1 << log_fo_table_size;
  924. GC_bool needs_barrier = FALSE;
  925. GC_ASSERT(I_HOLD_LOCK());
  926. # ifndef SMALL_CONFIG
  927. /* Save current GC_[dl/ll]_entries value for stats printing */
  928. GC_old_dl_entries = GC_dl_hashtbl.entries;
  929. # ifndef GC_LONG_REFS_NOT_NEEDED
  930. GC_old_ll_entries = GC_ll_hashtbl.entries;
  931. # endif
  932. # endif
  933. # ifndef GC_TOGGLE_REFS_NOT_NEEDED
  934. GC_mark_togglerefs();
  935. # endif
  936. GC_make_disappearing_links_disappear(&GC_dl_hashtbl);
  937. /* Mark all objects reachable via chains of 1 or more pointers */
  938. /* from finalizable objects. */
  939. GC_ASSERT(GC_mark_state == MS_NONE);
  940. for (i = 0; i < fo_size; i++) {
  941. for (curr_fo = GC_fnlz_roots.fo_head[i];
  942. curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
  943. GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
  944. real_ptr = (ptr_t)GC_REVEAL_POINTER(curr_fo->fo_hidden_base);
  945. if (!GC_is_marked(real_ptr)) {
  946. GC_MARKED_FOR_FINALIZATION(real_ptr);
  947. GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
  948. if (GC_is_marked(real_ptr)) {
  949. WARN("Finalization cycle involving %p\n", real_ptr);
  950. }
  951. }
  952. }
  953. }
  954. /* Enqueue for finalization all objects that are still */
  955. /* unreachable. */
  956. GC_bytes_finalized = 0;
  957. for (i = 0; i < fo_size; i++) {
  958. curr_fo = GC_fnlz_roots.fo_head[i];
  959. prev_fo = 0;
  960. while (curr_fo != 0) {
  961. real_ptr = (ptr_t)GC_REVEAL_POINTER(curr_fo->fo_hidden_base);
  962. if (!GC_is_marked(real_ptr)) {
  963. if (!GC_java_finalization) {
  964. GC_set_mark_bit(real_ptr);
  965. }
  966. /* Delete from hash table */
  967. next_fo = fo_next(curr_fo);
  968. if (NULL == prev_fo) {
  969. GC_fnlz_roots.fo_head[i] = next_fo;
  970. needs_barrier = TRUE;
  971. } else {
  972. fo_set_next(prev_fo, next_fo);
  973. GC_dirty(prev_fo);
  974. }
  975. GC_fo_entries--;
  976. if (GC_object_finalized_proc)
  977. GC_object_finalized_proc(real_ptr);
  978. /* Add to list of objects awaiting finalization. */
  979. fo_set_next(curr_fo, GC_fnlz_roots.finalize_now);
  980. GC_dirty(curr_fo);
  981. SET_FINALIZE_NOW(curr_fo);
  982. /* unhide object pointer so any future collections will */
  983. /* see it. */
  984. curr_fo -> fo_hidden_base =
  985. (word)GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
  986. GC_bytes_finalized +=
  987. curr_fo -> fo_object_size
  988. + sizeof(struct finalizable_object);
  989. GC_ASSERT(GC_is_marked(GC_base(curr_fo)));
  990. curr_fo = next_fo;
  991. } else {
  992. prev_fo = curr_fo;
  993. curr_fo = fo_next(curr_fo);
  994. }
  995. }
  996. }
  997. if (GC_java_finalization) {
  998. /* make sure we mark everything reachable from objects finalized
  999. using the no_order mark_proc */
  1000. for (curr_fo = GC_fnlz_roots.finalize_now;
  1001. curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
  1002. real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
  1003. if (!GC_is_marked(real_ptr)) {
  1004. if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
  1005. GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
  1006. }
  1007. if (curr_fo -> fo_mark_proc != GC_unreachable_finalize_mark_proc) {
  1008. GC_set_mark_bit(real_ptr);
  1009. }
  1010. }
  1011. }
  1012. /* now revive finalize-when-unreachable objects reachable from
  1013. other finalizable objects */
  1014. if (need_unreachable_finalization) {
  1015. curr_fo = GC_fnlz_roots.finalize_now;
  1016. # if defined(GC_ASSERTIONS) || defined(LINT2)
  1017. if (curr_fo != NULL && log_fo_table_size < 0)
  1018. ABORT("log_size is negative");
  1019. # endif
  1020. prev_fo = NULL;
  1021. while (curr_fo != NULL) {
  1022. next_fo = fo_next(curr_fo);
  1023. if (curr_fo -> fo_mark_proc == GC_unreachable_finalize_mark_proc) {
  1024. real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
  1025. if (!GC_is_marked(real_ptr)) {
  1026. GC_set_mark_bit(real_ptr);
  1027. } else {
  1028. if (NULL == prev_fo) {
  1029. SET_FINALIZE_NOW(next_fo);
  1030. } else {
  1031. fo_set_next(prev_fo, next_fo);
  1032. GC_dirty(prev_fo);
  1033. }
  1034. curr_fo -> fo_hidden_base =
  1035. GC_HIDE_POINTER(curr_fo -> fo_hidden_base);
  1036. GC_bytes_finalized -=
  1037. curr_fo->fo_object_size + sizeof(struct finalizable_object);
  1038. i = HASH2(real_ptr, log_fo_table_size);
  1039. fo_set_next(curr_fo, GC_fnlz_roots.fo_head[i]);
  1040. GC_dirty(curr_fo);
  1041. GC_fo_entries++;
  1042. GC_fnlz_roots.fo_head[i] = curr_fo;
  1043. curr_fo = prev_fo;
  1044. needs_barrier = TRUE;
  1045. }
  1046. }
  1047. prev_fo = curr_fo;
  1048. curr_fo = next_fo;
  1049. }
  1050. }
  1051. }
  1052. if (needs_barrier)
  1053. GC_dirty(GC_fnlz_roots.fo_head); /* entire object */
  1054. GC_remove_dangling_disappearing_links(&GC_dl_hashtbl);
  1055. # ifndef GC_TOGGLE_REFS_NOT_NEEDED
  1056. GC_clear_togglerefs();
  1057. # endif
  1058. # ifndef GC_LONG_REFS_NOT_NEEDED
  1059. GC_make_disappearing_links_disappear(&GC_ll_hashtbl);
  1060. GC_remove_dangling_disappearing_links(&GC_ll_hashtbl);
  1061. # endif
  1062. if (GC_fail_count) {
  1063. /* Don't prevent running finalizers if there has been an allocation */
  1064. /* failure recently. */
  1065. # ifdef THREADS
  1066. GC_reset_finalizer_nested();
  1067. # else
  1068. GC_finalizer_nested = 0;
  1069. # endif
  1070. }
  1071. }
  1072. #ifndef JAVA_FINALIZATION_NOT_NEEDED
  1073. /* Enqueue all remaining finalizers to be run. */
  1074. STATIC void GC_enqueue_all_finalizers(void)
  1075. {
  1076. struct finalizable_object * next_fo;
  1077. int i;
  1078. int fo_size;
  1079. GC_ASSERT(I_HOLD_LOCK());
  1080. fo_size = log_fo_table_size == -1 ? 0 : 1 << log_fo_table_size;
  1081. GC_bytes_finalized = 0;
  1082. for (i = 0; i < fo_size; i++) {
  1083. struct finalizable_object * curr_fo = GC_fnlz_roots.fo_head[i];
  1084. GC_fnlz_roots.fo_head[i] = NULL;
  1085. while (curr_fo != NULL) {
  1086. ptr_t real_ptr = (ptr_t)GC_REVEAL_POINTER(curr_fo->fo_hidden_base);
  1087. GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
  1088. GC_set_mark_bit(real_ptr);
  1089. next_fo = fo_next(curr_fo);
  1090. /* Add to list of objects awaiting finalization. */
  1091. fo_set_next(curr_fo, GC_fnlz_roots.finalize_now);
  1092. GC_dirty(curr_fo);
  1093. SET_FINALIZE_NOW(curr_fo);
  1094. /* unhide object pointer so any future collections will */
  1095. /* see it. */
  1096. curr_fo -> fo_hidden_base =
  1097. (word)GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
  1098. GC_bytes_finalized +=
  1099. curr_fo -> fo_object_size + sizeof(struct finalizable_object);
  1100. curr_fo = next_fo;
  1101. }
  1102. }
  1103. GC_fo_entries = 0; /* all entries deleted from the hash table */
  1104. }
  1105. /* Invoke all remaining finalizers that haven't yet been run.
  1106. * This is needed for strict compliance with the Java standard,
  1107. * which can make the runtime guarantee that all finalizers are run.
  1108. * Unfortunately, the Java standard implies we have to keep running
  1109. * finalizers until there are no more left, a potential infinite loop.
  1110. * YUCK.
  1111. * Note that this is even more dangerous than the usual Java
  1112. * finalizers, in that objects reachable from static variables
  1113. * may have been finalized when these finalizers are run.
  1114. * Finalizers run at this point must be prepared to deal with a
  1115. * mostly broken world.
  1116. * This routine is externally callable, so is called without
  1117. * the allocation lock.
  1118. */
  1119. GC_API void GC_CALL GC_finalize_all(void)
  1120. {
  1121. DCL_LOCK_STATE;
  1122. LOCK();
  1123. while (GC_fo_entries > 0) {
  1124. GC_enqueue_all_finalizers();
  1125. UNLOCK();
  1126. GC_invoke_finalizers();
  1127. /* Running the finalizers in this thread is arguably not a good */
  1128. /* idea when we should be notifying another thread to run them. */
  1129. /* But otherwise we don't have a great way to wait for them to */
  1130. /* run. */
  1131. LOCK();
  1132. }
  1133. UNLOCK();
  1134. }
  1135. #endif /* !JAVA_FINALIZATION_NOT_NEEDED */
  1136. static volatile GC_bool GC_interrupt_finalizers = FALSE;
  1137. void
  1138. GC_set_interrupt_finalizers(void)
  1139. {
  1140. GC_interrupt_finalizers = TRUE;
  1141. }
  1142. /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
  1143. /* finalizers can only be called from some kind of "safe state" and */
  1144. /* getting into that safe state is expensive.) */
  1145. GC_API int GC_CALL GC_should_invoke_finalizers(void)
  1146. {
  1147. # ifdef AO_HAVE_load
  1148. return AO_load((volatile AO_t *)&GC_fnlz_roots.finalize_now) != 0;
  1149. # else
  1150. return GC_fnlz_roots.finalize_now != NULL;
  1151. # endif /* !THREADS */
  1152. }
  1153. /* Invoke finalizers for all objects that are ready to be finalized. */
  1154. /* Should be called without allocation lock. */
  1155. GC_API int GC_CALL GC_invoke_finalizers(void)
  1156. {
  1157. int count = 0;
  1158. word bytes_freed_before = 0; /* initialized to prevent warning. */
  1159. DCL_LOCK_STATE;
  1160. while (GC_should_invoke_finalizers() && !GC_interrupt_finalizers) {
  1161. struct finalizable_object * curr_fo;
  1162. # ifdef THREADS
  1163. LOCK();
  1164. # endif
  1165. if (count == 0) {
  1166. bytes_freed_before = GC_bytes_freed;
  1167. /* Don't do this outside, since we need the lock. */
  1168. }
  1169. curr_fo = GC_fnlz_roots.finalize_now;
  1170. # ifdef THREADS
  1171. if (curr_fo != NULL)
  1172. SET_FINALIZE_NOW(fo_next(curr_fo));
  1173. UNLOCK();
  1174. if (curr_fo == 0) break;
  1175. # else
  1176. GC_fnlz_roots.finalize_now = fo_next(curr_fo);
  1177. # endif
  1178. fo_set_next(curr_fo, 0);
  1179. (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
  1180. curr_fo -> fo_client_data);
  1181. curr_fo -> fo_client_data = 0;
  1182. ++count;
  1183. /* Explicit freeing of curr_fo is probably a bad idea. */
  1184. /* It throws off accounting if nearly all objects are */
  1185. /* finalizable. Otherwise it should not matter. */
  1186. }
  1187. /* bytes_freed_before is initialized whenever count != 0 */
  1188. if (count != 0
  1189. # if defined(THREADS) && !defined(THREAD_SANITIZER)
  1190. /* A quick check whether some memory was freed. */
  1191. /* The race with GC_free() is safe to be ignored */
  1192. /* because we only need to know if the current */
  1193. /* thread has deallocated something. */
  1194. && bytes_freed_before != GC_bytes_freed
  1195. # endif
  1196. ) {
  1197. LOCK();
  1198. GC_finalizer_bytes_freed += (GC_bytes_freed - bytes_freed_before);
  1199. UNLOCK();
  1200. }
  1201. return count;
  1202. }
  1203. static word last_finalizer_notification = 0;
  1204. GC_INNER void GC_notify_or_invoke_finalizers(void)
  1205. {
  1206. GC_finalizer_notifier_proc notifier_fn = 0;
  1207. # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
  1208. static word last_back_trace_gc_no = 1; /* Skip first one. */
  1209. # endif
  1210. DCL_LOCK_STATE;
  1211. # if defined(THREADS) && !defined(KEEP_BACK_PTRS) \
  1212. && !defined(MAKE_BACK_GRAPH)
  1213. /* Quick check (while unlocked) for an empty finalization queue. */
  1214. if (!GC_should_invoke_finalizers())
  1215. return;
  1216. # endif
  1217. LOCK();
  1218. /* This is a convenient place to generate backtraces if appropriate, */
  1219. /* since that code is not callable with the allocation lock. */
  1220. # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
  1221. if (GC_gc_no > last_back_trace_gc_no) {
  1222. # ifdef KEEP_BACK_PTRS
  1223. long i;
  1224. /* Stops when GC_gc_no wraps; that's OK. */
  1225. last_back_trace_gc_no = (word)(-1); /* disable others. */
  1226. for (i = 0; i < GC_backtraces; ++i) {
  1227. /* FIXME: This tolerates concurrent heap mutation, */
  1228. /* which may cause occasional mysterious results. */
  1229. /* We need to release the GC lock, since GC_print_callers */
  1230. /* acquires it. It probably shouldn't. */
  1231. UNLOCK();
  1232. GC_generate_random_backtrace_no_gc();
  1233. LOCK();
  1234. }
  1235. last_back_trace_gc_no = GC_gc_no;
  1236. # endif
  1237. # ifdef MAKE_BACK_GRAPH
  1238. if (GC_print_back_height) {
  1239. GC_print_back_graph_stats();
  1240. }
  1241. # endif
  1242. }
  1243. # endif
  1244. if (NULL == GC_fnlz_roots.finalize_now) {
  1245. UNLOCK();
  1246. return;
  1247. }
  1248. if (!GC_finalize_on_demand) {
  1249. unsigned char *pnested = GC_check_finalizer_nested();
  1250. UNLOCK();
  1251. /* Skip GC_invoke_finalizers() if nested */
  1252. if (pnested != NULL) {
  1253. (void) GC_invoke_finalizers();
  1254. *pnested = 0; /* Reset since no more finalizers. */
  1255. # ifndef THREADS
  1256. GC_ASSERT(NULL == GC_fnlz_roots.finalize_now);
  1257. # endif /* Otherwise GC can run concurrently and add more */
  1258. }
  1259. return;
  1260. }
  1261. /* These variables require synchronization to avoid data races. */
  1262. if (last_finalizer_notification != GC_gc_no) {
  1263. last_finalizer_notification = GC_gc_no;
  1264. notifier_fn = GC_finalizer_notifier;
  1265. }
  1266. UNLOCK();
  1267. if (notifier_fn != 0)
  1268. (*notifier_fn)(); /* Invoke the notifier */
  1269. }
  1270. #ifndef SMALL_CONFIG
  1271. # ifndef GC_LONG_REFS_NOT_NEEDED
  1272. # define IF_LONG_REFS_PRESENT_ELSE(x,y) (x)
  1273. # else
  1274. # define IF_LONG_REFS_PRESENT_ELSE(x,y) (y)
  1275. # endif
  1276. GC_INNER void GC_print_finalization_stats(void)
  1277. {
  1278. struct finalizable_object *fo;
  1279. unsigned long ready = 0;
  1280. GC_log_printf("%lu finalization entries;"
  1281. " %lu/%lu short/long disappearing links alive\n",
  1282. (unsigned long)GC_fo_entries,
  1283. (unsigned long)GC_dl_hashtbl.entries,
  1284. (unsigned long)IF_LONG_REFS_PRESENT_ELSE(
  1285. GC_ll_hashtbl.entries, 0));
  1286. for (fo = GC_fnlz_roots.finalize_now; fo != NULL; fo = fo_next(fo))
  1287. ++ready;
  1288. GC_log_printf("%lu finalization-ready objects;"
  1289. " %ld/%ld short/long links cleared\n",
  1290. ready,
  1291. (long)GC_old_dl_entries - (long)GC_dl_hashtbl.entries,
  1292. (long)IF_LONG_REFS_PRESENT_ELSE(
  1293. GC_old_ll_entries - GC_ll_hashtbl.entries, 0));
  1294. }
  1295. #endif /* !SMALL_CONFIG */
  1296. #endif /* !GC_NO_FINALIZATION */