finalize.c 49 KB

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