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;
370 static void push_and_mark_object (GC_PTR p)
374 PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top,
375 &(GC_mark_stack[GC_mark_stack_size]));
377 while (!GC_mark_stack_empty()) MARK_FROM_MARK_STACK();
379 if (GC_mark_state != MS_NONE)
380 while (!GC_mark_some((ptr_t)0)) {}
383 static void GC_mark_togglerefs ()
386 if (!GC_toggleref_array)
389 GC_set_mark_bit ((GC_PTR)GC_toggleref_array);
390 for (i = 0; i < GC_toggleref_array_size; ++i) {
391 if (GC_toggleref_array [i].strong_ref) {
392 GC_PTR object = GC_toggleref_array [i].strong_ref;
394 push_and_mark_object (object);
399 static void GC_clear_togglerefs ()
402 for (i = 0; i < GC_toggleref_array_size; ++i) {
403 if (GC_toggleref_array [i].weak_ref) {
404 GC_PTR object = REVEAL_POINTER (GC_toggleref_array [i].weak_ref);
406 if (!GC_is_marked (object)) {
407 GC_toggleref_array [i].weak_ref = (GC_hidden_pointer)NULL; /* We defer compaction to only happen on the callback step. */
409 /*No need to copy, boehm is non-moving */
417 void GC_toggleref_register_callback(int (*proccess_toggleref) (GC_PTR obj))
419 GC_toggleref_callback = proccess_toggleref;
423 ensure_toggleref_capacity (int capacity)
425 if (!GC_toggleref_array) {
426 GC_toggleref_array_capacity = 32;
427 GC_toggleref_array = (GCToggleRef *) GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE (GC_toggleref_array_capacity * sizeof (GCToggleRef), NORMAL);
429 if (GC_toggleref_array_size + capacity >= GC_toggleref_array_capacity) {
431 int old_capacity = GC_toggleref_array_capacity;
432 while (GC_toggleref_array_capacity < GC_toggleref_array_size + capacity)
433 GC_toggleref_array_capacity *= 2;
435 tmp = (GCToggleRef *) GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE (GC_toggleref_array_capacity * sizeof (GCToggleRef), NORMAL);
436 memcpy (tmp, GC_toggleref_array, GC_toggleref_array_size * sizeof (GCToggleRef));
437 GC_INTERNAL_FREE (GC_toggleref_array);
438 GC_toggleref_array = tmp;
443 GC_toggleref_add (GC_PTR object, int strong_ref)
451 if (!GC_toggleref_callback)
454 ensure_toggleref_capacity (1);
455 GC_toggleref_array [GC_toggleref_array_size].strong_ref = strong_ref ? object : NULL;
456 GC_toggleref_array [GC_toggleref_array_size].weak_ref = strong_ref ? (GC_hidden_pointer)NULL : HIDE_POINTER (object);
457 ++GC_toggleref_array_size;
468 # if defined(__STDC__) || defined(__cplusplus)
469 int GC_register_long_link(GC_PTR * link, GC_PTR obj)
471 int GC_register_long_link(link, obj)
476 return GC_register_disappearing_link_inner(&GC_ll_hashtbl, link, obj);
479 # if defined(__STDC__) || defined(__cplusplus)
480 int GC_unregister_long_link(GC_PTR * link)
482 int GC_unregister_long_link(link)
486 return GC_unregister_disappearing_link_inner(&GC_ll_hashtbl, link);
489 /* Possible finalization_marker procedures. Note that mark stack */
490 /* overflow is handled by the caller, and is not a disaster. */
491 GC_API void GC_normal_finalize_mark_proc(p)
496 PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top,
497 &(GC_mark_stack[GC_mark_stack_size]));
500 /* This only pays very partial attention to the mark descriptor. */
501 /* It does the right thing for normal and atomic objects, and treats */
502 /* most others as normal. */
503 GC_API void GC_ignore_self_finalize_mark_proc(p)
507 word descr = hhdr -> hb_descr;
510 ptr_t target_limit = p + WORDS_TO_BYTES(hhdr -> hb_sz) - 1;
512 if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
513 scan_limit = p + descr - sizeof(word);
515 scan_limit = target_limit + 1 - sizeof(word);
517 for (q = p; q <= scan_limit; q += ALIGNMENT) {
519 if (r < p || r > target_limit) {
520 GC_PUSH_ONE_HEAP((word)r, q);
526 GC_API void GC_null_finalize_mark_proc(p)
533 /* Register a finalization function. See gc.h for details. */
534 /* in the nonthreads case, we try to avoid disabling signals, */
535 /* since it can be expensive. Threads packages typically */
536 /* make it cheaper. */
537 /* The last parameter is a procedure that determines */
538 /* marking for finalization ordering. Any objects marked */
539 /* by that procedure will be guaranteed to not have been */
540 /* finalized when this finalizer is invoked. */
541 GC_API void GC_register_finalizer_inner(obj, fn, cd, ofn, ocd, mp)
543 GC_finalization_proc fn;
545 GC_finalization_proc * ofn;
547 finalization_mark_proc * mp;
550 struct finalizable_object * curr_fo, * prev_fo;
552 struct finalizable_object *new_fo;
560 if (log_fo_table_size == -1
561 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
565 GC_grow_table((struct hash_chain_entry ***)(&fo_head),
568 if (GC_print_stats) {
569 GC_printf1("Grew fo table to %lu entries\n",
570 (unsigned long)(1 << log_fo_table_size));
577 /* in the THREADS case signals are disabled and we hold allocation */
578 /* lock; otherwise neither is true. Proceed carefully. */
580 index = HASH2(base, log_fo_table_size);
581 prev_fo = 0; curr_fo = fo_head[index];
582 while (curr_fo != 0) {
583 if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
584 /* Interruption by a signal in the middle of this */
585 /* should be safe. The client may see only *ocd */
586 /* updated, but we'll declare that to be his */
588 if (ocd) *ocd = (GC_PTR) curr_fo -> fo_client_data;
589 if (ofn) *ofn = curr_fo -> fo_fn;
590 /* Delete the structure for base. */
592 fo_head[index] = fo_next(curr_fo);
594 fo_set_next(prev_fo, fo_next(curr_fo));
598 /* May not happen if we get a signal. But a high */
599 /* estimate will only make the table larger than */
601 # if !defined(THREADS) && !defined(DBG_HDRS_ALL)
602 GC_free((GC_PTR)curr_fo);
605 curr_fo -> fo_fn = fn;
606 curr_fo -> fo_client_data = (ptr_t)cd;
607 curr_fo -> fo_mark_proc = mp;
608 /* Reinsert it. We deleted it first to maintain */
609 /* consistency in the event of a signal. */
611 fo_head[index] = curr_fo;
613 fo_set_next(prev_fo, curr_fo);
623 curr_fo = fo_next(curr_fo);
636 /* We won't collect it, hence finalizer wouldn't be run. */
643 new_fo = (struct finalizable_object *)
644 GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
650 new_fo = (struct finalizable_object *)
651 GC_oom_fn(sizeof(struct finalizable_object));
653 GC_finalization_failures++;
656 /* It's not likely we'll make it here, but ... */
662 new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
663 new_fo -> fo_fn = fn;
664 new_fo -> fo_client_data = (ptr_t)cd;
665 new_fo -> fo_object_size = hhdr -> hb_sz;
666 new_fo -> fo_mark_proc = mp;
667 fo_set_next(new_fo, fo_head[index]);
669 fo_head[index] = new_fo;
676 # if defined(__STDC__)
677 void GC_register_finalizer(void * obj,
678 GC_finalization_proc fn, void * cd,
679 GC_finalization_proc *ofn, void ** ocd)
681 void GC_register_finalizer(obj, fn, cd, ofn, ocd)
683 GC_finalization_proc fn;
685 GC_finalization_proc * ofn;
689 GC_register_finalizer_inner(obj, fn, cd, ofn,
690 ocd, GC_normal_finalize_mark_proc);
693 # if defined(__STDC__)
694 void GC_register_finalizer_ignore_self(void * obj,
695 GC_finalization_proc fn, void * cd,
696 GC_finalization_proc *ofn, void ** ocd)
698 void GC_register_finalizer_ignore_self(obj, fn, cd, ofn, ocd)
700 GC_finalization_proc fn;
702 GC_finalization_proc * ofn;
706 GC_register_finalizer_inner(obj, fn, cd, ofn,
707 ocd, GC_ignore_self_finalize_mark_proc);
710 # if defined(__STDC__)
711 void GC_register_finalizer_no_order(void * obj,
712 GC_finalization_proc fn, void * cd,
713 GC_finalization_proc *ofn, void ** ocd)
715 void GC_register_finalizer_no_order(obj, fn, cd, ofn, ocd)
717 GC_finalization_proc fn;
719 GC_finalization_proc * ofn;
723 GC_register_finalizer_inner(obj, fn, cd, ofn,
724 ocd, GC_null_finalize_mark_proc);
729 static void GC_dump_finalization_links(struct dl_hashtbl_s *dl_hashtbl)
731 struct disappearing_link *curr_dl;
732 ptr_t real_ptr, real_link;
733 size_t dl_size = dl_hashtbl->log_size == -1 ? 0 :
734 1 << dl_hashtbl->log_size;
737 for (i = 0; i < dl_size; i++) {
738 for (curr_dl = dl_hashtbl -> head[i]; curr_dl != 0;
739 curr_dl = dl_next(curr_dl)) {
740 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
741 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
742 GC_printf2("Object: %lx, link: %lx\n", real_ptr, real_link);
747 void GC_dump_finalization()
749 struct finalizable_object * curr_fo;
751 int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
754 GC_printf0("Disappearing (short) links:\n");
755 GC_dump_finalization_links(&GC_dl_hashtbl);
756 GC_printf0("Disappearing long links:\n");
757 GC_dump_finalization_links(&GC_ll_hashtbl);
759 GC_printf0("Finalizers:\n");
760 for (i = 0; i < fo_size; i++) {
761 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
762 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
763 GC_printf1("Finalizable object: 0x%lx\n", real_ptr);
769 static void GC_make_disappearing_links_disappear(struct dl_hashtbl_s *dl_hashtbl)
771 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
772 ptr_t real_ptr, real_link;
774 int dl_size = (dl_hashtbl -> log_size == -1 ) ? 0 : (1 << dl_hashtbl -> log_size);
776 for (i = 0; i < dl_size; i++) {
777 curr_dl = dl_hashtbl -> head[i];
779 while (curr_dl != 0) {
780 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
781 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
782 if (!GC_is_marked(real_ptr)) {
783 *(word *)real_link = 0;
784 next_dl = dl_next(curr_dl);
786 dl_hashtbl -> head[i] = next_dl;
788 dl_set_next(prev_dl, next_dl);
790 GC_clear_mark_bit((ptr_t)curr_dl);
791 dl_hashtbl -> entries--;
795 curr_dl = dl_next(curr_dl);
801 static void GC_remove_dangling_disappearing_links(struct dl_hashtbl_s *dl_hashtbl)
803 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
804 ptr_t real_ptr, real_link;
806 int dl_size = (dl_hashtbl -> log_size == -1 ) ? 0 : (1 << dl_hashtbl -> log_size);
808 for (i = 0; i < dl_size; i++) {
809 curr_dl = dl_hashtbl -> head[i];
811 while (curr_dl != 0) {
812 real_link = GC_base((ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link));
813 if (real_link != 0 && !GC_is_marked(real_link)) {
814 next_dl = dl_next(curr_dl);
816 dl_hashtbl -> head[i] = next_dl;
818 dl_set_next(prev_dl, next_dl);
820 GC_clear_mark_bit((ptr_t)curr_dl);
821 dl_hashtbl -> entries--;
825 curr_dl = dl_next(curr_dl);
831 /* Called with world stopped. Cause disappearing links to disappear, */
832 /* and invoke finalizers. */
835 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
838 int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
840 GC_mark_togglerefs();
842 /* Make non-tracking disappearing links disappear */
843 GC_make_disappearing_links_disappear(&GC_dl_hashtbl);
845 /* Mark all objects reachable via chains of 1 or more pointers */
846 /* from finalizable objects. */
847 GC_ASSERT(GC_mark_state == MS_NONE);
848 for (i = 0; i < fo_size; i++) {
849 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
850 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
851 if (!GC_is_marked(real_ptr)) {
852 GC_MARKED_FOR_FINALIZATION(real_ptr);
853 GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
854 if (GC_is_marked(real_ptr)) {
855 WARN("Finalization cycle involving %lx\n", real_ptr);
860 /* Enqueue for finalization all objects that are still */
862 GC_words_finalized = 0;
863 for (i = 0; i < fo_size; i++) {
864 curr_fo = fo_head[i];
866 while (curr_fo != 0) {
867 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
868 if (!GC_is_marked(real_ptr)) {
869 if (!GC_java_finalization) {
870 GC_set_mark_bit(real_ptr);
872 /* Delete from hash table */
873 next_fo = fo_next(curr_fo);
875 fo_head[i] = next_fo;
877 fo_set_next(prev_fo, next_fo);
880 /* Add to list of objects awaiting finalization. */
881 fo_set_next(curr_fo, GC_finalize_now);
882 GC_finalize_now = curr_fo;
883 /* unhide object pointer so any future collections will */
885 curr_fo -> fo_hidden_base =
886 (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
887 GC_words_finalized +=
888 ALIGNED_WORDS(curr_fo -> fo_object_size)
889 + ALIGNED_WORDS(sizeof(struct finalizable_object));
890 GC_ASSERT(GC_is_marked(GC_base((ptr_t)curr_fo)));
894 curr_fo = fo_next(curr_fo);
899 if (GC_java_finalization) {
900 /* make sure we mark everything reachable from objects finalized
901 using the no_order mark_proc */
902 for (curr_fo = GC_finalize_now;
903 curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
904 real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
905 if (!GC_is_marked(real_ptr)) {
906 if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
907 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
909 GC_set_mark_bit(real_ptr);
914 /* Remove dangling disappearing links. */
915 GC_remove_dangling_disappearing_links(&GC_dl_hashtbl);
917 GC_clear_togglerefs ();
919 /* Make long links disappear and remove dangling ones. */
920 GC_make_disappearing_links_disappear(&GC_ll_hashtbl);
921 GC_remove_dangling_disappearing_links(&GC_ll_hashtbl);
924 #ifndef JAVA_FINALIZATION_NOT_NEEDED
926 /* Enqueue all remaining finalizers to be run - Assumes lock is
927 * held, and signals are disabled */
928 void GC_enqueue_all_finalizers()
930 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
935 fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
936 GC_words_finalized = 0;
937 for (i = 0; i < fo_size; i++) {
938 curr_fo = fo_head[i];
940 while (curr_fo != 0) {
941 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
942 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
943 GC_set_mark_bit(real_ptr);
945 /* Delete from hash table */
946 next_fo = fo_next(curr_fo);
948 fo_head[i] = next_fo;
950 fo_set_next(prev_fo, next_fo);
954 /* Add to list of objects awaiting finalization. */
955 fo_set_next(curr_fo, GC_finalize_now);
956 GC_finalize_now = curr_fo;
958 /* unhide object pointer so any future collections will */
960 curr_fo -> fo_hidden_base =
961 (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
963 GC_words_finalized +=
964 ALIGNED_WORDS(curr_fo -> fo_object_size)
965 + ALIGNED_WORDS(sizeof(struct finalizable_object));
973 /* Invoke all remaining finalizers that haven't yet been run.
974 * This is needed for strict compliance with the Java standard,
975 * which can make the runtime guarantee that all finalizers are run.
976 * Unfortunately, the Java standard implies we have to keep running
977 * finalizers until there are no more left, a potential infinite loop.
979 * Note that this is even more dangerous than the usual Java
980 * finalizers, in that objects reachable from static variables
981 * may have been finalized when these finalizers are run.
982 * Finalizers run at this point must be prepared to deal with a
983 * mostly broken world.
984 * This routine is externally callable, so is called without
985 * the allocation lock.
987 GC_API void GC_finalize_all()
993 while (GC_fo_entries > 0) {
994 GC_enqueue_all_finalizers();
997 GC_INVOKE_FINALIZERS();
1006 /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
1007 /* finalizers can only be called from some kind of `safe state' and */
1008 /* getting into that safe state is expensive.) */
1009 int GC_should_invoke_finalizers GC_PROTO((void))
1011 return GC_finalize_now != 0;
1014 /* Invoke finalizers for all objects that are ready to be finalized. */
1015 /* Should be called without allocation lock. */
1016 int GC_invoke_finalizers()
1018 struct finalizable_object * curr_fo;
1020 word mem_freed_before;
1023 while (GC_finalize_now != 0) {
1029 mem_freed_before = GC_mem_freed;
1031 curr_fo = GC_finalize_now;
1033 if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
1036 if (curr_fo == 0) break;
1038 GC_finalize_now = fo_next(curr_fo);
1040 fo_set_next(curr_fo, 0);
1041 (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
1042 curr_fo -> fo_client_data);
1043 curr_fo -> fo_client_data = 0;
1046 /* This is probably a bad idea. It throws off accounting if */
1047 /* nearly all objects are finalizable. O.w. it shouldn't */
1049 GC_free((GC_PTR)curr_fo);
1052 if (count != 0 && mem_freed_before != GC_mem_freed) {
1054 GC_finalizer_mem_freed += (GC_mem_freed - mem_freed_before);
1060 void (* GC_finalizer_notifier)() = (void (*) GC_PROTO((void)))0;
1062 static GC_word last_finalizer_notification = 0;
1064 void GC_notify_or_invoke_finalizers GC_PROTO((void))
1066 /* This is a convenient place to generate backtraces if appropriate, */
1067 /* since that code is not callable with the allocation lock. */
1068 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
1069 static word last_back_trace_gc_no = 1; /* Skip first one. */
1071 if (GC_gc_no > last_back_trace_gc_no) {
1074 # ifdef KEEP_BACK_PTRS
1076 /* Stops when GC_gc_no wraps; that's OK. */
1077 last_back_trace_gc_no = (word)(-1); /* disable others. */
1078 for (i = 0; i < GC_backtraces; ++i) {
1079 /* FIXME: This tolerates concurrent heap mutation, */
1080 /* which may cause occasional mysterious results. */
1081 /* We need to release the GC lock, since GC_print_callers */
1082 /* acquires it. It probably shouldn't. */
1084 GC_generate_random_backtrace_no_gc();
1087 last_back_trace_gc_no = GC_gc_no;
1090 # ifdef MAKE_BACK_GRAPH
1091 if (GC_print_back_height)
1092 GC_print_back_graph_stats();
1096 if (GC_finalize_now == 0) return;
1097 if (!GC_finalize_on_demand) {
1098 (void) GC_invoke_finalizers();
1100 GC_ASSERT(GC_finalize_now == 0);
1101 # endif /* Otherwise GC can run concurrently and add more */
1104 if (GC_finalizer_notifier != (void (*) GC_PROTO((void)))0
1105 && last_finalizer_notification != GC_gc_no) {
1106 last_finalizer_notification = GC_gc_no;
1107 GC_finalizer_notifier();
1112 GC_PTR GC_call_with_alloc_lock(GC_fn_type fn,
1115 GC_PTR GC_call_with_alloc_lock(fn, client_data)
1128 result = (*fn)(client_data);
1130 # ifndef GC_ASSERTIONS
1131 UNSET_LOCK_HOLDER();
1132 # endif /* o.w. UNLOCK() does it implicitly */
1139 #if !defined(NO_DEBUGGING)
1141 void GC_print_finalization_stats()
1143 struct finalizable_object *fo = GC_finalize_now;
1146 GC_printf3("%lu finalization table entries; %lu/%lu short/long disappearing links alive\n",
1147 GC_fo_entries, (unsigned long)GC_dl_hashtbl.entries, (unsigned long)GC_ll_hashtbl.entries);
1148 for (; 0 != fo; fo = fo_next(fo)) ++ready;
1149 GC_printf1("%lu objects are eligible for immediate finalization\n", ready);
1152 #endif /* NO_DEBUGGING */