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, May 19, 1994 2:08 pm PDT */
15 # define I_HIDE_POINTERS
20 # define HASH3(addr,size,log_size) \
21 ((((word)(addr) >> 3) ^ ((word)(addr) >> (3+(log_size)))) \
23 #define HASH2(addr,log_size) HASH3(addr, 1 << log_size, log_size)
25 struct hash_chain_entry {
27 struct hash_chain_entry * next;
30 unsigned GC_finalization_failures = 0;
31 /* Number of finalization requests that failed for lack of memory. */
33 static struct disappearing_link {
34 struct hash_chain_entry prolog;
35 # define dl_hidden_link prolog.hidden_key
36 /* Field to be cleared. */
37 # define dl_next(x) (struct disappearing_link *)((x) -> prolog.next)
38 # define dl_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
40 word dl_hidden_obj; /* Pointer to object base */
43 static signed_word log_dl_table_size = -1;
45 /* current size of array pointed to by dl_head. */
46 /* -1 ==> size is 0. */
48 word GC_dl_entries = 0; /* Number of entries currently in disappearing */
51 static struct finalizable_object {
52 struct hash_chain_entry prolog;
53 # define fo_hidden_base prolog.hidden_key
54 /* Pointer to object base. */
55 # define fo_next(x) (struct finalizable_object *)((x) -> prolog.next)
56 # define fo_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
57 GC_finalization_proc fo_fn; /* Finalizer. */
59 word fo_object_size; /* In bytes. */
62 struct finalizable_object * GC_finalize_now = 0;
63 /* LIst of objects that should be finalized now. */
65 static signed_word log_fo_table_size = -1;
67 word GC_fo_entries = 0;
70 void GC_push_finalizer_structures()
72 GC_push_all((ptr_t)(&dl_head), (ptr_t)(&dl_head) + sizeof(word));
73 GC_push_all((ptr_t)(&fo_head), (ptr_t)(&fo_head) + sizeof(word));
77 # define ALLOC(x, t) t *x = GC_NEW(t)
79 /* Double the size of a hash table. *size_ptr is the log of its current */
80 /* size. May be a noop. */
81 /* *table is a pointer to an array of hash headers. If we succeed, we */
82 /* update both *table and *log_size_ptr. */
83 /* Lock is held. Signals are disabled. */
84 void GC_grow_table(table, log_size_ptr)
85 struct hash_chain_entry ***table;
86 signed_word * log_size_ptr;
89 register struct hash_chain_entry *p;
90 int log_old_size = *log_size_ptr;
91 register int log_new_size = log_old_size + 1;
92 word old_size = ((log_old_size == -1)? 0: (1 << log_old_size));
93 register word new_size = 1 << log_new_size;
94 struct hash_chain_entry **new_table = (struct hash_chain_entry **)
95 GC_malloc_ignore_off_page_inner(
96 (size_t)new_size * sizeof(struct hash_chain_entry *));
100 ABORT("Insufficient space for initial table allocation");
105 for (i = 0; i < old_size; i++) {
108 register ptr_t real_key = (ptr_t)REVEAL_POINTER(p -> hidden_key);
109 register struct hash_chain_entry *next = p -> next;
110 register int new_hash = HASH3(real_key, new_size, log_new_size);
112 p -> next = new_table[new_hash];
113 new_table[new_hash] = p;
117 *log_size_ptr = log_new_size;
122 int GC_register_disappearing_link(link)
127 base = (ptr_t)GC_base((extern_ptr_t)link);
129 ABORT("Bad arg to GC_register_disappearing_link");
130 return(GC_general_register_disappearing_link(link, base));
133 int GC_general_register_disappearing_link(link, obj)
137 struct disappearing_link *curr_dl;
139 struct disappearing_link * new_dl;
142 if ((word)link & (ALIGNMENT-1))
143 ABORT("Bad arg to GC_general_register_disappearing_link");
148 if (log_dl_table_size == -1
149 || GC_dl_entries > ((word)1 << log_dl_table_size)) {
153 GC_grow_table((struct hash_chain_entry ***)(&dl_head),
156 GC_printf1("Grew dl table to %lu entries\n",
157 (unsigned long)(1 << log_dl_table_size));
163 index = HASH2(link, log_dl_table_size);
164 curr_dl = dl_head[index];
165 for (curr_dl = dl_head[index]; curr_dl != 0; curr_dl = dl_next(curr_dl)) {
166 if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
167 curr_dl -> dl_hidden_obj = HIDE_POINTER(obj);
176 new_dl = (struct disappearing_link *)
177 GC_generic_malloc_inner(sizeof(struct disappearing_link),NORMAL);
179 new_dl = GC_NEW(struct disappearing_link);
182 new_dl -> dl_hidden_obj = HIDE_POINTER(obj);
183 new_dl -> dl_hidden_link = HIDE_POINTER(link);
184 dl_set_next(new_dl, dl_head[index]);
185 dl_head[index] = new_dl;
188 GC_finalization_failures++;
197 int GC_unregister_disappearing_link(link)
200 struct disappearing_link *curr_dl, *prev_dl;
206 index = HASH2(link, log_dl_table_size);
207 if (((unsigned long)link & (ALIGNMENT-1))) goto out;
208 prev_dl = 0; curr_dl = dl_head[index];
209 while (curr_dl != 0) {
210 if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
212 dl_head[index] = dl_next(curr_dl);
214 dl_set_next(prev_dl, dl_next(curr_dl));
219 GC_free((extern_ptr_t)curr_dl);
223 curr_dl = dl_next(curr_dl);
231 /* Register a finalization function. See gc.h for details. */
232 /* in the nonthreads case, we try to avoid disabling signals, */
233 /* since it can be expensive. Threads packages typically */
234 /* make it cheaper. */
235 void GC_register_finalizer(obj, fn, cd, ofn, ocd)
237 GC_finalization_proc fn;
239 GC_finalization_proc * ofn;
243 struct finalizable_object * curr_fo, * prev_fo;
245 struct finalizable_object *new_fo;
252 if (log_fo_table_size == -1
253 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
257 GC_grow_table((struct hash_chain_entry ***)(&fo_head),
260 GC_printf1("Grew fo table to %lu entries\n",
261 (unsigned long)(1 << log_fo_table_size));
267 /* in the THREADS case signals are disabled and we hold allocation */
268 /* lock; otherwise neither is true. Proceed carefully. */
270 index = HASH2(base, log_fo_table_size);
271 prev_fo = 0; curr_fo = fo_head[index];
272 while (curr_fo != 0) {
273 if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
274 /* Interruption by a signal in the middle of this */
275 /* should be safe. The client may see only *ocd */
276 /* updated, but we'll declare that to be his */
278 if (ocd) *ocd = (extern_ptr_t) curr_fo -> fo_client_data;
279 if (ofn) *ofn = curr_fo -> fo_fn;
280 /* Delete the structure for base. */
282 fo_head[index] = fo_next(curr_fo);
284 fo_set_next(prev_fo, fo_next(curr_fo));
288 /* May not happen if we get a signal. But a high */
289 /* estimate will only make the table larger than */
292 GC_free((extern_ptr_t)curr_fo);
295 curr_fo -> fo_fn = fn;
296 curr_fo -> fo_client_data = (ptr_t)cd;
297 /* Reinsert it. We deleted it first to maintain */
298 /* consistency in the event of a signal. */
300 fo_head[index] = curr_fo;
302 fo_set_next(prev_fo, curr_fo);
312 curr_fo = fo_next(curr_fo);
324 new_fo = (struct finalizable_object *)
325 GC_generic_malloc_inner(sizeof(struct finalizable_object),NORMAL);
327 new_fo = GC_NEW(struct finalizable_object);
330 new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
331 new_fo -> fo_fn = fn;
332 new_fo -> fo_client_data = (ptr_t)cd;
333 new_fo -> fo_object_size = GC_size(base);
334 fo_set_next(new_fo, fo_head[index]);
336 fo_head[index] = new_fo;
338 GC_finalization_failures++;
346 /* Called with world stopped. Cause disappearing links to disappear, */
347 /* and invoke finalizers. */
350 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
351 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
352 ptr_t real_ptr, real_link;
354 int dl_size = 1 << log_dl_table_size;
355 int fo_size = 1 << log_fo_table_size;
357 /* Make disappearing links disappear */
358 for (i = 0; i < dl_size; i++) {
359 curr_dl = dl_head[i];
361 while (curr_dl != 0) {
362 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
363 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
364 if (!GC_is_marked(real_ptr)) {
365 *(word *)real_link = 0;
366 next_dl = dl_next(curr_dl);
368 dl_head[i] = next_dl;
370 dl_set_next(prev_dl, next_dl);
372 GC_clear_mark_bit((ptr_t)curr_dl);
377 curr_dl = dl_next(curr_dl);
381 /* Mark all objects reachable via chains of 1 or more pointers */
382 /* from finalizable objects. */
384 if (GC_mark_state != MS_NONE) ABORT("Bad mark state");
386 for (i = 0; i < fo_size; i++) {
387 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
388 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
389 if (!GC_is_marked(real_ptr)) {
390 hdr * hhdr = HDR(real_ptr);
392 PUSH_OBJ((word *)real_ptr, hhdr, GC_mark_stack_top,
393 &(GC_mark_stack[GC_mark_stack_size]));
394 while (!GC_mark_stack_empty()) GC_mark_from_mark_stack();
395 if (GC_mark_state != MS_NONE) {
396 /* Mark stack overflowed. Very unlikely. */
398 if (GC_mark_state != MS_INVALID) ABORT("Bad mark state");
399 GC_printf0("Mark stack overflowed in finalization!!\n");
401 /* Make mark bits consistent again. Forget about */
402 /* finalizing this object for now. */
403 GC_set_mark_bit(real_ptr);
404 while (!GC_mark_some());
407 if (GC_is_marked(real_ptr)) {
408 --> Report finalization cycle here, if desired
415 /* Enqueue for finalization all objects that are still */
417 for (i = 0; i < fo_size; i++) {
418 curr_fo = fo_head[i];
420 while (curr_fo != 0) {
421 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
422 if (!GC_is_marked(real_ptr)) {
423 GC_set_mark_bit(real_ptr);
424 /* Delete from hash table */
425 next_fo = fo_next(curr_fo);
427 fo_head[i] = next_fo;
429 fo_set_next(prev_fo, next_fo);
432 /* Add to list of objects awaiting finalization. */
433 fo_set_next(curr_fo, GC_finalize_now);
434 GC_finalize_now = curr_fo;
436 if (!GC_is_marked((ptr_t)curr_fo)) {
437 ABORT("GC_finalize: found accessible unmarked object\n");
443 curr_fo = fo_next(curr_fo);
447 /* Remove dangling disappearing links. */
448 for (i = 0; i < dl_size; i++) {
449 curr_dl = dl_head[i];
451 while (curr_dl != 0) {
452 real_link = GC_base((ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link));
453 if (real_link != 0 && !GC_is_marked(real_link)) {
454 next_dl = dl_next(curr_dl);
456 dl_head[i] = next_dl;
458 dl_set_next(prev_dl, next_dl);
460 GC_clear_mark_bit((ptr_t)curr_dl);
465 curr_dl = dl_next(curr_dl);
471 /* Invoke finalizers for all objects that are ready to be finalized. */
472 /* Should be called without allocation lock. */
473 void GC_invoke_finalizers()
476 register struct finalizable_object * curr_fo;
479 while (GC_finalize_now != 0) {
484 curr_fo = GC_finalize_now;
486 if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
489 if (curr_fo == 0) break;
491 GC_finalize_now = fo_next(curr_fo);
493 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
494 (*(curr_fo -> fo_fn))(real_ptr, curr_fo -> fo_client_data);
496 GC_free((extern_ptr_t)curr_fo);
502 extern_ptr_t GC_call_with_alloc_lock(GC_fn_type fn, extern_ptr_t client_data)
504 extern_ptr_t GC_call_with_alloc_lock(fn, client_data)
506 extern_ptr_t client_data;
516 result = (*fn)(client_data);