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, June 9, 1994 2:17 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_generic_malloc_inner_ignore_off_page(
96 (size_t)new_size * sizeof(struct hash_chain_entry *), NORMAL);
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 # if defined(__STDC__)
236 void GC_register_finalizer(void * obj,
237 GC_finalization_proc fn, void * cd,
238 GC_finalization_proc *ofn, void ** ocd)
240 void GC_register_finalizer(obj, fn, cd, ofn, ocd)
242 GC_finalization_proc fn;
244 GC_finalization_proc * ofn;
249 struct finalizable_object * curr_fo, * prev_fo;
251 struct finalizable_object *new_fo;
258 if (log_fo_table_size == -1
259 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
263 GC_grow_table((struct hash_chain_entry ***)(&fo_head),
266 GC_printf1("Grew fo table to %lu entries\n",
267 (unsigned long)(1 << log_fo_table_size));
273 /* in the THREADS case signals are disabled and we hold allocation */
274 /* lock; otherwise neither is true. Proceed carefully. */
276 index = HASH2(base, log_fo_table_size);
277 prev_fo = 0; curr_fo = fo_head[index];
278 while (curr_fo != 0) {
279 if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
280 /* Interruption by a signal in the middle of this */
281 /* should be safe. The client may see only *ocd */
282 /* updated, but we'll declare that to be his */
284 if (ocd) *ocd = (extern_ptr_t) curr_fo -> fo_client_data;
285 if (ofn) *ofn = curr_fo -> fo_fn;
286 /* Delete the structure for base. */
288 fo_head[index] = fo_next(curr_fo);
290 fo_set_next(prev_fo, fo_next(curr_fo));
294 /* May not happen if we get a signal. But a high */
295 /* estimate will only make the table larger than */
298 GC_free((extern_ptr_t)curr_fo);
301 curr_fo -> fo_fn = fn;
302 curr_fo -> fo_client_data = (ptr_t)cd;
303 /* Reinsert it. We deleted it first to maintain */
304 /* consistency in the event of a signal. */
306 fo_head[index] = curr_fo;
308 fo_set_next(prev_fo, curr_fo);
318 curr_fo = fo_next(curr_fo);
330 new_fo = (struct finalizable_object *)
331 GC_generic_malloc_inner(sizeof(struct finalizable_object),NORMAL);
333 new_fo = GC_NEW(struct finalizable_object);
336 new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
337 new_fo -> fo_fn = fn;
338 new_fo -> fo_client_data = (ptr_t)cd;
339 new_fo -> fo_object_size = GC_size(base);
340 fo_set_next(new_fo, fo_head[index]);
342 fo_head[index] = new_fo;
344 GC_finalization_failures++;
352 /* Called with world stopped. Cause disappearing links to disappear, */
353 /* and invoke finalizers. */
356 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
357 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
358 ptr_t real_ptr, real_link;
360 int dl_size = 1 << log_dl_table_size;
361 int fo_size = 1 << log_fo_table_size;
363 /* Make disappearing links disappear */
364 for (i = 0; i < dl_size; i++) {
365 curr_dl = dl_head[i];
367 while (curr_dl != 0) {
368 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
369 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
370 if (!GC_is_marked(real_ptr)) {
371 *(word *)real_link = 0;
372 next_dl = dl_next(curr_dl);
374 dl_head[i] = next_dl;
376 dl_set_next(prev_dl, next_dl);
378 GC_clear_mark_bit((ptr_t)curr_dl);
383 curr_dl = dl_next(curr_dl);
387 /* Mark all objects reachable via chains of 1 or more pointers */
388 /* from finalizable objects. */
390 if (GC_mark_state != MS_NONE) ABORT("Bad mark state");
392 for (i = 0; i < fo_size; i++) {
393 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
394 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
395 if (!GC_is_marked(real_ptr)) {
396 hdr * hhdr = HDR(real_ptr);
398 PUSH_OBJ((word *)real_ptr, hhdr, GC_mark_stack_top,
399 &(GC_mark_stack[GC_mark_stack_size]));
400 while (!GC_mark_stack_empty()) GC_mark_from_mark_stack();
401 if (GC_mark_state != MS_NONE) {
402 /* Mark stack overflowed. Very unlikely. */
404 if (GC_mark_state != MS_INVALID) ABORT("Bad mark state");
405 GC_printf0("Mark stack overflowed in finalization!!\n");
407 /* Make mark bits consistent again. Forget about */
408 /* finalizing this object for now. */
409 GC_set_mark_bit(real_ptr);
410 while (!GC_mark_some());
413 if (GC_is_marked(real_ptr)) {
414 --> Report finalization cycle here, if desired
421 /* Enqueue for finalization all objects that are still */
423 for (i = 0; i < fo_size; i++) {
424 curr_fo = fo_head[i];
426 while (curr_fo != 0) {
427 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
428 if (!GC_is_marked(real_ptr)) {
429 GC_set_mark_bit(real_ptr);
430 /* Delete from hash table */
431 next_fo = fo_next(curr_fo);
433 fo_head[i] = next_fo;
435 fo_set_next(prev_fo, next_fo);
438 /* Add to list of objects awaiting finalization. */
439 fo_set_next(curr_fo, GC_finalize_now);
440 GC_finalize_now = curr_fo;
442 if (!GC_is_marked((ptr_t)curr_fo)) {
443 ABORT("GC_finalize: found accessible unmarked object\n");
449 curr_fo = fo_next(curr_fo);
453 /* Remove dangling disappearing links. */
454 for (i = 0; i < dl_size; i++) {
455 curr_dl = dl_head[i];
457 while (curr_dl != 0) {
458 real_link = GC_base((ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link));
459 if (real_link != 0 && !GC_is_marked(real_link)) {
460 next_dl = dl_next(curr_dl);
462 dl_head[i] = next_dl;
464 dl_set_next(prev_dl, next_dl);
466 GC_clear_mark_bit((ptr_t)curr_dl);
471 curr_dl = dl_next(curr_dl);
477 /* Invoke finalizers for all objects that are ready to be finalized. */
478 /* Should be called without allocation lock. */
479 void GC_invoke_finalizers()
482 register struct finalizable_object * curr_fo;
485 while (GC_finalize_now != 0) {
490 curr_fo = GC_finalize_now;
492 if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
495 if (curr_fo == 0) break;
497 GC_finalize_now = fo_next(curr_fo);
499 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
500 (*(curr_fo -> fo_fn))(real_ptr, curr_fo -> fo_client_data);
502 GC_free((extern_ptr_t)curr_fo);
508 extern_ptr_t GC_call_with_alloc_lock(GC_fn_type fn, extern_ptr_t client_data)
510 extern_ptr_t GC_call_with_alloc_lock(fn, client_data)
512 extern_ptr_t client_data;
522 result = (*fn)(client_data);