gc6.0alpha3 tarball import
[platform/upstream/libgc.git] / finalize.c
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.
5
6  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
8  *
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.
14  */
15 /* Boehm, February 1, 1996 1:19 pm PST */
16 # define I_HIDE_POINTERS
17 # include "private/gc_priv.h"
18 # include "private/gc_mark.h"
19
20 # ifdef FINALIZE_ON_DEMAND
21     int GC_finalize_on_demand = 1;
22 # else
23     int GC_finalize_on_demand = 0;
24 # endif
25
26 # ifdef JAVA_FINALIZATION
27     int GC_java_finalization = 1;
28 # else
29     int GC_java_finalization = 0;
30 # endif
31
32 /* Type of mark procedure used for marking from finalizable object.     */
33 /* This procedure normally does not mark the object, only its           */
34 /* descendents.                                                         */
35 typedef void finalization_mark_proc(/* ptr_t finalizable_obj_ptr */); 
36
37 # define HASH3(addr,size,log_size) \
38     ((((word)(addr) >> 3) ^ ((word)(addr) >> (3+(log_size)))) \
39     & ((size) - 1))
40 #define HASH2(addr,log_size) HASH3(addr, 1 << log_size, log_size)
41
42 struct hash_chain_entry {
43     word hidden_key;
44     struct hash_chain_entry * next;
45 };
46
47 unsigned GC_finalization_failures = 0;
48         /* Number of finalization requests that failed for lack of memory. */
49
50 static struct disappearing_link {
51     struct hash_chain_entry prolog;
52 #   define dl_hidden_link prolog.hidden_key
53                                 /* Field to be cleared.         */
54 #   define dl_next(x) (struct disappearing_link *)((x) -> prolog.next)
55 #   define dl_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
56
57     word dl_hidden_obj;         /* Pointer to object base       */
58 } **dl_head = 0;
59
60 static signed_word log_dl_table_size = -1;
61                         /* Binary log of                                */
62                         /* current size of array pointed to by dl_head. */
63                         /* -1 ==> size is 0.                            */
64
65 word GC_dl_entries = 0; /* Number of entries currently in disappearing  */
66                         /* link table.                                  */
67
68 static struct finalizable_object {
69     struct hash_chain_entry prolog;
70 #   define fo_hidden_base prolog.hidden_key
71                                 /* Pointer to object base.      */
72                                 /* No longer hidden once object */
73                                 /* is on finalize_now queue.    */
74 #   define fo_next(x) (struct finalizable_object *)((x) -> prolog.next)
75 #   define fo_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
76     GC_finalization_proc fo_fn; /* Finalizer.                   */
77     ptr_t fo_client_data;
78     word fo_object_size;        /* In bytes.                    */
79     finalization_mark_proc * fo_mark_proc;      /* Mark-through procedure */
80 } **fo_head = 0;
81
82 struct finalizable_object * GC_finalize_now = 0;
83         /* LIst of objects that should be finalized now.        */
84
85 static signed_word log_fo_table_size = -1;
86
87 word GC_fo_entries = 0;
88
89 # ifdef SRC_M3
90 void GC_push_finalizer_structures()
91 {
92     GC_push_all((ptr_t)(&dl_head), (ptr_t)(&dl_head) + sizeof(word));
93     GC_push_all((ptr_t)(&fo_head), (ptr_t)(&fo_head) + sizeof(word));
94 }
95 # endif
96
97 /* Double the size of a hash table. *size_ptr is the log of its current */
98 /* size.  May be a noop.                                                */
99 /* *table is a pointer to an array of hash headers.  If we succeed, we  */
100 /* update both *table and *log_size_ptr.                                */
101 /* Lock is held.  Signals are disabled.                                 */
102 void GC_grow_table(table, log_size_ptr)
103 struct hash_chain_entry ***table;
104 signed_word * log_size_ptr;
105 {
106     register word i;
107     register struct hash_chain_entry *p;
108     int log_old_size = *log_size_ptr;
109     register int log_new_size = log_old_size + 1;
110     word old_size = ((log_old_size == -1)? 0: (1 << log_old_size));
111     register word new_size = 1 << log_new_size;
112     struct hash_chain_entry **new_table = (struct hash_chain_entry **)
113         GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
114                 (size_t)new_size * sizeof(struct hash_chain_entry *), NORMAL);
115     
116     if (new_table == 0) {
117         if (table == 0) {
118             ABORT("Insufficient space for initial table allocation");
119         } else {
120             return;
121         }
122     }
123     for (i = 0; i < old_size; i++) {
124       p = (*table)[i];
125       while (p != 0) {
126         register ptr_t real_key = (ptr_t)REVEAL_POINTER(p -> hidden_key);
127         register struct hash_chain_entry *next = p -> next;
128         register int new_hash = HASH3(real_key, new_size, log_new_size);
129         
130         p -> next = new_table[new_hash];
131         new_table[new_hash] = p;
132         p = next;
133       }
134     }
135     *log_size_ptr = log_new_size;
136     *table = new_table;
137 }
138
139 # if defined(__STDC__) || defined(__cplusplus)
140     int GC_register_disappearing_link(GC_PTR * link)
141 # else
142     int GC_register_disappearing_link(link)
143     GC_PTR * link;
144 # endif
145 {
146     ptr_t base;
147     
148     base = (ptr_t)GC_base((GC_PTR)link);
149     if (base == 0)
150         ABORT("Bad arg to GC_register_disappearing_link");
151     return(GC_general_register_disappearing_link(link, base));
152 }
153
154 # if defined(__STDC__) || defined(__cplusplus)
155     int GC_general_register_disappearing_link(GC_PTR * link,
156                                               GC_PTR obj)
157 # else
158     int GC_general_register_disappearing_link(link, obj)
159     GC_PTR * link;
160     GC_PTR obj;
161 # endif
162
163 {
164     struct disappearing_link *curr_dl;
165     int index;
166     struct disappearing_link * new_dl;
167     DCL_LOCK_STATE;
168     
169     if ((word)link & (ALIGNMENT-1))
170         ABORT("Bad arg to GC_general_register_disappearing_link");
171 #   ifdef THREADS
172         DISABLE_SIGNALS();
173         LOCK();
174 #   endif
175     if (log_dl_table_size == -1
176         || GC_dl_entries > ((word)1 << log_dl_table_size)) {
177 #       ifndef THREADS
178             DISABLE_SIGNALS();
179 #       endif
180         GC_grow_table((struct hash_chain_entry ***)(&dl_head),
181                       &log_dl_table_size);
182 #       ifdef PRINTSTATS
183             GC_printf1("Grew dl table to %lu entries\n",
184                         (unsigned long)(1 << log_dl_table_size));
185 #       endif
186 #       ifndef THREADS
187             ENABLE_SIGNALS();
188 #       endif
189     }
190     index = HASH2(link, log_dl_table_size);
191     curr_dl = dl_head[index];
192     for (curr_dl = dl_head[index]; curr_dl != 0; curr_dl = dl_next(curr_dl)) {
193         if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
194             curr_dl -> dl_hidden_obj = HIDE_POINTER(obj);
195 #           ifdef THREADS
196                 UNLOCK();
197                 ENABLE_SIGNALS();
198 #           endif
199             return(1);
200         }
201     }
202     new_dl = (struct disappearing_link *)
203         GC_INTERNAL_MALLOC(sizeof(struct disappearing_link),NORMAL);
204     if (new_dl != 0) {
205         new_dl -> dl_hidden_obj = HIDE_POINTER(obj);
206         new_dl -> dl_hidden_link = HIDE_POINTER(link);
207         dl_set_next(new_dl, dl_head[index]);
208         dl_head[index] = new_dl;
209         GC_dl_entries++;
210     } else {
211         GC_finalization_failures++;
212     }
213 #   ifdef THREADS
214         UNLOCK();
215         ENABLE_SIGNALS();
216 #   endif
217     return(0);
218 }
219
220 # if defined(__STDC__) || defined(__cplusplus)
221     int GC_unregister_disappearing_link(GC_PTR * link)
222 # else
223     int GC_unregister_disappearing_link(link)
224     GC_PTR * link;
225 # endif
226 {
227     struct disappearing_link *curr_dl, *prev_dl;
228     int index;
229     DCL_LOCK_STATE;
230     
231     DISABLE_SIGNALS();
232     LOCK();
233     index = HASH2(link, log_dl_table_size);
234     if (((unsigned long)link & (ALIGNMENT-1))) goto out;
235     prev_dl = 0; curr_dl = dl_head[index];
236     while (curr_dl != 0) {
237         if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
238             if (prev_dl == 0) {
239                 dl_head[index] = dl_next(curr_dl);
240             } else {
241                 dl_set_next(prev_dl, dl_next(curr_dl));
242             }
243             GC_dl_entries--;
244             UNLOCK();
245             ENABLE_SIGNALS();
246 #           ifdef DBG_HDRS_ALL
247               dl_next(curr_dl) = 0;
248 #           else
249               GC_free((GC_PTR)curr_dl);
250 #           endif
251             return(1);
252         }
253         prev_dl = curr_dl;
254         curr_dl = dl_next(curr_dl);
255     }
256 out:
257     UNLOCK();
258     ENABLE_SIGNALS();
259     return(0);
260 }
261
262 /* Possible finalization_marker procedures.  Note that mark stack       */
263 /* overflow is handled by the caller, and is not a disaster.            */
264 GC_API void GC_normal_finalize_mark_proc(p)
265 ptr_t p;
266 {
267     hdr * hhdr = HDR(p);
268     
269     PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top,
270              &(GC_mark_stack[GC_mark_stack_size]));
271 }
272
273 /* This only pays very partial attention to the mark descriptor.        */
274 /* It does the right thing for normal and atomic objects, and treats    */
275 /* most others as normal.                                               */
276 GC_API void GC_ignore_self_finalize_mark_proc(p)
277 ptr_t p;
278 {
279     hdr * hhdr = HDR(p);
280     word descr = hhdr -> hb_descr;
281     ptr_t q, r;
282     ptr_t scan_limit;
283     ptr_t target_limit = p + WORDS_TO_BYTES(hhdr -> hb_sz) - 1;
284     
285     if ((descr & DS_TAGS) == DS_LENGTH) {
286        scan_limit = p + descr - sizeof(word);
287     } else {
288        scan_limit = target_limit + 1 - sizeof(word);
289     }
290     for (q = p; q <= scan_limit; q += ALIGNMENT) {
291         r = *(ptr_t *)q;
292         if (r < p || r > target_limit) {
293             GC_PUSH_ONE_HEAP((word)r, q);
294         }
295     }
296 }
297
298 /*ARGSUSED*/
299 GC_API void GC_null_finalize_mark_proc(p)
300 ptr_t p;
301 {
302 }
303
304
305
306 /* Register a finalization function.  See gc.h for details.     */
307 /* in the nonthreads case, we try to avoid disabling signals,   */
308 /* since it can be expensive.  Threads packages typically       */
309 /* make it cheaper.                                             */
310 /* The last parameter is a procedure that determines            */
311 /* marking for finalization ordering.  Any objects marked       */
312 /* by that procedure will be guaranteed to not have been        */
313 /* finalized when this finalizer is invoked.                    */
314 GC_API void GC_register_finalizer_inner(obj, fn, cd, ofn, ocd, mp)
315 GC_PTR obj;
316 GC_finalization_proc fn;
317 GC_PTR cd;
318 GC_finalization_proc * ofn;
319 GC_PTR * ocd;
320 finalization_mark_proc * mp;
321 {
322     ptr_t base;
323     struct finalizable_object * curr_fo, * prev_fo;
324     int index;
325     struct finalizable_object *new_fo;
326     DCL_LOCK_STATE;
327
328 #   ifdef THREADS
329         DISABLE_SIGNALS();
330         LOCK();
331 #   endif
332     if (log_fo_table_size == -1
333         || GC_fo_entries > ((word)1 << log_fo_table_size)) {
334 #       ifndef THREADS
335             DISABLE_SIGNALS();
336 #       endif
337         GC_grow_table((struct hash_chain_entry ***)(&fo_head),
338                       &log_fo_table_size);
339 #       ifdef PRINTSTATS
340             GC_printf1("Grew fo table to %lu entries\n",
341                         (unsigned long)(1 << log_fo_table_size));
342 #       endif
343 #       ifndef THREADS
344             ENABLE_SIGNALS();
345 #       endif
346     }
347     /* in the THREADS case signals are disabled and we hold allocation  */
348     /* lock; otherwise neither is true.  Proceed carefully.             */
349     base = (ptr_t)obj;
350     index = HASH2(base, log_fo_table_size);
351     prev_fo = 0; curr_fo = fo_head[index];
352     while (curr_fo != 0) {
353         if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
354             /* Interruption by a signal in the middle of this   */
355             /* should be safe.  The client may see only *ocd    */
356             /* updated, but we'll declare that to be his        */
357             /* problem.                                         */
358             if (ocd) *ocd = (GC_PTR) curr_fo -> fo_client_data;
359             if (ofn) *ofn = curr_fo -> fo_fn;
360             /* Delete the structure for base. */
361                 if (prev_fo == 0) {
362                   fo_head[index] = fo_next(curr_fo);
363                 } else {
364                   fo_set_next(prev_fo, fo_next(curr_fo));
365                 }
366             if (fn == 0) {
367                 GC_fo_entries--;
368                   /* May not happen if we get a signal.  But a high     */
369                   /* estimate will only make the table larger than      */
370                   /* necessary.                                         */
371 #               if !defined(THREADS) && !defined(DBG_HDRS_ALL)
372                   GC_free((GC_PTR)curr_fo);
373 #               endif
374             } else {
375                 curr_fo -> fo_fn = fn;
376                 curr_fo -> fo_client_data = (ptr_t)cd;
377                 curr_fo -> fo_mark_proc = mp;
378                 /* Reinsert it.  We deleted it first to maintain        */
379                 /* consistency in the event of a signal.                */
380                 if (prev_fo == 0) {
381                   fo_head[index] = curr_fo;
382                 } else {
383                   fo_set_next(prev_fo, curr_fo);
384                 }
385             }
386 #           ifdef THREADS
387                 UNLOCK();
388                 ENABLE_SIGNALS();
389 #           endif
390             return;
391         }
392         prev_fo = curr_fo;
393         curr_fo = fo_next(curr_fo);
394     }
395     if (ofn) *ofn = 0;
396     if (ocd) *ocd = 0;
397     if (fn == 0) {
398 #       ifdef THREADS
399             UNLOCK();
400             ENABLE_SIGNALS();
401 #       endif
402         return;
403     }
404     new_fo = (struct finalizable_object *)
405         GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
406     if (new_fo != 0) {
407         new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
408         new_fo -> fo_fn = fn;
409         new_fo -> fo_client_data = (ptr_t)cd;
410         new_fo -> fo_object_size = GC_size(base);
411         new_fo -> fo_mark_proc = mp;
412         fo_set_next(new_fo, fo_head[index]);
413         GC_fo_entries++;
414         fo_head[index] = new_fo;
415     } else {
416         GC_finalization_failures++;
417     }
418 #   ifdef THREADS
419         UNLOCK();
420         ENABLE_SIGNALS();
421 #   endif
422 }
423
424 # if defined(__STDC__)
425     void GC_register_finalizer(void * obj,
426                                GC_finalization_proc fn, void * cd,
427                                GC_finalization_proc *ofn, void ** ocd)
428 # else
429     void GC_register_finalizer(obj, fn, cd, ofn, ocd)
430     GC_PTR obj;
431     GC_finalization_proc fn;
432     GC_PTR cd;
433     GC_finalization_proc * ofn;
434     GC_PTR * ocd;
435 # endif
436 {
437     GC_register_finalizer_inner(obj, fn, cd, ofn,
438                                 ocd, GC_normal_finalize_mark_proc);
439 }
440
441 # if defined(__STDC__)
442     void GC_register_finalizer_ignore_self(void * obj,
443                                GC_finalization_proc fn, void * cd,
444                                GC_finalization_proc *ofn, void ** ocd)
445 # else
446     void GC_register_finalizer_ignore_self(obj, fn, cd, ofn, ocd)
447     GC_PTR obj;
448     GC_finalization_proc fn;
449     GC_PTR cd;
450     GC_finalization_proc * ofn;
451     GC_PTR * ocd;
452 # endif
453 {
454     GC_register_finalizer_inner(obj, fn, cd, ofn,
455                                 ocd, GC_ignore_self_finalize_mark_proc);
456 }
457
458 # if defined(__STDC__)
459     void GC_register_finalizer_no_order(void * obj,
460                                GC_finalization_proc fn, void * cd,
461                                GC_finalization_proc *ofn, void ** ocd)
462 # else
463     void GC_register_finalizer_no_order(obj, fn, cd, ofn, ocd)
464     GC_PTR obj;
465     GC_finalization_proc fn;
466     GC_PTR cd;
467     GC_finalization_proc * ofn;
468     GC_PTR * ocd;
469 # endif
470 {
471     GC_register_finalizer_inner(obj, fn, cd, ofn,
472                                 ocd, GC_null_finalize_mark_proc);
473 }
474
475 #ifndef NO_DEBUGGING
476 void GC_dump_finalization()
477 {
478     struct disappearing_link * curr_dl;
479     struct finalizable_object * curr_fo;
480     ptr_t real_ptr, real_link;
481     int dl_size = (log_dl_table_size == -1 ) ? 0 : (1 << log_dl_table_size);
482     int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
483     int i;
484
485     GC_printf0("Disappearing links:\n");
486     for (i = 0; i < dl_size; i++) {
487       for (curr_dl = dl_head[i]; curr_dl != 0; curr_dl = dl_next(curr_dl)) {
488         real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
489         real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
490         GC_printf2("Object: 0x%lx, Link:0x%lx\n", real_ptr, real_link);
491       }
492     }
493     GC_printf0("Finalizers:\n");
494     for (i = 0; i < fo_size; i++) {
495       for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
496         real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
497         GC_printf1("Finalizable object: 0x%lx\n", real_ptr);
498       }
499     }
500 }
501 #endif
502
503 /* Called with world stopped.  Cause disappearing links to disappear,   */
504 /* and invoke finalizers.                                               */
505 void GC_finalize()
506 {
507     struct disappearing_link * curr_dl, * prev_dl, * next_dl;
508     struct finalizable_object * curr_fo, * prev_fo, * next_fo;
509     ptr_t real_ptr, real_link;
510     register int i;
511     int dl_size = (log_dl_table_size == -1 ) ? 0 : (1 << log_dl_table_size);
512     int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
513     
514   /* Make disappearing links disappear */
515     for (i = 0; i < dl_size; i++) {
516       curr_dl = dl_head[i];
517       prev_dl = 0;
518       while (curr_dl != 0) {
519         real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
520         real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
521         if (!GC_is_marked(real_ptr)) {
522             *(word *)real_link = 0;
523             next_dl = dl_next(curr_dl);
524             if (prev_dl == 0) {
525                 dl_head[i] = next_dl;
526             } else {
527                 dl_set_next(prev_dl, next_dl);
528             }
529             GC_clear_mark_bit((ptr_t)curr_dl);
530             GC_dl_entries--;
531             curr_dl = next_dl;
532         } else {
533             prev_dl = curr_dl;
534             curr_dl = dl_next(curr_dl);
535         }
536       }
537     }
538   /* Mark all objects reachable via chains of 1 or more pointers        */
539   /* from finalizable objects.                                          */
540 #   ifdef PRINTSTATS
541         if (GC_mark_state != MS_NONE) ABORT("Bad mark state");
542 #   endif
543     for (i = 0; i < fo_size; i++) {
544       for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
545         real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
546         if (!GC_is_marked(real_ptr)) {
547             GC_MARKED_FOR_FINALIZATION(real_ptr);
548             GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
549             if (GC_is_marked(real_ptr)) {
550                 WARN("Finalization cycle involving %lx\n", real_ptr);
551             }
552         }
553       }
554     }
555   /* Enqueue for finalization all objects that are still                */
556   /* unreachable.                                                       */
557     GC_words_finalized = 0;
558     for (i = 0; i < fo_size; i++) {
559       curr_fo = fo_head[i];
560       prev_fo = 0;
561       while (curr_fo != 0) {
562         real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
563         if (!GC_is_marked(real_ptr)) {
564             if (!GC_java_finalization) {
565               GC_set_mark_bit(real_ptr);
566             }
567             /* Delete from hash table */
568               next_fo = fo_next(curr_fo);
569               if (prev_fo == 0) {
570                 fo_head[i] = next_fo;
571               } else {
572                 fo_set_next(prev_fo, next_fo);
573               }
574               GC_fo_entries--;
575             /* Add to list of objects awaiting finalization.    */
576               fo_set_next(curr_fo, GC_finalize_now);
577               GC_finalize_now = curr_fo;
578               /* unhide object pointer so any future collections will   */
579               /* see it.                                                */
580               curr_fo -> fo_hidden_base = 
581                         (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
582               GC_words_finalized +=
583                         ALIGNED_WORDS(curr_fo -> fo_object_size)
584                         + ALIGNED_WORDS(sizeof(struct finalizable_object));
585 #           ifdef PRINTSTATS
586               if (!GC_is_marked((ptr_t)curr_fo)) {
587                 ABORT("GC_finalize: found accessible unmarked object\n");
588               }
589 #           endif
590             curr_fo = next_fo;
591         } else {
592             prev_fo = curr_fo;
593             curr_fo = fo_next(curr_fo);
594         }
595       }
596     }
597
598   if (GC_java_finalization) {
599     /* make sure we mark everything reachable from objects finalized
600        using the no_order mark_proc */
601       for (curr_fo = GC_finalize_now; 
602          curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
603         real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
604         if (!GC_is_marked(real_ptr)) {
605             if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
606                 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
607             }
608             GC_set_mark_bit(real_ptr);
609         }
610       }
611   }
612
613   /* Remove dangling disappearing links. */
614     for (i = 0; i < dl_size; i++) {
615       curr_dl = dl_head[i];
616       prev_dl = 0;
617       while (curr_dl != 0) {
618         real_link = GC_base((ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link));
619         if (real_link != 0 && !GC_is_marked(real_link)) {
620             next_dl = dl_next(curr_dl);
621             if (prev_dl == 0) {
622                 dl_head[i] = next_dl;
623             } else {
624                 dl_set_next(prev_dl, next_dl);
625             }
626             GC_clear_mark_bit((ptr_t)curr_dl);
627             GC_dl_entries--;
628             curr_dl = next_dl;
629         } else {
630             prev_dl = curr_dl;
631             curr_dl = dl_next(curr_dl);
632         }
633       }
634     }
635 }
636
637 #ifndef JAVA_FINALIZATION_NOT_NEEDED
638
639 /* Enqueue all remaining finalizers to be run - Assumes lock is
640  * held, and signals are disabled */
641 void GC_enqueue_all_finalizers()
642 {
643     struct finalizable_object * curr_fo, * prev_fo, * next_fo;
644     ptr_t real_ptr;
645     register int i;
646     int fo_size;
647     
648     fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
649     GC_words_finalized = 0;
650     for (i = 0; i < fo_size; i++) {
651         curr_fo = fo_head[i];
652         prev_fo = 0;
653       while (curr_fo != 0) {
654           real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
655           GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
656           GC_set_mark_bit(real_ptr);
657  
658           /* Delete from hash table */
659           next_fo = fo_next(curr_fo);
660           if (prev_fo == 0) {
661               fo_head[i] = next_fo;
662           } else {
663               fo_set_next(prev_fo, next_fo);
664           }
665           GC_fo_entries--;
666
667           /* Add to list of objects awaiting finalization.      */
668           fo_set_next(curr_fo, GC_finalize_now);
669           GC_finalize_now = curr_fo;
670
671           /* unhide object pointer so any future collections will       */
672           /* see it.                                            */
673           curr_fo -> fo_hidden_base = 
674                         (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
675
676           GC_words_finalized +=
677                 ALIGNED_WORDS(curr_fo -> fo_object_size)
678                         + ALIGNED_WORDS(sizeof(struct finalizable_object));
679           curr_fo = next_fo;
680         }
681     }
682
683     return;
684 }
685
686 /* Invoke all remaining finalizers that haven't yet been run. 
687  * This is needed for strict compliance with the Java standard, 
688  * which can make the runtime guarantee that all finalizers are run.
689  * Unfortunately, the Java standard implies we have to keep running
690  * finalizers until there are no more left, a potential infinite loop.
691  * YUCK.
692  * Note that this is even more dangerous than the usual Java
693  * finalizers, in that objects reachable from static variables
694  * may have been finalized when these finalizers are run.
695  * Finalizers run at this point must be prepared to deal with a
696  * mostly broken world.
697  * This routine is externally callable, so is called without 
698  * the allocation lock. 
699  */
700 GC_API void GC_finalize_all()
701 {
702     DCL_LOCK_STATE;
703
704     DISABLE_SIGNALS();
705     LOCK();
706     while (GC_fo_entries > 0) {
707       GC_enqueue_all_finalizers();
708       UNLOCK();
709       ENABLE_SIGNALS();
710       GC_INVOKE_FINALIZERS();
711       DISABLE_SIGNALS();
712       LOCK();
713     }
714     UNLOCK();
715     ENABLE_SIGNALS();
716 }
717 #endif
718
719 /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
720 /* finalizers can only be called from some kind of `safe state' and     */
721 /* getting into that safe state is expensive.)                          */
722 int GC_should_invoke_finalizers GC_PROTO((void))
723 {
724     return GC_finalize_now != 0;
725 }
726
727 /* Invoke finalizers for all objects that are ready to be finalized.    */
728 /* Should be called without allocation lock.                            */
729 int GC_invoke_finalizers()
730 {
731     register struct finalizable_object * curr_fo;
732     register int count = 0;
733     DCL_LOCK_STATE;
734     
735     while (GC_finalize_now != 0) {
736 #       ifdef THREADS
737             DISABLE_SIGNALS();
738             LOCK();
739 #       endif
740         curr_fo = GC_finalize_now;
741 #       ifdef THREADS
742             if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
743             UNLOCK();
744             ENABLE_SIGNALS();
745             if (curr_fo == 0) break;
746 #       else
747             GC_finalize_now = fo_next(curr_fo);
748 #       endif
749         fo_set_next(curr_fo, 0);
750         (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
751                               curr_fo -> fo_client_data);
752         curr_fo -> fo_client_data = 0;
753         ++count;
754 #       ifdef UNDEFINED
755             /* This is probably a bad idea.  It throws off accounting if */
756             /* nearly all objects are finalizable.  O.w. it shouldn't    */
757             /* matter.                                                   */
758             GC_free((GC_PTR)curr_fo);
759 #       endif
760     }
761     return count;
762 }
763
764 # ifdef __STDC__
765     GC_PTR GC_call_with_alloc_lock(GC_fn_type fn,
766                                          GC_PTR client_data)
767 # else
768     GC_PTR GC_call_with_alloc_lock(fn, client_data)
769     GC_fn_type fn;
770     GC_PTR client_data;
771 # endif
772 {
773     GC_PTR result;
774     DCL_LOCK_STATE;
775     
776 #   ifdef THREADS
777       DISABLE_SIGNALS();
778       LOCK();
779       SET_LOCK_HOLDER();
780 #   endif
781     result = (*fn)(client_data);
782 #   ifdef THREADS
783 #     ifndef GC_ASSERTIONS
784         UNSET_LOCK_HOLDER();
785 #     endif /* o.w. UNLOCK() does it implicitly */
786       UNLOCK();
787       ENABLE_SIGNALS();
788 #   endif
789     return(result);
790 }