[mono-api-html] Prettier output using singular and plural forms in sections
[mono-project.git] / libgc / finalize.c
blob91f6970180cd26352a864464a0ac44184d2a07ce
1 /*
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;
21 # else
22 int GC_finalize_on_demand = 0;
23 # endif
25 # ifdef JAVA_FINALIZATION
26 int GC_java_finalization = 1;
27 # else
28 int GC_java_finalization = 0;
29 # endif
31 /* Type of mark procedure used for marking from finalizable object. */
32 /* This procedure normally does not mark the object, only its */
33 /* descendents. */
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)))) \
38 & ((size) - 1))
39 #define HASH2(addr,log_size) HASH3(addr, 1 << log_size, log_size)
41 struct hash_chain_entry {
42 word hidden_key;
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 */
59 struct dl_hashtbl_s {
60 struct disappearing_link **head;
61 signed_word log_size;
62 word entries;
65 /* Forward decls. */
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. */
84 ptr_t fo_client_data;
85 word fo_object_size; /* In bytes. */
86 finalization_mark_proc * fo_mark_proc; /* Mark-through procedure */
87 } **fo_head = 0;
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;
116 register word i;
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) {
127 if (table == 0) {
128 ABORT("Insufficient space for initial table allocation");
129 } else {
130 return;
133 for (i = 0; i < old_size; i++) {
134 p = (*table)[i];
135 while (p != 0) {
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;
142 p = next;
145 *log_size_ptr = log_new_size;
146 *table = new_table;
149 # if defined(__STDC__) || defined(__cplusplus)
150 int GC_register_disappearing_link(GC_PTR * link)
151 # else
152 int GC_register_disappearing_link(link)
153 GC_PTR * link;
154 # endif
156 ptr_t base;
158 base = (ptr_t)GC_base((GC_PTR)link);
159 if (base == 0)
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,
166 GC_PTR obj)
167 # else
168 int GC_general_register_disappearing_link(link, obj)
169 GC_PTR * link;
170 GC_PTR obj;
171 # endif
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,
178 GC_PTR obj)
179 # else
180 static int GC_register_disappearing_link_inner(dl_hashtbl, link, obj)
181 struct dl_hashtbl_s *dl_hashtbl
182 GC_PTR * link;
183 GC_PTR obj;
184 # endif
186 struct disappearing_link *curr_dl;
187 int index;
188 struct disappearing_link * new_dl;
189 DCL_LOCK_STATE;
191 if ((word)link & (ALIGNMENT-1))
192 ABORT("Bad arg to GC_general_register_disappearing_link");
193 # ifdef THREADS
194 DISABLE_SIGNALS();
195 LOCK();
196 # endif
197 if (dl_hashtbl -> log_size == -1
198 || dl_hashtbl -> entries > ((word)1 << dl_hashtbl -> log_size)) {
199 # ifndef THREADS
200 DISABLE_SIGNALS();
201 # endif
202 GC_grow_table((struct hash_chain_entry ***)(&dl_hashtbl -> head),
203 &dl_hashtbl -> log_size);
204 # ifdef CONDPRINT
205 if (GC_print_stats) {
206 GC_printf1("Grew dl table to %lu entries\n",
207 (unsigned long)(1 << dl_hashtbl -> log_size));
209 # endif
210 # ifndef THREADS
211 ENABLE_SIGNALS();
212 # endif
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);
219 # ifdef THREADS
220 UNLOCK();
221 ENABLE_SIGNALS();
222 # endif
223 return(1);
226 new_dl = (struct disappearing_link *)
227 GC_INTERNAL_MALLOC(sizeof(struct disappearing_link),NORMAL);
228 if (0 == new_dl) {
229 # ifdef THREADS
230 UNLOCK();
231 ENABLE_SIGNALS();
232 # endif
233 new_dl = (struct disappearing_link *)
234 GC_oom_fn(sizeof(struct disappearing_link));
235 if (0 == new_dl) {
236 GC_finalization_failures++;
237 return(0);
239 /* It's not likely we'll make it here, but ... */
240 # ifdef THREADS
241 DISABLE_SIGNALS();
242 LOCK();
243 # endif
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++;
250 # ifdef THREADS
251 UNLOCK();
252 ENABLE_SIGNALS();
253 # endif
254 return(0);
257 # if defined(__STDC__) || defined(__cplusplus)
258 int GC_unregister_disappearing_link(GC_PTR * link)
259 # else
260 int GC_unregister_disappearing_link(link)
261 GC_PTR * link;
262 # endif
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)
269 # else
270 static int GC_unregister_disappearing_link_inner(dl_hashtbl, link)
271 struct dl_hashtbl_s *dl_hashtbl;
272 GC_PTR * link;
273 # endif
275 struct disappearing_link *curr_dl, *prev_dl;
276 int index;
277 DCL_LOCK_STATE;
279 DISABLE_SIGNALS();
280 LOCK();
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)) {
286 if (prev_dl == 0) {
287 dl_hashtbl -> head[index] = dl_next(curr_dl);
288 } else {
289 dl_set_next(prev_dl, dl_next(curr_dl));
291 dl_hashtbl -> entries--;
292 UNLOCK();
293 ENABLE_SIGNALS();
294 # ifdef DBG_HDRS_ALL
295 dl_set_next(curr_dl, 0);
296 # else
297 GC_free((GC_PTR)curr_dl);
298 # endif
299 return(1);
301 prev_dl = curr_dl;
302 curr_dl = dl_next(curr_dl);
304 out:
305 UNLOCK();
306 ENABLE_SIGNALS();
307 return(0);
310 /* toggleref support */
311 typedef struct {
312 GC_PTR strong_ref;
313 GC_hidden_pointer weak_ref;
314 } GCToggleRef;
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;
322 void
323 GC_process_togglerefs (void)
325 int i, w;
326 int toggle_ref_counts [3] = { 0, 0, 0 };
328 for (i = w = 0; i < GC_toggleref_array_size; ++i) {
329 int res;
330 GCToggleRef r = GC_toggleref_array [i];
332 GC_PTR obj;
334 if (r.strong_ref)
335 obj = r.strong_ref;
336 else if (r.weak_ref)
337 obj = REVEAL_POINTER (r.weak_ref);
338 else
339 continue;
341 res = GC_toggleref_callback (obj);
342 ++toggle_ref_counts [res];
343 switch (res) {
344 case 0:
345 break;
346 case 1:
347 GC_toggleref_array [w].strong_ref = obj;
348 GC_toggleref_array [w].weak_ref = (GC_hidden_pointer)NULL;
349 ++w;
350 break;
351 case 2:
352 GC_toggleref_array [w].strong_ref = NULL;
353 GC_toggleref_array [w].weak_ref = HIDE_POINTER (obj);
354 ++w;
355 break;
356 default:
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)
372 hdr * hhdr = HDR(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();
378 GC_set_mark_bit (p);
379 if (GC_mark_state != MS_NONE)
380 while (!GC_mark_some((ptr_t)0)) {}
383 static void GC_mark_togglerefs ()
385 int i;
386 if (!GC_toggleref_array)
387 return;
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 ()
401 int i;
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. */
408 } else {
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;
422 static void
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) {
430 GCToggleRef *tmp;
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;
442 void
443 GC_toggleref_add (GC_PTR object, int strong_ref)
445 DCL_LOCK_STATE;
446 # ifdef THREADS
447 DISABLE_SIGNALS();
448 LOCK();
449 # endif
451 if (!GC_toggleref_callback)
452 goto end;
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;
459 end:
460 # ifdef THREADS
461 UNLOCK();
462 ENABLE_SIGNALS();
463 # endif
468 # if defined(__STDC__) || defined(__cplusplus)
469 int GC_register_long_link(GC_PTR * link, GC_PTR obj)
470 # else
471 int GC_register_long_link(link, obj)
472 GC_PTR * link;
473 GC_PTR obj;
474 # endif
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)
481 # else
482 int GC_unregister_long_link(link)
483 GC_PTR * link;
484 # endif
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)
492 ptr_t p;
494 hdr * hhdr = HDR(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)
504 ptr_t p;
506 hdr * hhdr = HDR(p);
507 word descr = hhdr -> hb_descr;
508 ptr_t q, r;
509 ptr_t scan_limit;
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);
514 } else {
515 scan_limit = target_limit + 1 - sizeof(word);
517 for (q = p; q <= scan_limit; q += ALIGNMENT) {
518 r = *(ptr_t *)q;
519 if (r < p || r > target_limit) {
520 GC_PUSH_ONE_HEAP((word)r, q);
525 /*ARGSUSED*/
526 GC_API void GC_null_finalize_mark_proc(p)
527 ptr_t 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)
542 GC_PTR obj;
543 GC_finalization_proc fn;
544 GC_PTR cd;
545 GC_finalization_proc * ofn;
546 GC_PTR * ocd;
547 finalization_mark_proc * mp;
549 ptr_t base;
550 struct finalizable_object * curr_fo, * prev_fo;
551 int index;
552 struct finalizable_object *new_fo;
553 hdr *hhdr;
554 DCL_LOCK_STATE;
556 # ifdef THREADS
557 DISABLE_SIGNALS();
558 LOCK();
559 # endif
560 if (log_fo_table_size == -1
561 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
562 # ifndef THREADS
563 DISABLE_SIGNALS();
564 # endif
565 GC_grow_table((struct hash_chain_entry ***)(&fo_head),
566 &log_fo_table_size);
567 # ifdef CONDPRINT
568 if (GC_print_stats) {
569 GC_printf1("Grew fo table to %lu entries\n",
570 (unsigned long)(1 << log_fo_table_size));
572 # endif
573 # ifndef THREADS
574 ENABLE_SIGNALS();
575 # endif
577 /* in the THREADS case signals are disabled and we hold allocation */
578 /* lock; otherwise neither is true. Proceed carefully. */
579 base = (ptr_t)obj;
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 */
587 /* problem. */
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. */
591 if (prev_fo == 0) {
592 fo_head[index] = fo_next(curr_fo);
593 } else {
594 fo_set_next(prev_fo, fo_next(curr_fo));
596 if (fn == 0) {
597 GC_fo_entries--;
598 /* May not happen if we get a signal. But a high */
599 /* estimate will only make the table larger than */
600 /* necessary. */
601 # if !defined(THREADS) && !defined(DBG_HDRS_ALL)
602 GC_free((GC_PTR)curr_fo);
603 # endif
604 } else {
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. */
610 if (prev_fo == 0) {
611 fo_head[index] = curr_fo;
612 } else {
613 fo_set_next(prev_fo, curr_fo);
616 # ifdef THREADS
617 UNLOCK();
618 ENABLE_SIGNALS();
619 # endif
620 return;
622 prev_fo = curr_fo;
623 curr_fo = fo_next(curr_fo);
625 if (ofn) *ofn = 0;
626 if (ocd) *ocd = 0;
627 if (fn == 0) {
628 # ifdef THREADS
629 UNLOCK();
630 ENABLE_SIGNALS();
631 # endif
632 return;
634 GET_HDR(base, hhdr);
635 if (0 == hhdr) {
636 /* We won't collect it, hence finalizer wouldn't be run. */
637 # ifdef THREADS
638 UNLOCK();
639 ENABLE_SIGNALS();
640 # endif
641 return;
643 new_fo = (struct finalizable_object *)
644 GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
645 if (0 == new_fo) {
646 # ifdef THREADS
647 UNLOCK();
648 ENABLE_SIGNALS();
649 # endif
650 new_fo = (struct finalizable_object *)
651 GC_oom_fn(sizeof(struct finalizable_object));
652 if (0 == new_fo) {
653 GC_finalization_failures++;
654 return;
656 /* It's not likely we'll make it here, but ... */
657 # ifdef THREADS
658 DISABLE_SIGNALS();
659 LOCK();
660 # endif
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]);
668 GC_fo_entries++;
669 fo_head[index] = new_fo;
670 # ifdef THREADS
671 UNLOCK();
672 ENABLE_SIGNALS();
673 # endif
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)
680 # else
681 void GC_register_finalizer(obj, fn, cd, ofn, ocd)
682 GC_PTR obj;
683 GC_finalization_proc fn;
684 GC_PTR cd;
685 GC_finalization_proc * ofn;
686 GC_PTR * ocd;
687 # endif
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)
697 # else
698 void GC_register_finalizer_ignore_self(obj, fn, cd, ofn, ocd)
699 GC_PTR obj;
700 GC_finalization_proc fn;
701 GC_PTR cd;
702 GC_finalization_proc * ofn;
703 GC_PTR * ocd;
704 # endif
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)
714 # else
715 void GC_register_finalizer_no_order(obj, fn, cd, ofn, ocd)
716 GC_PTR obj;
717 GC_finalization_proc fn;
718 GC_PTR cd;
719 GC_finalization_proc * ofn;
720 GC_PTR * ocd;
721 # endif
723 GC_register_finalizer_inner(obj, fn, cd, ofn,
724 ocd, GC_null_finalize_mark_proc);
727 #ifndef NO_DEBUGGING
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;
735 int i;
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;
750 ptr_t real_ptr;
751 int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
752 int i;
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);
767 #endif
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;
773 register int i;
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];
778 prev_dl = 0;
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);
785 if (prev_dl == 0) {
786 dl_hashtbl -> head[i] = next_dl;
787 } else {
788 dl_set_next(prev_dl, next_dl);
790 GC_clear_mark_bit((ptr_t)curr_dl);
791 dl_hashtbl -> entries--;
792 curr_dl = next_dl;
793 } else {
794 prev_dl = curr_dl;
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;
805 register int i;
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];
810 prev_dl = 0;
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);
815 if (prev_dl == 0) {
816 dl_hashtbl -> head[i] = next_dl;
817 } else {
818 dl_set_next(prev_dl, next_dl);
820 GC_clear_mark_bit((ptr_t)curr_dl);
821 dl_hashtbl -> entries--;
822 curr_dl = next_dl;
823 } else {
824 prev_dl = curr_dl;
825 curr_dl = dl_next(curr_dl);
831 /* Called with world stopped. Cause disappearing links to disappear, */
832 /* and invoke finalizers. */
833 void GC_finalize()
835 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
836 ptr_t real_ptr;
837 register int i;
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 */
861 /* unreachable. */
862 GC_words_finalized = 0;
863 for (i = 0; i < fo_size; i++) {
864 curr_fo = fo_head[i];
865 prev_fo = 0;
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);
874 if (prev_fo == 0) {
875 fo_head[i] = next_fo;
876 } else {
877 fo_set_next(prev_fo, next_fo);
879 GC_fo_entries--;
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 */
884 /* see it. */
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)));
891 curr_fo = next_fo;
892 } else {
893 prev_fo = 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;
931 ptr_t real_ptr;
932 register int i;
933 int fo_size;
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];
939 prev_fo = 0;
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);
947 if (prev_fo == 0) {
948 fo_head[i] = next_fo;
949 } else {
950 fo_set_next(prev_fo, next_fo);
952 GC_fo_entries--;
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 */
959 /* see it. */
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));
966 curr_fo = next_fo;
970 return;
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.
978 * YUCK.
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()
989 DCL_LOCK_STATE;
991 DISABLE_SIGNALS();
992 LOCK();
993 while (GC_fo_entries > 0) {
994 GC_enqueue_all_finalizers();
995 UNLOCK();
996 ENABLE_SIGNALS();
997 GC_INVOKE_FINALIZERS();
998 DISABLE_SIGNALS();
999 LOCK();
1001 UNLOCK();
1002 ENABLE_SIGNALS();
1004 #endif
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;
1019 int count = 0;
1020 word mem_freed_before;
1021 DCL_LOCK_STATE;
1023 while (GC_finalize_now != 0) {
1024 # ifdef THREADS
1025 DISABLE_SIGNALS();
1026 LOCK();
1027 # endif
1028 if (count == 0) {
1029 mem_freed_before = GC_mem_freed;
1031 curr_fo = GC_finalize_now;
1032 # ifdef THREADS
1033 if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
1034 UNLOCK();
1035 ENABLE_SIGNALS();
1036 if (curr_fo == 0) break;
1037 # else
1038 GC_finalize_now = fo_next(curr_fo);
1039 # endif
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;
1044 ++count;
1045 # ifdef UNDEFINED
1046 /* This is probably a bad idea. It throws off accounting if */
1047 /* nearly all objects are finalizable. O.w. it shouldn't */
1048 /* matter. */
1049 GC_free((GC_PTR)curr_fo);
1050 # endif
1052 if (count != 0 && mem_freed_before != GC_mem_freed) {
1053 LOCK();
1054 GC_finalizer_mem_freed += (GC_mem_freed - mem_freed_before);
1055 UNLOCK();
1057 return count;
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) {
1072 word i;
1074 # ifdef KEEP_BACK_PTRS
1075 LOCK();
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. */
1083 UNLOCK();
1084 GC_generate_random_backtrace_no_gc();
1085 LOCK();
1087 last_back_trace_gc_no = GC_gc_no;
1088 UNLOCK();
1089 # endif
1090 # ifdef MAKE_BACK_GRAPH
1091 if (GC_print_back_height)
1092 GC_print_back_graph_stats();
1093 # endif
1095 # endif
1096 if (GC_finalize_now == 0) return;
1097 if (!GC_finalize_on_demand) {
1098 (void) GC_invoke_finalizers();
1099 # ifndef THREADS
1100 GC_ASSERT(GC_finalize_now == 0);
1101 # endif /* Otherwise GC can run concurrently and add more */
1102 return;
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();
1111 # ifdef __STDC__
1112 GC_PTR GC_call_with_alloc_lock(GC_fn_type fn,
1113 GC_PTR client_data)
1114 # else
1115 GC_PTR GC_call_with_alloc_lock(fn, client_data)
1116 GC_fn_type fn;
1117 GC_PTR client_data;
1118 # endif
1120 GC_PTR result;
1121 DCL_LOCK_STATE;
1123 # ifdef THREADS
1124 DISABLE_SIGNALS();
1125 LOCK();
1126 SET_LOCK_HOLDER();
1127 # endif
1128 result = (*fn)(client_data);
1129 # ifdef THREADS
1130 # ifndef GC_ASSERTIONS
1131 UNSET_LOCK_HOLDER();
1132 # endif /* o.w. UNLOCK() does it implicitly */
1133 UNLOCK();
1134 ENABLE_SIGNALS();
1135 # endif
1136 return(result);
1139 #if !defined(NO_DEBUGGING)
1141 void GC_print_finalization_stats()
1143 struct finalizable_object *fo = GC_finalize_now;
1144 size_t ready = 0;
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 */