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.
16 /* Boehm, February 1, 1996 1:19 pm PST */
17 # define I_HIDE_POINTERS
18 # include "private/gc_pmark.h"
20 # ifdef FINALIZE_ON_DEMAND
21 int GC_finalize_on_demand = 1;
23 int GC_finalize_on_demand = 0;
26 # ifdef JAVA_FINALIZATION
27 int GC_java_finalization = 1;
29 int GC_java_finalization = 0;
32 /* Type of mark procedure used for marking from finalizable object. */
33 /* This procedure normally does not mark the object, only its */
35 typedef void (* finalization_mark_proc)(ptr_t /* finalizable_obj_ptr */);
37 # define HASH3(addr,size,log_size) \
38 ((((word)(addr) >> 3) ^ ((word)(addr) >> (3+(log_size)))) \
40 #define HASH2(addr,log_size) HASH3(addr, 1 << log_size, log_size)
42 struct hash_chain_entry {
44 struct hash_chain_entry * next;
47 static struct disappearing_link {
48 struct hash_chain_entry prolog;
49 # define dl_hidden_link prolog.hidden_key
50 /* Field to be cleared. */
51 # define dl_next(x) (struct disappearing_link *)((x) -> prolog.next)
52 # define dl_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
54 word dl_hidden_obj; /* Pointer to object base */
57 static signed_word log_dl_table_size = -1;
59 /* current size of array pointed to by dl_head. */
60 /* -1 ==> size is 0. */
62 STATIC word GC_dl_entries = 0;
63 /* Number of entries currently in disappearing */
66 static struct finalizable_object {
67 struct hash_chain_entry prolog;
68 # define fo_hidden_base prolog.hidden_key
69 /* Pointer to object base. */
70 /* No longer hidden once object */
71 /* is on finalize_now queue. */
72 # define fo_next(x) (struct finalizable_object *)((x) -> prolog.next)
73 # define fo_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
74 GC_finalization_proc fo_fn; /* Finalizer. */
76 word fo_object_size; /* In bytes. */
77 finalization_mark_proc fo_mark_proc; /* Mark-through procedure */
80 STATIC struct finalizable_object * GC_finalize_now = 0;
81 /* List of objects that should be finalized now. */
83 static signed_word log_fo_table_size = -1;
85 word GC_fo_entries = 0;
87 void GC_push_finalizer_structures(void)
89 GC_push_all((ptr_t)(&dl_head), (ptr_t)(&dl_head) + sizeof(word));
90 GC_push_all((ptr_t)(&fo_head), (ptr_t)(&fo_head) + sizeof(word));
91 GC_push_all((ptr_t)(&GC_finalize_now),
92 (ptr_t)(&GC_finalize_now) + sizeof(word));
95 /* Double the size of a hash table. *size_ptr is the log of its current */
96 /* size. May be a no-op. */
97 /* *table is a pointer to an array of hash headers. If we succeed, we */
98 /* update both *table and *log_size_ptr. */
100 STATIC void GC_grow_table(struct hash_chain_entry ***table,
101 signed_word *log_size_ptr)
104 register struct hash_chain_entry *p;
105 signed_word log_old_size = *log_size_ptr;
106 signed_word log_new_size = log_old_size + 1;
107 word old_size = ((log_old_size == -1)? 0: (1 << log_old_size));
108 word new_size = (word)1 << log_new_size;
109 /* FIXME: Power of 2 size often gets rounded up to one more page. */
110 struct hash_chain_entry **new_table = (struct hash_chain_entry **)
111 GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
112 (size_t)new_size * sizeof(struct hash_chain_entry *), NORMAL);
114 if (new_table == 0) {
116 ABORT("Insufficient space for initial table allocation");
121 for (i = 0; i < old_size; i++) {
124 ptr_t real_key = (ptr_t)REVEAL_POINTER(p -> hidden_key);
125 struct hash_chain_entry *next = p -> next;
126 size_t new_hash = HASH3(real_key, new_size, log_new_size);
128 p -> next = new_table[new_hash];
129 new_table[new_hash] = p;
133 *log_size_ptr = log_new_size;
137 GC_API int GC_CALL GC_register_disappearing_link(void * * link)
141 base = (ptr_t)GC_base((void *)link);
143 ABORT("Bad arg to GC_register_disappearing_link");
144 return(GC_general_register_disappearing_link(link, base));
147 GC_API int GC_CALL GC_general_register_disappearing_link(void * * link,
150 struct disappearing_link *curr_dl;
152 struct disappearing_link * new_dl;
155 if (((word)link & (ALIGNMENT-1)) || link == NULL)
156 ABORT("Bad arg to GC_general_register_disappearing_link");
160 GC_ASSERT(obj != NULL && GC_base(obj) == obj);
161 if (log_dl_table_size == -1
162 || GC_dl_entries > ((word)1 << log_dl_table_size)) {
163 GC_grow_table((struct hash_chain_entry ***)(&dl_head),
165 if (GC_print_stats) {
166 GC_log_printf("Grew dl table to %u entries\n",
167 (1 << (unsigned)log_dl_table_size));
170 index = HASH2(link, log_dl_table_size);
171 for (curr_dl = dl_head[index]; curr_dl != 0; curr_dl = dl_next(curr_dl)) {
172 if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
173 curr_dl -> dl_hidden_obj = HIDE_POINTER(obj);
180 new_dl = (struct disappearing_link *)
181 GC_INTERNAL_MALLOC(sizeof(struct disappearing_link),NORMAL);
186 new_dl = (struct disappearing_link *)
187 GC_oom_fn(sizeof(struct disappearing_link));
191 /* It's not likely we'll make it here, but ... */
196 new_dl -> dl_hidden_obj = HIDE_POINTER(obj);
197 new_dl -> dl_hidden_link = HIDE_POINTER(link);
198 dl_set_next(new_dl, dl_head[index]);
199 dl_head[index] = new_dl;
207 GC_API int GC_CALL GC_unregister_disappearing_link(void * * link)
209 struct disappearing_link *curr_dl, *prev_dl;
213 if (((word)link & (ALIGNMENT-1)) != 0) return(0); /* Nothing to do. */
216 index = HASH2(link, log_dl_table_size);
217 prev_dl = 0; curr_dl = dl_head[index];
218 while (curr_dl != 0) {
219 if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
221 dl_head[index] = dl_next(curr_dl);
223 dl_set_next(prev_dl, dl_next(curr_dl));
228 dl_set_next(curr_dl, 0);
230 GC_free((void *)curr_dl);
235 curr_dl = dl_next(curr_dl);
241 /* Possible finalization_marker procedures. Note that mark stack */
242 /* overflow is handled by the caller, and is not a disaster. */
243 STATIC void GC_normal_finalize_mark_proc(ptr_t p)
247 PUSH_OBJ(p, hhdr, GC_mark_stack_top,
248 &(GC_mark_stack[GC_mark_stack_size]));
251 /* This only pays very partial attention to the mark descriptor. */
252 /* It does the right thing for normal and atomic objects, and treats */
253 /* most others as normal. */
254 STATIC void GC_ignore_self_finalize_mark_proc(ptr_t p)
257 word descr = hhdr -> hb_descr;
261 ptr_t target_limit = p + hhdr -> hb_sz - 1;
263 if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
264 scan_limit = p + descr - sizeof(word);
266 scan_limit = target_limit + 1 - sizeof(word);
268 for (q = p; q <= scan_limit; q += ALIGNMENT) {
270 if ((ptr_t)r < p || (ptr_t)r > target_limit) {
271 GC_PUSH_ONE_HEAP(r, q);
277 STATIC void GC_null_finalize_mark_proc(ptr_t p)
281 /* Possible finalization_marker procedures. Note that mark stack */
282 /* overflow is handled by the caller, and is not a disaster. */
284 /* GC_unreachable_finalize_mark_proc is an alias for normal marking, */
285 /* but it is explicitly tested for, and triggers different */
286 /* behavior. Objects registered in this way are not finalized */
287 /* if they are reachable by other finalizable objects, even if those */
288 /* other objects specify no ordering. */
289 STATIC void GC_unreachable_finalize_mark_proc(ptr_t p)
291 GC_normal_finalize_mark_proc(p);
296 /* Register a finalization function. See gc.h for details. */
297 /* The last parameter is a procedure that determines */
298 /* marking for finalization ordering. Any objects marked */
299 /* by that procedure will be guaranteed to not have been */
300 /* finalized when this finalizer is invoked. */
301 STATIC void GC_register_finalizer_inner(void * obj,
302 GC_finalization_proc fn, void *cd,
303 GC_finalization_proc *ofn, void **ocd,
304 finalization_mark_proc mp)
307 struct finalizable_object * curr_fo, * prev_fo;
309 struct finalizable_object *new_fo;
316 if (log_fo_table_size == -1
317 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
318 GC_grow_table((struct hash_chain_entry ***)(&fo_head),
320 if (GC_print_stats) {
321 GC_log_printf("Grew fo table to %u entries\n",
322 (1 << (unsigned)log_fo_table_size));
325 /* in the THREADS case we hold allocation lock. */
327 index = HASH2(base, log_fo_table_size);
328 prev_fo = 0; curr_fo = fo_head[index];
329 while (curr_fo != 0) {
330 GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
331 if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
332 /* Interruption by a signal in the middle of this */
333 /* should be safe. The client may see only *ocd */
334 /* updated, but we'll declare that to be his */
336 if (ocd) *ocd = (void *) (curr_fo -> fo_client_data);
337 if (ofn) *ofn = curr_fo -> fo_fn;
338 /* Delete the structure for base. */
340 fo_head[index] = fo_next(curr_fo);
342 fo_set_next(prev_fo, fo_next(curr_fo));
346 /* May not happen if we get a signal. But a high */
347 /* estimate will only make the table larger than */
349 # if !defined(THREADS) && !defined(DBG_HDRS_ALL)
350 GC_free((void *)curr_fo);
353 curr_fo -> fo_fn = fn;
354 curr_fo -> fo_client_data = (ptr_t)cd;
355 curr_fo -> fo_mark_proc = mp;
356 /* Reinsert it. We deleted it first to maintain */
357 /* consistency in the event of a signal. */
359 fo_head[index] = curr_fo;
361 fo_set_next(prev_fo, curr_fo);
370 curr_fo = fo_next(curr_fo);
382 /* We won't collect it, hence finalizer wouldn't be run. */
388 new_fo = (struct finalizable_object *)
389 GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
390 if (EXPECT(0 == new_fo, FALSE)) {
394 new_fo = (struct finalizable_object *)
395 GC_oom_fn(sizeof(struct finalizable_object));
399 /* It's not likely we'll make it here, but ... */
404 GC_ASSERT(GC_size(new_fo) >= sizeof(struct finalizable_object));
405 new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
406 new_fo -> fo_fn = fn;
407 new_fo -> fo_client_data = (ptr_t)cd;
408 new_fo -> fo_object_size = hhdr -> hb_sz;
409 new_fo -> fo_mark_proc = mp;
410 fo_set_next(new_fo, fo_head[index]);
412 fo_head[index] = new_fo;
418 GC_API void GC_CALL GC_register_finalizer(void * obj,
419 GC_finalization_proc fn, void * cd,
420 GC_finalization_proc *ofn, void ** ocd)
422 GC_register_finalizer_inner(obj, fn, cd, ofn,
423 ocd, GC_normal_finalize_mark_proc);
426 GC_API void GC_CALL GC_register_finalizer_ignore_self(void * obj,
427 GC_finalization_proc fn, void * cd,
428 GC_finalization_proc *ofn, void ** ocd)
430 GC_register_finalizer_inner(obj, fn, cd, ofn,
431 ocd, GC_ignore_self_finalize_mark_proc);
434 GC_API void GC_CALL GC_register_finalizer_no_order(void * obj,
435 GC_finalization_proc fn, void * cd,
436 GC_finalization_proc *ofn, void ** ocd)
438 GC_register_finalizer_inner(obj, fn, cd, ofn,
439 ocd, GC_null_finalize_mark_proc);
442 static GC_bool need_unreachable_finalization = FALSE;
443 /* Avoid the work if this isn't used. */
445 GC_API void GC_CALL GC_register_finalizer_unreachable(void * obj,
446 GC_finalization_proc fn, void * cd,
447 GC_finalization_proc *ofn, void ** ocd)
449 need_unreachable_finalization = TRUE;
450 GC_ASSERT(GC_java_finalization);
451 GC_register_finalizer_inner(obj, fn, cd, ofn,
452 ocd, GC_unreachable_finalize_mark_proc);
456 void GC_dump_finalization(void)
458 struct disappearing_link * curr_dl;
459 struct finalizable_object * curr_fo;
460 ptr_t real_ptr, real_link;
461 int dl_size = (log_dl_table_size == -1 ) ? 0 : (1 << log_dl_table_size);
462 int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
465 GC_printf("Disappearing links:\n");
466 for (i = 0; i < dl_size; i++) {
467 for (curr_dl = dl_head[i]; curr_dl != 0; curr_dl = dl_next(curr_dl)) {
468 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
469 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
470 GC_printf("Object: %p, Link:%p\n", real_ptr, real_link);
473 GC_printf("Finalizers:\n");
474 for (i = 0; i < fo_size; i++) {
475 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
476 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
477 GC_printf("Finalizable object: %p\n", real_ptr);
483 /* Called with held lock (but the world is running). */
484 /* Cause disappearing links to disappear and unreachable objects to be */
485 /* enqueued for finalization. */
486 void GC_finalize(void)
488 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
489 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
490 ptr_t real_ptr, real_link;
492 size_t dl_size = (log_dl_table_size == -1 ) ? 0 : (1 << log_dl_table_size);
493 size_t fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
495 /* Make disappearing links disappear */
496 for (i = 0; i < dl_size; i++) {
497 curr_dl = dl_head[i];
499 while (curr_dl != 0) {
500 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
501 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
502 if (!GC_is_marked(real_ptr)) {
503 *(word *)real_link = 0;
504 next_dl = dl_next(curr_dl);
506 dl_head[i] = next_dl;
508 dl_set_next(prev_dl, next_dl);
510 GC_clear_mark_bit((ptr_t)curr_dl);
515 curr_dl = dl_next(curr_dl);
519 /* Mark all objects reachable via chains of 1 or more pointers */
520 /* from finalizable objects. */
521 GC_ASSERT(GC_mark_state == MS_NONE);
522 for (i = 0; i < fo_size; i++) {
523 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
524 GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
525 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
526 if (!GC_is_marked(real_ptr)) {
527 GC_MARKED_FOR_FINALIZATION(real_ptr);
528 GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
529 if (GC_is_marked(real_ptr)) {
530 WARN("Finalization cycle involving %p\n", real_ptr);
535 /* Enqueue for finalization all objects that are still */
537 GC_bytes_finalized = 0;
538 for (i = 0; i < fo_size; i++) {
539 curr_fo = fo_head[i];
541 while (curr_fo != 0) {
542 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
543 if (!GC_is_marked(real_ptr)) {
544 if (!GC_java_finalization) {
545 GC_set_mark_bit(real_ptr);
547 /* Delete from hash table */
548 next_fo = fo_next(curr_fo);
550 fo_head[i] = next_fo;
552 fo_set_next(prev_fo, next_fo);
555 /* Add to list of objects awaiting finalization. */
556 fo_set_next(curr_fo, GC_finalize_now);
557 GC_finalize_now = curr_fo;
558 /* unhide object pointer so any future collections will */
560 curr_fo -> fo_hidden_base =
561 (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
562 GC_bytes_finalized +=
563 curr_fo -> fo_object_size
564 + sizeof(struct finalizable_object);
565 GC_ASSERT(GC_is_marked(GC_base((ptr_t)curr_fo)));
569 curr_fo = fo_next(curr_fo);
574 if (GC_java_finalization) {
575 /* make sure we mark everything reachable from objects finalized
576 using the no_order mark_proc */
577 for (curr_fo = GC_finalize_now;
578 curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
579 real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
580 if (!GC_is_marked(real_ptr)) {
581 if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
582 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
584 if (curr_fo -> fo_mark_proc != GC_unreachable_finalize_mark_proc) {
585 GC_set_mark_bit(real_ptr);
590 /* now revive finalize-when-unreachable objects reachable from
591 other finalizable objects */
592 if (need_unreachable_finalization) {
593 curr_fo = GC_finalize_now;
595 while (curr_fo != 0) {
596 next_fo = fo_next(curr_fo);
597 if (curr_fo -> fo_mark_proc == GC_unreachable_finalize_mark_proc) {
598 real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
599 if (!GC_is_marked(real_ptr)) {
600 GC_set_mark_bit(real_ptr);
603 GC_finalize_now = next_fo;
605 fo_set_next(prev_fo, next_fo);
607 curr_fo -> fo_hidden_base =
608 (word) HIDE_POINTER(curr_fo -> fo_hidden_base);
609 GC_bytes_finalized -=
610 curr_fo -> fo_object_size + sizeof(struct finalizable_object);
612 i = HASH2(real_ptr, log_fo_table_size);
613 fo_set_next (curr_fo, fo_head[i]);
615 fo_head[i] = curr_fo;
625 /* Remove dangling disappearing links. */
626 for (i = 0; i < dl_size; i++) {
627 curr_dl = dl_head[i];
629 while (curr_dl != 0) {
630 real_link = GC_base((ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link));
631 if (real_link != 0 && !GC_is_marked(real_link)) {
632 next_dl = dl_next(curr_dl);
634 dl_head[i] = next_dl;
636 dl_set_next(prev_dl, next_dl);
638 GC_clear_mark_bit((ptr_t)curr_dl);
643 curr_dl = dl_next(curr_dl);
649 #ifndef JAVA_FINALIZATION_NOT_NEEDED
651 /* Enqueue all remaining finalizers to be run - Assumes lock is held. */
652 STATIC void GC_enqueue_all_finalizers(void)
654 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
659 fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
660 GC_bytes_finalized = 0;
661 for (i = 0; i < fo_size; i++) {
662 curr_fo = fo_head[i];
664 while (curr_fo != 0) {
665 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
666 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
667 GC_set_mark_bit(real_ptr);
669 /* Delete from hash table */
670 next_fo = fo_next(curr_fo);
672 fo_head[i] = next_fo;
674 fo_set_next(prev_fo, next_fo);
678 /* Add to list of objects awaiting finalization. */
679 fo_set_next(curr_fo, GC_finalize_now);
680 GC_finalize_now = curr_fo;
682 /* unhide object pointer so any future collections will */
684 curr_fo -> fo_hidden_base =
685 (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
687 GC_bytes_finalized +=
688 curr_fo -> fo_object_size + sizeof(struct finalizable_object);
696 /* Invoke all remaining finalizers that haven't yet been run.
697 * This is needed for strict compliance with the Java standard,
698 * which can make the runtime guarantee that all finalizers are run.
699 * Unfortunately, the Java standard implies we have to keep running
700 * finalizers until there are no more left, a potential infinite loop.
702 * Note that this is even more dangerous than the usual Java
703 * finalizers, in that objects reachable from static variables
704 * may have been finalized when these finalizers are run.
705 * Finalizers run at this point must be prepared to deal with a
706 * mostly broken world.
707 * This routine is externally callable, so is called without
708 * the allocation lock.
710 GC_API void GC_CALL GC_finalize_all(void)
715 while (GC_fo_entries > 0) {
716 GC_enqueue_all_finalizers();
718 GC_invoke_finalizers();
719 /* Running the finalizers in this thread is arguably not a good */
720 /* idea when we should be notifying another thread to run them. */
721 /* But otherwise we don't have a great way to wait for them to */
729 /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
730 /* finalizers can only be called from some kind of `safe state' and */
731 /* getting into that safe state is expensive.) */
732 GC_API int GC_CALL GC_should_invoke_finalizers(void)
734 return GC_finalize_now != 0;
737 /* Invoke finalizers for all objects that are ready to be finalized. */
738 /* Should be called without allocation lock. */
739 GC_API int GC_CALL GC_invoke_finalizers(void)
741 struct finalizable_object * curr_fo;
743 word bytes_freed_before = 0; /* initialized to prevent warning. */
746 while (GC_finalize_now != 0) {
751 bytes_freed_before = GC_bytes_freed;
752 /* Don't do this outside, since we need the lock. */
754 curr_fo = GC_finalize_now;
756 if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
758 if (curr_fo == 0) break;
760 GC_finalize_now = fo_next(curr_fo);
762 fo_set_next(curr_fo, 0);
763 (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
764 curr_fo -> fo_client_data);
765 curr_fo -> fo_client_data = 0;
768 /* This is probably a bad idea. It throws off accounting if */
769 /* nearly all objects are finalizable. O.w. it shouldn't */
771 GC_free((void *)curr_fo);
774 /* bytes_freed_before is initialized whenever count != 0 */
775 if (count != 0 && bytes_freed_before != GC_bytes_freed) {
777 GC_finalizer_bytes_freed += (GC_bytes_freed - bytes_freed_before);
783 GC_finalizer_notifier_proc GC_finalizer_notifier =
784 (GC_finalizer_notifier_proc)0;
786 static GC_word last_finalizer_notification = 0;
788 void GC_notify_or_invoke_finalizers(void)
790 /* This is a convenient place to generate backtraces if appropriate, */
791 /* since that code is not callable with the allocation lock. */
792 # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
793 static word last_back_trace_gc_no = 1; /* Skip first one. */
795 if (GC_gc_no > last_back_trace_gc_no) {
796 # ifdef KEEP_BACK_PTRS
799 /* Stops when GC_gc_no wraps; that's OK. */
800 last_back_trace_gc_no = (word)(-1); /* disable others. */
801 for (i = 0; i < GC_backtraces; ++i) {
802 /* FIXME: This tolerates concurrent heap mutation, */
803 /* which may cause occasional mysterious results. */
804 /* We need to release the GC lock, since GC_print_callers */
805 /* acquires it. It probably shouldn't. */
807 GC_generate_random_backtrace_no_gc();
810 last_back_trace_gc_no = GC_gc_no;
813 # ifdef MAKE_BACK_GRAPH
814 if (GC_print_back_height)
815 GC_print_back_graph_stats();
819 if (GC_finalize_now == 0) return;
820 if (!GC_finalize_on_demand) {
821 (void) GC_invoke_finalizers();
823 GC_ASSERT(GC_finalize_now == 0);
824 # endif /* Otherwise GC can run concurrently and add more */
827 if (GC_finalizer_notifier != (GC_finalizer_notifier_proc)0
828 && last_finalizer_notification != GC_gc_no) {
829 last_finalizer_notification = GC_gc_no;
830 GC_finalizer_notifier();
834 GC_API void * GC_CALL GC_call_with_alloc_lock(GC_fn_type fn,
842 /* FIXME - This looks wrong!! */
845 result = (*fn)(client_data);
847 # ifndef GC_ASSERTIONS
849 # endif /* o.w. UNLOCK() does it implicitly */
855 #if !defined(NO_DEBUGGING)
857 void GC_print_finalization_stats(void)
859 struct finalizable_object *fo = GC_finalize_now;
862 GC_printf("%u finalization table entries; %u disappearing links\n",
863 (unsigned)GC_fo_entries, (unsigned)GC_dl_entries);
864 for (; 0 != fo; fo = fo_next(fo)) ++ready;
865 GC_printf("%u objects are eligible for immediate finalization\n", ready);
868 #endif /* NO_DEBUGGING */