* optimize.c (update_cloned_parm): New function.
[official-gcc.git] / boehm-gc / finalize.c
blob7ff6798cfa5e51580090712da9eee6ea4a4af7d2
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 "gc_priv.h"
18 # include "gc_mark.h"
20 # ifdef FINALIZE_ON_DEMAND
21 int GC_finalize_on_demand = 1;
22 # else
23 int GC_finalize_on_demand = 0;
24 # endif
26 # ifdef JAVA_FINALIZATION
27 int GC_java_finalization = 1;
28 # else
29 int GC_java_finalization = 0;
30 # endif
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 */);
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)
42 struct hash_chain_entry {
43 word hidden_key;
44 struct hash_chain_entry * next;
47 unsigned GC_finalization_failures = 0;
48 /* Number of finalization requests that failed for lack of memory. */
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)
57 word dl_hidden_obj; /* Pointer to object base */
58 } **dl_head = 0;
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. */
65 word GC_dl_entries = 0; /* Number of entries currently in disappearing */
66 /* link table. */
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;
82 struct finalizable_object * GC_finalize_now = 0;
83 /* LIst of objects that should be finalized now. */
85 static signed_word log_fo_table_size = -1;
87 word GC_fo_entries = 0;
89 # ifdef SRC_M3
90 void GC_push_finalizer_structures()
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));
95 # endif
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;
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_generic_malloc_inner_ignore_off_page(
114 (size_t)new_size * sizeof(struct hash_chain_entry *), NORMAL);
116 if (new_table == 0) {
117 if (table == 0) {
118 ABORT("Insufficient space for initial table allocation");
119 } else {
120 return;
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);
130 p -> next = new_table[new_hash];
131 new_table[new_hash] = p;
132 p = next;
135 *log_size_ptr = log_new_size;
136 *table = new_table;
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
146 ptr_t base;
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));
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
164 struct disappearing_link *curr_dl;
165 int index;
166 struct disappearing_link * new_dl;
167 DCL_LOCK_STATE;
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
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);
202 # ifdef THREADS
203 new_dl = (struct disappearing_link *)
204 GC_generic_malloc_inner(sizeof(struct disappearing_link),NORMAL);
205 # else
206 new_dl = (struct disappearing_link *)
207 GC_malloc(sizeof(struct disappearing_link));
208 # endif
209 if (new_dl != 0) {
210 new_dl -> dl_hidden_obj = HIDE_POINTER(obj);
211 new_dl -> dl_hidden_link = HIDE_POINTER(link);
212 dl_set_next(new_dl, dl_head[index]);
213 dl_head[index] = new_dl;
214 GC_dl_entries++;
215 } else {
216 GC_finalization_failures++;
218 # ifdef THREADS
219 UNLOCK();
220 ENABLE_SIGNALS();
221 # endif
222 return(0);
225 # if defined(__STDC__) || defined(__cplusplus)
226 int GC_unregister_disappearing_link(GC_PTR * link)
227 # else
228 int GC_unregister_disappearing_link(link)
229 GC_PTR * link;
230 # endif
232 struct disappearing_link *curr_dl, *prev_dl;
233 int index;
234 DCL_LOCK_STATE;
236 DISABLE_SIGNALS();
237 LOCK();
238 index = HASH2(link, log_dl_table_size);
239 if (((unsigned long)link & (ALIGNMENT-1))) goto out;
240 prev_dl = 0; curr_dl = dl_head[index];
241 while (curr_dl != 0) {
242 if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
243 if (prev_dl == 0) {
244 dl_head[index] = dl_next(curr_dl);
245 } else {
246 dl_set_next(prev_dl, dl_next(curr_dl));
248 GC_dl_entries--;
249 UNLOCK();
250 ENABLE_SIGNALS();
251 GC_free((GC_PTR)curr_dl);
252 return(1);
254 prev_dl = curr_dl;
255 curr_dl = dl_next(curr_dl);
257 out:
258 UNLOCK();
259 ENABLE_SIGNALS();
260 return(0);
263 /* Possible finalization_marker procedures. Note that mark stack */
264 /* overflow is handled by the caller, and is not a disaster. */
265 GC_API void GC_normal_finalize_mark_proc(p)
266 ptr_t p;
268 hdr * hhdr = HDR(p);
270 PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top,
271 &(GC_mark_stack[GC_mark_stack_size]));
274 /* This only pays very partial attention to the mark descriptor. */
275 /* It does the right thing for normal and atomic objects, and treats */
276 /* most others as normal. */
277 GC_API void GC_ignore_self_finalize_mark_proc(p)
278 ptr_t p;
280 hdr * hhdr = HDR(p);
281 word descr = hhdr -> hb_descr;
282 ptr_t q, r;
283 ptr_t scan_limit;
284 ptr_t target_limit = p + WORDS_TO_BYTES(hhdr -> hb_sz) - 1;
286 if ((descr & DS_TAGS) == DS_LENGTH) {
287 scan_limit = p + descr - sizeof(word);
288 } else {
289 scan_limit = target_limit + 1 - sizeof(word);
291 for (q = p; q <= scan_limit; q += ALIGNMENT) {
292 r = *(ptr_t *)q;
293 if (r < p || r > target_limit) {
294 GC_PUSH_ONE_HEAP((word)r, q);
299 /*ARGSUSED*/
300 GC_API void GC_null_finalize_mark_proc(p)
301 ptr_t p;
307 /* Register a finalization function. See gc.h for details. */
308 /* in the nonthreads case, we try to avoid disabling signals, */
309 /* since it can be expensive. Threads packages typically */
310 /* make it cheaper. */
311 /* The last parameter is a procedure that determines */
312 /* marking for finalization ordering. Any objects marked */
313 /* by that procedure will be guaranteed to not have been */
314 /* finalized when this finalizer is invoked. */
315 GC_API void GC_register_finalizer_inner(obj, fn, cd, ofn, ocd, mp)
316 GC_PTR obj;
317 GC_finalization_proc fn;
318 GC_PTR cd;
319 GC_finalization_proc * ofn;
320 GC_PTR * ocd;
321 finalization_mark_proc * mp;
323 ptr_t base;
324 struct finalizable_object * curr_fo, * prev_fo;
325 int index;
326 struct finalizable_object *new_fo;
327 hdr *hhdr;
328 DCL_LOCK_STATE;
330 # ifdef THREADS
331 DISABLE_SIGNALS();
332 LOCK();
333 # endif
334 if (log_fo_table_size == -1
335 || GC_fo_entries > ((word)1 << log_fo_table_size)) {
336 # ifndef THREADS
337 DISABLE_SIGNALS();
338 # endif
339 GC_grow_table((struct hash_chain_entry ***)(&fo_head),
340 &log_fo_table_size);
341 # ifdef PRINTSTATS
342 GC_printf1("Grew fo table to %lu entries\n",
343 (unsigned long)(1 << log_fo_table_size));
344 # endif
345 # ifndef THREADS
346 ENABLE_SIGNALS();
347 # endif
349 /* in the THREADS case signals are disabled and we hold allocation */
350 /* lock; otherwise neither is true. Proceed carefully. */
351 base = (ptr_t)obj;
352 index = HASH2(base, log_fo_table_size);
353 prev_fo = 0; curr_fo = fo_head[index];
354 while (curr_fo != 0) {
355 if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
356 /* Interruption by a signal in the middle of this */
357 /* should be safe. The client may see only *ocd */
358 /* updated, but we'll declare that to be his */
359 /* problem. */
360 if (ocd) *ocd = (GC_PTR) curr_fo -> fo_client_data;
361 if (ofn) *ofn = curr_fo -> fo_fn;
362 /* Delete the structure for base. */
363 if (prev_fo == 0) {
364 fo_head[index] = fo_next(curr_fo);
365 } else {
366 fo_set_next(prev_fo, fo_next(curr_fo));
368 if (fn == 0) {
369 GC_fo_entries--;
370 /* May not happen if we get a signal. But a high */
371 /* estimate will only make the table larger than */
372 /* necessary. */
373 # ifndef THREADS
374 GC_free((GC_PTR)curr_fo);
375 # endif
376 } else {
377 curr_fo -> fo_fn = fn;
378 curr_fo -> fo_client_data = (ptr_t)cd;
379 curr_fo -> fo_mark_proc = mp;
380 /* Reinsert it. We deleted it first to maintain */
381 /* consistency in the event of a signal. */
382 if (prev_fo == 0) {
383 fo_head[index] = curr_fo;
384 } else {
385 fo_set_next(prev_fo, curr_fo);
388 # ifdef THREADS
389 UNLOCK();
390 ENABLE_SIGNALS();
391 # endif
392 return;
394 prev_fo = curr_fo;
395 curr_fo = fo_next(curr_fo);
397 if (ofn) *ofn = 0;
398 if (ocd) *ocd = 0;
399 if (fn == 0) {
400 # ifdef THREADS
401 UNLOCK();
402 ENABLE_SIGNALS();
403 # endif
404 return;
406 GET_HDR(base, hhdr);
407 if (0 == hhdr) {
408 /* We won't collect it, hence finalizer wouldn't be run. */
409 /* This is changed for gcj, but it will be in version 6.0 of the */
410 /* standard collector distribution. It costs virtually nothing */
411 /* here, but it's expensive to check in the hash synchronization */
412 /* code, where it matters. -HB */
413 # ifdef THREADS
414 UNLOCK();
415 ENABLE_SIGNALS();
416 # endif
417 return;
419 # ifdef THREADS
420 new_fo = (struct finalizable_object *)
421 GC_generic_malloc_inner(sizeof(struct finalizable_object),NORMAL);
422 # else
423 new_fo = (struct finalizable_object *)
424 GC_malloc(sizeof(struct finalizable_object));
425 # endif
426 if (new_fo != 0) {
427 new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
428 new_fo -> fo_fn = fn;
429 new_fo -> fo_client_data = (ptr_t)cd;
430 new_fo -> fo_object_size = hhdr -> hb_sz;
431 new_fo -> fo_mark_proc = mp;
432 fo_set_next(new_fo, fo_head[index]);
433 GC_fo_entries++;
434 fo_head[index] = new_fo;
435 } else {
436 GC_finalization_failures++;
438 # ifdef THREADS
439 UNLOCK();
440 ENABLE_SIGNALS();
441 # endif
444 # if defined(__STDC__)
445 void GC_register_finalizer(void * obj,
446 GC_finalization_proc fn, void * cd,
447 GC_finalization_proc *ofn, void ** ocd)
448 # else
449 void GC_register_finalizer(obj, fn, cd, ofn, ocd)
450 GC_PTR obj;
451 GC_finalization_proc fn;
452 GC_PTR cd;
453 GC_finalization_proc * ofn;
454 GC_PTR * ocd;
455 # endif
457 GC_register_finalizer_inner(obj, fn, cd, ofn,
458 ocd, GC_normal_finalize_mark_proc);
461 # if defined(__STDC__)
462 void GC_register_finalizer_ignore_self(void * obj,
463 GC_finalization_proc fn, void * cd,
464 GC_finalization_proc *ofn, void ** ocd)
465 # else
466 void GC_register_finalizer_ignore_self(obj, fn, cd, ofn, ocd)
467 GC_PTR obj;
468 GC_finalization_proc fn;
469 GC_PTR cd;
470 GC_finalization_proc * ofn;
471 GC_PTR * ocd;
472 # endif
474 GC_register_finalizer_inner(obj, fn, cd, ofn,
475 ocd, GC_ignore_self_finalize_mark_proc);
478 # if defined(__STDC__)
479 void GC_register_finalizer_no_order(void * obj,
480 GC_finalization_proc fn, void * cd,
481 GC_finalization_proc *ofn, void ** ocd)
482 # else
483 void GC_register_finalizer_no_order(obj, fn, cd, ofn, ocd)
484 GC_PTR obj;
485 GC_finalization_proc fn;
486 GC_PTR cd;
487 GC_finalization_proc * ofn;
488 GC_PTR * ocd;
489 # endif
491 GC_register_finalizer_inner(obj, fn, cd, ofn,
492 ocd, GC_null_finalize_mark_proc);
495 /* Called with world stopped. Cause disappearing links to disappear, */
496 /* and invoke finalizers. */
497 void GC_finalize()
499 struct disappearing_link * curr_dl, * prev_dl, * next_dl;
500 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
501 ptr_t real_ptr, real_link;
502 register int i;
503 int dl_size = (log_dl_table_size == -1 ) ? 0 : (1 << log_dl_table_size);
504 int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
506 /* Make disappearing links disappear */
507 for (i = 0; i < dl_size; i++) {
508 curr_dl = dl_head[i];
509 prev_dl = 0;
510 while (curr_dl != 0) {
511 real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
512 real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
513 if (!GC_is_marked(real_ptr)) {
514 *(word *)real_link = 0;
515 next_dl = dl_next(curr_dl);
516 if (prev_dl == 0) {
517 dl_head[i] = next_dl;
518 } else {
519 dl_set_next(prev_dl, next_dl);
521 GC_clear_mark_bit((ptr_t)curr_dl);
522 GC_dl_entries--;
523 curr_dl = next_dl;
524 } else {
525 prev_dl = curr_dl;
526 curr_dl = dl_next(curr_dl);
530 /* Mark all objects reachable via chains of 1 or more pointers */
531 /* from finalizable objects. */
532 # ifdef PRINTSTATS
533 if (GC_mark_state != MS_NONE) ABORT("Bad mark state");
534 # endif
535 for (i = 0; i < fo_size; i++) {
536 for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
537 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
538 if (!GC_is_marked(real_ptr)) {
539 GC_MARKED_FOR_FINALIZATION(real_ptr);
540 GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
541 if (GC_is_marked(real_ptr)) {
542 WARN("Finalization cycle involving %lx\n", real_ptr);
547 /* Enqueue for finalization all objects that are still */
548 /* unreachable. */
549 GC_words_finalized = 0;
550 for (i = 0; i < fo_size; i++) {
551 curr_fo = fo_head[i];
552 prev_fo = 0;
553 while (curr_fo != 0) {
554 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
555 if (!GC_is_marked(real_ptr)) {
556 if (!GC_java_finalization) {
557 GC_set_mark_bit(real_ptr);
559 /* Delete from hash table */
560 next_fo = fo_next(curr_fo);
561 if (prev_fo == 0) {
562 fo_head[i] = next_fo;
563 } else {
564 fo_set_next(prev_fo, next_fo);
566 GC_fo_entries--;
567 /* Add to list of objects awaiting finalization. */
568 fo_set_next(curr_fo, GC_finalize_now);
569 GC_finalize_now = curr_fo;
570 /* unhide object pointer so any future collections will */
571 /* see it. */
572 curr_fo -> fo_hidden_base =
573 (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
574 GC_words_finalized +=
575 ALIGNED_WORDS(curr_fo -> fo_object_size)
576 + ALIGNED_WORDS(sizeof(struct finalizable_object));
577 # ifdef PRINTSTATS
578 if (!GC_is_marked((ptr_t)curr_fo)) {
579 ABORT("GC_finalize: found accessible unmarked object\n");
581 # endif
582 curr_fo = next_fo;
583 } else {
584 prev_fo = curr_fo;
585 curr_fo = fo_next(curr_fo);
590 if (GC_java_finalization) {
591 /* make sure we mark everything reachable from objects finalized
592 using the no_order mark_proc */
593 for (curr_fo = GC_finalize_now;
594 curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
595 real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
596 if (!GC_is_marked(real_ptr)) {
597 if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
598 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
600 GC_set_mark_bit(real_ptr);
605 /* Remove dangling disappearing links. */
606 for (i = 0; i < dl_size; i++) {
607 curr_dl = dl_head[i];
608 prev_dl = 0;
609 while (curr_dl != 0) {
610 real_link = GC_base((ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link));
611 if (real_link != 0 && !GC_is_marked(real_link)) {
612 next_dl = dl_next(curr_dl);
613 if (prev_dl == 0) {
614 dl_head[i] = next_dl;
615 } else {
616 dl_set_next(prev_dl, next_dl);
618 GC_clear_mark_bit((ptr_t)curr_dl);
619 GC_dl_entries--;
620 curr_dl = next_dl;
621 } else {
622 prev_dl = curr_dl;
623 curr_dl = dl_next(curr_dl);
629 #ifndef JAVA_FINALIZATION_NOT_NEEDED
631 /* Enqueue all remaining finalizers to be run - Assumes lock is
632 * held, and signals are disabled */
633 void GC_enqueue_all_finalizers()
635 struct finalizable_object * curr_fo, * prev_fo, * next_fo;
636 ptr_t real_ptr;
637 register int i;
638 int fo_size;
640 fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
641 GC_words_finalized = 0;
642 for (i = 0; i < fo_size; i++) {
643 curr_fo = fo_head[i];
644 prev_fo = 0;
645 while (curr_fo != 0) {
646 real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
647 GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
648 GC_set_mark_bit(real_ptr);
650 /* Delete from hash table */
651 next_fo = fo_next(curr_fo);
652 if (prev_fo == 0) {
653 fo_head[i] = next_fo;
654 } else {
655 fo_set_next(prev_fo, next_fo);
657 GC_fo_entries--;
659 /* Add to list of objects awaiting finalization. */
660 fo_set_next(curr_fo, GC_finalize_now);
661 GC_finalize_now = curr_fo;
663 /* unhide object pointer so any future collections will */
664 /* see it. */
665 curr_fo -> fo_hidden_base =
666 (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
668 GC_words_finalized +=
669 ALIGNED_WORDS(curr_fo -> fo_object_size)
670 + ALIGNED_WORDS(sizeof(struct finalizable_object));
671 curr_fo = next_fo;
675 return;
678 /* Invoke all remaining finalizers that haven't yet been run.
679 * This is needed for strict compliance with the Java standard,
680 * which can make the runtime guarantee that all finalizers are run.
681 * Unfortunately, the Java standard implies we have to keep running
682 * finalizers until there are no more left, a potential infinite loop.
683 * YUCK.
684 * Note that this is even more dangerous than the usual Java
685 * finalizers, in that objects reachable from static variables
686 * may have been finalized when these finalizers are run.
687 * Finalizers run at this point must be prepared to deal with a
688 * mostly broken world.
689 * This routine is externally callable, so is called without
690 * the allocation lock.
692 GC_API void GC_finalize_all()
694 DCL_LOCK_STATE;
696 DISABLE_SIGNALS();
697 LOCK();
698 while (GC_fo_entries > 0) {
699 GC_enqueue_all_finalizers();
700 UNLOCK();
701 ENABLE_SIGNALS();
702 GC_INVOKE_FINALIZERS();
703 DISABLE_SIGNALS();
704 LOCK();
706 UNLOCK();
707 ENABLE_SIGNALS();
709 #endif
711 /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
712 /* finalizers can only be called from some kind of `safe state' and */
713 /* getting into that safe state is expensive.) */
714 int GC_should_invoke_finalizers GC_PROTO((void))
716 return GC_finalize_now != 0;
719 /* Invoke finalizers for all objects that are ready to be finalized. */
720 /* Should be called without allocation lock. */
721 int GC_invoke_finalizers()
723 register struct finalizable_object * curr_fo;
724 register int count = 0;
725 DCL_LOCK_STATE;
727 while (GC_finalize_now != 0) {
728 # ifdef THREADS
729 DISABLE_SIGNALS();
730 LOCK();
731 # endif
732 curr_fo = GC_finalize_now;
733 # ifdef THREADS
734 if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
735 UNLOCK();
736 ENABLE_SIGNALS();
737 if (curr_fo == 0) break;
738 # else
739 GC_finalize_now = fo_next(curr_fo);
740 # endif
741 fo_set_next(curr_fo, 0);
742 (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
743 curr_fo -> fo_client_data);
744 curr_fo -> fo_client_data = 0;
745 ++count;
746 # ifdef UNDEFINED
747 /* This is probably a bad idea. It throws off accounting if */
748 /* nearly all objects are finalizable. O.w. it shouldn't */
749 /* matter. */
750 GC_free((GC_PTR)curr_fo);
751 # endif
753 return count;
756 # ifdef __STDC__
757 GC_PTR GC_call_with_alloc_lock(GC_fn_type fn,
758 GC_PTR client_data)
759 # else
760 GC_PTR GC_call_with_alloc_lock(fn, client_data)
761 GC_fn_type fn;
762 GC_PTR client_data;
763 # endif
765 GC_PTR result;
766 DCL_LOCK_STATE;
768 # ifdef THREADS
769 DISABLE_SIGNALS();
770 LOCK();
771 SET_LOCK_HOLDER();
772 # endif
773 result = (*fn)(client_data);
774 # ifdef THREADS
775 UNSET_LOCK_HOLDER();
776 UNLOCK();
777 ENABLE_SIGNALS();
778 # endif
779 return(result);