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 || 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 /* toggleref support */
266 GC_hidden_pointer weak_ref;
269 static int (*GC_toggleref_callback) (GC_PTR obj);
270 static GCToggleRef *GC_toggleref_array;
271 static int GC_toggleref_array_size;
272 static int GC_toggleref_array_capacity;
276 GC_process_togglerefs (void)
279 int toggle_ref_counts [3] = { 0, 0, 0 };
281 for (i = w = 0; i < GC_toggleref_array_size; ++i) {
283 GCToggleRef r = GC_toggleref_array [i];
290 obj = GC_REVEAL_POINTER (r.weak_ref);
294 res = GC_toggleref_callback (obj);
295 ++toggle_ref_counts [res];
300 GC_toggleref_array [w].strong_ref = obj;
301 GC_toggleref_array [w].weak_ref = (GC_hidden_pointer)NULL;
305 GC_toggleref_array [w].strong_ref = NULL;
306 GC_toggleref_array [w].weak_ref = GC_HIDE_POINTER (obj);
310 ABORT("Invalid callback result");
314 for (i = w; i < GC_toggleref_array_size; ++i) {
315 GC_toggleref_array [i].strong_ref = NULL;
316 GC_toggleref_array [i].weak_ref = (GC_hidden_pointer)NULL;
319 GC_toggleref_array_size = w;
323 static void push_and_mark_object (GC_PTR p)
327 PUSH_OBJ(p, hhdr, GC_mark_stack_top,
328 &(GC_mark_stack[GC_mark_stack_size]));
330 while (!GC_mark_stack_empty()) MARK_FROM_MARK_STACK();
332 if (GC_mark_state != MS_NONE)
333 while (!GC_mark_some((ptr_t)0)) {}
336 static void GC_mark_togglerefs ()
339 if (!GC_toggleref_array)
342 GC_set_mark_bit (GC_toggleref_array);
343 for (i = 0; i < GC_toggleref_array_size; ++i) {
344 if (GC_toggleref_array [i].strong_ref) {
345 GC_PTR object = GC_toggleref_array [i].strong_ref;
347 push_and_mark_object (object);
352 static void GC_clear_togglerefs ()
355 for (i = 0; i < GC_toggleref_array_size; ++i) {
356 if (GC_toggleref_array [i].weak_ref) {
357 GC_PTR object = GC_REVEAL_POINTER (GC_toggleref_array [i].weak_ref);
359 if (!GC_is_marked (object)) {
360 GC_toggleref_array [i].weak_ref = (GC_hidden_pointer)NULL; /* We defer compaction to only happen on the callback step. */
362 /*No need to copy, boehm is non-moving */
370 void GC_toggleref_register_callback(int (*proccess_toggleref) (GC_PTR obj))
372 GC_toggleref_callback = proccess_toggleref;
376 ensure_toggleref_capacity (int capacity)
378 if (!GC_toggleref_array) {
379 GC_toggleref_array_capacity = 32;
380 GC_toggleref_array = (GCToggleRef *) GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE (GC_toggleref_array_capacity * sizeof (GCToggleRef), NORMAL);
382 if (GC_toggleref_array_size + capacity >= GC_toggleref_array_capacity) {
384 int old_capacity = GC_toggleref_array_capacity;
385 while (GC_toggleref_array_capacity < GC_toggleref_array_size + capacity)
386 GC_toggleref_array_capacity *= 2;
388 tmp = (GCToggleRef *) GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE (GC_toggleref_array_capacity * sizeof (GCToggleRef), NORMAL);
389 memcpy (tmp, GC_toggleref_array, GC_toggleref_array_size * sizeof (GCToggleRef));
391 GC_INTERNAL_FREE(GC_toggleref_array);
392 GC_toggleref_array = tmp;
397 GC_toggleref_add (GC_PTR object, int strong_ref)
402 if (!GC_toggleref_callback)
405 ensure_toggleref_capacity (1);
406 GC_toggleref_array [GC_toggleref_array_size].strong_ref = strong_ref ? object : NULL;
407 GC_toggleref_array [GC_toggleref_array_size].weak_ref = strong_ref ? (GC_hidden_pointer)NULL : GC_HIDE_POINTER (object);
408 ++GC_toggleref_array_size;
415 /* Finalizer callback support. */
416 STATIC GC_await_finalize_proc GC_object_finalized_proc = 0;
418 GC_API void GC_CALL GC_set_await_finalize_proc(GC_await_finalize_proc fn)
423 GC_object_finalized_proc = fn;
427 GC_API GC_await_finalize_proc GC_CALL GC_get_await_finalize_proc(void)
429 GC_await_finalize_proc fn;
433 fn = GC_object_finalized_proc;
438 #ifndef GC_LONG_REFS_NOT_NEEDED
439 GC_API int GC_CALL GC_register_long_link(void * * link, const void * obj)
441 if (((word)link & (ALIGNMENT-1)) != 0 || NULL == link)
442 ABORT("Bad arg to GC_register_long_link");
443 return GC_register_disappearing_link_inner(&GC_ll_hashtbl, link, obj,
447 GC_API int GC_CALL GC_unregister_long_link(void * * link)
449 struct disappearing_link *curr_dl;
452 if (((word)link & (ALIGNMENT-1)) != 0) return(0); /* Nothing to do. */
455 curr_dl = GC_unregister_disappearing_link_inner(&GC_ll_hashtbl, link);
457 if (NULL == curr_dl) return 0;
458 FREE_DL_ENTRY(curr_dl);
461 #endif /* !GC_LONG_REFS_NOT_NEEDED */
463 #ifndef GC_MOVE_DISAPPEARING_LINK_NOT_NEEDED
464 /* Moves a link. Assume the lock is held. */
465 STATIC int GC_move_disappearing_link_inner(
466 struct dl_hashtbl_s *dl_hashtbl,
467 void **link, void **new_link)
469 struct disappearing_link *curr_dl, *prev_dl, *new_dl;
470 size_t curr_index, new_index;
471 word curr_hidden_link;
472 word new_hidden_link;
474 /* Find current link. */
475 curr_index = HASH2(link, dl_hashtbl -> log_size);
476 curr_hidden_link = GC_HIDE_POINTER(link);
478 for (curr_dl = dl_hashtbl -> head[curr_index]; curr_dl;
479 curr_dl = dl_next(curr_dl)) {
480 if (curr_dl -> dl_hidden_link == curr_hidden_link)
485 if (NULL == curr_dl) {
489 if (link == new_link) {
490 return GC_SUCCESS; /* Nothing to do. */
493 /* link found; now check new_link not present. */
494 new_index = HASH2(new_link, dl_hashtbl -> log_size);
495 new_hidden_link = GC_HIDE_POINTER(new_link);
496 for (new_dl = dl_hashtbl -> head[new_index]; new_dl;
497 new_dl = dl_next(new_dl)) {
498 if (new_dl -> dl_hidden_link == new_hidden_link) {
499 /* Target already registered; bail. */
504 /* Remove from old, add to new, update link. */
505 if (NULL == prev_dl) {
506 dl_hashtbl -> head[curr_index] = dl_next(curr_dl);
508 dl_set_next(prev_dl, dl_next(curr_dl));
510 curr_dl -> dl_hidden_link = new_hidden_link;
511 dl_set_next(curr_dl, dl_hashtbl -> head[new_index]);
512 dl_hashtbl -> head[new_index] = curr_dl;
516 GC_API int GC_CALL GC_move_disappearing_link(void **link, void **new_link)
521 if (((word)new_link & (ALIGNMENT-1)) != 0 || new_link == NULL)
522 ABORT("Bad new_link arg to GC_move_disappearing_link");
523 if (((word)link & (ALIGNMENT-1)) != 0)
524 return GC_NOT_FOUND; /* Nothing to do. */
527 result = GC_move_disappearing_link_inner(&GC_dl_hashtbl, link, new_link);
532 # ifndef GC_LONG_REFS_NOT_NEEDED
533 GC_API int GC_CALL GC_move_long_link(void **link, void **new_link)
538 if (((word)new_link & (ALIGNMENT-1)) != 0 || new_link == NULL)
539 ABORT("Bad new_link arg to GC_move_disappearing_link");
540 if (((word)link & (ALIGNMENT-1)) != 0)
541 return GC_NOT_FOUND; /* Nothing to do. */
544 result = GC_move_disappearing_link_inner(&GC_ll_hashtbl, link, new_link);
548 # endif /* !GC_LONG_REFS_NOT_NEEDED */
549 #endif /* !GC_MOVE_DISAPPEARING_LINK_NOT_NEEDED */
551 /* Possible finalization_marker procedures. Note that mark stack */
552 /* overflow is handled by the caller, and is not a disaster. */
553 STATIC void GC_normal_finalize_mark_proc(ptr_t p)
557 PUSH_OBJ(p, hhdr, GC_mark_stack_top,
558 &(GC_mark_stack[GC_mark_stack_size]));
561 /* This only pays very partial attention to the mark descriptor. */
562 /* It does the right thing for normal and atomic objects, and treats */
563 /* most others as normal. */
564 STATIC void GC_ignore_self_finalize_mark_proc(ptr_t p)
567 word descr = hhdr -> hb_descr;
571 ptr_t target_limit = p + hhdr -> hb_sz - 1;
573 if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
574 scan_limit = p + descr - sizeof(word);
576 scan_limit = target_limit + 1 - sizeof(word);
578 for (q = p; (word)q <= (word)scan_limit; q += ALIGNMENT) {
580 if (r < (word)p || r > (word)target_limit) {
581 GC_PUSH_ONE_HEAP(r, q, GC_mark_stack_top);
586 STATIC void GC_null_finalize_mark_proc(ptr_t p GC_ATTR_UNUSED) {}
588 /* Possible finalization_marker procedures. Note that mark stack */
589 /* overflow is handled by the caller, and is not a disaster. */
591 /* GC_unreachable_finalize_mark_proc is an alias for normal marking, */
592 /* but it is explicitly tested for, and triggers different */
593 /* behavior. Objects registered in this way are not finalized */
594 /* if they are reachable by other finalizable objects, even if those */
595 /* other objects specify no ordering. */
596 STATIC void GC_unreachable_finalize_mark_proc(ptr_t p)
598 GC_normal_finalize_mark_proc(p);
601 /* Register a finalization function. See gc.h for details. */
602 /* The last parameter is a procedure that determines */
603 /* marking for finalization ordering. Any objects marked */
604 /* by that procedure will be guaranteed to not have been */
605 /* finalized when this finalizer is invoked. */
606 STATIC void GC_register_finalizer_inner(void * obj,
607 GC_finalization_proc fn, void *cd,
608 GC_finalization_proc *ofn, void **ocd,
609 finalization_mark_proc mp)
612 struct finalizable_object * curr_fo, * prev_fo;
614 struct finalizable_object *new_fo = 0;
615 hdr *hhdr = NULL; /* initialized to prevent warning. */
620 if (log_fo_table_size == -1
621 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
622 GC_grow_table((struct hash_chain_entry ***)&GC_fnlz_roots.fo_head,
624 GC_COND_LOG_PRINTF("Grew fo table to %u entries\n",
625 1 << (unsigned)log_fo_table_size);
627 /* in the THREADS case we hold allocation lock. */
630 index = HASH2(base, 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(base)) {
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 base. */
643 GC_fnlz_roots.fo_head[index] = fo_next(curr_fo);
645 fo_set_next(prev_fo, fo_next(curr_fo));
649 /* May not happen if we get a signal. But a high */
650 /* estimate will only make the table larger than */
652 # if !defined(THREADS) && !defined(DBG_HDRS_ALL)
653 GC_free((void *)curr_fo);
656 curr_fo -> fo_fn = fn;
657 curr_fo -> fo_client_data = (ptr_t)cd;
658 curr_fo -> fo_mark_proc = mp;
659 /* Reinsert it. We deleted it first to maintain */
660 /* consistency in the event of a signal. */
662 GC_fnlz_roots.fo_head[index] = curr_fo;
664 fo_set_next(prev_fo, curr_fo);
668 # ifndef DBG_HDRS_ALL
669 if (EXPECT(new_fo != 0, FALSE)) {
670 /* Free unused new_fo returned by GC_oom_fn() */
671 GC_free((void *)new_fo);
677 curr_fo = fo_next(curr_fo);
679 if (EXPECT(new_fo != 0, FALSE)) {
680 /* new_fo is returned by GC_oom_fn(), so fn != 0 and hhdr != 0. */
690 if (EXPECT(0 == hhdr, FALSE)) {
691 /* We won't collect it, hence finalizer wouldn't be run. */
697 new_fo = (struct finalizable_object *)
698 GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
699 if (EXPECT(new_fo != 0, TRUE))
703 new_fo = (struct finalizable_object *)
704 (*oom_fn)(sizeof(struct finalizable_object));
706 /* No enough memory. *ocd and *ofn remains unchanged. */
709 /* It's not likely we'll make it here, but ... */
711 /* Recalculate index since the table may grow and */
712 /* check again that our finalizer is not in the table. */
714 GC_ASSERT(GC_size(new_fo) >= sizeof(struct finalizable_object));
717 new_fo -> fo_hidden_base = GC_HIDE_POINTER(base);
718 new_fo -> fo_fn = fn;
719 new_fo -> fo_client_data = (ptr_t)cd;
720 new_fo -> fo_object_size = hhdr -> hb_sz;
721 new_fo -> fo_mark_proc = mp;
722 fo_set_next(new_fo, GC_fnlz_roots.fo_head[index]);
724 GC_fnlz_roots.fo_head[index] = new_fo;
728 GC_API void GC_CALL GC_register_finalizer(void * obj,
729 GC_finalization_proc fn, void * cd,
730 GC_finalization_proc *ofn, void ** ocd)
732 GC_register_finalizer_inner(obj, fn, cd, ofn,
733 ocd, GC_normal_finalize_mark_proc);
736 GC_API void GC_CALL GC_register_finalizer_ignore_self(void * obj,
737 GC_finalization_proc fn, void * cd,
738 GC_finalization_proc *ofn, void ** ocd)
740 GC_register_finalizer_inner(obj, fn, cd, ofn,
741 ocd, GC_ignore_self_finalize_mark_proc);
744 GC_API void GC_CALL GC_register_finalizer_no_order(void * obj,
745 GC_finalization_proc fn, void * cd,
746 GC_finalization_proc *ofn, void ** ocd)
748 GC_register_finalizer_inner(obj, fn, cd, ofn,
749 ocd, GC_null_finalize_mark_proc);
752 static GC_bool need_unreachable_finalization = FALSE;
753 /* Avoid the work if this isn't used. */
755 GC_API void GC_CALL GC_register_finalizer_unreachable(void * obj,
756 GC_finalization_proc fn, void * cd,
757 GC_finalization_proc *ofn, void ** ocd)
759 need_unreachable_finalization = TRUE;
760 GC_ASSERT(GC_java_finalization);
761 GC_register_finalizer_inner(obj, fn, cd, ofn,
762 ocd, GC_unreachable_finalize_mark_proc);
766 STATIC void GC_dump_finalization_links(
767 const struct dl_hashtbl_s *dl_hashtbl)
769 struct disappearing_link *curr_dl;
770 ptr_t real_ptr, real_link;
771 size_t dl_size = dl_hashtbl->log_size == -1 ? 0 :
772 1 << dl_hashtbl->log_size;
775 for (i = 0; i < dl_size; i++) {
776 for (curr_dl = dl_hashtbl -> head[i]; curr_dl != 0;
777 curr_dl = dl_next(curr_dl)) {
778 real_ptr = GC_REVEAL_POINTER(curr_dl -> dl_hidden_obj);
779 real_link = GC_REVEAL_POINTER(curr_dl -> dl_hidden_link);
780 GC_printf("Object: %p, link: %p\n", real_ptr, real_link);
785 void GC_dump_finalization(void)
787 struct finalizable_object * curr_fo;
788 size_t fo_size = log_fo_table_size == -1 ? 0 : 1 << log_fo_table_size;
792 GC_printf("Disappearing (short) links:\n");
793 GC_dump_finalization_links(&GC_dl_hashtbl);
794 # ifndef GC_LONG_REFS_NOT_NEEDED
795 GC_printf("Disappearing long links:\n");
796 GC_dump_finalization_links(&GC_ll_hashtbl);
798 GC_printf("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 real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
803 GC_printf("Finalizable object: %p\n", real_ptr);
807 #endif /* !NO_DEBUGGING */
810 STATIC word GC_old_dl_entries = 0; /* for stats printing */
811 # ifndef GC_LONG_REFS_NOT_NEEDED
812 STATIC word GC_old_ll_entries = 0;
814 #endif /* !SMALL_CONFIG */
817 /* Global variables to minimize the level of recursion when a client */
818 /* finalizer allocates memory. */
819 STATIC int GC_finalizer_nested = 0;
820 /* Only the lowest byte is used, the rest is */
821 /* padding for proper global data alignment */
822 /* required for some compilers (like Watcom). */
823 STATIC unsigned GC_finalizer_skipped = 0;
825 /* Checks and updates the level of finalizers recursion. */
826 /* Returns NULL if GC_invoke_finalizers() should not be called by the */
827 /* collector (to minimize the risk of a deep finalizers recursion), */
828 /* otherwise returns a pointer to GC_finalizer_nested. */
829 STATIC unsigned char *GC_check_finalizer_nested(void)
831 unsigned nesting_level = *(unsigned char *)&GC_finalizer_nested;
833 /* We are inside another GC_invoke_finalizers(). */
834 /* Skip some implicitly-called GC_invoke_finalizers() */
835 /* depending on the nesting (recursion) level. */
836 if (++GC_finalizer_skipped < (1U << nesting_level)) return NULL;
837 GC_finalizer_skipped = 0;
839 *(char *)&GC_finalizer_nested = (char)(nesting_level + 1);
840 return (unsigned char *)&GC_finalizer_nested;
844 #define ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr_dl, prev_dl) \
847 size_t dl_size = dl_hashtbl->log_size == -1 ? 0 : \
848 1 << dl_hashtbl->log_size; \
849 for (i = 0; i < dl_size; i++) { \
850 curr_dl = dl_hashtbl -> head[i]; \
854 #define ITERATE_DL_HASHTBL_END(curr_dl, prev_dl) \
856 curr_dl = dl_next(curr_dl); \
861 #define DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr_dl, prev_dl, next_dl) \
863 next_dl = dl_next(curr_dl); \
864 if (NULL == prev_dl) { \
865 dl_hashtbl -> head[i] = next_dl; \
867 dl_set_next(prev_dl, next_dl); \
869 GC_clear_mark_bit(curr_dl); \
870 dl_hashtbl -> entries--; \
875 GC_INLINE void GC_make_disappearing_links_disappear(
876 struct dl_hashtbl_s* dl_hashtbl)
878 struct disappearing_link *curr, *prev, *next;
879 ptr_t real_ptr, real_link;
881 ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr, prev)
882 real_ptr = GC_REVEAL_POINTER(curr -> dl_hidden_obj);
883 real_link = GC_REVEAL_POINTER(curr -> dl_hidden_link);
884 if (!GC_is_marked(real_ptr)) {
885 *(word *)real_link = 0;
886 GC_clear_mark_bit(curr);
887 DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr, prev, next);
889 ITERATE_DL_HASHTBL_END(curr, prev)
892 GC_INLINE void GC_remove_dangling_disappearing_links(
893 struct dl_hashtbl_s* dl_hashtbl)
895 struct disappearing_link *curr, *prev, *next;
898 ITERATE_DL_HASHTBL_BEGIN(dl_hashtbl, curr, prev)
899 real_link = GC_base(GC_REVEAL_POINTER(curr -> dl_hidden_link));
900 if (NULL != real_link && !GC_is_marked(real_link)) {
901 GC_clear_mark_bit(curr);
902 DELETE_DL_HASHTBL_ENTRY(dl_hashtbl, curr, prev, next);
904 ITERATE_DL_HASHTBL_END(curr, prev)
907 /* Called with held lock (but the world is running). */
908 /* Cause disappearing links to disappear and unreachable objects to be */
909 /* enqueued for finalization. */
910 GC_INNER void GC_finalize(void)
912 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
915 size_t fo_size = log_fo_table_size == -1 ? 0 : 1 << log_fo_table_size;
917 # ifndef SMALL_CONFIG
918 /* Save current GC_[dl/ll]_entries value for stats printing */
919 GC_old_dl_entries = GC_dl_hashtbl.entries;
920 # ifndef GC_LONG_REFS_NOT_NEEDED
921 GC_old_ll_entries = GC_ll_hashtbl.entries;
925 GC_mark_togglerefs();
926 GC_make_disappearing_links_disappear(&GC_dl_hashtbl);
928 /* Mark all objects reachable via chains of 1 or more pointers */
929 /* from finalizable objects. */
930 GC_ASSERT(GC_mark_state == MS_NONE);
931 for (i = 0; i < fo_size; i++) {
932 for (curr_fo = GC_fnlz_roots.fo_head[i];
933 curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
934 GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
935 real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
936 if (!GC_is_marked(real_ptr)) {
937 GC_MARKED_FOR_FINALIZATION(real_ptr);
938 GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
939 if (GC_is_marked(real_ptr)) {
940 WARN("Finalization cycle involving %p\n", real_ptr);
945 /* Enqueue for finalization all objects that are still */
947 GC_bytes_finalized = 0;
948 for (i = 0; i < fo_size; i++) {
949 curr_fo = GC_fnlz_roots.fo_head[i];
951 while (curr_fo != 0) {
952 real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
953 if (!GC_is_marked(real_ptr)) {
954 if (!GC_java_finalization) {
955 GC_set_mark_bit(real_ptr);
957 /* Delete from hash table */
958 next_fo = fo_next(curr_fo);
959 if (NULL == prev_fo) {
960 GC_fnlz_roots.fo_head[i] = next_fo;
962 fo_set_next(prev_fo, next_fo);
965 if (GC_object_finalized_proc)
966 GC_object_finalized_proc(real_ptr);
968 /* Add to list of objects awaiting finalization. */
969 fo_set_next(curr_fo, GC_fnlz_roots.finalize_now);
970 GC_fnlz_roots.finalize_now = curr_fo;
971 /* unhide object pointer so any future collections will */
973 curr_fo -> fo_hidden_base =
974 (word)GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
975 GC_bytes_finalized +=
976 curr_fo -> fo_object_size
977 + sizeof(struct finalizable_object);
978 GC_ASSERT(GC_is_marked(GC_base(curr_fo)));
982 curr_fo = fo_next(curr_fo);
987 if (GC_java_finalization) {
988 /* make sure we mark everything reachable from objects finalized
989 using the no_order mark_proc */
990 for (curr_fo = GC_fnlz_roots.finalize_now;
991 curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
992 real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
993 if (!GC_is_marked(real_ptr)) {
994 if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
995 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
997 if (curr_fo -> fo_mark_proc != GC_unreachable_finalize_mark_proc) {
998 GC_set_mark_bit(real_ptr);
1003 /* now revive finalize-when-unreachable objects reachable from
1004 other finalizable objects */
1005 if (need_unreachable_finalization) {
1006 curr_fo = GC_fnlz_roots.finalize_now;
1008 while (curr_fo != NULL) {
1009 next_fo = fo_next(curr_fo);
1010 if (curr_fo -> fo_mark_proc == GC_unreachable_finalize_mark_proc) {
1011 real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
1012 if (!GC_is_marked(real_ptr)) {
1013 GC_set_mark_bit(real_ptr);
1015 if (NULL == prev_fo) {
1016 GC_fnlz_roots.finalize_now = next_fo;
1018 fo_set_next(prev_fo, next_fo);
1020 curr_fo -> fo_hidden_base =
1021 GC_HIDE_POINTER(curr_fo -> fo_hidden_base);
1022 GC_bytes_finalized -=
1023 curr_fo->fo_object_size + sizeof(struct finalizable_object);
1025 i = HASH2(real_ptr, log_fo_table_size);
1026 fo_set_next(curr_fo, GC_fnlz_roots.fo_head[i]);
1028 GC_fnlz_roots.fo_head[i] = curr_fo;
1038 GC_remove_dangling_disappearing_links(&GC_dl_hashtbl);
1039 GC_clear_togglerefs ();
1040 # ifndef GC_LONG_REFS_NOT_NEEDED
1041 GC_make_disappearing_links_disappear(&GC_ll_hashtbl);
1042 GC_remove_dangling_disappearing_links(&GC_ll_hashtbl);
1045 if (GC_fail_count) {
1046 /* Don't prevent running finalizers if there has been an allocation */
1047 /* failure recently. */
1049 GC_reset_finalizer_nested();
1051 GC_finalizer_nested = 0;
1056 #ifndef JAVA_FINALIZATION_NOT_NEEDED
1058 /* Enqueue all remaining finalizers to be run - Assumes lock is held. */
1059 STATIC void GC_enqueue_all_finalizers(void)
1061 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
1066 fo_size = log_fo_table_size == -1 ? 0 : 1 << log_fo_table_size;
1067 GC_bytes_finalized = 0;
1068 for (i = 0; i < fo_size; i++) {
1069 curr_fo = GC_fnlz_roots.fo_head[i];
1071 while (curr_fo != NULL) {
1072 real_ptr = GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
1073 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
1074 GC_set_mark_bit(real_ptr);
1076 /* Delete from hash table */
1077 next_fo = fo_next(curr_fo);
1079 GC_fnlz_roots.fo_head[i] = next_fo;
1081 fo_set_next(prev_fo, next_fo);
1085 /* Add to list of objects awaiting finalization. */
1086 fo_set_next(curr_fo, GC_fnlz_roots.finalize_now);
1087 GC_fnlz_roots.finalize_now = curr_fo;
1089 /* unhide object pointer so any future collections will */
1091 curr_fo -> fo_hidden_base =
1092 (word)GC_REVEAL_POINTER(curr_fo -> fo_hidden_base);
1093 GC_bytes_finalized +=
1094 curr_fo -> fo_object_size + sizeof(struct finalizable_object);
1100 /* Invoke all remaining finalizers that haven't yet been run.
1101 * This is needed for strict compliance with the Java standard,
1102 * which can make the runtime guarantee that all finalizers are run.
1103 * Unfortunately, the Java standard implies we have to keep running
1104 * finalizers until there are no more left, a potential infinite loop.
1106 * Note that this is even more dangerous than the usual Java
1107 * finalizers, in that objects reachable from static variables
1108 * may have been finalized when these finalizers are run.
1109 * Finalizers run at this point must be prepared to deal with a
1110 * mostly broken world.
1111 * This routine is externally callable, so is called without
1112 * the allocation lock.
1114 GC_API void GC_CALL GC_finalize_all(void)
1119 while (GC_fo_entries > 0) {
1120 GC_enqueue_all_finalizers();
1122 GC_invoke_finalizers();
1123 /* Running the finalizers in this thread is arguably not a good */
1124 /* idea when we should be notifying another thread to run them. */
1125 /* But otherwise we don't have a great way to wait for them to */
1132 #endif /* !JAVA_FINALIZATION_NOT_NEEDED */
1134 /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
1135 /* finalizers can only be called from some kind of "safe state" and */
1136 /* getting into that safe state is expensive.) */
1137 GC_API int GC_CALL GC_should_invoke_finalizers(void)
1139 return GC_fnlz_roots.finalize_now != NULL;
1142 /* Invoke finalizers for all objects that are ready to be finalized. */
1143 /* Should be called without allocation lock. */
1144 GC_API int GC_CALL GC_invoke_finalizers(void)
1146 struct finalizable_object * curr_fo;
1148 word bytes_freed_before = 0; /* initialized to prevent warning. */
1151 while (GC_fnlz_roots.finalize_now != NULL) {
1156 bytes_freed_before = GC_bytes_freed;
1157 /* Don't do this outside, since we need the lock. */
1159 curr_fo = GC_fnlz_roots.finalize_now;
1161 if (curr_fo != 0) GC_fnlz_roots.finalize_now = fo_next(curr_fo);
1163 if (curr_fo == 0) break;
1165 GC_fnlz_roots.finalize_now = fo_next(curr_fo);
1167 fo_set_next(curr_fo, 0);
1168 (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
1169 curr_fo -> fo_client_data);
1170 curr_fo -> fo_client_data = 0;
1173 /* This is probably a bad idea. It throws off accounting if */
1174 /* nearly all objects are finalizable. O.w. it shouldn't */
1176 GC_free((void *)curr_fo);
1179 /* bytes_freed_before is initialized whenever count != 0 */
1180 if (count != 0 && bytes_freed_before != GC_bytes_freed) {
1182 GC_finalizer_bytes_freed += (GC_bytes_freed - bytes_freed_before);
1188 static word last_finalizer_notification = 0;
1190 GC_INNER void GC_notify_or_invoke_finalizers(void)
1192 GC_finalizer_notifier_proc notifier_fn = 0;
1193 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1194 static word last_back_trace_gc_no = 1; /* Skip first one. */
1198 # if defined(THREADS) && !defined(KEEP_BACK_PTRS) \
1199 && !defined(MAKE_BACK_GRAPH)
1200 /* Quick check (while unlocked) for an empty finalization queue. */
1201 if (NULL == GC_fnlz_roots.finalize_now) return;
1205 /* This is a convenient place to generate backtraces if appropriate, */
1206 /* since that code is not callable with the allocation lock. */
1207 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1208 if (GC_gc_no > last_back_trace_gc_no) {
1209 # ifdef KEEP_BACK_PTRS
1211 /* Stops when GC_gc_no wraps; that's OK. */
1212 last_back_trace_gc_no = (word)(-1); /* disable others. */
1213 for (i = 0; i < GC_backtraces; ++i) {
1214 /* FIXME: This tolerates concurrent heap mutation, */
1215 /* which may cause occasional mysterious results. */
1216 /* We need to release the GC lock, since GC_print_callers */
1217 /* acquires it. It probably shouldn't. */
1219 GC_generate_random_backtrace_no_gc();
1222 last_back_trace_gc_no = GC_gc_no;
1224 # ifdef MAKE_BACK_GRAPH
1225 if (GC_print_back_height) {
1227 GC_print_back_graph_stats();
1233 if (NULL == GC_fnlz_roots.finalize_now) {
1238 if (!GC_finalize_on_demand) {
1239 unsigned char *pnested = GC_check_finalizer_nested();
1241 /* Skip GC_invoke_finalizers() if nested */
1242 if (pnested != NULL) {
1243 (void) GC_invoke_finalizers();
1244 *pnested = 0; /* Reset since no more finalizers. */
1246 GC_ASSERT(NULL == GC_fnlz_roots.finalize_now);
1247 # endif /* Otherwise GC can run concurrently and add more */
1252 /* These variables require synchronization to avoid data races. */
1253 if (last_finalizer_notification != GC_gc_no) {
1254 last_finalizer_notification = GC_gc_no;
1255 notifier_fn = GC_finalizer_notifier;
1258 if (notifier_fn != 0)
1259 (*notifier_fn)(); /* Invoke the notifier */
1262 #ifndef SMALL_CONFIG
1263 # ifndef GC_LONG_REFS_NOT_NEEDED
1264 # define IF_LONG_REFS_PRESENT_ELSE(x,y) (x)
1266 # define IF_LONG_REFS_PRESENT_ELSE(x,y) (y)
1269 GC_INNER void GC_print_finalization_stats(void)
1271 struct finalizable_object *fo;
1272 unsigned long ready = 0;
1274 GC_log_printf("%lu finalization entries;"
1275 " %lu/%lu short/long disappearing links alive\n",
1276 (unsigned long)GC_fo_entries,
1277 (unsigned long)GC_dl_hashtbl.entries,
1278 (unsigned long)IF_LONG_REFS_PRESENT_ELSE(
1279 GC_ll_hashtbl.entries, 0));
1281 for (fo = GC_fnlz_roots.finalize_now; fo != NULL; fo = fo_next(fo))
1283 GC_log_printf("%lu finalization-ready objects;"
1284 " %ld/%ld short/long links cleared\n",
1286 (long)GC_old_dl_entries - (long)GC_dl_hashtbl.entries,
1287 (long)IF_LONG_REFS_PRESENT_ELSE(
1288 GC_old_ll_entries - GC_ll_hashtbl.entries, 0));
1290 #endif /* !SMALL_CONFIG */
1292 #endif /* !GC_NO_FINALIZATION */