~ubuntu-branches/ubuntu/quantal/libgc/quantal

« back to all changes in this revision

Viewing changes to finalize.c

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2011-02-19 12:19:56 UTC
  • mfrom: (1.3.2 upstream) (0.1.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20110219121956-67rb69xlt5nud3v2
Tags: 1:7.1-5
Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3
3
 * Copyright (c) 1991-1996 by Xerox Corporation.  All rights reserved.
4
4
 * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
 
5
 * Copyright (C) 2007 Free Software Foundation, Inc
5
6
 
6
7
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7
8
 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
85
86
 
86
87
word GC_fo_entries = 0;
87
88
 
88
 
void GC_push_finalizer_structures GC_PROTO((void))
 
89
void GC_push_finalizer_structures(void)
89
90
{
90
91
    GC_push_all((ptr_t)(&dl_head), (ptr_t)(&dl_head) + sizeof(word));
91
92
    GC_push_all((ptr_t)(&fo_head), (ptr_t)(&fo_head) + sizeof(word));
98
99
/* *table is a pointer to an array of hash headers.  If we succeed, we  */
99
100
/* update both *table and *log_size_ptr.                                */
100
101
/* Lock is held.  Signals are disabled.                                 */
101
 
void GC_grow_table(table, log_size_ptr)
102
 
struct hash_chain_entry ***table;
103
 
signed_word * log_size_ptr;
 
102
void GC_grow_table(struct hash_chain_entry ***table,
 
103
                   signed_word *log_size_ptr)
104
104
{
105
105
    register word i;
106
106
    register struct hash_chain_entry *p;
107
 
    int log_old_size = *log_size_ptr;
108
 
    register int log_new_size = log_old_size + 1;
 
107
    signed_word log_old_size = *log_size_ptr;
 
108
    signed_word log_new_size = log_old_size + 1;
109
109
    word old_size = ((log_old_size == -1)? 0: (1 << log_old_size));
110
 
    register word new_size = 1 << log_new_size;
 
110
    word new_size = (word)1 << log_new_size;
 
111
    /* FIXME: Power of 2 size often gets rounded up to one more page. */
111
112
    struct hash_chain_entry **new_table = (struct hash_chain_entry **)
112
113
        GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
113
114
                (size_t)new_size * sizeof(struct hash_chain_entry *), NORMAL);
114
115
    
115
116
    if (new_table == 0) {
116
 
        if (table == 0) {
 
117
        if (*table == 0) {
117
118
            ABORT("Insufficient space for initial table allocation");
118
119
        } else {
119
120
            return;
122
123
    for (i = 0; i < old_size; i++) {
123
124
      p = (*table)[i];
124
125
      while (p != 0) {
125
 
        register ptr_t real_key = (ptr_t)REVEAL_POINTER(p -> hidden_key);
126
 
        register struct hash_chain_entry *next = p -> next;
127
 
        register int new_hash = HASH3(real_key, new_size, log_new_size);
 
126
        ptr_t real_key = (ptr_t)REVEAL_POINTER(p -> hidden_key);
 
127
        struct hash_chain_entry *next = p -> next;
 
128
        size_t new_hash = HASH3(real_key, new_size, log_new_size);
128
129
        
129
130
        p -> next = new_table[new_hash];
130
131
        new_table[new_hash] = p;
135
136
    *table = new_table;
136
137
}
137
138
 
138
 
# if defined(__STDC__) || defined(__cplusplus)
139
 
    int GC_register_disappearing_link(GC_PTR * link)
140
 
# else
141
 
    int GC_register_disappearing_link(link)
142
 
    GC_PTR * link;
143
 
# endif
 
139
int GC_register_disappearing_link(void * * link)
144
140
{
145
141
    ptr_t base;
146
142
    
147
 
    base = (ptr_t)GC_base((GC_PTR)link);
 
143
    base = (ptr_t)GC_base((void *)link);
148
144
    if (base == 0)
149
145
        ABORT("Bad arg to GC_register_disappearing_link");
150
146
    return(GC_general_register_disappearing_link(link, base));
151
147
}
152
148
 
153
 
# if defined(__STDC__) || defined(__cplusplus)
154
 
    int GC_general_register_disappearing_link(GC_PTR * link,
155
 
                                              GC_PTR obj)
156
 
# else
157
 
    int GC_general_register_disappearing_link(link, obj)
158
 
    GC_PTR * link;
159
 
    GC_PTR obj;
160
 
# endif
161
 
 
 
149
int GC_general_register_disappearing_link(void * * link, void * obj)
162
150
{
163
151
    struct disappearing_link *curr_dl;
164
 
    int index;
 
152
    size_t index;
165
153
    struct disappearing_link * new_dl;
166
154
    DCL_LOCK_STATE;
167
155
    
168
156
    if ((word)link & (ALIGNMENT-1))
169
157
        ABORT("Bad arg to GC_general_register_disappearing_link");
170
158
#   ifdef THREADS
171
 
        DISABLE_SIGNALS();
172
159
        LOCK();
173
160
#   endif
174
161
    if (log_dl_table_size == -1
175
162
        || GC_dl_entries > ((word)1 << log_dl_table_size)) {
176
 
#       ifndef THREADS
177
 
            DISABLE_SIGNALS();
178
 
#       endif
179
163
        GC_grow_table((struct hash_chain_entry ***)(&dl_head),
180
164
                      &log_dl_table_size);
181
 
#       ifdef CONDPRINT
182
 
          if (GC_print_stats) {
183
 
            GC_printf1("Grew dl table to %lu entries\n",
184
 
                        (unsigned long)(1 << log_dl_table_size));
185
 
          }
186
 
#       endif
187
 
#       ifndef THREADS
188
 
            ENABLE_SIGNALS();
189
 
#       endif
 
165
        if (GC_print_stats) {
 
166
            GC_log_printf("Grew dl table to %u entries\n",
 
167
                      (1 << log_dl_table_size));
 
168
        }
190
169
    }
191
170
    index = HASH2(link, log_dl_table_size);
192
171
    curr_dl = dl_head[index];
195
174
            curr_dl -> dl_hidden_obj = HIDE_POINTER(obj);
196
175
#           ifdef THREADS
197
176
                UNLOCK();
198
 
                ENABLE_SIGNALS();
199
177
#           endif
200
178
            return(1);
201
179
        }
205
183
    if (0 == new_dl) {
206
184
#     ifdef THREADS
207
185
        UNLOCK();
208
 
        ENABLE_SIGNALS();
209
186
#     endif
210
187
      new_dl = (struct disappearing_link *)
211
188
              GC_oom_fn(sizeof(struct disappearing_link));
212
189
      if (0 == new_dl) {
213
190
        GC_finalization_failures++;
214
 
        return(0);
 
191
        return(2);
215
192
      }
216
193
      /* It's not likely we'll make it here, but ... */
217
194
#     ifdef THREADS
218
 
        DISABLE_SIGNALS();
219
195
        LOCK();
220
196
#     endif
221
197
    }
226
202
    GC_dl_entries++;
227
203
#   ifdef THREADS
228
204
        UNLOCK();
229
 
        ENABLE_SIGNALS();
230
205
#   endif
231
206
    return(0);
232
207
}
233
208
 
234
 
# if defined(__STDC__) || defined(__cplusplus)
235
 
    int GC_unregister_disappearing_link(GC_PTR * link)
236
 
# else
237
 
    int GC_unregister_disappearing_link(link)
238
 
    GC_PTR * link;
239
 
# endif
 
209
int GC_unregister_disappearing_link(void * * link)
240
210
{
241
211
    struct disappearing_link *curr_dl, *prev_dl;
242
 
    int index;
 
212
    size_t index;
243
213
    DCL_LOCK_STATE;
244
214
    
245
 
    DISABLE_SIGNALS();
246
215
    LOCK();
247
216
    index = HASH2(link, log_dl_table_size);
248
 
    if (((unsigned long)link & (ALIGNMENT-1))) goto out;
 
217
    if (((word)link & (ALIGNMENT-1))) goto out;
249
218
    prev_dl = 0; curr_dl = dl_head[index];
250
219
    while (curr_dl != 0) {
251
220
        if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
256
225
            }
257
226
            GC_dl_entries--;
258
227
            UNLOCK();
259
 
            ENABLE_SIGNALS();
260
228
#           ifdef DBG_HDRS_ALL
261
229
              dl_set_next(curr_dl, 0);
262
230
#           else
263
 
              GC_free((GC_PTR)curr_dl);
 
231
              GC_free((void *)curr_dl);
264
232
#           endif
265
233
            return(1);
266
234
        }
269
237
    }
270
238
out:
271
239
    UNLOCK();
272
 
    ENABLE_SIGNALS();
273
240
    return(0);
274
241
}
275
242
 
276
243
/* Possible finalization_marker procedures.  Note that mark stack       */
277
244
/* overflow is handled by the caller, and is not a disaster.            */
278
 
GC_API void GC_normal_finalize_mark_proc(p)
279
 
ptr_t p;
 
245
GC_API void GC_normal_finalize_mark_proc(ptr_t p)
280
246
{
281
247
    hdr * hhdr = HDR(p);
282
248
    
283
 
    PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top,
 
249
    PUSH_OBJ(p, hhdr, GC_mark_stack_top,
284
250
             &(GC_mark_stack[GC_mark_stack_size]));
285
251
}
286
252
 
287
253
/* This only pays very partial attention to the mark descriptor.        */
288
254
/* It does the right thing for normal and atomic objects, and treats    */
289
255
/* most others as normal.                                               */
290
 
GC_API void GC_ignore_self_finalize_mark_proc(p)
291
 
ptr_t p;
 
256
GC_API void GC_ignore_self_finalize_mark_proc(ptr_t p)
292
257
{
293
258
    hdr * hhdr = HDR(p);
294
259
    word descr = hhdr -> hb_descr;
295
260
    ptr_t q, r;
296
261
    ptr_t scan_limit;
297
 
    ptr_t target_limit = p + WORDS_TO_BYTES(hhdr -> hb_sz) - 1;
 
262
    ptr_t target_limit = p + hhdr -> hb_sz - 1;
298
263
    
299
264
    if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
300
265
       scan_limit = p + descr - sizeof(word);
304
269
    for (q = p; q <= scan_limit; q += ALIGNMENT) {
305
270
        r = *(ptr_t *)q;
306
271
        if (r < p || r > target_limit) {
307
 
            GC_PUSH_ONE_HEAP((word)r, q);
 
272
            GC_PUSH_ONE_HEAP(r, q);
308
273
        }
309
274
    }
310
275
}
311
276
 
312
277
/*ARGSUSED*/
313
 
GC_API void GC_null_finalize_mark_proc(p)
314
 
ptr_t p;
315
 
{
 
278
GC_API void GC_null_finalize_mark_proc(ptr_t p)
 
279
{
 
280
}
 
281
 
 
282
/* Possible finalization_marker procedures.  Note that mark stack       */
 
283
/* overflow is handled by the caller, and is not a disaster.            */
 
284
 
 
285
/* GC_unreachable_finalize_mark_proc is an alias for normal marking,    */
 
286
/* but it is explicitly tested for, and triggers different              */
 
287
/* behavior.  Objects registered in this way are not finalized          */
 
288
/* if they are reachable by other finalizable objects, eve if those     */
 
289
/* other objects specify no ordering.                                   */
 
290
GC_API void GC_unreachable_finalize_mark_proc(ptr_t p)
 
291
{
 
292
    GC_normal_finalize_mark_proc(p);
316
293
}
317
294
 
318
295
 
325
302
/* marking for finalization ordering.  Any objects marked       */
326
303
/* by that procedure will be guaranteed to not have been        */
327
304
/* finalized when this finalizer is invoked.                    */
328
 
GC_API void GC_register_finalizer_inner(obj, fn, cd, ofn, ocd, mp)
329
 
GC_PTR obj;
330
 
GC_finalization_proc fn;
331
 
GC_PTR cd;
332
 
GC_finalization_proc * ofn;
333
 
GC_PTR * ocd;
334
 
finalization_mark_proc * mp;
 
305
GC_API void GC_register_finalizer_inner(void * obj,
 
306
                                        GC_finalization_proc fn, void *cd,
 
307
                                        GC_finalization_proc *ofn, void **ocd,
 
308
                                        finalization_mark_proc mp)
335
309
{
336
310
    ptr_t base;
337
311
    struct finalizable_object * curr_fo, * prev_fo;
338
 
    int index;
 
312
    size_t index;
339
313
    struct finalizable_object *new_fo;
340
314
    hdr *hhdr;
341
315
    DCL_LOCK_STATE;
342
316
 
343
317
#   ifdef THREADS
344
 
        DISABLE_SIGNALS();
345
318
        LOCK();
346
319
#   endif
347
320
    if (log_fo_table_size == -1
348
321
        || GC_fo_entries > ((word)1 << log_fo_table_size)) {
349
 
#       ifndef THREADS
350
 
            DISABLE_SIGNALS();
351
 
#       endif
352
322
        GC_grow_table((struct hash_chain_entry ***)(&fo_head),
353
323
                      &log_fo_table_size);
354
 
#       ifdef CONDPRINT
355
 
          if (GC_print_stats) {
356
 
            GC_printf1("Grew fo table to %lu entries\n",
357
 
                        (unsigned long)(1 << log_fo_table_size));
358
 
          }
359
 
#       endif
360
 
#       ifndef THREADS
361
 
            ENABLE_SIGNALS();
362
 
#       endif
 
324
        if (GC_print_stats) {
 
325
            GC_log_printf("Grew fo table to %u entries\n",
 
326
                          (1 << log_fo_table_size));
 
327
        }
363
328
    }
364
329
    /* in the THREADS case signals are disabled and we hold allocation  */
365
330
    /* lock; otherwise neither is true.  Proceed carefully.             */
367
332
    index = HASH2(base, log_fo_table_size);
368
333
    prev_fo = 0; curr_fo = fo_head[index];
369
334
    while (curr_fo != 0) {
 
335
        GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
370
336
        if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
371
337
            /* Interruption by a signal in the middle of this   */
372
338
            /* should be safe.  The client may see only *ocd    */
373
339
            /* updated, but we'll declare that to be his        */
374
340
            /* problem.                                         */
375
 
            if (ocd) *ocd = (GC_PTR) curr_fo -> fo_client_data;
 
341
            if (ocd) *ocd = (void *) (curr_fo -> fo_client_data);
376
342
            if (ofn) *ofn = curr_fo -> fo_fn;
377
343
            /* Delete the structure for base. */
378
344
                if (prev_fo == 0) {
386
352
                  /* estimate will only make the table larger than      */
387
353
                  /* necessary.                                         */
388
354
#               if !defined(THREADS) && !defined(DBG_HDRS_ALL)
389
 
                  GC_free((GC_PTR)curr_fo);
 
355
                  GC_free((void *)curr_fo);
390
356
#               endif
391
357
            } else {
392
358
                curr_fo -> fo_fn = fn;
402
368
            }
403
369
#           ifdef THREADS
404
370
                UNLOCK();
405
 
                ENABLE_SIGNALS();
406
371
#           endif
407
372
            return;
408
373
        }
414
379
    if (fn == 0) {
415
380
#       ifdef THREADS
416
381
            UNLOCK();
417
 
            ENABLE_SIGNALS();
418
382
#       endif
419
383
        return;
420
384
    }
423
387
      /* We won't collect it, hence finalizer wouldn't be run. */
424
388
#     ifdef THREADS
425
389
          UNLOCK();
426
 
          ENABLE_SIGNALS();
427
390
#     endif
428
391
      return;
429
392
    }
430
393
    new_fo = (struct finalizable_object *)
431
394
        GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
432
 
    if (0 == new_fo) {
 
395
    if (EXPECT(0 == new_fo, FALSE)) {
433
396
#     ifdef THREADS
434
397
        UNLOCK();
435
 
        ENABLE_SIGNALS();
436
398
#     endif
437
399
      new_fo = (struct finalizable_object *)
438
400
              GC_oom_fn(sizeof(struct finalizable_object));
442
404
      }
443
405
      /* It's not likely we'll make it here, but ... */
444
406
#     ifdef THREADS
445
 
        DISABLE_SIGNALS();
446
407
        LOCK();
447
408
#     endif
448
409
    }
 
410
    GC_ASSERT(GC_size(new_fo) >= sizeof(struct finalizable_object));
449
411
    new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
450
412
    new_fo -> fo_fn = fn;
451
413
    new_fo -> fo_client_data = (ptr_t)cd;
456
418
    fo_head[index] = new_fo;
457
419
#   ifdef THREADS
458
420
        UNLOCK();
459
 
        ENABLE_SIGNALS();
460
421
#   endif
461
422
}
462
423
 
463
 
# if defined(__STDC__)
464
 
    void GC_register_finalizer(void * obj,
 
424
void GC_register_finalizer(void * obj,
465
425
                               GC_finalization_proc fn, void * cd,
466
426
                               GC_finalization_proc *ofn, void ** ocd)
467
 
# else
468
 
    void GC_register_finalizer(obj, fn, cd, ofn, ocd)
469
 
    GC_PTR obj;
470
 
    GC_finalization_proc fn;
471
 
    GC_PTR cd;
472
 
    GC_finalization_proc * ofn;
473
 
    GC_PTR * ocd;
474
 
# endif
475
427
{
476
428
    GC_register_finalizer_inner(obj, fn, cd, ofn,
477
429
                                ocd, GC_normal_finalize_mark_proc);
478
430
}
479
431
 
480
 
# if defined(__STDC__)
481
 
    void GC_register_finalizer_ignore_self(void * obj,
 
432
void GC_register_finalizer_ignore_self(void * obj,
482
433
                               GC_finalization_proc fn, void * cd,
483
434
                               GC_finalization_proc *ofn, void ** ocd)
484
 
# else
485
 
    void GC_register_finalizer_ignore_self(obj, fn, cd, ofn, ocd)
486
 
    GC_PTR obj;
487
 
    GC_finalization_proc fn;
488
 
    GC_PTR cd;
489
 
    GC_finalization_proc * ofn;
490
 
    GC_PTR * ocd;
491
 
# endif
492
435
{
493
436
    GC_register_finalizer_inner(obj, fn, cd, ofn,
494
437
                                ocd, GC_ignore_self_finalize_mark_proc);
495
438
}
496
439
 
497
 
# if defined(__STDC__)
498
 
    void GC_register_finalizer_no_order(void * obj,
 
440
void GC_register_finalizer_no_order(void * obj,
499
441
                               GC_finalization_proc fn, void * cd,
500
442
                               GC_finalization_proc *ofn, void ** ocd)
501
 
# else
502
 
    void GC_register_finalizer_no_order(obj, fn, cd, ofn, ocd)
503
 
    GC_PTR obj;
504
 
    GC_finalization_proc fn;
505
 
    GC_PTR cd;
506
 
    GC_finalization_proc * ofn;
507
 
    GC_PTR * ocd;
508
 
# endif
509
443
{
510
444
    GC_register_finalizer_inner(obj, fn, cd, ofn,
511
445
                                ocd, GC_null_finalize_mark_proc);
512
446
}
513
447
 
 
448
static GC_bool need_unreachable_finalization = FALSE;
 
449
        /* Avoid the work if this isn't used.   */
 
450
 
 
451
void GC_register_finalizer_unreachable(void * obj,
 
452
                               GC_finalization_proc fn, void * cd,
 
453
                               GC_finalization_proc *ofn, void ** ocd)
 
454
{
 
455
    need_unreachable_finalization = TRUE;
 
456
    GC_ASSERT(GC_java_finalization);
 
457
    GC_register_finalizer_inner(obj, fn, cd, ofn,
 
458
                                ocd, GC_unreachable_finalize_mark_proc);
 
459
}
 
460
 
514
461
#ifndef NO_DEBUGGING
515
 
void GC_dump_finalization()
 
462
void GC_dump_finalization(void)
516
463
{
517
464
    struct disappearing_link * curr_dl;
518
465
    struct finalizable_object * curr_fo;
521
468
    int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
522
469
    int i;
523
470
 
524
 
    GC_printf0("Disappearing links:\n");
 
471
    GC_printf("Disappearing links:\n");
525
472
    for (i = 0; i < dl_size; i++) {
526
473
      for (curr_dl = dl_head[i]; curr_dl != 0; curr_dl = dl_next(curr_dl)) {
527
474
        real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
528
475
        real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
529
 
        GC_printf2("Object: 0x%lx, Link:0x%lx\n", real_ptr, real_link);
 
476
        GC_printf("Object: %p, Link:%p\n", real_ptr, real_link);
530
477
      }
531
478
    }
532
 
    GC_printf0("Finalizers:\n");
 
479
    GC_printf("Finalizers:\n");
533
480
    for (i = 0; i < fo_size; i++) {
534
481
      for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
535
482
        real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
536
 
        GC_printf1("Finalizable object: 0x%lx\n", real_ptr);
 
483
        GC_printf("Finalizable object: %p\n", real_ptr);
537
484
      }
538
485
    }
539
486
}
541
488
 
542
489
/* Called with world stopped.  Cause disappearing links to disappear,   */
543
490
/* and invoke finalizers.                                               */
544
 
void GC_finalize()
 
491
void GC_finalize(void)
545
492
{
546
493
    struct disappearing_link * curr_dl, * prev_dl, * next_dl;
547
494
    struct finalizable_object * curr_fo, * prev_fo, * next_fo;
548
495
    ptr_t real_ptr, real_link;
549
 
    register int i;
550
 
    int dl_size = (log_dl_table_size == -1 ) ? 0 : (1 << log_dl_table_size);
551
 
    int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
 
496
    size_t i;
 
497
    size_t dl_size = (log_dl_table_size == -1 ) ? 0 : (1 << log_dl_table_size);
 
498
    size_t fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
552
499
    
553
500
  /* Make disappearing links disappear */
554
501
    for (i = 0; i < dl_size; i++) {
579
526
    GC_ASSERT(GC_mark_state == MS_NONE);
580
527
    for (i = 0; i < fo_size; i++) {
581
528
      for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
 
529
        GC_ASSERT(GC_size(curr_fo) >= sizeof(struct finalizable_object));
582
530
        real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
583
531
        if (!GC_is_marked(real_ptr)) {
584
532
            GC_MARKED_FOR_FINALIZATION(real_ptr);
591
539
    }
592
540
  /* Enqueue for finalization all objects that are still                */
593
541
  /* unreachable.                                                       */
594
 
    GC_words_finalized = 0;
 
542
    GC_bytes_finalized = 0;
595
543
    for (i = 0; i < fo_size; i++) {
596
544
      curr_fo = fo_head[i];
597
545
      prev_fo = 0;
616
564
              /* see it.                                                */
617
565
              curr_fo -> fo_hidden_base = 
618
566
                        (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
619
 
              GC_words_finalized +=
620
 
                        ALIGNED_WORDS(curr_fo -> fo_object_size)
621
 
                        + ALIGNED_WORDS(sizeof(struct finalizable_object));
 
567
              GC_bytes_finalized +=
 
568
                        curr_fo -> fo_object_size
 
569
                        + sizeof(struct finalizable_object);
622
570
            GC_ASSERT(GC_is_marked(GC_base((ptr_t)curr_fo)));
623
571
            curr_fo = next_fo;
624
572
        } else {
638
586
            if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
639
587
                GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
640
588
            }
641
 
            GC_set_mark_bit(real_ptr);
 
589
            if (curr_fo -> fo_mark_proc != GC_unreachable_finalize_mark_proc) {
 
590
                GC_set_mark_bit(real_ptr);
 
591
            }
642
592
        }
643
593
      }
 
594
 
 
595
    /* now revive finalize-when-unreachable objects reachable from
 
596
       other finalizable objects */
 
597
      if (need_unreachable_finalization) {
 
598
        curr_fo = GC_finalize_now;
 
599
        prev_fo = 0;
 
600
        while (curr_fo != 0) {
 
601
          next_fo = fo_next(curr_fo);
 
602
          if (curr_fo -> fo_mark_proc == GC_unreachable_finalize_mark_proc) {
 
603
            real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
 
604
            if (!GC_is_marked(real_ptr)) {
 
605
              GC_set_mark_bit(real_ptr);
 
606
            } else {
 
607
              if (prev_fo == 0)
 
608
                GC_finalize_now = next_fo;
 
609
              else
 
610
                fo_set_next(prev_fo, next_fo);
 
611
 
 
612
              curr_fo -> fo_hidden_base =
 
613
                        (word) HIDE_POINTER(curr_fo -> fo_hidden_base);
 
614
              GC_bytes_finalized -=
 
615
                        curr_fo -> fo_object_size + sizeof(struct finalizable_object);
 
616
 
 
617
              i = HASH2(real_ptr, log_fo_table_size);
 
618
              fo_set_next (curr_fo, fo_head[i]);
 
619
              GC_fo_entries++;
 
620
              fo_head[i] = curr_fo;
 
621
              curr_fo = prev_fo;
 
622
            }
 
623
          }
 
624
          prev_fo = curr_fo;
 
625
          curr_fo = next_fo;
 
626
        }
 
627
      }
644
628
  }
645
629
 
646
630
  /* Remove dangling disappearing links. */
671
655
 
672
656
/* Enqueue all remaining finalizers to be run - Assumes lock is
673
657
 * held, and signals are disabled */
674
 
void GC_enqueue_all_finalizers()
 
658
void GC_enqueue_all_finalizers(void)
675
659
{
676
660
    struct finalizable_object * curr_fo, * prev_fo, * next_fo;
677
661
    ptr_t real_ptr;
679
663
    int fo_size;
680
664
    
681
665
    fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
682
 
    GC_words_finalized = 0;
 
666
    GC_bytes_finalized = 0;
683
667
    for (i = 0; i < fo_size; i++) {
684
668
        curr_fo = fo_head[i];
685
669
        prev_fo = 0;
706
690
          curr_fo -> fo_hidden_base = 
707
691
                        (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
708
692
 
709
 
          GC_words_finalized +=
710
 
                ALIGNED_WORDS(curr_fo -> fo_object_size)
711
 
                        + ALIGNED_WORDS(sizeof(struct finalizable_object));
 
693
          GC_bytes_finalized +=
 
694
                curr_fo -> fo_object_size + sizeof(struct finalizable_object);
712
695
          curr_fo = next_fo;
713
696
        }
714
697
    }
730
713
 * This routine is externally callable, so is called without 
731
714
 * the allocation lock. 
732
715
 */
733
 
GC_API void GC_finalize_all()
 
716
GC_API void GC_finalize_all(void)
734
717
{
735
718
    DCL_LOCK_STATE;
736
719
 
737
 
    DISABLE_SIGNALS();
738
720
    LOCK();
739
721
    while (GC_fo_entries > 0) {
740
722
      GC_enqueue_all_finalizers();
741
723
      UNLOCK();
742
 
      ENABLE_SIGNALS();
743
724
      GC_INVOKE_FINALIZERS();
744
 
      DISABLE_SIGNALS();
745
725
      LOCK();
746
726
    }
747
727
    UNLOCK();
748
 
    ENABLE_SIGNALS();
749
728
}
750
729
#endif
751
730
 
752
731
/* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
753
732
/* finalizers can only be called from some kind of `safe state' and     */
754
733
/* getting into that safe state is expensive.)                          */
755
 
int GC_should_invoke_finalizers GC_PROTO((void))
 
734
int GC_should_invoke_finalizers(void)
756
735
{
757
736
    return GC_finalize_now != 0;
758
737
}
759
738
 
760
739
/* Invoke finalizers for all objects that are ready to be finalized.    */
761
740
/* Should be called without allocation lock.                            */
762
 
int GC_invoke_finalizers()
 
741
int GC_invoke_finalizers(void)
763
742
{
764
743
    struct finalizable_object * curr_fo;
765
744
    int count = 0;
766
 
    word mem_freed_before;
 
745
    word bytes_freed_before;
767
746
    DCL_LOCK_STATE;
768
747
    
769
748
    while (GC_finalize_now != 0) {
770
749
#       ifdef THREADS
771
 
            DISABLE_SIGNALS();
772
750
            LOCK();
773
751
#       endif
774
752
        if (count == 0) {
775
 
            mem_freed_before = GC_mem_freed;
 
753
            bytes_freed_before = GC_bytes_freed;
 
754
            /* Don't do this outside, since we need the lock. */
776
755
        }
777
756
        curr_fo = GC_finalize_now;
778
757
#       ifdef THREADS
779
758
            if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
780
759
            UNLOCK();
781
 
            ENABLE_SIGNALS();
782
760
            if (curr_fo == 0) break;
783
761
#       else
784
762
            GC_finalize_now = fo_next(curr_fo);
792
770
            /* This is probably a bad idea.  It throws off accounting if */
793
771
            /* nearly all objects are finalizable.  O.w. it shouldn't    */
794
772
            /* matter.                                                   */
795
 
            GC_free((GC_PTR)curr_fo);
 
773
            GC_free((void *)curr_fo);
796
774
#       endif
797
775
    }
798
 
    if (count != 0 && mem_freed_before != GC_mem_freed) {
 
776
    /* bytes_freed_before is initialized whenever count != 0 */
 
777
    if (count != 0 && bytes_freed_before != GC_bytes_freed) {
799
778
        LOCK();
800
 
        GC_finalizer_mem_freed += (GC_mem_freed - mem_freed_before);
 
779
        GC_finalizer_bytes_freed += (GC_bytes_freed - bytes_freed_before);
801
780
        UNLOCK();
802
781
    }
803
782
    return count;
804
783
}
805
784
 
806
 
void (* GC_finalizer_notifier)() = (void (*) GC_PROTO((void)))0;
 
785
void (* GC_finalizer_notifier)() = (void (*) (void))0;
807
786
 
808
787
static GC_word last_finalizer_notification = 0;
809
788
 
810
 
void GC_notify_or_invoke_finalizers GC_PROTO((void))
 
789
void GC_notify_or_invoke_finalizers(void)
811
790
{
812
791
    /* This is a convenient place to generate backtraces if appropriate, */
813
792
    /* since that code is not callable with the allocation lock.         */
816
795
 
817
796
      if (GC_gc_no > last_back_trace_gc_no) {
818
797
        word i;
819
 
 
 
798
  
820
799
#       ifdef KEEP_BACK_PTRS
821
800
          LOCK();
822
 
          /* Stops when GC_gc_no wraps; that's OK.      */
 
801
          /* Stops when GC_gc_no wraps; that's OK.      */
823
802
          last_back_trace_gc_no = (word)(-1);  /* disable others. */
824
803
          for (i = 0; i < GC_backtraces; ++i) {
825
804
              /* FIXME: This tolerates concurrent heap mutation,        */
847
826
#       endif   /* Otherwise GC can run concurrently and add more */
848
827
        return;
849
828
    }
850
 
    if (GC_finalizer_notifier != (void (*) GC_PROTO((void)))0
 
829
    if (GC_finalizer_notifier != (void (*) (void))0
851
830
        && last_finalizer_notification != GC_gc_no) {
852
831
        last_finalizer_notification = GC_gc_no;
853
832
        GC_finalizer_notifier();
854
833
    }
855
834
}
856
835
 
857
 
# ifdef __STDC__
858
 
    GC_PTR GC_call_with_alloc_lock(GC_fn_type fn,
859
 
                                         GC_PTR client_data)
860
 
# else
861
 
    GC_PTR GC_call_with_alloc_lock(fn, client_data)
862
 
    GC_fn_type fn;
863
 
    GC_PTR client_data;
864
 
# endif
 
836
void * GC_call_with_alloc_lock(GC_fn_type fn, void * client_data)
865
837
{
866
 
    GC_PTR result;
 
838
    void * result;
867
839
    DCL_LOCK_STATE;
868
840
    
869
841
#   ifdef THREADS
870
 
      DISABLE_SIGNALS();
871
842
      LOCK();
 
843
      /* FIXME - This looks wrong!! */
872
844
      SET_LOCK_HOLDER();
873
845
#   endif
874
846
    result = (*fn)(client_data);
877
849
        UNSET_LOCK_HOLDER();
878
850
#     endif /* o.w. UNLOCK() does it implicitly */
879
851
      UNLOCK();
880
 
      ENABLE_SIGNALS();
881
852
#   endif
882
853
    return(result);
883
854
}
884
855
 
885
856
#if !defined(NO_DEBUGGING)
886
857
 
887
 
void GC_print_finalization_stats()
 
858
void GC_print_finalization_stats(void)
888
859
{
889
860
    struct finalizable_object *fo = GC_finalize_now;
890
861
    size_t ready = 0;
891
862
 
892
 
    GC_printf2("%lu finalization table entries; %lu disappearing links\n",
 
863
    GC_printf("%u finalization table entries; %u disappearing links\n",
893
864
               GC_fo_entries, GC_dl_entries);
894
865
    for (; 0 != fo; fo = fo_next(fo)) ++ready;
895
 
    GC_printf1("%lu objects are eligible for immediate finalization\n", ready);
 
866
    GC_printf("%u objects are eligible for immediate finalization\n", ready);
896
867
}
897
868
 
898
869
#endif /* NO_DEBUGGING */