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 int (*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) {
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];
347 GC_toggleref_array [w].strong_ref = obj;
348 GC_toggleref_array [w].weak_ref = (GC_hidden_pointer)NULL;
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_finalizer_notify_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_toggleref_register_callback(int (*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);
438 if (GC_toggleref_array_size + capacity >= GC_toggleref_array_capacity) {
440 int old_capacity = GC_toggleref_array_capacity;
441 while (GC_toggleref_array_capacity < GC_toggleref_array_size + capacity)
442 GC_toggleref_array_capacity *= 2;
444 tmp = (GCToggleRef *) GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE (GC_toggleref_array_capacity * sizeof (GCToggleRef), NORMAL);
445 memcpy (tmp, GC_toggleref_array, GC_toggleref_array_size * sizeof (GCToggleRef));
446 GC_INTERNAL_FREE (GC_toggleref_array);
447 GC_toggleref_array = tmp;
452 GC_toggleref_add (GC_PTR object, int strong_ref)
460 if (!GC_toggleref_callback)
463 ensure_toggleref_capacity (1);
464 GC_toggleref_array [GC_toggleref_array_size].strong_ref = strong_ref ? object : NULL;
465 GC_toggleref_array [GC_toggleref_array_size].weak_ref = strong_ref ? (GC_hidden_pointer)NULL : HIDE_POINTER (object);
466 ++GC_toggleref_array_size;
477 # if defined(__STDC__) || defined(__cplusplus)
478 int GC_register_long_link(GC_PTR * link, GC_PTR obj)
480 int GC_register_long_link(link, obj)
485 return GC_register_disappearing_link_inner(&GC_ll_hashtbl, link, obj);
488 # if defined(__STDC__) || defined(__cplusplus)
489 int GC_unregister_long_link(GC_PTR * link)
491 int GC_unregister_long_link(link)
495 return GC_unregister_disappearing_link_inner(&GC_ll_hashtbl, link);
498 /* Possible finalization_marker procedures. Note that mark stack */
499 /* overflow is handled by the caller, and is not a disaster. */
500 GC_API void GC_normal_finalize_mark_proc(p)
505 PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top,
506 &(GC_mark_stack[GC_mark_stack_size]));
509 /* This only pays very partial attention to the mark descriptor. */
510 /* It does the right thing for normal and atomic objects, and treats */
511 /* most others as normal. */
512 GC_API void GC_ignore_self_finalize_mark_proc(p)
516 word descr = hhdr -> hb_descr;
519 ptr_t target_limit = p + WORDS_TO_BYTES(hhdr -> hb_sz) - 1;
521 if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
522 scan_limit = p + descr - sizeof(word);
524 scan_limit = target_limit + 1 - sizeof(word);
526 for (q = p; q <= scan_limit; q += ALIGNMENT) {
528 if (r < p || r > target_limit) {
529 GC_PUSH_ONE_HEAP((word)r, q);
535 GC_API void GC_null_finalize_mark_proc(p)
542 /* Register a finalization function. See gc.h for details. */
543 /* in the nonthreads case, we try to avoid disabling signals, */
544 /* since it can be expensive. Threads packages typically */
545 /* make it cheaper. */
546 /* The last parameter is a procedure that determines */
547 /* marking for finalization ordering. Any objects marked */
548 /* by that procedure will be guaranteed to not have been */
549 /* finalized when this finalizer is invoked. */
550 GC_API void GC_register_finalizer_inner(obj, fn, cd, ofn, ocd, mp)
552 GC_finalization_proc fn;
554 GC_finalization_proc * ofn;
556 finalization_mark_proc * mp;
559 struct finalizable_object * curr_fo, * prev_fo;
561 struct finalizable_object *new_fo;
569 if (log_fo_table_size == -1
570 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
574 GC_grow_table((struct hash_chain_entry ***)(&fo_head),
577 if (GC_print_stats) {
578 GC_printf1("Grew fo table to %lu entries\n",
579 (unsigned long)(1 << log_fo_table_size));
586 /* in the THREADS case signals are disabled and we hold allocation */
587 /* lock; otherwise neither is true. Proceed carefully. */
589 index = HASH2(base, log_fo_table_size);
590 prev_fo = 0; curr_fo = fo_head[index];
591 while (curr_fo != 0) {
592 if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
593 /* Interruption by a signal in the middle of this */
594 /* should be safe. The client may see only *ocd */
595 /* updated, but we'll declare that to be his */
597 if (ocd) *ocd = (GC_PTR) curr_fo -> fo_client_data;
598 if (ofn) *ofn = curr_fo -> fo_fn;
599 /* Delete the structure for base. */
601 fo_head[index] = fo_next(curr_fo);
603 fo_set_next(prev_fo, fo_next(curr_fo));
607 /* May not happen if we get a signal. But a high */
608 /* estimate will only make the table larger than */
610 # if !defined(THREADS) && !defined(DBG_HDRS_ALL)
611 GC_free((GC_PTR)curr_fo);
614 curr_fo -> fo_fn = fn;
615 curr_fo -> fo_client_data = (ptr_t)cd;
616 curr_fo -> fo_mark_proc = mp;
617 /* Reinsert it. We deleted it first to maintain */
618 /* consistency in the event of a signal. */
620 fo_head[index] = curr_fo;
622 fo_set_next(prev_fo, curr_fo);
632 curr_fo = fo_next(curr_fo);
645 /* We won't collect it, hence finalizer wouldn't be run. */
652 new_fo = (struct finalizable_object *)
653 GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
659 new_fo = (struct finalizable_object *)
660 GC_oom_fn(sizeof(struct finalizable_object));
662 GC_finalization_failures++;
665 /* It's not likely we'll make it here, but ... */
671 new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
672 new_fo -> fo_fn = fn;
673 new_fo -> fo_client_data = (ptr_t)cd;
674 new_fo -> fo_object_size = hhdr -> hb_sz;
675 new_fo -> fo_mark_proc = mp;
676 fo_set_next(new_fo, fo_head[index]);
678 fo_head[index] = new_fo;
685 # if defined(__STDC__)
686 void GC_register_finalizer(void * obj,
687 GC_finalization_proc fn, void * cd,
688 GC_finalization_proc *ofn, void ** ocd)
690 void GC_register_finalizer(obj, fn, cd, ofn, ocd)
692 GC_finalization_proc fn;
694 GC_finalization_proc * ofn;
698 GC_register_finalizer_inner(obj, fn, cd, ofn,
699 ocd, GC_normal_finalize_mark_proc);
702 # if defined(__STDC__)
703 void GC_register_finalizer_ignore_self(void * obj,
704 GC_finalization_proc fn, void * cd,
705 GC_finalization_proc *ofn, void ** ocd)
707 void GC_register_finalizer_ignore_self(obj, fn, cd, ofn, ocd)
709 GC_finalization_proc fn;
711 GC_finalization_proc * ofn;
715 GC_register_finalizer_inner(obj, fn, cd, ofn,
716 ocd, GC_ignore_self_finalize_mark_proc);
719 # if defined(__STDC__)
720 void GC_register_finalizer_no_order(void * obj,
721 GC_finalization_proc fn, void * cd,
722 GC_finalization_proc *ofn, void ** ocd)
724 void GC_register_finalizer_no_order(obj, fn, cd, ofn, ocd)
726 GC_finalization_proc fn;
728 GC_finalization_proc * ofn;
732 GC_register_finalizer_inner(obj, fn, cd, ofn,
733 ocd, GC_null_finalize_mark_proc);
738 static void GC_dump_finalization_links(struct dl_hashtbl_s *dl_hashtbl)
740 struct disappearing_link *curr_dl;
741 ptr_t real_ptr, real_link;
742 size_t dl_size = dl_hashtbl->log_size == -1 ? 0 :
743 1 << dl_hashtbl->log_size;
746 for (i = 0; i < dl_size; i++) {
747 for (curr_dl = dl_hashtbl -> head[i]; curr_dl != 0;
748 curr_dl = dl_next(curr_dl)) {
749 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
750 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
751 GC_printf2("Object: %lx, link: %lx\n", real_ptr, real_link);
756 void GC_dump_finalization()
758 struct finalizable_object * curr_fo;
760 int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
763 GC_printf0("Disappearing (short) links:\n");
764 GC_dump_finalization_links(&GC_dl_hashtbl);
765 GC_printf0("Disappearing long links:\n");
766 GC_dump_finalization_links(&GC_ll_hashtbl);
768 GC_printf0("Finalizers:\n");
769 for (i = 0; i < fo_size; i++) {
770 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
771 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
772 GC_printf1("Finalizable object: 0x%lx\n", real_ptr);
778 static void GC_make_disappearing_links_disappear(struct dl_hashtbl_s *dl_hashtbl)
780 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
781 ptr_t real_ptr, real_link;
783 int dl_size = (dl_hashtbl -> log_size == -1 ) ? 0 : (1 << dl_hashtbl -> log_size);
785 for (i = 0; i < dl_size; i++) {
786 curr_dl = dl_hashtbl -> head[i];
788 while (curr_dl != 0) {
789 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
790 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
791 if (!GC_is_marked(real_ptr)) {
792 *(word *)real_link = 0;
793 next_dl = dl_next(curr_dl);
795 dl_hashtbl -> head[i] = next_dl;
797 dl_set_next(prev_dl, next_dl);
799 GC_clear_mark_bit((ptr_t)curr_dl);
800 dl_hashtbl -> entries--;
804 curr_dl = dl_next(curr_dl);
810 static void GC_remove_dangling_disappearing_links(struct dl_hashtbl_s *dl_hashtbl)
812 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
813 ptr_t real_ptr, real_link;
815 int dl_size = (dl_hashtbl -> log_size == -1 ) ? 0 : (1 << dl_hashtbl -> log_size);
817 for (i = 0; i < dl_size; i++) {
818 curr_dl = dl_hashtbl -> head[i];
820 while (curr_dl != 0) {
821 real_link = GC_base((ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link));
822 if (real_link != 0 && !GC_is_marked(real_link)) {
823 next_dl = dl_next(curr_dl);
825 dl_hashtbl -> head[i] = next_dl;
827 dl_set_next(prev_dl, next_dl);
829 GC_clear_mark_bit((ptr_t)curr_dl);
830 dl_hashtbl -> entries--;
834 curr_dl = dl_next(curr_dl);
840 /* Called with world stopped. Cause disappearing links to disappear, */
841 /* and invoke finalizers. */
844 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
847 int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
849 GC_mark_togglerefs();
851 /* Make non-tracking disappearing links disappear */
852 GC_make_disappearing_links_disappear(&GC_dl_hashtbl);
854 /* Mark all objects reachable via chains of 1 or more pointers */
855 /* from finalizable objects. */
856 GC_ASSERT(GC_mark_state == MS_NONE);
857 for (i = 0; i < fo_size; i++) {
858 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
859 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
860 if (!GC_is_marked(real_ptr)) {
861 GC_MARKED_FOR_FINALIZATION(real_ptr);
862 GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
863 if (GC_is_marked(real_ptr)) {
864 WARN("Finalization cycle involving %lx\n", real_ptr);
869 /* Enqueue for finalization all objects that are still */
871 GC_words_finalized = 0;
872 for (i = 0; i < fo_size; i++) {
873 curr_fo = fo_head[i];
875 while (curr_fo != 0) {
876 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
877 if (!GC_is_marked(real_ptr)) {
878 if (!GC_java_finalization) {
879 GC_set_mark_bit(real_ptr);
881 /* Delete from hash table */
882 next_fo = fo_next(curr_fo);
884 fo_head[i] = next_fo;
886 fo_set_next(prev_fo, next_fo);
890 if (GC_object_finalized_proc)
891 GC_object_finalized_proc (real_ptr);
893 /* Add to list of objects awaiting finalization. */
894 fo_set_next(curr_fo, GC_finalize_now);
895 GC_finalize_now = curr_fo;
896 /* unhide object pointer so any future collections will */
898 curr_fo -> fo_hidden_base =
899 (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
900 GC_words_finalized +=
901 ALIGNED_WORDS(curr_fo -> fo_object_size)
902 + ALIGNED_WORDS(sizeof(struct finalizable_object));
903 GC_ASSERT(GC_is_marked(GC_base((ptr_t)curr_fo)));
907 curr_fo = fo_next(curr_fo);
912 if (GC_java_finalization) {
913 /* make sure we mark everything reachable from objects finalized
914 using the no_order mark_proc */
915 for (curr_fo = GC_finalize_now;
916 curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
917 real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
918 if (!GC_is_marked(real_ptr)) {
919 if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
920 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
922 GC_set_mark_bit(real_ptr);
927 /* Remove dangling disappearing links. */
928 GC_remove_dangling_disappearing_links(&GC_dl_hashtbl);
930 GC_clear_togglerefs ();
932 /* Make long links disappear and remove dangling ones. */
933 GC_make_disappearing_links_disappear(&GC_ll_hashtbl);
934 GC_remove_dangling_disappearing_links(&GC_ll_hashtbl);
937 #ifndef JAVA_FINALIZATION_NOT_NEEDED
939 /* Enqueue all remaining finalizers to be run - Assumes lock is
940 * held, and signals are disabled */
941 void GC_enqueue_all_finalizers()
943 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
948 fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
949 GC_words_finalized = 0;
950 for (i = 0; i < fo_size; i++) {
951 curr_fo = fo_head[i];
953 while (curr_fo != 0) {
954 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
955 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
956 GC_set_mark_bit(real_ptr);
958 /* Delete from hash table */
959 next_fo = fo_next(curr_fo);
961 fo_head[i] = next_fo;
963 fo_set_next(prev_fo, next_fo);
967 /* Add to list of objects awaiting finalization. */
968 fo_set_next(curr_fo, GC_finalize_now);
969 GC_finalize_now = curr_fo;
971 /* unhide object pointer so any future collections will */
973 curr_fo -> fo_hidden_base =
974 (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
976 GC_words_finalized +=
977 ALIGNED_WORDS(curr_fo -> fo_object_size)
978 + ALIGNED_WORDS(sizeof(struct finalizable_object));
986 /* Invoke all remaining finalizers that haven't yet been run.
987 * This is needed for strict compliance with the Java standard,
988 * which can make the runtime guarantee that all finalizers are run.
989 * Unfortunately, the Java standard implies we have to keep running
990 * finalizers until there are no more left, a potential infinite loop.
992 * Note that this is even more dangerous than the usual Java
993 * finalizers, in that objects reachable from static variables
994 * may have been finalized when these finalizers are run.
995 * Finalizers run at this point must be prepared to deal with a
996 * mostly broken world.
997 * This routine is externally callable, so is called without
998 * the allocation lock.
1000 GC_API void GC_finalize_all()
1006 while (GC_fo_entries > 0) {
1007 GC_enqueue_all_finalizers();
1010 GC_INVOKE_FINALIZERS();
1019 /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
1020 /* finalizers can only be called from some kind of `safe state' and */
1021 /* getting into that safe state is expensive.) */
1022 int GC_should_invoke_finalizers GC_PROTO((void))
1024 return GC_finalize_now != 0;
1027 /* Invoke finalizers for all objects that are ready to be finalized. */
1028 /* Should be called without allocation lock. */
1029 int GC_invoke_finalizers()
1031 struct finalizable_object * curr_fo;
1033 word mem_freed_before;
1036 while (GC_finalize_now != 0) {
1042 mem_freed_before = GC_mem_freed;
1044 curr_fo = GC_finalize_now;
1046 if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
1049 if (curr_fo == 0) break;
1051 GC_finalize_now = fo_next(curr_fo);
1053 fo_set_next(curr_fo, 0);
1054 (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
1055 curr_fo -> fo_client_data);
1056 curr_fo -> fo_client_data = 0;
1059 /* This is probably a bad idea. It throws off accounting if */
1060 /* nearly all objects are finalizable. O.w. it shouldn't */
1062 GC_free((GC_PTR)curr_fo);
1065 if (count != 0 && mem_freed_before != GC_mem_freed) {
1067 GC_finalizer_mem_freed += (GC_mem_freed - mem_freed_before);
1073 void (* GC_finalizer_notifier)() = (void (*) GC_PROTO((void)))0;
1075 static GC_word last_finalizer_notification = 0;
1077 void GC_notify_or_invoke_finalizers GC_PROTO((void))
1079 /* This is a convenient place to generate backtraces if appropriate, */
1080 /* since that code is not callable with the allocation lock. */
1081 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1082 static word last_back_trace_gc_no = 1; /* Skip first one. */
1084 if (GC_gc_no > last_back_trace_gc_no) {
1087 # ifdef KEEP_BACK_PTRS
1089 /* Stops when GC_gc_no wraps; that's OK. */
1090 last_back_trace_gc_no = (word)(-1); /* disable others. */
1091 for (i = 0; i < GC_backtraces; ++i) {
1092 /* FIXME: This tolerates concurrent heap mutation, */
1093 /* which may cause occasional mysterious results. */
1094 /* We need to release the GC lock, since GC_print_callers */
1095 /* acquires it. It probably shouldn't. */
1097 GC_generate_random_backtrace_no_gc();
1100 last_back_trace_gc_no = GC_gc_no;
1103 # ifdef MAKE_BACK_GRAPH
1104 if (GC_print_back_height)
1105 GC_print_back_graph_stats();
1109 if (GC_finalize_now == 0) return;
1110 if (!GC_finalize_on_demand) {
1111 (void) GC_invoke_finalizers();
1113 GC_ASSERT(GC_finalize_now == 0);
1114 # endif /* Otherwise GC can run concurrently and add more */
1117 if (GC_finalizer_notifier != (void (*) GC_PROTO((void)))0
1118 && last_finalizer_notification != GC_gc_no) {
1119 last_finalizer_notification = GC_gc_no;
1120 GC_finalizer_notifier();
1125 GC_PTR GC_call_with_alloc_lock(GC_fn_type fn,
1128 GC_PTR GC_call_with_alloc_lock(fn, client_data)
1141 result = (*fn)(client_data);
1143 # ifndef GC_ASSERTIONS
1144 UNSET_LOCK_HOLDER();
1145 # endif /* o.w. UNLOCK() does it implicitly */
1152 #if !defined(NO_DEBUGGING)
1154 void GC_print_finalization_stats()
1156 struct finalizable_object *fo = GC_finalize_now;
1159 GC_printf3("%lu finalization table entries; %lu/%lu short/long disappearing links alive\n",
1160 GC_fo_entries, (unsigned long)GC_dl_hashtbl.entries, (unsigned long)GC_ll_hashtbl.entries);
1161 for (; 0 != fo; fo = fo_next(fo)) ++ready;
1162 GC_printf1("%lu objects are eligible for immediate finalization\n", ready);
1165 #endif /* NO_DEBUGGING */