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.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
15 /* Boehm, February 1, 1996 1:19 pm PST */
16 # define I_HIDE_POINTERS
17 # include "private/gc_pmark.h"
19 # ifdef FINALIZE_ON_DEMAND
20 int GC_finalize_on_demand = 1;
22 int GC_finalize_on_demand = 0;
25 # ifdef JAVA_FINALIZATION
26 int GC_java_finalization = 1;
28 int GC_java_finalization = 0;
31 /* Type of mark procedure used for marking from finalizable object. */
32 /* This procedure normally does not mark the object, only its */
34 typedef void finalization_mark_proc(/* ptr_t finalizable_obj_ptr */);
36 # define HASH3(addr,size,log_size) \
37 ((((word)(addr) >> 3) ^ ((word)(addr) >> (3+(log_size)))) \
39 #define HASH2(addr,log_size) HASH3(addr, 1 << log_size, log_size)
41 struct hash_chain_entry {
43 struct hash_chain_entry * next;
46 unsigned GC_finalization_failures = 0;
47 /* Number of finalization requests that failed for lack of memory. */
49 struct disappearing_link {
50 struct hash_chain_entry prolog;
51 # define dl_hidden_link prolog.hidden_key
52 /* Field to be cleared. */
53 # define dl_next(x) (struct disappearing_link *)((x) -> prolog.next)
54 # define dl_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
56 word dl_hidden_obj; /* Pointer to object base */
60 struct disappearing_link **head;
66 static int GC_register_disappearing_link_inner(struct dl_hashtbl_s *dl_hashtbl, GC_PTR * link, GC_PTR obj);
67 static int GC_unregister_disappearing_link_inner(struct dl_hashtbl_s *dl_hashtbl, GC_PTR * link);
69 static struct dl_hashtbl_s GC_dl_hashtbl = {
70 /* head */ NULL, /* log_size */ -1, /* entries */ 0 };
72 static struct dl_hashtbl_s GC_ll_hashtbl = { NULL, -1, 0 };
75 static struct finalizable_object {
76 struct hash_chain_entry prolog;
77 # define fo_hidden_base prolog.hidden_key
78 /* Pointer to object base. */
79 /* No longer hidden once object */
80 /* is on finalize_now queue. */
81 # define fo_next(x) (struct finalizable_object *)((x) -> prolog.next)
82 # define fo_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
83 GC_finalization_proc fo_fn; /* Finalizer. */
85 word fo_object_size; /* In bytes. */
86 finalization_mark_proc * fo_mark_proc; /* Mark-through procedure */
89 struct finalizable_object * GC_finalize_now = 0;
90 /* LIst of objects that should be finalized now. */
92 static signed_word log_fo_table_size = -1;
94 word GC_fo_entries = 0;
96 void GC_push_finalizer_structures GC_PROTO((void))
98 GC_push_all((ptr_t)(&GC_ll_hashtbl.head),
99 (ptr_t)(&GC_ll_hashtbl.head) + sizeof(word));
100 GC_push_all((ptr_t)(&GC_dl_hashtbl.head),
101 (ptr_t)(&GC_dl_hashtbl.head) + sizeof(word));
102 GC_push_all((ptr_t)(&fo_head), (ptr_t)(&fo_head) + sizeof(word));
103 GC_push_all((ptr_t)(&GC_finalize_now),
104 (ptr_t)(&GC_finalize_now) + sizeof(word));
107 /* Double the size of a hash table. *size_ptr is the log of its current */
108 /* size. May be a noop. */
109 /* *table is a pointer to an array of hash headers. If we succeed, we */
110 /* update both *table and *log_size_ptr. */
111 /* Lock is held. Signals are disabled. */
112 void GC_grow_table(table, log_size_ptr)
113 struct hash_chain_entry ***table;
114 signed_word * log_size_ptr;
117 register struct hash_chain_entry *p;
118 int log_old_size = *log_size_ptr;
119 register int log_new_size = log_old_size + 1;
120 word old_size = ((log_old_size == -1)? 0: (1 << log_old_size));
121 register word new_size = 1 << log_new_size;
122 struct hash_chain_entry **new_table = (struct hash_chain_entry **)
123 GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
124 (size_t)new_size * sizeof(struct hash_chain_entry *), NORMAL);
126 if (new_table == 0) {
128 ABORT("Insufficient space for initial table allocation");
133 for (i = 0; i < old_size; i++) {
136 register ptr_t real_key = (ptr_t)REVEAL_POINTER(p -> hidden_key);
137 register struct hash_chain_entry *next = p -> next;
138 register int new_hash = HASH3(real_key, new_size, log_new_size);
140 p -> next = new_table[new_hash];
141 new_table[new_hash] = p;
145 *log_size_ptr = log_new_size;
149 # if defined(__STDC__) || defined(__cplusplus)
150 int GC_register_disappearing_link(GC_PTR * link)
152 int GC_register_disappearing_link(link)
158 base = (ptr_t)GC_base((GC_PTR)link);
160 ABORT("Bad arg to GC_register_disappearing_link");
161 return(GC_general_register_disappearing_link(link, base));
164 # if defined(__STDC__) || defined(__cplusplus)
165 int GC_general_register_disappearing_link(GC_PTR * link,
168 int GC_general_register_disappearing_link(link, obj)
173 return GC_register_disappearing_link_inner(&GC_dl_hashtbl, link, obj);
176 # if defined(__STDC__) || defined(__cplusplus)
177 static int GC_register_disappearing_link_inner(struct dl_hashtbl_s *dl_hashtbl, GC_PTR * link,
180 static int GC_register_disappearing_link_inner(dl_hashtbl, link, obj)
181 struct dl_hashtbl_s *dl_hashtbl
186 struct disappearing_link *curr_dl;
188 struct disappearing_link * new_dl;
191 if ((word)link & (ALIGNMENT-1))
192 ABORT("Bad arg to GC_general_register_disappearing_link");
197 if (dl_hashtbl -> log_size == -1
198 || dl_hashtbl -> entries > ((word)1 << dl_hashtbl -> log_size)) {
202 GC_grow_table((struct hash_chain_entry ***)(&dl_hashtbl -> head),
203 &dl_hashtbl -> log_size);
205 if (GC_print_stats) {
206 GC_printf1("Grew dl table to %lu entries\n",
207 (unsigned long)(1 << dl_hashtbl -> log_size));
214 index = HASH2(link, dl_hashtbl -> log_size);
215 curr_dl = dl_hashtbl -> head[index];
216 for (curr_dl = dl_hashtbl -> head[index]; curr_dl != 0; curr_dl = dl_next(curr_dl)) {
217 if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
218 curr_dl -> dl_hidden_obj = HIDE_POINTER(obj);
226 new_dl = (struct disappearing_link *)
227 GC_INTERNAL_MALLOC(sizeof(struct disappearing_link),NORMAL);
233 new_dl = (struct disappearing_link *)
234 GC_oom_fn(sizeof(struct disappearing_link));
236 GC_finalization_failures++;
239 /* It's not likely we'll make it here, but ... */
245 new_dl -> dl_hidden_obj = HIDE_POINTER(obj);
246 new_dl -> dl_hidden_link = HIDE_POINTER(link);
247 dl_set_next(new_dl, dl_hashtbl -> head[index]);
248 dl_hashtbl -> head[index] = new_dl;
249 dl_hashtbl -> entries++;
257 # if defined(__STDC__) || defined(__cplusplus)
258 int GC_unregister_disappearing_link(GC_PTR * link)
260 int GC_unregister_disappearing_link(link)
264 return GC_unregister_disappearing_link_inner(&GC_dl_hashtbl, link);
267 # if defined(__STDC__) || defined(__cplusplus)
268 static int GC_unregister_disappearing_link_inner(struct dl_hashtbl_s *dl_hashtbl, GC_PTR * link)
270 static int GC_unregister_disappearing_link_inner(dl_hashtbl, link)
271 struct dl_hashtbl_s *dl_hashtbl;
275 struct disappearing_link *curr_dl, *prev_dl;
281 index = HASH2(link, dl_hashtbl->log_size);
282 if (((unsigned long)link & (ALIGNMENT-1))) goto out;
283 prev_dl = 0; curr_dl = dl_hashtbl -> head[index];
284 while (curr_dl != 0) {
285 if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
287 dl_hashtbl -> head[index] = dl_next(curr_dl);
289 dl_set_next(prev_dl, dl_next(curr_dl));
291 dl_hashtbl -> entries--;
295 dl_set_next(curr_dl, 0);
297 GC_free((GC_PTR)curr_dl);
302 curr_dl = dl_next(curr_dl);
310 /* toggleref support */
313 GC_hidden_pointer weak_ref;
316 static GC_ToggleRefStatus (*GC_toggleref_callback) (GC_PTR obj);
317 static GCToggleRef *GC_toggleref_array;
318 static int GC_toggleref_array_size;
319 static int GC_toggleref_array_capacity;
323 GC_process_togglerefs (void)
326 int toggle_ref_counts [3] = { 0, 0, 0 };
328 for (i = w = 0; i < GC_toggleref_array_size; ++i) {
329 GC_ToggleRefStatus res;
330 GCToggleRef r = GC_toggleref_array [i];
337 obj = REVEAL_POINTER (r.weak_ref);
341 res = GC_toggleref_callback (obj);
342 ++toggle_ref_counts [res];
344 case GC_TOGGLE_REF_DROP:
346 case GC_TOGGLE_REF_STRONG:
347 GC_toggleref_array [w].strong_ref = obj;
348 GC_toggleref_array [w].weak_ref = (GC_hidden_pointer)NULL;
351 case GC_TOGGLE_REF_WEAK:
352 GC_toggleref_array [w].strong_ref = NULL;
353 GC_toggleref_array [w].weak_ref = HIDE_POINTER (obj);
357 ABORT("Invalid callback result");
361 for (i = w; i < GC_toggleref_array_size; ++i) {
362 GC_toggleref_array [w].strong_ref = NULL;
363 GC_toggleref_array [w].weak_ref = (GC_hidden_pointer)NULL;
366 GC_toggleref_array_size = w;
369 /* Finalizer proc support */
370 static void (*GC_object_finalized_proc) (GC_PTR obj);
373 GC_set_await_finalize_proc (void (*proc) (GC_PTR obj))
375 GC_object_finalized_proc = proc;
379 static void push_and_mark_object (GC_PTR p)
383 PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top,
384 &(GC_mark_stack[GC_mark_stack_size]));
386 while (!GC_mark_stack_empty()) MARK_FROM_MARK_STACK();
388 if (GC_mark_state != MS_NONE)
389 while (!GC_mark_some((ptr_t)0)) {}
392 static void GC_mark_togglerefs ()
395 if (!GC_toggleref_array)
398 GC_set_mark_bit ((GC_PTR)GC_toggleref_array);
399 for (i = 0; i < GC_toggleref_array_size; ++i) {
400 if (GC_toggleref_array [i].strong_ref) {
401 GC_PTR object = GC_toggleref_array [i].strong_ref;
403 push_and_mark_object (object);
408 static void GC_clear_togglerefs ()
411 for (i = 0; i < GC_toggleref_array_size; ++i) {
412 if (GC_toggleref_array [i].weak_ref) {
413 GC_PTR object = REVEAL_POINTER (GC_toggleref_array [i].weak_ref);
415 if (!GC_is_marked (object)) {
416 GC_toggleref_array [i].weak_ref = (GC_hidden_pointer)NULL; /* We defer compaction to only happen on the callback step. */
418 /*No need to copy, boehm is non-moving */
426 void GC_set_toggleref_func(GC_ToggleRefStatus (*proccess_toggleref) (GC_PTR obj))
428 GC_toggleref_callback = proccess_toggleref;
432 ensure_toggleref_capacity (int capacity)
434 if (!GC_toggleref_array) {
435 GC_toggleref_array_capacity = 32;
436 GC_toggleref_array = (GCToggleRef *) GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE (GC_toggleref_array_capacity * sizeof (GCToggleRef), NORMAL);
437 if (!GC_toggleref_array)
440 if (GC_toggleref_array_size + capacity >= GC_toggleref_array_capacity) {
442 int old_capacity = GC_toggleref_array_capacity;
443 while (GC_toggleref_array_capacity < GC_toggleref_array_size + capacity)
444 GC_toggleref_array_capacity *= 2;
446 tmp = (GCToggleRef *) GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE (GC_toggleref_array_capacity * sizeof (GCToggleRef), NORMAL);
449 memcpy (tmp, GC_toggleref_array, GC_toggleref_array_size * sizeof (GCToggleRef));
450 GC_INTERNAL_FREE (GC_toggleref_array);
451 GC_toggleref_array = tmp;
457 GC_toggleref_add (GC_PTR object, int strong_ref)
459 int res = GC_SUCCESS;
466 if (!GC_toggleref_callback)
469 if (!ensure_toggleref_capacity (1)) {
473 GC_toggleref_array [GC_toggleref_array_size].strong_ref = strong_ref ? object : NULL;
474 GC_toggleref_array [GC_toggleref_array_size].weak_ref = strong_ref ? (GC_hidden_pointer)NULL : HIDE_POINTER (object);
475 ++GC_toggleref_array_size;
487 # if defined(__STDC__) || defined(__cplusplus)
488 int GC_register_long_link(GC_PTR * link, GC_PTR obj)
490 int GC_register_long_link(link, obj)
495 return GC_register_disappearing_link_inner(&GC_ll_hashtbl, link, obj);
498 # if defined(__STDC__) || defined(__cplusplus)
499 int GC_unregister_long_link(GC_PTR * link)
501 int GC_unregister_long_link(link)
505 return GC_unregister_disappearing_link_inner(&GC_ll_hashtbl, link);
508 /* Possible finalization_marker procedures. Note that mark stack */
509 /* overflow is handled by the caller, and is not a disaster. */
510 GC_API void GC_normal_finalize_mark_proc(p)
515 PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top,
516 &(GC_mark_stack[GC_mark_stack_size]));
519 /* This only pays very partial attention to the mark descriptor. */
520 /* It does the right thing for normal and atomic objects, and treats */
521 /* most others as normal. */
522 GC_API void GC_ignore_self_finalize_mark_proc(p)
526 word descr = hhdr -> hb_descr;
529 ptr_t target_limit = p + WORDS_TO_BYTES(hhdr -> hb_sz) - 1;
531 if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
532 scan_limit = p + descr - sizeof(word);
534 scan_limit = target_limit + 1 - sizeof(word);
536 for (q = p; q <= scan_limit; q += ALIGNMENT) {
538 if (r < p || r > target_limit) {
539 GC_PUSH_ONE_HEAP((word)r, q);
545 GC_API void GC_null_finalize_mark_proc(p)
552 /* Register a finalization function. See gc.h for details. */
553 /* in the nonthreads case, we try to avoid disabling signals, */
554 /* since it can be expensive. Threads packages typically */
555 /* make it cheaper. */
556 /* The last parameter is a procedure that determines */
557 /* marking for finalization ordering. Any objects marked */
558 /* by that procedure will be guaranteed to not have been */
559 /* finalized when this finalizer is invoked. */
560 GC_API void GC_register_finalizer_inner(obj, fn, cd, ofn, ocd, mp)
562 GC_finalization_proc fn;
564 GC_finalization_proc * ofn;
566 finalization_mark_proc * mp;
569 struct finalizable_object * curr_fo, * prev_fo;
571 struct finalizable_object *new_fo;
579 if (log_fo_table_size == -1
580 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
584 GC_grow_table((struct hash_chain_entry ***)(&fo_head),
587 if (GC_print_stats) {
588 GC_printf1("Grew fo table to %lu entries\n",
589 (unsigned long)(1 << log_fo_table_size));
596 /* in the THREADS case signals are disabled and we hold allocation */
597 /* lock; otherwise neither is true. Proceed carefully. */
599 index = HASH2(base, log_fo_table_size);
600 prev_fo = 0; curr_fo = fo_head[index];
601 while (curr_fo != 0) {
602 if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
603 /* Interruption by a signal in the middle of this */
604 /* should be safe. The client may see only *ocd */
605 /* updated, but we'll declare that to be his */
607 if (ocd) *ocd = (GC_PTR) curr_fo -> fo_client_data;
608 if (ofn) *ofn = curr_fo -> fo_fn;
609 /* Delete the structure for base. */
611 fo_head[index] = fo_next(curr_fo);
613 fo_set_next(prev_fo, fo_next(curr_fo));
617 /* May not happen if we get a signal. But a high */
618 /* estimate will only make the table larger than */
620 # if !defined(THREADS) && !defined(DBG_HDRS_ALL)
621 GC_free((GC_PTR)curr_fo);
624 curr_fo -> fo_fn = fn;
625 curr_fo -> fo_client_data = (ptr_t)cd;
626 curr_fo -> fo_mark_proc = mp;
627 /* Reinsert it. We deleted it first to maintain */
628 /* consistency in the event of a signal. */
630 fo_head[index] = curr_fo;
632 fo_set_next(prev_fo, curr_fo);
642 curr_fo = fo_next(curr_fo);
655 /* We won't collect it, hence finalizer wouldn't be run. */
662 new_fo = (struct finalizable_object *)
663 GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
669 new_fo = (struct finalizable_object *)
670 GC_oom_fn(sizeof(struct finalizable_object));
672 GC_finalization_failures++;
675 /* It's not likely we'll make it here, but ... */
681 new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
682 new_fo -> fo_fn = fn;
683 new_fo -> fo_client_data = (ptr_t)cd;
684 new_fo -> fo_object_size = hhdr -> hb_sz;
685 new_fo -> fo_mark_proc = mp;
686 fo_set_next(new_fo, fo_head[index]);
688 fo_head[index] = new_fo;
695 # if defined(__STDC__)
696 void GC_register_finalizer(void * obj,
697 GC_finalization_proc fn, void * cd,
698 GC_finalization_proc *ofn, void ** ocd)
700 void GC_register_finalizer(obj, fn, cd, ofn, ocd)
702 GC_finalization_proc fn;
704 GC_finalization_proc * ofn;
708 GC_register_finalizer_inner(obj, fn, cd, ofn,
709 ocd, GC_normal_finalize_mark_proc);
712 # if defined(__STDC__)
713 void GC_register_finalizer_ignore_self(void * obj,
714 GC_finalization_proc fn, void * cd,
715 GC_finalization_proc *ofn, void ** ocd)
717 void GC_register_finalizer_ignore_self(obj, fn, cd, ofn, ocd)
719 GC_finalization_proc fn;
721 GC_finalization_proc * ofn;
725 GC_register_finalizer_inner(obj, fn, cd, ofn,
726 ocd, GC_ignore_self_finalize_mark_proc);
729 # if defined(__STDC__)
730 void GC_register_finalizer_no_order(void * obj,
731 GC_finalization_proc fn, void * cd,
732 GC_finalization_proc *ofn, void ** ocd)
734 void GC_register_finalizer_no_order(obj, fn, cd, ofn, ocd)
736 GC_finalization_proc fn;
738 GC_finalization_proc * ofn;
742 GC_register_finalizer_inner(obj, fn, cd, ofn,
743 ocd, GC_null_finalize_mark_proc);
748 static void GC_dump_finalization_links(struct dl_hashtbl_s *dl_hashtbl)
750 struct disappearing_link *curr_dl;
751 ptr_t real_ptr, real_link;
752 size_t dl_size = dl_hashtbl->log_size == -1 ? 0 :
753 1 << dl_hashtbl->log_size;
756 for (i = 0; i < dl_size; i++) {
757 for (curr_dl = dl_hashtbl -> head[i]; curr_dl != 0;
758 curr_dl = dl_next(curr_dl)) {
759 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
760 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
761 GC_printf2("Object: %lx, link: %lx\n", real_ptr, real_link);
766 void GC_dump_finalization()
768 struct finalizable_object * curr_fo;
770 int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
773 GC_printf0("Disappearing (short) links:\n");
774 GC_dump_finalization_links(&GC_dl_hashtbl);
775 GC_printf0("Disappearing long links:\n");
776 GC_dump_finalization_links(&GC_ll_hashtbl);
778 GC_printf0("Finalizers:\n");
779 for (i = 0; i < fo_size; i++) {
780 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
781 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
782 GC_printf1("Finalizable object: 0x%lx\n", real_ptr);
788 static void GC_make_disappearing_links_disappear(struct dl_hashtbl_s *dl_hashtbl)
790 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
791 ptr_t real_ptr, real_link;
793 int dl_size = (dl_hashtbl -> log_size == -1 ) ? 0 : (1 << dl_hashtbl -> log_size);
795 for (i = 0; i < dl_size; i++) {
796 curr_dl = dl_hashtbl -> head[i];
798 while (curr_dl != 0) {
799 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
800 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
801 if (!GC_is_marked(real_ptr)) {
802 *(word *)real_link = 0;
803 next_dl = dl_next(curr_dl);
805 dl_hashtbl -> head[i] = next_dl;
807 dl_set_next(prev_dl, next_dl);
809 GC_clear_mark_bit((ptr_t)curr_dl);
810 dl_hashtbl -> entries--;
814 curr_dl = dl_next(curr_dl);
820 static void GC_remove_dangling_disappearing_links(struct dl_hashtbl_s *dl_hashtbl)
822 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
823 ptr_t real_ptr, real_link;
825 int dl_size = (dl_hashtbl -> log_size == -1 ) ? 0 : (1 << dl_hashtbl -> log_size);
827 for (i = 0; i < dl_size; i++) {
828 curr_dl = dl_hashtbl -> head[i];
830 while (curr_dl != 0) {
831 real_link = GC_base((ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link));
832 if (real_link != 0 && !GC_is_marked(real_link)) {
833 next_dl = dl_next(curr_dl);
835 dl_hashtbl -> head[i] = next_dl;
837 dl_set_next(prev_dl, next_dl);
839 GC_clear_mark_bit((ptr_t)curr_dl);
840 dl_hashtbl -> entries--;
844 curr_dl = dl_next(curr_dl);
850 /* Called with world stopped. Cause disappearing links to disappear, */
851 /* and invoke finalizers. */
854 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
857 int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
859 GC_mark_togglerefs();
861 /* Make non-tracking disappearing links disappear */
862 GC_make_disappearing_links_disappear(&GC_dl_hashtbl);
864 /* Mark all objects reachable via chains of 1 or more pointers */
865 /* from finalizable objects. */
866 GC_ASSERT(GC_mark_state == MS_NONE);
867 for (i = 0; i < fo_size; i++) {
868 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
869 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
870 if (!GC_is_marked(real_ptr)) {
871 GC_MARKED_FOR_FINALIZATION(real_ptr);
872 GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
873 if (GC_is_marked(real_ptr)) {
874 WARN("Finalization cycle involving %lx\n", real_ptr);
879 /* Enqueue for finalization all objects that are still */
881 GC_words_finalized = 0;
882 for (i = 0; i < fo_size; i++) {
883 curr_fo = fo_head[i];
885 while (curr_fo != 0) {
886 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
887 if (!GC_is_marked(real_ptr)) {
888 if (!GC_java_finalization) {
889 GC_set_mark_bit(real_ptr);
891 /* Delete from hash table */
892 next_fo = fo_next(curr_fo);
894 fo_head[i] = next_fo;
896 fo_set_next(prev_fo, next_fo);
900 if (GC_object_finalized_proc)
901 GC_object_finalized_proc (real_ptr);
903 /* Add to list of objects awaiting finalization. */
904 fo_set_next(curr_fo, GC_finalize_now);
905 GC_finalize_now = curr_fo;
906 /* unhide object pointer so any future collections will */
908 curr_fo -> fo_hidden_base =
909 (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
910 GC_words_finalized +=
911 ALIGNED_WORDS(curr_fo -> fo_object_size)
912 + ALIGNED_WORDS(sizeof(struct finalizable_object));
913 GC_ASSERT(GC_is_marked(GC_base((ptr_t)curr_fo)));
917 curr_fo = fo_next(curr_fo);
922 if (GC_java_finalization) {
923 /* make sure we mark everything reachable from objects finalized
924 using the no_order mark_proc */
925 for (curr_fo = GC_finalize_now;
926 curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
927 real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
928 if (!GC_is_marked(real_ptr)) {
929 if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
930 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
932 GC_set_mark_bit(real_ptr);
937 /* Remove dangling disappearing links. */
938 GC_remove_dangling_disappearing_links(&GC_dl_hashtbl);
940 GC_clear_togglerefs ();
942 /* Make long links disappear and remove dangling ones. */
943 GC_make_disappearing_links_disappear(&GC_ll_hashtbl);
944 GC_remove_dangling_disappearing_links(&GC_ll_hashtbl);
947 #ifndef JAVA_FINALIZATION_NOT_NEEDED
949 /* Enqueue all remaining finalizers to be run - Assumes lock is
950 * held, and signals are disabled */
951 void GC_enqueue_all_finalizers()
953 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
958 fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
959 GC_words_finalized = 0;
960 for (i = 0; i < fo_size; i++) {
961 curr_fo = fo_head[i];
963 while (curr_fo != 0) {
964 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
965 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
966 GC_set_mark_bit(real_ptr);
968 /* Delete from hash table */
969 next_fo = fo_next(curr_fo);
971 fo_head[i] = next_fo;
973 fo_set_next(prev_fo, next_fo);
977 /* Add to list of objects awaiting finalization. */
978 fo_set_next(curr_fo, GC_finalize_now);
979 GC_finalize_now = curr_fo;
981 /* unhide object pointer so any future collections will */
983 curr_fo -> fo_hidden_base =
984 (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
986 GC_words_finalized +=
987 ALIGNED_WORDS(curr_fo -> fo_object_size)
988 + ALIGNED_WORDS(sizeof(struct finalizable_object));
996 /* Invoke all remaining finalizers that haven't yet been run.
997 * This is needed for strict compliance with the Java standard,
998 * which can make the runtime guarantee that all finalizers are run.
999 * Unfortunately, the Java standard implies we have to keep running
1000 * finalizers until there are no more left, a potential infinite loop.
1002 * Note that this is even more dangerous than the usual Java
1003 * finalizers, in that objects reachable from static variables
1004 * may have been finalized when these finalizers are run.
1005 * Finalizers run at this point must be prepared to deal with a
1006 * mostly broken world.
1007 * This routine is externally callable, so is called without
1008 * the allocation lock.
1010 GC_API void GC_finalize_all()
1016 while (GC_fo_entries > 0) {
1017 GC_enqueue_all_finalizers();
1020 GC_INVOKE_FINALIZERS();
1029 /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
1030 /* finalizers can only be called from some kind of `safe state' and */
1031 /* getting into that safe state is expensive.) */
1032 int GC_should_invoke_finalizers GC_PROTO((void))
1034 return GC_finalize_now != 0;
1037 /* Invoke finalizers for all objects that are ready to be finalized. */
1038 /* Should be called without allocation lock. */
1039 int GC_invoke_finalizers()
1041 struct finalizable_object * curr_fo;
1043 word mem_freed_before;
1046 while (GC_finalize_now != 0) {
1052 mem_freed_before = GC_mem_freed;
1054 curr_fo = GC_finalize_now;
1056 if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
1059 if (curr_fo == 0) break;
1061 GC_finalize_now = fo_next(curr_fo);
1063 fo_set_next(curr_fo, 0);
1064 (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
1065 curr_fo -> fo_client_data);
1066 curr_fo -> fo_client_data = 0;
1069 /* This is probably a bad idea. It throws off accounting if */
1070 /* nearly all objects are finalizable. O.w. it shouldn't */
1072 GC_free((GC_PTR)curr_fo);
1075 if (count != 0 && mem_freed_before != GC_mem_freed) {
1077 GC_finalizer_mem_freed += (GC_mem_freed - mem_freed_before);
1083 void (* GC_finalizer_notifier)() = (void (*) GC_PROTO((void)))0;
1085 static GC_word last_finalizer_notification = 0;
1087 void GC_notify_or_invoke_finalizers GC_PROTO((void))
1089 /* This is a convenient place to generate backtraces if appropriate, */
1090 /* since that code is not callable with the allocation lock. */
1091 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1092 static word last_back_trace_gc_no = 1; /* Skip first one. */
1094 if (GC_gc_no > last_back_trace_gc_no) {
1097 # ifdef KEEP_BACK_PTRS
1099 /* Stops when GC_gc_no wraps; that's OK. */
1100 last_back_trace_gc_no = (word)(-1); /* disable others. */
1101 for (i = 0; i < GC_backtraces; ++i) {
1102 /* FIXME: This tolerates concurrent heap mutation, */
1103 /* which may cause occasional mysterious results. */
1104 /* We need to release the GC lock, since GC_print_callers */
1105 /* acquires it. It probably shouldn't. */
1107 GC_generate_random_backtrace_no_gc();
1110 last_back_trace_gc_no = GC_gc_no;
1113 # ifdef MAKE_BACK_GRAPH
1114 if (GC_print_back_height)
1115 GC_print_back_graph_stats();
1119 if (GC_finalize_now == 0) return;
1120 if (!GC_finalize_on_demand) {
1121 (void) GC_invoke_finalizers();
1123 GC_ASSERT(GC_finalize_now == 0);
1124 # endif /* Otherwise GC can run concurrently and add more */
1127 if (GC_finalizer_notifier != (void (*) GC_PROTO((void)))0
1128 && last_finalizer_notification != GC_gc_no) {
1129 last_finalizer_notification = GC_gc_no;
1130 GC_finalizer_notifier();
1135 GC_PTR GC_call_with_alloc_lock(GC_fn_type fn,
1138 GC_PTR GC_call_with_alloc_lock(fn, client_data)
1151 result = (*fn)(client_data);
1153 # ifndef GC_ASSERTIONS
1154 UNSET_LOCK_HOLDER();
1155 # endif /* o.w. UNLOCK() does it implicitly */
1162 #if !defined(NO_DEBUGGING)
1164 void GC_print_finalization_stats()
1166 struct finalizable_object *fo = GC_finalize_now;
1169 GC_printf3("%lu finalization table entries; %lu/%lu short/long disappearing links alive\n",
1170 GC_fo_entries, (unsigned long)GC_dl_hashtbl.entries, (unsigned long)GC_ll_hashtbl.entries);
1171 for (; 0 != fo; fo = fo_next(fo)) ++ready;
1172 GC_printf1("%lu objects are eligible for immediate finalization\n", ready);
1175 #endif /* NO_DEBUGGING */