2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
14 /* Boehm, November 8, 1994 5:35 pm PST */
15 # define I_HIDE_POINTERS
19 /* Type of mark procedure used for marking from finalizable object. */
20 /* This procedure normally does not mark the object, only its */
22 typedef void finalization_mark_proc(/* ptr_t finalizable_obj_ptr */);
24 # define HASH3(addr,size,log_size) \
25 ((((word)(addr) >> 3) ^ ((word)(addr) >> (3+(log_size)))) \
27 #define HASH2(addr,log_size) HASH3(addr, 1 << log_size, log_size)
29 struct hash_chain_entry {
31 struct hash_chain_entry * next;
34 unsigned GC_finalization_failures = 0;
35 /* Number of finalization requests that failed for lack of memory. */
37 static struct disappearing_link {
38 struct hash_chain_entry prolog;
39 # define dl_hidden_link prolog.hidden_key
40 /* Field to be cleared. */
41 # define dl_next(x) (struct disappearing_link *)((x) -> prolog.next)
42 # define dl_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
44 word dl_hidden_obj; /* Pointer to object base */
47 static signed_word log_dl_table_size = -1;
49 /* current size of array pointed to by dl_head. */
50 /* -1 ==> size is 0. */
52 word GC_dl_entries = 0; /* Number of entries currently in disappearing */
55 static struct finalizable_object {
56 struct hash_chain_entry prolog;
57 # define fo_hidden_base prolog.hidden_key
58 /* Pointer to object base. */
59 # define fo_next(x) (struct finalizable_object *)((x) -> prolog.next)
60 # define fo_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
61 GC_finalization_proc fo_fn; /* Finalizer. */
63 word fo_object_size; /* In bytes. */
64 finalization_mark_proc * fo_mark_proc; /* Mark-through procedure */
67 struct finalizable_object * GC_finalize_now = 0;
68 /* LIst of objects that should be finalized now. */
70 static signed_word log_fo_table_size = -1;
72 word GC_fo_entries = 0;
75 void GC_push_finalizer_structures()
77 GC_push_all((ptr_t)(&dl_head), (ptr_t)(&dl_head) + sizeof(word));
78 GC_push_all((ptr_t)(&fo_head), (ptr_t)(&fo_head) + sizeof(word));
82 # define ALLOC(x, t) t *x = GC_NEW(t)
84 /* Double the size of a hash table. *size_ptr is the log of its current */
85 /* size. May be a noop. */
86 /* *table is a pointer to an array of hash headers. If we succeed, we */
87 /* update both *table and *log_size_ptr. */
88 /* Lock is held. Signals are disabled. */
89 void GC_grow_table(table, log_size_ptr)
90 struct hash_chain_entry ***table;
91 signed_word * log_size_ptr;
94 register struct hash_chain_entry *p;
95 int log_old_size = *log_size_ptr;
96 register int log_new_size = log_old_size + 1;
97 word old_size = ((log_old_size == -1)? 0: (1 << log_old_size));
98 register word new_size = 1 << log_new_size;
99 struct hash_chain_entry **new_table = (struct hash_chain_entry **)
100 GC_generic_malloc_inner_ignore_off_page(
101 (size_t)new_size * sizeof(struct hash_chain_entry *), NORMAL);
103 if (new_table == 0) {
105 ABORT("Insufficient space for initial table allocation");
110 for (i = 0; i < old_size; i++) {
113 register ptr_t real_key = (ptr_t)REVEAL_POINTER(p -> hidden_key);
114 register struct hash_chain_entry *next = p -> next;
115 register int new_hash = HASH3(real_key, new_size, log_new_size);
117 p -> next = new_table[new_hash];
118 new_table[new_hash] = p;
122 *log_size_ptr = log_new_size;
127 int GC_register_disappearing_link(link)
132 base = (ptr_t)GC_base((extern_ptr_t)link);
134 ABORT("Bad arg to GC_register_disappearing_link");
135 return(GC_general_register_disappearing_link(link, base));
138 int GC_general_register_disappearing_link(link, obj)
142 struct disappearing_link *curr_dl;
144 struct disappearing_link * new_dl;
147 if ((word)link & (ALIGNMENT-1))
148 ABORT("Bad arg to GC_general_register_disappearing_link");
153 if (log_dl_table_size == -1
154 || GC_dl_entries > ((word)1 << log_dl_table_size)) {
158 GC_grow_table((struct hash_chain_entry ***)(&dl_head),
161 GC_printf1("Grew dl table to %lu entries\n",
162 (unsigned long)(1 << log_dl_table_size));
168 index = HASH2(link, log_dl_table_size);
169 curr_dl = dl_head[index];
170 for (curr_dl = dl_head[index]; curr_dl != 0; curr_dl = dl_next(curr_dl)) {
171 if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
172 curr_dl -> dl_hidden_obj = HIDE_POINTER(obj);
181 new_dl = (struct disappearing_link *)
182 GC_generic_malloc_inner(sizeof(struct disappearing_link),NORMAL);
184 new_dl = GC_NEW(struct disappearing_link);
187 new_dl -> dl_hidden_obj = HIDE_POINTER(obj);
188 new_dl -> dl_hidden_link = HIDE_POINTER(link);
189 dl_set_next(new_dl, dl_head[index]);
190 dl_head[index] = new_dl;
193 GC_finalization_failures++;
202 int GC_unregister_disappearing_link(link)
205 struct disappearing_link *curr_dl, *prev_dl;
211 index = HASH2(link, log_dl_table_size);
212 if (((unsigned long)link & (ALIGNMENT-1))) goto out;
213 prev_dl = 0; curr_dl = dl_head[index];
214 while (curr_dl != 0) {
215 if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
217 dl_head[index] = dl_next(curr_dl);
219 dl_set_next(prev_dl, dl_next(curr_dl));
224 GC_free((extern_ptr_t)curr_dl);
228 curr_dl = dl_next(curr_dl);
236 /* Possible finalization_marker procedures. Note that mark stack */
237 /* overflow is handled by the caller, and is not a disaster. */
238 void GC_normal_finalize_mark_proc(p)
243 PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top,
244 &(GC_mark_stack[GC_mark_stack_size]));
247 /* This only pays very partial attention to the mark descriptor. */
248 /* It does the right thing for normal and atomic objects, and treats */
249 /* most others as normal. */
250 void GC_ignore_self_finalize_mark_proc(p)
254 word descr = hhdr -> hb_descr;
258 if ((descr & DS_TAGS) == DS_LENGTH) {
259 limit = p + descr - sizeof(word);
261 limit = p + WORDS_TO_BYTES(hhdr -> hb_sz - 1);
263 for (q = p; q <= limit; q += ALIGNMENT) {
265 if (r < p || r > limit) {
266 GC_PUSH_ONE_HEAP((word)r);
272 void GC_null_finalize_mark_proc(p)
279 /* Register a finalization function. See gc.h for details. */
280 /* in the nonthreads case, we try to avoid disabling signals, */
281 /* since it can be expensive. Threads packages typically */
282 /* make it cheaper. */
283 void GC_register_finalizer_inner(obj, fn, cd, ofn, ocd, mp)
285 GC_finalization_proc fn;
287 GC_finalization_proc * ofn;
289 finalization_mark_proc * mp;
292 struct finalizable_object * curr_fo, * prev_fo;
294 struct finalizable_object *new_fo;
301 if (log_fo_table_size == -1
302 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
306 GC_grow_table((struct hash_chain_entry ***)(&fo_head),
309 GC_printf1("Grew fo table to %lu entries\n",
310 (unsigned long)(1 << log_fo_table_size));
316 /* in the THREADS case signals are disabled and we hold allocation */
317 /* lock; otherwise neither is true. Proceed carefully. */
319 index = HASH2(base, log_fo_table_size);
320 prev_fo = 0; curr_fo = fo_head[index];
321 while (curr_fo != 0) {
322 if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
323 /* Interruption by a signal in the middle of this */
324 /* should be safe. The client may see only *ocd */
325 /* updated, but we'll declare that to be his */
327 if (ocd) *ocd = (extern_ptr_t) curr_fo -> fo_client_data;
328 if (ofn) *ofn = curr_fo -> fo_fn;
329 /* Delete the structure for base. */
331 fo_head[index] = fo_next(curr_fo);
333 fo_set_next(prev_fo, fo_next(curr_fo));
337 /* May not happen if we get a signal. But a high */
338 /* estimate will only make the table larger than */
341 GC_free((extern_ptr_t)curr_fo);
344 curr_fo -> fo_fn = fn;
345 curr_fo -> fo_client_data = (ptr_t)cd;
346 curr_fo -> fo_mark_proc = mp;
347 /* Reinsert it. We deleted it first to maintain */
348 /* consistency in the event of a signal. */
350 fo_head[index] = curr_fo;
352 fo_set_next(prev_fo, curr_fo);
362 curr_fo = fo_next(curr_fo);
374 new_fo = (struct finalizable_object *)
375 GC_generic_malloc_inner(sizeof(struct finalizable_object),NORMAL);
377 new_fo = GC_NEW(struct finalizable_object);
380 new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
381 new_fo -> fo_fn = fn;
382 new_fo -> fo_client_data = (ptr_t)cd;
383 new_fo -> fo_object_size = GC_size(base);
384 new_fo -> fo_mark_proc = mp;
385 fo_set_next(new_fo, fo_head[index]);
387 fo_head[index] = new_fo;
389 GC_finalization_failures++;
397 # if defined(__STDC__)
398 void GC_register_finalizer(void * obj,
399 GC_finalization_proc fn, void * cd,
400 GC_finalization_proc *ofn, void ** ocd)
402 void GC_register_finalizer(obj, fn, cd, ofn, ocd)
404 GC_finalization_proc fn;
406 GC_finalization_proc * ofn;
410 GC_register_finalizer_inner(obj, fn, cd, ofn,
411 ocd, GC_normal_finalize_mark_proc);
414 # if defined(__STDC__)
415 void GC_register_finalizer_ignore_self(void * obj,
416 GC_finalization_proc fn, void * cd,
417 GC_finalization_proc *ofn, void ** ocd)
419 void GC_register_finalizer_ignore_self(obj, fn, cd, ofn, ocd)
421 GC_finalization_proc fn;
423 GC_finalization_proc * ofn;
427 GC_register_finalizer_inner(obj, fn, cd, ofn,
428 ocd, GC_ignore_self_finalize_mark_proc);
431 # if defined(__STDC__)
432 void GC_register_finalizer_no_order(void * obj,
433 GC_finalization_proc fn, void * cd,
434 GC_finalization_proc *ofn, void ** ocd)
436 void GC_register_finalizer_no_order(obj, fn, cd, ofn, ocd)
438 GC_finalization_proc fn;
440 GC_finalization_proc * ofn;
444 GC_register_finalizer_inner(obj, fn, cd, ofn,
445 ocd, GC_null_finalize_mark_proc);
449 /* Called with world stopped. Cause disappearing links to disappear, */
450 /* and invoke finalizers. */
453 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
454 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
455 ptr_t real_ptr, real_link;
457 int dl_size = (log_dl_table_size == -1 ) ? 0 : (1 << log_dl_table_size);
458 int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
460 /* Make disappearing links disappear */
461 for (i = 0; i < dl_size; i++) {
462 curr_dl = dl_head[i];
464 while (curr_dl != 0) {
465 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
466 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
467 if (!GC_is_marked(real_ptr)) {
468 *(word *)real_link = 0;
469 next_dl = dl_next(curr_dl);
471 dl_head[i] = next_dl;
473 dl_set_next(prev_dl, next_dl);
475 GC_clear_mark_bit((ptr_t)curr_dl);
480 curr_dl = dl_next(curr_dl);
484 /* Mark all objects reachable via chains of 1 or more pointers */
485 /* from finalizable objects. */
487 if (GC_mark_state != MS_NONE) ABORT("Bad mark state");
489 for (i = 0; i < fo_size; i++) {
490 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
491 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
492 if (!GC_is_marked(real_ptr)) {
493 (*(curr_fo -> fo_mark_proc))(real_ptr);
494 while (!GC_mark_stack_empty()) GC_mark_from_mark_stack();
495 if (GC_mark_state != MS_NONE) {
496 /* Mark stack overflowed. Very unlikely. */
498 if (GC_mark_state != MS_INVALID) ABORT("Bad mark state");
499 GC_printf0("Mark stack overflowed in finalization!!\n");
501 /* Make mark bits consistent again. Forget about */
502 /* finalizing this object for now. */
503 GC_set_mark_bit(real_ptr);
504 while (!GC_mark_some());
507 if (GC_is_marked(real_ptr)) {
508 --> Report finalization cycle here, if desired
515 /* Enqueue for finalization all objects that are still */
517 for (i = 0; i < fo_size; i++) {
518 curr_fo = fo_head[i];
520 while (curr_fo != 0) {
521 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
522 if (!GC_is_marked(real_ptr)) {
523 GC_set_mark_bit(real_ptr);
524 /* Delete from hash table */
525 next_fo = fo_next(curr_fo);
527 fo_head[i] = next_fo;
529 fo_set_next(prev_fo, next_fo);
532 /* Add to list of objects awaiting finalization. */
533 fo_set_next(curr_fo, GC_finalize_now);
534 GC_finalize_now = curr_fo;
536 if (!GC_is_marked((ptr_t)curr_fo)) {
537 ABORT("GC_finalize: found accessible unmarked object\n");
543 curr_fo = fo_next(curr_fo);
547 /* Remove dangling disappearing links. */
548 for (i = 0; i < dl_size; i++) {
549 curr_dl = dl_head[i];
551 while (curr_dl != 0) {
552 real_link = GC_base((ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link));
553 if (real_link != 0 && !GC_is_marked(real_link)) {
554 next_dl = dl_next(curr_dl);
556 dl_head[i] = next_dl;
558 dl_set_next(prev_dl, next_dl);
560 GC_clear_mark_bit((ptr_t)curr_dl);
565 curr_dl = dl_next(curr_dl);
571 /* Invoke finalizers for all objects that are ready to be finalized. */
572 /* Should be called without allocation lock. */
573 void GC_invoke_finalizers()
576 register struct finalizable_object * curr_fo;
579 while (GC_finalize_now != 0) {
584 curr_fo = GC_finalize_now;
586 if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
589 if (curr_fo == 0) break;
591 GC_finalize_now = fo_next(curr_fo);
593 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
594 (*(curr_fo -> fo_fn))(real_ptr, curr_fo -> fo_client_data);
596 GC_free((extern_ptr_t)curr_fo);
602 extern_ptr_t GC_call_with_alloc_lock(GC_fn_type fn, extern_ptr_t client_data)
604 extern_ptr_t GC_call_with_alloc_lock(fn, client_data)
606 extern_ptr_t client_data;
616 result = (*fn)(client_data);