finalize.c 49 KB

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