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
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
17 #include "private/gc_pmark.h"
19 #ifndef GC_NO_FINALIZATION
21 /* Type of mark procedure used for marking from finalizable object. */
22 /* This procedure normally does not mark the object, only its */
24 typedef void (* finalization_mark_proc)(ptr_t /* finalizable_obj_ptr */);
26 #define HASH3(addr,size,log_size) \
27 ((((word)(addr) >> 3) ^ ((word)(addr) >> (3 + (log_size)))) \
29 #define HASH2(addr,log_size) HASH3(addr, 1 << (log_size), log_size)
31 struct hash_chain_entry {
33 struct hash_chain_entry * next;
36 struct disappearing_link {
37 struct hash_chain_entry prolog;
38 # define dl_hidden_link prolog.hidden_key
39 /* Field to be cleared. */
40 # define dl_next(x) (struct disappearing_link *)((x) -> prolog.next)
41 # define dl_set_next(x, y) \
42 (void)((x)->prolog.next = (struct hash_chain_entry *)(y))
43 word dl_hidden_obj; /* Pointer to object base */
47 struct disappearing_link **head;
52 STATIC struct dl_hashtbl_s GC_dl_hashtbl = {
53 /* head */ NULL, /* log_size */ -1, /* entries */ 0 };
54 #ifndef GC_LONG_REFS_NOT_NEEDED
55 STATIC struct dl_hashtbl_s GC_ll_hashtbl = { NULL, -1, 0 };
58 struct finalizable_object {
59 struct hash_chain_entry prolog;
60 # define fo_hidden_base prolog.hidden_key
61 /* Pointer to object base. */
62 /* No longer hidden once object */
63 /* is on finalize_now queue. */
64 # define fo_next(x) (struct finalizable_object *)((x) -> prolog.next)
65 # define fo_set_next(x,y) ((x)->prolog.next = (struct hash_chain_entry *)(y))
66 GC_finalization_proc fo_fn; /* Finalizer. */
68 word fo_object_size; /* In bytes. */
69 finalization_mark_proc fo_mark_proc; /* Mark-through procedure */
72 static signed_word log_fo_table_size = -1;
75 struct finalizable_object **fo_head;
76 /* List of objects that should be finalized now: */
77 struct finalizable_object *finalize_now;
78 } GC_fnlz_roots = { NULL, NULL };
80 GC_API void GC_CALL GC_push_finalizer_structures(void)
82 GC_ASSERT((word)&GC_dl_hashtbl.head % sizeof(word) == 0);
83 GC_ASSERT((word)&GC_fnlz_roots % sizeof(word) == 0);
84 # ifndef GC_LONG_REFS_NOT_NEEDED
85 GC_ASSERT((word)&GC_ll_hashtbl.head % sizeof(word) == 0);
86 GC_PUSH_ALL_SYM(GC_ll_hashtbl.head);
88 GC_PUSH_ALL_SYM(GC_dl_hashtbl.head);
89 GC_PUSH_ALL_SYM(GC_fnlz_roots);
92 /* Double the size of a hash table. *size_ptr is the log of its current */
93 /* 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)
100 register 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: (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 = (struct hash_chain_entry **)
107 GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
108 (size_t)new_size * sizeof(struct hash_chain_entry *), NORMAL);
110 if (new_table == 0) {
112 ABORT("Insufficient space for initial table allocation");
117 for (i = 0; i < old_size; i++) {
120 ptr_t real_key = GC_REVEAL_POINTER(p -> hidden_key);
121 struct hash_chain_entry *next = p -> next;
122 size_t new_hash = HASH3(real_key, new_size, log_new_size);
124 p -> next = new_table[new_hash];
125 new_table[new_hash] = p;
129 *log_size_ptr = log_new_size;
133 GC_API int GC_CALL GC_register_disappearing_link(void * * link)
137 base = (ptr_t)GC_base(link);
139 ABORT("Bad arg to GC_register_disappearing_link");
140 return(GC_general_register_disappearing_link(link, base));
143 STATIC int GC_register_disappearing_link_inner(
144 struct dl_hashtbl_s *dl_hashtbl, void **link,
145 const void *obj, const char *tbl_log_name)
147 struct disappearing_link *curr_dl;
149 struct disappearing_link * new_dl;
153 GC_ASSERT(obj != NULL && GC_base_C(obj) == obj);
154 if (dl_hashtbl -> log_size == -1
155 || dl_hashtbl -> entries > ((word)1 << dl_hashtbl -> log_size)) {
156 GC_grow_table((struct hash_chain_entry ***)&dl_hashtbl -> head,
157 &dl_hashtbl -> log_size);
158 GC_COND_LOG_PRINTF("Grew %s table to %u entries\n", tbl_log_name,
159 1 << (unsigned)dl_hashtbl -> log_size);
161 index = HASH2(link, dl_hashtbl -> log_size);
162 for (curr_dl = dl_hashtbl -> head[index]; curr_dl != 0;
163 curr_dl = dl_next(curr_dl)) {
164 if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
165 curr_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
170 new_dl = (struct disappearing_link *)
171 GC_INTERNAL_MALLOC(sizeof(struct disappearing_link),NORMAL);
173 GC_oom_func oom_fn = GC_oom_fn;
175 new_dl = (struct disappearing_link *)
176 (*oom_fn)(sizeof(struct disappearing_link));
180 /* It's not likely we'll make it here, but ... */
182 /* Recalculate index since the table may grow. */
183 index = HASH2(link, dl_hashtbl -> log_size);
184 /* Check again that our disappearing link not in the table. */
185 for (curr_dl = dl_hashtbl -> head[index]; curr_dl != 0;
186 curr_dl = dl_next(curr_dl)) {
187 if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
188 curr_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
190 # ifndef DBG_HDRS_ALL
191 /* Free unused new_dl returned by GC_oom_fn() */
192 GC_free((void *)new_dl);
198 new_dl -> dl_hidden_obj = GC_HIDE_POINTER(obj);
199 new_dl -> dl_hidden_link = GC_HIDE_POINTER(link);
200 dl_set_next(new_dl, dl_hashtbl -> head[index]);
201 dl_hashtbl -> head[index] = new_dl;
202 dl_hashtbl -> entries++;
207 GC_API int GC_CALL GC_general_register_disappearing_link(void * * link,
210 if (((word)link & (ALIGNMENT-1)) != 0 || !NONNULL_ARG_NOT_NULL(link))
211 ABORT("Bad arg to GC_general_register_disappearing_link");
212 return GC_register_disappearing_link_inner(&GC_dl_hashtbl, link, obj,
217 # define FREE_DL_ENTRY(curr_dl) dl_set_next(curr_dl, NULL)
219 # define FREE_DL_ENTRY(curr_dl) GC_free(curr_dl)
222 /* Unregisters given link and returns the link entry to free. */
223 /* Assume the lock is held. */
224 GC_INLINE struct disappearing_link *GC_unregister_disappearing_link_inner(
225 struct dl_hashtbl_s *dl_hashtbl, void **link)
227 struct disappearing_link *curr_dl;
228 struct disappearing_link *prev_dl = NULL;
229 size_t index = HASH2(link, dl_hashtbl->log_size);
231 for (curr_dl = dl_hashtbl -> head[index]; curr_dl;
232 curr_dl = dl_next(curr_dl)) {
233 if (curr_dl -> dl_hidden_link == GC_HIDE_POINTER(link)) {
234 /* Remove found entry from the table. */
235 if (NULL == prev_dl) {
236 dl_hashtbl -> head[index] = dl_next(curr_dl);
238 dl_set_next(prev_dl, dl_next(curr_dl));
240 dl_hashtbl -> entries--;
248 GC_API int GC_CALL GC_unregister_disappearing_link(void * * link)
250 struct disappearing_link *curr_dl;
253 if (((word)link & (ALIGNMENT-1)) != 0) return(0); /* Nothing to do. */
256 curr_dl = GC_unregister_disappearing_link_inner(&GC_dl_hashtbl, link);
258 if (NULL == curr_dl) return 0;
259 FREE_DL_ENTRY(curr_dl);
263 /* Toggle-ref support. */
264 #ifndef GC_TOGGLE_REFS_NOT_NEEDED
266 /* Lowest bit is used to distinguish between choices. */
268 GC_hidden_pointer weak_ref;
271 STATIC GC_toggleref_func GC_toggleref_callback = 0;
272 STATIC GCToggleRef *GC_toggleref_arr = NULL;
273 STATIC int GC_toggleref_array_size = 0;
274 STATIC int GC_toggleref_array_capacity = 0;
276 GC_INNER void GC_process_togglerefs(void)
281 GC_ASSERT(I_HOLD_LOCK());
282 for (i = 0; i < GC_toggleref_array_size; ++i) {
283 GCToggleRef r = GC_toggleref_arr[i];
284 void *obj = r.strong_ref;
286 if (((word)obj & 1) != 0) {
287 obj = GC_REVEAL_POINTER(r.weak_ref);
292 switch (GC_toggleref_callback(obj)) {
293 case GC_TOGGLE_REF_DROP:
295 case GC_TOGGLE_REF_STRONG:
296 GC_toggleref_arr[new_size++].strong_ref = obj;
298 case GC_TOGGLE_REF_WEAK:
299 GC_toggleref_arr[new_size++].weak_ref = GC_HIDE_POINTER(obj);
302 ABORT("Bad toggle-ref status returned by callback");
306 if (new_size < GC_toggleref_array_size) {
307 BZERO(&GC_toggleref_arr[new_size],
308 (GC_toggleref_array_size - new_size) * sizeof(GCToggleRef));
309 GC_toggleref_array_size = new_size;
313 STATIC void GC_normal_finalize_mark_proc(ptr_t);
315 static void push_and_mark_object(void *p)
317 GC_normal_finalize_mark_proc(p);
318 while (!GC_mark_stack_empty()) {
319 MARK_FROM_MARK_STACK();
322 if (GC_mark_state != MS_NONE) {
323 while (!GC_mark_some(0)) {
329 STATIC void GC_mark_togglerefs(void)
332 if (NULL == GC_toggleref_arr)
335 /* TODO: Hide GC_toggleref_arr to avoid its marking from roots. */
336 GC_set_mark_bit(GC_toggleref_arr);
337 for (i = 0; i < GC_toggleref_array_size; ++i) {
338 void *obj = GC_toggleref_arr[i].strong_ref;
339 if (obj != NULL && ((word)obj & 1) == 0) {
340 push_and_mark_object(obj);
345 STATIC void GC_clear_togglerefs(void)
348 for (i = 0; i < GC_toggleref_array_size; ++i) {
349 if ((GC_toggleref_arr[i].weak_ref & 1) != 0) {
350 if (!GC_is_marked(GC_REVEAL_POINTER(GC_toggleref_arr[i].weak_ref))) {
351 GC_toggleref_arr[i].weak_ref = 0;
353 /* No need to copy, BDWGC is a non-moving collector. */
359 GC_API void GC_CALL GC_set_toggleref_func(GC_toggleref_func fn)
364 GC_toggleref_callback = fn;
368 GC_API GC_toggleref_func GC_CALL GC_get_toggleref_func(void)
370 GC_toggleref_func fn;
374 fn = GC_toggleref_callback;
379 static GC_bool ensure_toggleref_capacity(int capacity_inc)
381 GC_ASSERT(capacity_inc >= 0);
382 if (NULL == GC_toggleref_arr) {
383 GC_toggleref_array_capacity = 32; /* initial capacity */
384 GC_toggleref_arr = GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
385 GC_toggleref_array_capacity * sizeof(GCToggleRef),
387 if (NULL == GC_toggleref_arr)
390 if ((unsigned)GC_toggleref_array_size + (unsigned)capacity_inc
391 >= (unsigned)GC_toggleref_array_capacity) {
392 GCToggleRef *new_array;
393 while ((unsigned)GC_toggleref_array_capacity
394 < (unsigned)GC_toggleref_array_size + (unsigned)capacity_inc) {
395 GC_toggleref_array_capacity *= 2;
396 if (GC_toggleref_array_capacity < 0) /* overflow */
400 new_array = GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
401 GC_toggleref_array_capacity * sizeof(GCToggleRef),
403 if (NULL == new_array)
405 BCOPY(GC_toggleref_arr, new_array,
406 GC_toggleref_array_size * sizeof(GCToggleRef));
407 GC_INTERNAL_FREE(GC_toggleref_arr);
408 GC_toggleref_arr = new_array;
413 GC_API int GC_CALL GC_toggleref_add(void *obj, int is_strong_ref)
415 int res = GC_SUCCESS;
418 GC_ASSERT(obj != NULL);
420 if (GC_toggleref_callback != 0) {
421 if (!ensure_toggleref_capacity(1)) {
424 GC_toggleref_arr[GC_toggleref_array_size++].strong_ref =
425 is_strong_ref ? obj : (void *)GC_HIDE_POINTER(obj);
431 #endif /* !GC_TOGGLE_REFS_NOT_NEEDED */
433 /* Finalizer callback support. */
434 STATIC GC_await_finalize_proc GC_object_finalized_proc = 0;
436 GC_API void GC_CALL GC_set_await_finalize_proc(GC_await_finalize_proc fn)
441 GC_object_finalized_proc = fn;
445 GC_API GC_await_finalize_proc GC_CALL GC_get_await_finalize_proc(void)
447 GC_await_finalize_proc fn;
451 fn = GC_object_finalized_proc;
456 #ifndef GC_LONG_REFS_NOT_NEEDED
457 GC_API int GC_CALL GC_register_long_link(void * * link, const void * obj)
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,
465 GC_API int GC_CALL GC_unregister_long_link(void * * link)
467 struct disappearing_link *curr_dl;
470 if (((word)link & (ALIGNMENT-1)) != 0) return(0); /* Nothing to do. */
473 curr_dl = GC_unregister_disappearing_link_inner(&GC_ll_hashtbl, link);
475 if (NULL == curr_dl) return 0;
476 FREE_DL_ENTRY(curr_dl);
479 #endif /* !GC_LONG_REFS_NOT_NEEDED */
481 #ifndef GC_MOVE_DISAPPEARING_LINK_NOT_NEEDED
482 /* Moves a link. Assume the lock is held. */
483 STATIC int GC_move_disappearing_link_inner(
484 struct dl_hashtbl_s *dl_hashtbl,
485 void **link, void **new_link)
487 struct disappearing_link *curr_dl, *prev_dl, *new_dl;
488 size_t curr_index, new_index;
489 word curr_hidden_link;
490 word new_hidden_link;
492 /* Find current link. */
493 curr_index = HASH2(link, dl_hashtbl -> log_size);
494 curr_hidden_link = GC_HIDE_POINTER(link);
496 for (curr_dl = dl_hashtbl -> head[curr_index]; curr_dl;
497 curr_dl = dl_next(curr_dl)) {
498 if (curr_dl -> dl_hidden_link == curr_hidden_link)
503 if (NULL == curr_dl) {
507 if (link == new_link) {
508 return GC_SUCCESS; /* Nothing to do. */
511 /* link found; now check new_link not present. */
512 new_index = HASH2(new_link, dl_hashtbl -> log_size);
513 new_hidden_link = GC_HIDE_POINTER(new_link);
514 for (new_dl = dl_hashtbl -> head[new_index]; new_dl;
515 new_dl = dl_next(new_dl)) {
516 if (new_dl -> dl_hidden_link == new_hidden_link) {
517 /* Target already registered; bail. */
522 /* Remove from old, add to new, update link. */
523 if (NULL == prev_dl) {
524 dl_hashtbl -> head[curr_index] = dl_next(curr_dl);
526 dl_set_next(prev_dl, dl_next(curr_dl));
528 curr_dl -> dl_hidden_link = new_hidden_link;
529 dl_set_next(curr_dl, dl_hashtbl -> head[new_index]);
530 dl_hashtbl -> head[new_index] = curr_dl;
534 GC_API int GC_CALL GC_move_disappearing_link(void **link, void **new_link)
539 if (((word)new_link & (ALIGNMENT-1)) != 0
540 || !NONNULL_ARG_NOT_NULL(new_link))
541 ABORT("Bad new_link arg to GC_move_disappearing_link");
542 if (((word)link & (ALIGNMENT-1)) != 0)
543 return GC_NOT_FOUND; /* Nothing to do. */
546 result = GC_move_disappearing_link_inner(&GC_dl_hashtbl, link, new_link);
551 # ifndef GC_LONG_REFS_NOT_NEEDED
552 GC_API int GC_CALL GC_move_long_link(void **link, void **new_link)
557 if (((word)new_link & (ALIGNMENT-1)) != 0
558 || !NONNULL_ARG_NOT_NULL(new_link))
559 ABORT("Bad new_link arg to GC_move_disappearing_link");
560 if (((word)link & (ALIGNMENT-1)) != 0)
561 return GC_NOT_FOUND; /* Nothing to do. */
564 result = GC_move_disappearing_link_inner(&GC_ll_hashtbl, link, new_link);
568 # endif /* !GC_LONG_REFS_NOT_NEEDED */
569 #endif /* !GC_MOVE_DISAPPEARING_LINK_NOT_NEEDED */
571 /* Possible finalization_marker procedures. Note that mark stack */
572 /* overflow is handled by the caller, and is not a disaster. */
573 STATIC void GC_normal_finalize_mark_proc(ptr_t p)
577 PUSH_OBJ(p, hhdr, GC_mark_stack_top,
578 &(GC_mark_stack[GC_mark_stack_size]));
581 /* This only pays very partial attention to the mark descriptor. */
582 /* It does the right thing for normal and atomic objects, and treats */
583 /* most others as normal. */
584 STATIC void GC_ignore_self_finalize_mark_proc(ptr_t p)
587 word descr = hhdr -> hb_descr;
591 ptr_t target_limit = p + hhdr -> hb_sz - 1;
593 if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
594 scan_limit = p + descr - sizeof(word);
596 scan_limit = target_limit + 1 - sizeof(word);
598 for (q = p; (word)q <= (word)scan_limit; q += ALIGNMENT) {
600 if (r < (word)p || r > (word)target_limit) {
601 GC_PUSH_ONE_HEAP(r, q, GC_mark_stack_top);
606 STATIC void GC_null_finalize_mark_proc(ptr_t p GC_ATTR_UNUSED) {}
608 /* Possible finalization_marker procedures. Note that mark stack */
609 /* overflow is handled by the caller, and is not a disaster. */
611 /* GC_unreachable_finalize_mark_proc is an alias for normal marking, */
612 /* but it is explicitly tested for, and triggers different */
613 /* behavior. Objects registered in this way are not finalized */
614 /* if they are reachable by other finalizable objects, even if those */
615 /* other objects specify no ordering. */
616 STATIC void GC_unreachable_finalize_mark_proc(ptr_t p)
618 GC_normal_finalize_mark_proc(p);
621 /* Register a finalization function. See gc.h for details. */
622 /* The last parameter is a procedure that determines */
623 /* marking for finalization ordering. Any objects marked */
624 /* by that procedure will be guaranteed to not have been */
625 /* finalized when this finalizer is invoked. */
626 STATIC void GC_register_finalizer_inner(void * obj,
627 GC_finalization_proc fn, void *cd,
628 GC_finalization_proc *ofn, void **ocd,
629 finalization_mark_proc mp)
632 struct finalizable_object * curr_fo, * prev_fo;
634 struct finalizable_object *new_fo = 0;
635 hdr *hhdr = NULL; /* initialized to prevent warning. */
640 if (log_fo_table_size == -1
641 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
642 GC_grow_table((struct hash_chain_entry ***)&GC_fnlz_roots.fo_head,
644 GC_COND_LOG_PRINTF("Grew fo table to %u entries\n",
645 1 << (unsigned)log_fo_table_size);
647 /* in the THREADS case we hold allocation lock. */
650 index = HASH2(base, log_fo_table_size);
652 curr_fo = GC_fnlz_roots.fo_head[index];
653 while (curr_fo != 0) {
654 GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
655 if (curr_fo -> fo_hidden_base == GC_HIDE_POINTER(base)) {
656 /* Interruption by a signal in the middle of this */
657 /* should be safe. The client may see only *ocd */
658 /* updated, but we'll declare that to be his problem. */
659 if (ocd) *ocd = (void *) (curr_fo -> fo_client_data);
660 if (ofn) *ofn = curr_fo -> fo_fn;
661 /* Delete the structure for base. */
663 GC_fnlz_roots.fo_head[index] = fo_next(curr_fo);
665 fo_set_next(prev_fo, fo_next(curr_fo));
669 /* May not happen if we get a signal. But a high */
670 /* estimate will only make the table larger than */
672 # if !defined(THREADS) && !defined(DBG_HDRS_ALL)
673 GC_free((void *)curr_fo);
676 curr_fo -> fo_fn = fn;
677 curr_fo -> fo_client_data = (ptr_t)cd;
678 curr_fo -> fo_mark_proc = mp;
679 /* Reinsert it. We deleted it first to maintain */
680 /* consistency in the event of a signal. */
682 GC_fnlz_roots.fo_head[index] = curr_fo;
684 fo_set_next(prev_fo, curr_fo);
688 # ifndef DBG_HDRS_ALL
689 if (EXPECT(new_fo != 0, FALSE)) {
690 /* Free unused new_fo returned by GC_oom_fn() */
691 GC_free((void *)new_fo);
697 curr_fo = fo_next(curr_fo);
699 if (EXPECT(new_fo != 0, FALSE)) {
700 /* new_fo is returned by GC_oom_fn(), so fn != 0 and hhdr != 0. */
710 if (EXPECT(0 == hhdr, FALSE)) {
711 /* We won't collect it, hence finalizer wouldn't be run. */
717 new_fo = (struct finalizable_object *)
718 GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
719 if (EXPECT(new_fo != 0, TRUE))
723 new_fo = (struct finalizable_object *)
724 (*oom_fn)(sizeof(struct finalizable_object));
726 /* No enough memory. *ocd and *ofn remains unchanged. */
729 /* It's not likely we'll make it here, but ... */
731 /* Recalculate index since the table may grow and */
732 /* check again that our finalizer is not in the table. */
734 GC_ASSERT(GC_size(new_fo) >= sizeof(struct finalizable_object));
737 new_fo -> fo_hidden_base = GC_HIDE_POINTER(base);
738 new_fo -> fo_fn = fn;
739 new_fo -> fo_client_data = (ptr_t)cd;
740 new_fo -> fo_object_size = hhdr -> hb_sz;
741 new_fo -> fo_mark_proc = mp;
742 fo_set_next(new_fo, GC_fnlz_roots.fo_head[index]);
744 GC_fnlz_roots.fo_head[index] = new_fo;
748 GC_API void GC_CALL GC_register_finalizer(void * obj,
749 GC_finalization_proc fn, void * cd,
750 GC_finalization_proc *ofn, void ** ocd)
752 GC_register_finalizer_inner(obj, fn, cd, ofn,
753 ocd, GC_normal_finalize_mark_proc);
756 GC_API void GC_CALL GC_register_finalizer_ignore_self(void * obj,
757 GC_finalization_proc fn, void * cd,
758 GC_finalization_proc *ofn, void ** ocd)
760 GC_register_finalizer_inner(obj, fn, cd, ofn,
761 ocd, GC_ignore_self_finalize_mark_proc);
764 GC_API void GC_CALL GC_register_finalizer_no_order(void * obj,
765 GC_finalization_proc fn, void * cd,
766 GC_finalization_proc *ofn, void ** ocd)
768 GC_register_finalizer_inner(obj, fn, cd, ofn,
769 ocd, GC_null_finalize_mark_proc);
772 static GC_bool need_unreachable_finalization = FALSE;
773 /* Avoid the work if this isn't used. */
775 GC_API void GC_CALL GC_register_finalizer_unreachable(void * obj,
776 GC_finalization_proc fn, void * cd,
777 GC_finalization_proc *ofn, void ** ocd)
779 need_unreachable_finalization = TRUE;
780 GC_ASSERT(GC_java_finalization);
781 GC_register_finalizer_inner(obj, fn, cd, ofn,
782 ocd, GC_unreachable_finalize_mark_proc);
786 STATIC void GC_dump_finalization_links(
787 const struct dl_hashtbl_s *dl_hashtbl)
789 struct disappearing_link *curr_dl;
790 ptr_t real_ptr, real_link;
791 size_t dl_size = dl_hashtbl->log_size == -1 ? 0 :
792 1 << dl_hashtbl->log_size;
795 for (i = 0; i < dl_size; i++) {
796 for (curr_dl = dl_hashtbl -> head[i]; curr_dl != 0;
797 curr_dl = dl_next(curr_dl)) {
798 real_ptr = GC_REVEAL_POINTER(curr_dl -> dl_hidden_obj);
799 real_link = GC_REVEAL_POINTER(curr_dl -> dl_hidden_link);
800 GC_printf("Object: %p, link: %p\n", real_ptr, real_link);
805 void GC_dump_finalization(void)
807 struct finalizable_object * curr_fo;
808 size_t fo_size = log_fo_table_size == -1 ? 0 : 1 << log_fo_table_size;
812 GC_printf("Disappearing (short) links:\n");
813 GC_dump_finalization_links(&GC_dl_hashtbl);
814 # ifndef GC_LONG_REFS_NOT_NEEDED
815 GC_printf("Disappearing long links:\n");
816 GC_dump_finalization_links(&GC_ll_hashtbl);
818 GC_printf("Finalizers:\n");
819 for (i = 0; i < fo_size; i++) {
820 for (curr_fo = GC_fnlz_roots.fo_head[i];
821 curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
822 real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
823 GC_printf("Finalizable object: %p\n", real_ptr);
827 #endif /* !NO_DEBUGGING */
830 STATIC word GC_old_dl_entries = 0; /* for stats printing */
831 # ifndef GC_LONG_REFS_NOT_NEEDED
832 STATIC word GC_old_ll_entries = 0;
834 #endif /* !SMALL_CONFIG */
837 /* Global variables to minimize the level of recursion when a client */
838 /* finalizer allocates memory. */
839 STATIC int GC_finalizer_nested = 0;
840 /* Only the lowest byte is used, the rest is */
841 /* padding for proper global data alignment */
842 /* required for some compilers (like Watcom). */
843 STATIC unsigned GC_finalizer_skipped = 0;
845 /* Checks and updates the level of finalizers recursion. */
846 /* Returns NULL if GC_invoke_finalizers() should not be called by the */
847 /* collector (to minimize the risk of a deep finalizers recursion), */
848 /* otherwise returns a pointer to GC_finalizer_nested. */
849 STATIC unsigned char *GC_check_finalizer_nested(void)
851 unsigned nesting_level = *(unsigned char *)&GC_finalizer_nested;
853 /* We are inside another GC_invoke_finalizers(). */
854 /* Skip some implicitly-called GC_invoke_finalizers() */
855 /* depending on the nesting (recursion) level. */
856 if (++GC_finalizer_skipped < (1U << nesting_level)) return NULL;
857 GC_finalizer_skipped = 0;
859 *(char *)&GC_finalizer_nested = (char)(nesting_level + 1);
860 return (unsigned char *)&GC_finalizer_nested;
864 #define ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr_dl, prev_dl) \
867 size_t dl_size = dl_hashtbl->log_size == -1 ? 0 : \
868 1 << dl_hashtbl->log_size; \
869 for (i = 0; i < dl_size; i++) { \
870 curr_dl = dl_hashtbl -> head[i]; \
874 #define ITERATE_DL_HASHTBL_END(curr_dl, prev_dl) \
876 curr_dl = dl_next(curr_dl); \
881 #define DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr_dl, prev_dl, next_dl) \
883 next_dl = dl_next(curr_dl); \
884 if (NULL == prev_dl) { \
885 dl_hashtbl -> head[i] = next_dl; \
887 dl_set_next(prev_dl, next_dl); \
889 GC_clear_mark_bit(curr_dl); \
890 dl_hashtbl -> entries--; \
895 GC_INLINE void GC_make_disappearing_links_disappear(
896 struct dl_hashtbl_s* dl_hashtbl)
898 struct disappearing_link *curr, *prev, *next;
899 ptr_t real_ptr, real_link;
901 ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr, prev)
902 real_ptr = GC_REVEAL_POINTER(curr -> dl_hidden_obj);
903 real_link = GC_REVEAL_POINTER(curr -> dl_hidden_link);
904 if (!GC_is_marked(real_ptr)) {
905 *(word *)real_link = 0;
906 GC_clear_mark_bit(curr);
907 DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr, prev, next);
909 ITERATE_DL_HASHTBL_END(curr, prev)
912 GC_INLINE void GC_remove_dangling_disappearing_links(
913 struct dl_hashtbl_s* dl_hashtbl)
915 struct disappearing_link *curr, *prev, *next;
918 ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr, prev)
919 real_link = GC_base(GC_REVEAL_POINTER(curr -> dl_hidden_link));
920 if (NULL != real_link && !GC_is_marked(real_link)) {
921 GC_clear_mark_bit(curr);
922 DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr, prev, next);
924 ITERATE_DL_HASHTBL_END(curr, prev)
927 /* Called with held lock (but the world is running). */
928 /* Cause disappearing links to disappear and unreachable objects to be */
929 /* enqueued for finalization. */
930 GC_INNER void GC_finalize(void)
932 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
935 size_t fo_size = log_fo_table_size == -1 ? 0 : 1 << log_fo_table_size;
937 # ifndef SMALL_CONFIG
938 /* Save current GC_[dl/ll]_entries value for stats printing */
939 GC_old_dl_entries = GC_dl_hashtbl.entries;
940 # ifndef GC_LONG_REFS_NOT_NEEDED
941 GC_old_ll_entries = GC_ll_hashtbl.entries;
945 # ifndef GC_TOGGLE_REFS_NOT_NEEDED
946 GC_mark_togglerefs();
948 GC_make_disappearing_links_disappear(&GC_dl_hashtbl);
950 /* Mark all objects reachable via chains of 1 or more pointers */
951 /* from finalizable objects. */
952 GC_ASSERT(GC_mark_state == MS_NONE);
953 for (i = 0; i < fo_size; i++) {
954 for (curr_fo = GC_fnlz_roots.fo_head[i];
955 curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
956 GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
957 real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
958 if (!GC_is_marked(real_ptr)) {
959 GC_MARKED_FOR_FINALIZATION(real_ptr);
960 GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
961 if (GC_is_marked(real_ptr)) {
962 WARN("Finalization cycle involving %p\n", real_ptr);
967 /* Enqueue for finalization all objects that are still */
969 GC_bytes_finalized = 0;
970 for (i = 0; i < fo_size; i++) {
971 curr_fo = GC_fnlz_roots.fo_head[i];
973 while (curr_fo != 0) {
974 real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
975 if (!GC_is_marked(real_ptr)) {
976 if (!GC_java_finalization) {
977 GC_set_mark_bit(real_ptr);
979 /* Delete from hash table */
980 next_fo = fo_next(curr_fo);
981 if (NULL == prev_fo) {
982 GC_fnlz_roots.fo_head[i] = next_fo;
984 fo_set_next(prev_fo, next_fo);
987 if (GC_object_finalized_proc)
988 GC_object_finalized_proc(real_ptr);
990 /* Add to list of objects awaiting finalization. */
991 fo_set_next(curr_fo, GC_fnlz_roots.finalize_now);
992 GC_fnlz_roots.finalize_now = curr_fo;
993 /* unhide object pointer so any future collections will */
995 curr_fo -> fo_hidden_base =
996 (word)GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
997 GC_bytes_finalized +=
998 curr_fo -> fo_object_size
999 + sizeof(struct finalizable_object);
1000 GC_ASSERT(GC_is_marked(GC_base(curr_fo)));
1004 curr_fo = fo_next(curr_fo);
1009 if (GC_java_finalization) {
1010 /* make sure we mark everything reachable from objects finalized
1011 using the no_order mark_proc */
1012 for (curr_fo = GC_fnlz_roots.finalize_now;
1013 curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
1014 real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
1015 if (!GC_is_marked(real_ptr)) {
1016 if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
1017 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
1019 if (curr_fo -> fo_mark_proc != GC_unreachable_finalize_mark_proc) {
1020 GC_set_mark_bit(real_ptr);
1025 /* now revive finalize-when-unreachable objects reachable from
1026 other finalizable objects */
1027 if (need_unreachable_finalization) {
1028 curr_fo = GC_fnlz_roots.finalize_now;
1030 while (curr_fo != NULL) {
1031 next_fo = fo_next(curr_fo);
1032 if (curr_fo -> fo_mark_proc == GC_unreachable_finalize_mark_proc) {
1033 real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
1034 if (!GC_is_marked(real_ptr)) {
1035 GC_set_mark_bit(real_ptr);
1037 if (NULL == prev_fo) {
1038 GC_fnlz_roots.finalize_now = next_fo;
1040 fo_set_next(prev_fo, next_fo);
1042 curr_fo -> fo_hidden_base =
1043 GC_HIDE_POINTER(curr_fo -> fo_hidden_base);
1044 GC_bytes_finalized -=
1045 curr_fo->fo_object_size + sizeof(struct finalizable_object);
1047 i = HASH2(real_ptr, log_fo_table_size);
1048 fo_set_next(curr_fo, GC_fnlz_roots.fo_head[i]);
1050 GC_fnlz_roots.fo_head[i] = curr_fo;
1060 GC_remove_dangling_disappearing_links(&GC_dl_hashtbl);
1061 # ifndef GC_TOGGLE_REFS_NOT_NEEDED
1062 GC_clear_togglerefs();
1064 # ifndef GC_LONG_REFS_NOT_NEEDED
1065 GC_make_disappearing_links_disappear(&GC_ll_hashtbl);
1066 GC_remove_dangling_disappearing_links(&GC_ll_hashtbl);
1069 if (GC_fail_count) {
1070 /* Don't prevent running finalizers if there has been an allocation */
1071 /* failure recently. */
1073 GC_reset_finalizer_nested();
1075 GC_finalizer_nested = 0;
1080 #ifndef JAVA_FINALIZATION_NOT_NEEDED
1082 /* Enqueue all remaining finalizers to be run - Assumes lock is held. */
1083 STATIC void GC_enqueue_all_finalizers(void)
1085 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
1090 fo_size = log_fo_table_size == -1 ? 0 : 1 << log_fo_table_size;
1091 GC_bytes_finalized = 0;
1092 for (i = 0; i < fo_size; i++) {
1093 curr_fo = GC_fnlz_roots.fo_head[i];
1095 while (curr_fo != NULL) {
1096 real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
1097 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
1098 GC_set_mark_bit(real_ptr);
1100 /* Delete from hash table */
1101 next_fo = fo_next(curr_fo);
1103 GC_fnlz_roots.fo_head[i] = next_fo;
1105 fo_set_next(prev_fo, next_fo);
1109 /* Add to list of objects awaiting finalization. */
1110 fo_set_next(curr_fo, GC_fnlz_roots.finalize_now);
1111 GC_fnlz_roots.finalize_now = curr_fo;
1113 /* unhide object pointer so any future collections will */
1115 curr_fo -> fo_hidden_base =
1116 (word)GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
1117 GC_bytes_finalized +=
1118 curr_fo -> fo_object_size + sizeof(struct finalizable_object);
1124 /* Invoke all remaining finalizers that haven't yet been run.
1125 * This is needed for strict compliance with the Java standard,
1126 * which can make the runtime guarantee that all finalizers are run.
1127 * Unfortunately, the Java standard implies we have to keep running
1128 * finalizers until there are no more left, a potential infinite loop.
1130 * Note that this is even more dangerous than the usual Java
1131 * finalizers, in that objects reachable from static variables
1132 * may have been finalized when these finalizers are run.
1133 * Finalizers run at this point must be prepared to deal with a
1134 * mostly broken world.
1135 * This routine is externally callable, so is called without
1136 * the allocation lock.
1138 GC_API void GC_CALL GC_finalize_all(void)
1143 while (GC_fo_entries > 0) {
1144 GC_enqueue_all_finalizers();
1146 GC_invoke_finalizers();
1147 /* Running the finalizers in this thread is arguably not a good */
1148 /* idea when we should be notifying another thread to run them. */
1149 /* But otherwise we don't have a great way to wait for them to */
1156 #endif /* !JAVA_FINALIZATION_NOT_NEEDED */
1158 /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
1159 /* finalizers can only be called from some kind of "safe state" and */
1160 /* getting into that safe state is expensive.) */
1161 GC_API int GC_CALL GC_should_invoke_finalizers(void)
1163 return GC_fnlz_roots.finalize_now != NULL;
1166 /* Invoke finalizers for all objects that are ready to be finalized. */
1167 /* Should be called without allocation lock. */
1168 GC_API int GC_CALL GC_invoke_finalizers(void)
1170 struct finalizable_object * curr_fo;
1172 word bytes_freed_before = 0; /* initialized to prevent warning. */
1175 while (GC_fnlz_roots.finalize_now != NULL) {
1180 bytes_freed_before = GC_bytes_freed;
1181 /* Don't do this outside, since we need the lock. */
1183 curr_fo = GC_fnlz_roots.finalize_now;
1185 if (curr_fo != 0) GC_fnlz_roots.finalize_now = fo_next(curr_fo);
1187 if (curr_fo == 0) break;
1189 GC_fnlz_roots.finalize_now = fo_next(curr_fo);
1191 fo_set_next(curr_fo, 0);
1192 (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
1193 curr_fo -> fo_client_data);
1194 curr_fo -> fo_client_data = 0;
1197 /* This is probably a bad idea. It throws off accounting if */
1198 /* nearly all objects are finalizable. O.w. it shouldn't */
1200 GC_free((void *)curr_fo);
1203 /* bytes_freed_before is initialized whenever count != 0 */
1204 if (count != 0 && bytes_freed_before != GC_bytes_freed) {
1206 GC_finalizer_bytes_freed += (GC_bytes_freed - bytes_freed_before);
1212 static word last_finalizer_notification = 0;
1214 GC_INNER void GC_notify_or_invoke_finalizers(void)
1216 GC_finalizer_notifier_proc notifier_fn = 0;
1217 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1218 static word last_back_trace_gc_no = 1; /* Skip first one. */
1222 # if defined(THREADS) && !defined(KEEP_BACK_PTRS) \
1223 && !defined(MAKE_BACK_GRAPH)
1224 /* Quick check (while unlocked) for an empty finalization queue. */
1225 if (NULL == GC_fnlz_roots.finalize_now) return;
1229 /* This is a convenient place to generate backtraces if appropriate, */
1230 /* since that code is not callable with the allocation lock. */
1231 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1232 if (GC_gc_no > last_back_trace_gc_no) {
1233 # ifdef KEEP_BACK_PTRS
1235 /* Stops when GC_gc_no wraps; that's OK. */
1236 last_back_trace_gc_no = (word)(-1); /* disable others. */
1237 for (i = 0; i < GC_backtraces; ++i) {
1238 /* FIXME: This tolerates concurrent heap mutation, */
1239 /* which may cause occasional mysterious results. */
1240 /* We need to release the GC lock, since GC_print_callers */
1241 /* acquires it. It probably shouldn't. */
1243 GC_generate_random_backtrace_no_gc();
1246 last_back_trace_gc_no = GC_gc_no;
1248 # ifdef MAKE_BACK_GRAPH
1249 if (GC_print_back_height) {
1251 GC_print_back_graph_stats();
1257 if (NULL == GC_fnlz_roots.finalize_now) {
1262 if (!GC_finalize_on_demand) {
1263 unsigned char *pnested = GC_check_finalizer_nested();
1265 /* Skip GC_invoke_finalizers() if nested */
1266 if (pnested != NULL) {
1267 (void) GC_invoke_finalizers();
1268 *pnested = 0; /* Reset since no more finalizers. */
1270 GC_ASSERT(NULL == GC_fnlz_roots.finalize_now);
1271 # endif /* Otherwise GC can run concurrently and add more */
1276 /* These variables require synchronization to avoid data races. */
1277 if (last_finalizer_notification != GC_gc_no) {
1278 last_finalizer_notification = GC_gc_no;
1279 notifier_fn = GC_finalizer_notifier;
1282 if (notifier_fn != 0)
1283 (*notifier_fn)(); /* Invoke the notifier */
1286 #ifndef SMALL_CONFIG
1287 # ifndef GC_LONG_REFS_NOT_NEEDED
1288 # define IF_LONG_REFS_PRESENT_ELSE(x,y) (x)
1290 # define IF_LONG_REFS_PRESENT_ELSE(x,y) (y)
1293 GC_INNER void GC_print_finalization_stats(void)
1295 struct finalizable_object *fo;
1296 unsigned long ready = 0;
1298 GC_log_printf("%lu finalization entries;"
1299 " %lu/%lu short/long disappearing links alive\n",
1300 (unsigned long)GC_fo_entries,
1301 (unsigned long)GC_dl_hashtbl.entries,
1302 (unsigned long)IF_LONG_REFS_PRESENT_ELSE(
1303 GC_ll_hashtbl.entries, 0));
1305 for (fo = GC_fnlz_roots.finalize_now; fo != NULL; fo = fo_next(fo))
1307 GC_log_printf("%lu finalization-ready objects;"
1308 " %ld/%ld short/long links cleared\n",
1310 (long)GC_old_dl_entries - (long)GC_dl_hashtbl.entries,
1311 (long)IF_LONG_REFS_PRESENT_ELSE(
1312 GC_old_ll_entries - GC_ll_hashtbl.entries, 0));
1314 #endif /* !SMALL_CONFIG */
1316 #endif /* !GC_NO_FINALIZATION */