OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [boehm-gc/] [alloc.c] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/*
2
 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3
 * Copyright (c) 1991-1996 by Xerox Corporation.  All rights reserved.
4
 * Copyright (c) 1998 by Silicon Graphics.  All rights reserved.
5
 * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
6
 *
7
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8
 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
9
 *
10
 * Permission is hereby granted to use or copy this program
11
 * for any purpose,  provided the above notices are retained on all copies.
12
 * Permission to modify the code and to distribute modified code is granted,
13
 * provided the above notices are retained, and a notice that the code was
14
 * modified is included with the above copyright notice.
15
 *
16
 */
17
 
18
 
19
# include "private/gc_priv.h"
20
 
21
# include <stdio.h>
22
# if !defined(MACOS) && !defined(MSWINCE)
23
#   include <signal.h>
24
#   include <sys/types.h>
25
# endif
26
 
27
/*
28
 * Separate free lists are maintained for different sized objects
29
 * up to MAXOBJSZ.
30
 * The call GC_allocobj(i,k) ensures that the freelist for
31
 * kind k objects of size i points to a non-empty
32
 * free list. It returns a pointer to the first entry on the free list.
33
 * In a single-threaded world, GC_allocobj may be called to allocate
34
 * an object of (small) size i as follows:
35
 *
36
 *            opp = &(GC_objfreelist[i]);
37
 *            if (*opp == 0) GC_allocobj(i, NORMAL);
38
 *            ptr = *opp;
39
 *            *opp = obj_link(ptr);
40
 *
41
 * Note that this is very fast if the free list is non-empty; it should
42
 * only involve the execution of 4 or 5 simple instructions.
43
 * All composite objects on freelists are cleared, except for
44
 * their first word.
45
 */
46
 
47
/*
48
 *  The allocator uses GC_allochblk to allocate large chunks of objects.
49
 * These chunks all start on addresses which are multiples of
50
 * HBLKSZ.   Each allocated chunk has an associated header,
51
 * which can be located quickly based on the address of the chunk.
52
 * (See headers.c for details.)
53
 * This makes it possible to check quickly whether an
54
 * arbitrary address corresponds to an object administered by the
55
 * allocator.
56
 */
57
 
58
word GC_non_gc_bytes = 0;  /* Number of bytes not intended to be collected */
59
 
60
word GC_gc_no = 0;
61
 
62
#ifndef SMALL_CONFIG
63
  int GC_incremental = 0;  /* By default, stop the world.        */
64
#endif
65
 
66
int GC_parallel = FALSE;   /* By default, parallel GC is off.   */
67
 
68
int GC_full_freq = 19;     /* Every 20th collection is a full   */
69
                           /* collection, whether we need it    */
70
                           /* or not.                           */
71
 
72
GC_bool GC_need_full_gc = FALSE;
73
                           /* Need full GC do to heap growth.   */
74
 
75
#ifdef THREADS
76
  GC_bool GC_world_stopped = FALSE;
77
# define IF_THREADS(x) x
78
#else
79
# define IF_THREADS(x)
80
#endif
81
 
82
word GC_used_heap_size_after_full = 0;
83
 
84
char * GC_copyright[] =
85
{"Copyright 1988,1989 Hans-J. Boehm and Alan J. Demers ",
86
"Copyright (c) 1991-1995 by Xerox Corporation.  All rights reserved. ",
87
"Copyright (c) 1996-1998 by Silicon Graphics.  All rights reserved. ",
88
"Copyright (c) 1999-2001 by Hewlett-Packard Company.  All rights reserved. ",
89
"THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY",
90
" EXPRESSED OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.",
91
"See source code for details." };
92
 
93
# include "version.h"
94
 
95
/* some more variables */
96
 
97
extern signed_word GC_mem_found;  /* Number of reclaimed longwords      */
98
                                  /* after garbage collection           */
99
 
100
GC_bool GC_dont_expand = 0;
101
 
102
word GC_free_space_divisor = 3;
103
 
104
extern GC_bool GC_collection_in_progress();
105
                /* Collection is in progress, or was abandoned. */
106
 
107
int GC_never_stop_func GC_PROTO((void)) { return(0); }
108
 
109
unsigned long GC_time_limit = TIME_LIMIT;
110
 
111
CLOCK_TYPE GC_start_time;       /* Time at which we stopped world.      */
112
                                /* used only in GC_timeout_stop_func.   */
113
 
114
int GC_n_attempts = 0;           /* Number of attempts at finishing      */
115
                                /* collection within GC_time_limit.     */
116
 
117
#if defined(SMALL_CONFIG) || defined(NO_CLOCK)
118
#   define GC_timeout_stop_func GC_never_stop_func
119
#else
120
  int GC_timeout_stop_func GC_PROTO((void))
121
  {
122
    CLOCK_TYPE current_time;
123
    static unsigned count = 0;
124
    unsigned long time_diff;
125
 
126
    if ((count++ & 3) != 0) return(0);
127
    GET_TIME(current_time);
128
    time_diff = MS_TIME_DIFF(current_time,GC_start_time);
129
    if (time_diff >= GC_time_limit) {
130
#       ifdef CONDPRINT
131
          if (GC_print_stats) {
132
            GC_printf0("Abandoning stopped marking after ");
133
            GC_printf1("%lu msecs", (unsigned long)time_diff);
134
            GC_printf1("(attempt %ld)\n", (unsigned long) GC_n_attempts);
135
          }
136
#       endif
137
        return(1);
138
    }
139
    return(0);
140
  }
141
#endif /* !SMALL_CONFIG */
142
 
143
/* Return the minimum number of words that must be allocated between    */
144
/* collections to amortize the collection cost.                         */
145
static word min_words_allocd()
146
{
147
#   ifdef THREADS
148
        /* We punt, for now. */
149
        register signed_word stack_size = 10000;
150
#   else
151
        int dummy;
152
        register signed_word stack_size = (ptr_t)(&dummy) - GC_stackbottom;
153
#   endif
154
    word total_root_size;           /* includes double stack size,      */
155
                                    /* since the stack is expensive     */
156
                                    /* to scan.                         */
157
    word scan_size;             /* Estimate of memory to be scanned     */
158
                                /* during normal GC.                    */
159
 
160
    if (stack_size < 0) stack_size = -stack_size;
161
    total_root_size = 2 * stack_size + GC_root_size;
162
    scan_size = BYTES_TO_WORDS(GC_heapsize - GC_large_free_bytes
163
                               + (GC_large_free_bytes >> 2)
164
                                   /* use a bit more of large empty heap */
165
                               + total_root_size);
166
    if (TRUE_INCREMENTAL) {
167
        return scan_size / (2 * GC_free_space_divisor);
168
    } else {
169
        return scan_size / GC_free_space_divisor;
170
    }
171
}
172
 
173
/* Return the number of words allocated, adjusted for explicit storage  */
174
/* management, etc..  This number is used in deciding when to trigger   */
175
/* collections.                                                         */
176
word GC_adj_words_allocd()
177
{
178
    register signed_word result;
179
    register signed_word expl_managed =
180
                BYTES_TO_WORDS((long)GC_non_gc_bytes
181
                                - (long)GC_non_gc_bytes_at_gc);
182
 
183
    /* Don't count what was explicitly freed, or newly allocated for    */
184
    /* explicit management.  Note that deallocating an explicitly       */
185
    /* managed object should not alter result, assuming the client      */
186
    /* is playing by the rules.                                         */
187
    result = (signed_word)GC_words_allocd
188
             - (signed_word)GC_mem_freed
189
             + (signed_word)GC_finalizer_mem_freed - expl_managed;
190
    if (result > (signed_word)GC_words_allocd) {
191
        result = GC_words_allocd;
192
        /* probably client bug or unfortunate scheduling */
193
    }
194
    result += GC_words_finalized;
195
        /* We count objects enqueued for finalization as though they    */
196
        /* had been reallocated this round. Finalization is user        */
197
        /* visible progress.  And if we don't count this, we have       */
198
        /* stability problems for programs that finalize all objects.   */
199
    result += GC_words_wasted;
200
        /* This doesn't reflect useful work.  But if there is lots of   */
201
        /* new fragmentation, the same is probably true of the heap,    */
202
        /* and the collection will be correspondingly cheaper.          */
203
    if (result < (signed_word)(GC_words_allocd >> 3)) {
204
        /* Always count at least 1/8 of the allocations.  We don't want */
205
        /* to collect too infrequently, since that would inhibit        */
206
        /* coalescing of free storage blocks.                           */
207
        /* This also makes us partially robust against client bugs.     */
208
        return(GC_words_allocd >> 3);
209
    } else {
210
        return(result);
211
    }
212
}
213
 
214
 
215
/* Clear up a few frames worth of garbage left at the top of the stack. */
216
/* This is used to prevent us from accidentally treating garbade left   */
217
/* on the stack by other parts of the collector as roots.  This         */
218
/* differs from the code in misc.c, which actually tries to keep the    */
219
/* stack clear of long-lived, client-generated garbage.                 */
220
void GC_clear_a_few_frames()
221
{
222
#   define NWORDS 64
223
    word frames[NWORDS];
224
    register int i;
225
 
226
    for (i = 0; i < NWORDS; i++) frames[i] = 0;
227
}
228
 
229
/* Heap size at which we need a collection to avoid expanding past      */
230
/* limits used by blacklisting.                                         */
231
static word GC_collect_at_heapsize = (word)(-1);
232
 
233
/* Have we allocated enough to amortize a collection? */
234
GC_bool GC_should_collect()
235
{
236
    return(GC_adj_words_allocd() >= min_words_allocd()
237
           || GC_heapsize >= GC_collect_at_heapsize);
238
}
239
 
240
 
241
void GC_notify_full_gc()
242
{
243
    if (GC_start_call_back != (void (*) GC_PROTO((void)))0) {
244
        (*GC_start_call_back)();
245
    }
246
}
247
 
248
GC_bool GC_is_full_gc = FALSE;
249
 
250
/*
251
 * Initiate a garbage collection if appropriate.
252
 * Choose judiciously
253
 * between partial, full, and stop-world collections.
254
 * Assumes lock held, signals disabled.
255
 */
256
void GC_maybe_gc()
257
{
258
    static int n_partial_gcs = 0;
259
 
260
    if (GC_should_collect()) {
261
        if (!GC_incremental) {
262
            GC_gcollect_inner();
263
            n_partial_gcs = 0;
264
            return;
265
        } else {
266
#         ifdef PARALLEL_MARK
267
            GC_wait_for_reclaim();
268
#         endif
269
          if (GC_need_full_gc || n_partial_gcs >= GC_full_freq) {
270
#           ifdef CONDPRINT
271
              if (GC_print_stats) {
272
                GC_printf2(
273
                  "***>Full mark for collection %lu after %ld allocd bytes\n",
274
                  (unsigned long) GC_gc_no+1,
275
                  (long)WORDS_TO_BYTES(GC_words_allocd));
276
              }
277
#           endif
278
            GC_promote_black_lists();
279
            (void)GC_reclaim_all((GC_stop_func)0, TRUE);
280
            GC_clear_marks();
281
            n_partial_gcs = 0;
282
            GC_notify_full_gc();
283
            GC_is_full_gc = TRUE;
284
          } else {
285
            n_partial_gcs++;
286
          }
287
        }
288
        /* We try to mark with the world stopped.       */
289
        /* If we run out of time, this turns into       */
290
        /* incremental marking.                 */
291
#       ifndef NO_CLOCK
292
          if (GC_time_limit != GC_TIME_UNLIMITED) { GET_TIME(GC_start_time); }
293
#       endif
294
        if (GC_stopped_mark(GC_time_limit == GC_TIME_UNLIMITED?
295
                            GC_never_stop_func : GC_timeout_stop_func)) {
296
#           ifdef SAVE_CALL_CHAIN
297
                GC_save_callers(GC_last_stack);
298
#           endif
299
            GC_finish_collection();
300
        } else {
301
            if (!GC_is_full_gc) {
302
                /* Count this as the first attempt */
303
                GC_n_attempts++;
304
            }
305
        }
306
    }
307
}
308
 
309
 
310
/*
311
 * Stop the world garbage collection.  Assumes lock held, signals disabled.
312
 * If stop_func is not GC_never_stop_func, then abort if stop_func returns TRUE.
313
 * Return TRUE if we successfully completed the collection.
314
 */
315
GC_bool GC_try_to_collect_inner(stop_func)
316
GC_stop_func stop_func;
317
{
318
#   ifdef CONDPRINT
319
        CLOCK_TYPE start_time, current_time;
320
#   endif
321
    if (GC_dont_gc) return FALSE;
322
    if (GC_incremental && GC_collection_in_progress()) {
323
#   ifdef CONDPRINT
324
      if (GC_print_stats) {
325
        GC_printf0(
326
            "GC_try_to_collect_inner: finishing collection in progress\n");
327
      }
328
#   endif /* CONDPRINT */
329
      /* Just finish collection already in progress.    */
330
        while(GC_collection_in_progress()) {
331
            if (stop_func()) return(FALSE);
332
            GC_collect_a_little_inner(1);
333
        }
334
    }
335
    if (stop_func == GC_never_stop_func) GC_notify_full_gc();
336
#   ifdef CONDPRINT
337
      if (GC_print_stats) {
338
        if (GC_print_stats) GET_TIME(start_time);
339
        GC_printf2(
340
           "Initiating full world-stop collection %lu after %ld allocd bytes\n",
341
           (unsigned long) GC_gc_no+1,
342
           (long)WORDS_TO_BYTES(GC_words_allocd));
343
      }
344
#   endif
345
    GC_promote_black_lists();
346
    /* Make sure all blocks have been reclaimed, so sweep routines      */
347
    /* don't see cleared mark bits.                                     */
348
    /* If we're guaranteed to finish, then this is unnecessary.         */
349
    /* In the find_leak case, we have to finish to guarantee that       */
350
    /* previously unmarked objects are not reported as leaks.           */
351
#       ifdef PARALLEL_MARK
352
            GC_wait_for_reclaim();
353
#       endif
354
        if ((GC_find_leak || stop_func != GC_never_stop_func)
355
            && !GC_reclaim_all(stop_func, FALSE)) {
356
            /* Aborted.  So far everything is still consistent. */
357
            return(FALSE);
358
        }
359
    GC_invalidate_mark_state();  /* Flush mark stack.   */
360
    GC_clear_marks();
361
#   ifdef SAVE_CALL_CHAIN
362
        GC_save_callers(GC_last_stack);
363
#   endif
364
    GC_is_full_gc = TRUE;
365
    if (!GC_stopped_mark(stop_func)) {
366
      if (!GC_incremental) {
367
        /* We're partially done and have no way to complete or use      */
368
        /* current work.  Reestablish invariants as cheaply as          */
369
        /* possible.                                                    */
370
        GC_invalidate_mark_state();
371
        GC_unpromote_black_lists();
372
      } /* else we claim the world is already still consistent.  We'll  */
373
        /* finish incrementally.                                        */
374
      return(FALSE);
375
    }
376
    GC_finish_collection();
377
#   if defined(CONDPRINT)
378
      if (GC_print_stats) {
379
        GET_TIME(current_time);
380
        GC_printf1("Complete collection took %lu msecs\n",
381
                   MS_TIME_DIFF(current_time,start_time));
382
      }
383
#   endif
384
    return(TRUE);
385
}
386
 
387
 
388
 
389
/*
390
 * Perform n units of garbage collection work.  A unit is intended to touch
391
 * roughly GC_RATE pages.  Every once in a while, we do more than that.
392
 * This needa to be a fairly large number with our current incremental
393
 * GC strategy, since otherwise we allocate too much during GC, and the
394
 * cleanup gets expensive.
395
 */
396
# define GC_RATE 10 
397
# define MAX_PRIOR_ATTEMPTS 1
398
        /* Maximum number of prior attempts at world stop marking       */
399
        /* A value of 1 means that we finish the second time, no matter */
400
        /* how long it takes.  Doesn't count the initial root scan      */
401
        /* for a full GC.                                               */
402
 
403
int GC_deficit = 0;      /* The number of extra calls to GC_mark_some    */
404
                        /* that we have made.                           */
405
 
406
void GC_collect_a_little_inner(n)
407
int n;
408
{
409
    register int i;
410
 
411
    if (GC_dont_gc) return;
412
    if (GC_incremental && GC_collection_in_progress()) {
413
        for (i = GC_deficit; i < GC_RATE*n; i++) {
414
            if (GC_mark_some((ptr_t)0)) {
415
                /* Need to finish a collection */
416
#               ifdef SAVE_CALL_CHAIN
417
                    GC_save_callers(GC_last_stack);
418
#               endif
419
#               ifdef PARALLEL_MARK
420
                    GC_wait_for_reclaim();
421
#               endif
422
                if (GC_n_attempts < MAX_PRIOR_ATTEMPTS
423
                    && GC_time_limit != GC_TIME_UNLIMITED) {
424
                  GET_TIME(GC_start_time);
425
                  if (!GC_stopped_mark(GC_timeout_stop_func)) {
426
                    GC_n_attempts++;
427
                    break;
428
                  }
429
                } else {
430
                  (void)GC_stopped_mark(GC_never_stop_func);
431
                }
432
                GC_finish_collection();
433
                break;
434
            }
435
        }
436
        if (GC_deficit > 0) GC_deficit -= GC_RATE*n;
437
        if (GC_deficit < 0) GC_deficit = 0;
438
    } else {
439
        GC_maybe_gc();
440
    }
441
}
442
 
443
int GC_collect_a_little GC_PROTO(())
444
{
445
    int result;
446
    DCL_LOCK_STATE;
447
 
448
    DISABLE_SIGNALS();
449
    LOCK();
450
    GC_collect_a_little_inner(1);
451
    result = (int)GC_collection_in_progress();
452
    UNLOCK();
453
    ENABLE_SIGNALS();
454
    if (!result && GC_debugging_started) GC_print_all_smashed();
455
    return(result);
456
}
457
 
458
/*
459
 * Assumes lock is held, signals are disabled.
460
 * We stop the world.
461
 * If stop_func() ever returns TRUE, we may fail and return FALSE.
462
 * Increment GC_gc_no if we succeed.
463
 */
464
GC_bool GC_stopped_mark(stop_func)
465
GC_stop_func stop_func;
466
{
467
    register int i;
468
    int dummy;
469
#   if defined(PRINTTIMES) || defined(CONDPRINT)
470
        CLOCK_TYPE start_time, current_time;
471
#   endif
472
 
473
#   ifdef PRINTTIMES
474
        GET_TIME(start_time);
475
#   endif
476
#   if defined(CONDPRINT) && !defined(PRINTTIMES)
477
        if (GC_print_stats) GET_TIME(start_time);
478
#   endif
479
#   if defined(REGISTER_LIBRARIES_EARLY)
480
        GC_cond_register_dynamic_libraries();
481
#   endif
482
    STOP_WORLD();
483
    IF_THREADS(GC_world_stopped = TRUE);
484
#   ifdef CONDPRINT
485
      if (GC_print_stats) {
486
        GC_printf1("--> Marking for collection %lu ",
487
                   (unsigned long) GC_gc_no + 1);
488
        GC_printf2("after %lu allocd bytes + %lu wasted bytes\n",
489
                   (unsigned long) WORDS_TO_BYTES(GC_words_allocd),
490
                   (unsigned long) WORDS_TO_BYTES(GC_words_wasted));
491
      }
492
#   endif
493
#   ifdef MAKE_BACK_GRAPH
494
      if (GC_print_back_height) {
495
        GC_build_back_graph();
496
      }
497
#   endif
498
 
499
    /* Mark from all roots.  */
500
        /* Minimize junk left in my registers and on the stack */
501
            GC_clear_a_few_frames();
502
            GC_noop(0,0,0,0,0,0);
503
        GC_initiate_gc();
504
        for(i = 0;;i++) {
505
            if ((*stop_func)()) {
506
#                   ifdef CONDPRINT
507
                      if (GC_print_stats) {
508
                        GC_printf0("Abandoned stopped marking after ");
509
                        GC_printf1("%lu iterations\n",
510
                                   (unsigned long)i);
511
                      }
512
#                   endif
513
                    GC_deficit = i; /* Give the mutator a chance. */
514
                    IF_THREADS(GC_world_stopped = FALSE);
515
                    START_WORLD();
516
                    return(FALSE);
517
            }
518
            if (GC_mark_some((ptr_t)(&dummy))) break;
519
        }
520
 
521
    GC_gc_no++;
522
#   ifdef PRINTSTATS
523
      GC_printf2("Collection %lu reclaimed %ld bytes",
524
                  (unsigned long) GC_gc_no - 1,
525
                  (long)WORDS_TO_BYTES(GC_mem_found));
526
#   else
527
#     ifdef CONDPRINT
528
        if (GC_print_stats) {
529
          GC_printf1("Collection %lu finished", (unsigned long) GC_gc_no - 1);
530
        }
531
#     endif
532
#   endif /* !PRINTSTATS */
533
#   ifdef CONDPRINT
534
      if (GC_print_stats) {
535
        GC_printf1(" ---> heapsize = %lu bytes\n",
536
                   (unsigned long) GC_heapsize);
537
        /* Printf arguments may be pushed in funny places.  Clear the   */
538
        /* space.                                                       */
539
        GC_printf0("");
540
      }
541
#   endif  /* CONDPRINT  */
542
 
543
    /* Check all debugged objects for consistency */
544
        if (GC_debugging_started) {
545
            (*GC_check_heap)();
546
        }
547
 
548
    IF_THREADS(GC_world_stopped = FALSE);
549
    START_WORLD();
550
#   ifdef PRINTTIMES
551
        GET_TIME(current_time);
552
        GC_printf1("World-stopped marking took %lu msecs\n",
553
                   MS_TIME_DIFF(current_time,start_time));
554
#   else
555
#     ifdef CONDPRINT
556
        if (GC_print_stats) {
557
          GET_TIME(current_time);
558
          GC_printf1("World-stopped marking took %lu msecs\n",
559
                     MS_TIME_DIFF(current_time,start_time));
560
        }
561
#     endif
562
#   endif
563
    return(TRUE);
564
}
565
 
566
/* Set all mark bits for the free list whose first entry is q   */
567
#ifdef __STDC__
568
  void GC_set_fl_marks(ptr_t q)
569
#else
570
  void GC_set_fl_marks(q)
571
  ptr_t q;
572
#endif
573
{
574
   ptr_t p;
575
   struct hblk * h, * last_h = 0;
576
   hdr *hhdr;
577
   int word_no;
578
 
579
   for (p = q; p != 0; p = obj_link(p)){
580
        h = HBLKPTR(p);
581
        if (h != last_h) {
582
          last_h = h;
583
          hhdr = HDR(h);
584
        }
585
        word_no = (((word *)p) - ((word *)h));
586
        set_mark_bit_from_hdr(hhdr, word_no);
587
   }
588
}
589
 
590
/* Clear all mark bits for the free list whose first entry is q */
591
/* Decrement GC_mem_found by number of words on free list.      */
592
#ifdef __STDC__
593
  void GC_clear_fl_marks(ptr_t q)
594
#else
595
  void GC_clear_fl_marks(q)
596
  ptr_t q;
597
#endif
598
{
599
   ptr_t p;
600
   struct hblk * h, * last_h = 0;
601
   hdr *hhdr;
602
   int word_no;
603
 
604
   for (p = q; p != 0; p = obj_link(p)){
605
        h = HBLKPTR(p);
606
        if (h != last_h) {
607
          last_h = h;
608
          hhdr = HDR(h);
609
        }
610
        word_no = (((word *)p) - ((word *)h));
611
        clear_mark_bit_from_hdr(hhdr, word_no);
612
#       ifdef GATHERSTATS
613
            GC_mem_found -= hhdr -> hb_sz;
614
#       endif
615
   }
616
}
617
 
618
/* Finish up a collection.  Assumes lock is held, signals are disabled, */
619
/* but the world is otherwise running.                                  */
620
void GC_finish_collection()
621
{
622
#   ifdef PRINTTIMES
623
        CLOCK_TYPE start_time;
624
        CLOCK_TYPE finalize_time;
625
        CLOCK_TYPE done_time;
626
 
627
        GET_TIME(start_time);
628
        finalize_time = start_time;
629
#   endif
630
 
631
#   ifdef GATHERSTATS
632
        GC_mem_found = 0;
633
#   endif
634
#   if defined(LINUX) && defined(__ELF__) && !defined(SMALL_CONFIG)
635
        if (getenv("GC_PRINT_ADDRESS_MAP") != 0) {
636
          GC_print_address_map();
637
        }
638
#   endif
639
    COND_DUMP;
640
    if (GC_find_leak) {
641
      /* Mark all objects on the free list.  All objects should be */
642
      /* marked when we're done.                                   */
643
        {
644
          register word size;           /* current object size          */
645
          int kind;
646
          ptr_t q;
647
 
648
          for (kind = 0; kind < GC_n_kinds; kind++) {
649
            for (size = 1; size <= MAXOBJSZ; size++) {
650
              q = GC_obj_kinds[kind].ok_freelist[size];
651
              if (q != 0) GC_set_fl_marks(q);
652
            }
653
          }
654
        }
655
        GC_start_reclaim(TRUE);
656
          /* The above just checks; it doesn't really reclaim anything. */
657
    }
658
 
659
    GC_finalize();
660
#   ifdef STUBBORN_ALLOC
661
      GC_clean_changing_list();
662
#   endif
663
 
664
#   ifdef PRINTTIMES
665
      GET_TIME(finalize_time);
666
#   endif
667
 
668
    if (GC_print_back_height) {
669
#     ifdef MAKE_BACK_GRAPH
670
        GC_traverse_back_graph();
671
#     else
672
#       ifndef SMALL_CONFIG
673
          GC_err_printf0("Back height not available: "
674
                         "Rebuild collector with -DMAKE_BACK_GRAPH\n");
675
#       endif
676
#     endif
677
    }
678
 
679
    /* Clear free list mark bits, in case they got accidentally marked   */
680
    /* (or GC_find_leak is set and they were intentionally marked).      */
681
    /* Also subtract memory remaining from GC_mem_found count.           */
682
    /* Note that composite objects on free list are cleared.             */
683
    /* Thus accidentally marking a free list is not a problem;  only     */
684
    /* objects on the list itself will be marked, and that's fixed here. */
685
      {
686
        register word size;             /* current object size          */
687
        register ptr_t q;       /* pointer to current object    */
688
        int kind;
689
 
690
        for (kind = 0; kind < GC_n_kinds; kind++) {
691
          for (size = 1; size <= MAXOBJSZ; size++) {
692
            q = GC_obj_kinds[kind].ok_freelist[size];
693
            if (q != 0) GC_clear_fl_marks(q);
694
          }
695
        }
696
      }
697
 
698
 
699
#   ifdef PRINTSTATS
700
        GC_printf1("Bytes recovered before sweep - f.l. count = %ld\n",
701
                  (long)WORDS_TO_BYTES(GC_mem_found));
702
#   endif
703
    /* Reconstruct free lists to contain everything not marked */
704
        GC_start_reclaim(FALSE);
705
        if (GC_is_full_gc)  {
706
            GC_used_heap_size_after_full = USED_HEAP_SIZE;
707
            GC_need_full_gc = FALSE;
708
        } else {
709
            GC_need_full_gc =
710
                 BYTES_TO_WORDS(USED_HEAP_SIZE - GC_used_heap_size_after_full)
711
                 > min_words_allocd();
712
        }
713
 
714
#   ifdef PRINTSTATS
715
        GC_printf2(
716
                  "Immediately reclaimed %ld bytes in heap of size %lu bytes",
717
                  (long)WORDS_TO_BYTES(GC_mem_found),
718
                  (unsigned long)GC_heapsize);
719
#       ifdef USE_MUNMAP
720
          GC_printf1("(%lu unmapped)", GC_unmapped_bytes);
721
#       endif
722
        GC_printf2(
723
                "\n%lu (atomic) + %lu (composite) collectable bytes in use\n",
724
                (unsigned long)WORDS_TO_BYTES(GC_atomic_in_use),
725
                (unsigned long)WORDS_TO_BYTES(GC_composite_in_use));
726
#   endif
727
 
728
      GC_n_attempts = 0;
729
      GC_is_full_gc = FALSE;
730
    /* Reset or increment counters for next cycle */
731
      GC_words_allocd_before_gc += GC_words_allocd;
732
      GC_non_gc_bytes_at_gc = GC_non_gc_bytes;
733
      GC_words_allocd = 0;
734
      GC_words_wasted = 0;
735
      GC_mem_freed = 0;
736
      GC_finalizer_mem_freed = 0;
737
 
738
#   ifdef USE_MUNMAP
739
      GC_unmap_old();
740
#   endif
741
#   ifdef PRINTTIMES
742
        GET_TIME(done_time);
743
        GC_printf2("Finalize + initiate sweep took %lu + %lu msecs\n",
744
                   MS_TIME_DIFF(finalize_time,start_time),
745
                   MS_TIME_DIFF(done_time,finalize_time));
746
#   endif
747
}
748
 
749
/* Externally callable routine to invoke full, stop-world collection */
750
# if defined(__STDC__) || defined(__cplusplus)
751
    int GC_try_to_collect(GC_stop_func stop_func)
752
# else
753
    int GC_try_to_collect(stop_func)
754
    GC_stop_func stop_func;
755
# endif
756
{
757
    int result;
758
    DCL_LOCK_STATE;
759
 
760
    if (GC_debugging_started) GC_print_all_smashed();
761
    GC_INVOKE_FINALIZERS();
762
    DISABLE_SIGNALS();
763
    LOCK();
764
    ENTER_GC();
765
    if (!GC_is_initialized) GC_init_inner();
766
    /* Minimize junk left in my registers */
767
      GC_noop(0,0,0,0,0,0);
768
    result = (int)GC_try_to_collect_inner(stop_func);
769
    EXIT_GC();
770
    UNLOCK();
771
    ENABLE_SIGNALS();
772
    if(result) {
773
        if (GC_debugging_started) GC_print_all_smashed();
774
        GC_INVOKE_FINALIZERS();
775
    }
776
    return(result);
777
}
778
 
779
void GC_gcollect GC_PROTO(())
780
{
781
    (void)GC_try_to_collect(GC_never_stop_func);
782
    if (GC_have_errors) GC_print_all_errors();
783
}
784
 
785
word GC_n_heap_sects = 0;        /* Number of sections currently in heap. */
786
 
787
/*
788
 * Use the chunk of memory starting at p of size bytes as part of the heap.
789
 * Assumes p is HBLKSIZE aligned, and bytes is a multiple of HBLKSIZE.
790
 */
791
void GC_add_to_heap(p, bytes)
792
struct hblk *p;
793
word bytes;
794
{
795
    word words;
796
    hdr * phdr;
797
 
798
    if (GC_n_heap_sects >= MAX_HEAP_SECTS) {
799
        ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");
800
    }
801
    phdr = GC_install_header(p);
802
    if (0 == phdr) {
803
        /* This is extremely unlikely. Can't add it.  This will         */
804
        /* almost certainly result in a 0 return from the allocator,    */
805
        /* which is entirely appropriate.                               */
806
        return;
807
    }
808
    GC_heap_sects[GC_n_heap_sects].hs_start = (ptr_t)p;
809
    GC_heap_sects[GC_n_heap_sects].hs_bytes = bytes;
810
    GC_n_heap_sects++;
811
    words = BYTES_TO_WORDS(bytes);
812
    phdr -> hb_sz = words;
813
    phdr -> hb_map = (unsigned char *)1;   /* A value != GC_invalid_map */
814
    phdr -> hb_flags = 0;
815
    GC_freehblk(p);
816
    GC_heapsize += bytes;
817
    if ((ptr_t)p <= (ptr_t)GC_least_plausible_heap_addr
818
        || GC_least_plausible_heap_addr == 0) {
819
        GC_least_plausible_heap_addr = (GC_PTR)((ptr_t)p - sizeof(word));
820
                /* Making it a little smaller than necessary prevents   */
821
                /* us from getting a false hit from the variable        */
822
                /* itself.  There's some unintentional reflection       */
823
                /* here.                                                */
824
    }
825
    if ((ptr_t)p + bytes >= (ptr_t)GC_greatest_plausible_heap_addr) {
826
        GC_greatest_plausible_heap_addr = (GC_PTR)((ptr_t)p + bytes);
827
    }
828
}
829
 
830
# if !defined(NO_DEBUGGING)
831
void GC_print_heap_sects()
832
{
833
    register unsigned i;
834
 
835
    GC_printf1("Total heap size: %lu\n", (unsigned long) GC_heapsize);
836
    for (i = 0; i < GC_n_heap_sects; i++) {
837
        unsigned long start = (unsigned long) GC_heap_sects[i].hs_start;
838
        unsigned long len = (unsigned long) GC_heap_sects[i].hs_bytes;
839
        struct hblk *h;
840
        unsigned nbl = 0;
841
 
842
        GC_printf3("Section %ld from 0x%lx to 0x%lx ", (unsigned long)i,
843
                   start, (unsigned long)(start + len));
844
        for (h = (struct hblk *)start; h < (struct hblk *)(start + len); h++) {
845
            if (GC_is_black_listed(h, HBLKSIZE)) nbl++;
846
        }
847
        GC_printf2("%lu/%lu blacklisted\n", (unsigned long)nbl,
848
                   (unsigned long)(len/HBLKSIZE));
849
    }
850
}
851
# endif
852
 
853
GC_PTR GC_least_plausible_heap_addr = (GC_PTR)ONES;
854
GC_PTR GC_greatest_plausible_heap_addr = 0;
855
 
856
ptr_t GC_max(x,y)
857
ptr_t x, y;
858
{
859
    return(x > y? x : y);
860
}
861
 
862
ptr_t GC_min(x,y)
863
ptr_t x, y;
864
{
865
    return(x < y? x : y);
866
}
867
 
868
# if defined(__STDC__) || defined(__cplusplus)
869
    void GC_set_max_heap_size(GC_word n)
870
# else
871
    void GC_set_max_heap_size(n)
872
    GC_word n;
873
# endif
874
{
875
    GC_max_heapsize = n;
876
}
877
 
878
GC_word GC_max_retries = 0;
879
 
880
/*
881
 * this explicitly increases the size of the heap.  It is used
882
 * internally, but may also be invoked from GC_expand_hp by the user.
883
 * The argument is in units of HBLKSIZE.
884
 * Tiny values of n are rounded up.
885
 * Returns FALSE on failure.
886
 */
887
GC_bool GC_expand_hp_inner(n)
888
word n;
889
{
890
    word bytes;
891
    struct hblk * space;
892
    word expansion_slop;        /* Number of bytes by which we expect the */
893
                                /* heap to expand soon.                   */
894
 
895
    if (n < MINHINCR) n = MINHINCR;
896
    bytes = n * HBLKSIZE;
897
    /* Make sure bytes is a multiple of GC_page_size */
898
      {
899
        word mask = GC_page_size - 1;
900
        bytes += mask;
901
        bytes &= ~mask;
902
      }
903
 
904
    if (GC_max_heapsize != 0 && GC_heapsize + bytes > GC_max_heapsize) {
905
        /* Exceeded self-imposed limit */
906
        return(FALSE);
907
    }
908
    space = GET_MEM(bytes);
909
    if( space == 0 ) {
910
#       ifdef CONDPRINT
911
          if (GC_print_stats) {
912
            GC_printf1("Failed to expand heap by %ld bytes\n",
913
                       (unsigned long)bytes);
914
          }
915
#       endif
916
        return(FALSE);
917
    }
918
#   ifdef CONDPRINT
919
      if (GC_print_stats) {
920
        GC_printf2("Increasing heap size by %lu after %lu allocated bytes\n",
921
                   (unsigned long)bytes,
922
                   (unsigned long)WORDS_TO_BYTES(GC_words_allocd));
923
#       ifdef UNDEFINED
924
          GC_printf1("Root size = %lu\n", GC_root_size);
925
          GC_print_block_list(); GC_print_hblkfreelist();
926
          GC_printf0("\n");
927
#       endif
928
      }
929
#   endif
930
    expansion_slop = WORDS_TO_BYTES(min_words_allocd()) + 4*MAXHINCR*HBLKSIZE;
931
    if (GC_last_heap_addr == 0 && !((word)space & SIGNB)
932
        || GC_last_heap_addr != 0 && GC_last_heap_addr < (ptr_t)space) {
933
        /* Assume the heap is growing up */
934
        GC_greatest_plausible_heap_addr =
935
            (GC_PTR)GC_max((ptr_t)GC_greatest_plausible_heap_addr,
936
                           (ptr_t)space + bytes + expansion_slop);
937
    } else {
938
        /* Heap is growing down */
939
        GC_least_plausible_heap_addr =
940
            (GC_PTR)GC_min((ptr_t)GC_least_plausible_heap_addr,
941
                           (ptr_t)space - expansion_slop);
942
    }
943
#   if defined(LARGE_CONFIG)
944
      if (((ptr_t)GC_greatest_plausible_heap_addr <= (ptr_t)space + bytes
945
           || (ptr_t)GC_least_plausible_heap_addr >= (ptr_t)space)
946
          && GC_heapsize > 0) {
947
        /* GC_add_to_heap will fix this, but ... */
948
        WARN("Too close to address space limit: blacklisting ineffective\n", 0);
949
      }
950
#   endif
951
    GC_prev_heap_addr = GC_last_heap_addr;
952
    GC_last_heap_addr = (ptr_t)space;
953
    GC_add_to_heap(space, bytes);
954
    /* Force GC before we are likely to allocate past expansion_slop */
955
      GC_collect_at_heapsize =
956
          GC_heapsize + expansion_slop - 2*MAXHINCR*HBLKSIZE;
957
#     if defined(LARGE_CONFIG)
958
        if (GC_collect_at_heapsize < GC_heapsize /* wrapped */)
959
          GC_collect_at_heapsize = (word)(-1);
960
#     endif
961
    return(TRUE);
962
}
963
 
964
/* Really returns a bool, but it's externally visible, so that's clumsy. */
965
/* Arguments is in bytes.                                               */
966
# if defined(__STDC__) || defined(__cplusplus)
967
  int GC_expand_hp(size_t bytes)
968
# else
969
  int GC_expand_hp(bytes)
970
  size_t bytes;
971
# endif
972
{
973
    int result;
974
    DCL_LOCK_STATE;
975
 
976
    DISABLE_SIGNALS();
977
    LOCK();
978
    if (!GC_is_initialized) GC_init_inner();
979
    result = (int)GC_expand_hp_inner(divHBLKSZ((word)bytes));
980
    if (result) GC_requested_heapsize += bytes;
981
    UNLOCK();
982
    ENABLE_SIGNALS();
983
    return(result);
984
}
985
 
986
unsigned GC_fail_count = 0;
987
                        /* How many consecutive GC/expansion failures?  */
988
                        /* Reset by GC_allochblk.                       */
989
 
990
GC_bool GC_collect_or_expand(needed_blocks, ignore_off_page)
991
word needed_blocks;
992
GC_bool ignore_off_page;
993
{
994
    if (!GC_incremental && !GC_dont_gc &&
995
        (GC_dont_expand && GC_words_allocd > 0 || GC_should_collect())) {
996
      GC_gcollect_inner();
997
    } else {
998
      word blocks_to_get = GC_heapsize/(HBLKSIZE*GC_free_space_divisor)
999
                           + needed_blocks;
1000
 
1001
      if (blocks_to_get > MAXHINCR) {
1002
          word slop;
1003
 
1004
          if (ignore_off_page) {
1005
              slop = 4;
1006
          } else {
1007
              slop = 2*divHBLKSZ(BL_LIMIT);
1008
              if (slop > needed_blocks) slop = needed_blocks;
1009
          }
1010
          if (needed_blocks + slop > MAXHINCR) {
1011
              blocks_to_get = needed_blocks + slop;
1012
          } else {
1013
              blocks_to_get = MAXHINCR;
1014
          }
1015
      }
1016
      if (!GC_expand_hp_inner(blocks_to_get)
1017
        && !GC_expand_hp_inner(needed_blocks)) {
1018
        if (GC_fail_count++ < GC_max_retries) {
1019
            WARN("Out of Memory!  Trying to continue ...\n", 0);
1020
            GC_gcollect_inner();
1021
        } else {
1022
#           if !defined(AMIGA) || !defined(GC_AMIGA_FASTALLOC)
1023
              WARN("Out of Memory!  Returning NIL!\n", 0);
1024
#           endif
1025
            return(FALSE);
1026
        }
1027
      } else {
1028
#         ifdef CONDPRINT
1029
            if (GC_fail_count && GC_print_stats) {
1030
              GC_printf0("Memory available again ...\n");
1031
            }
1032
#         endif
1033
      }
1034
    }
1035
    return(TRUE);
1036
}
1037
 
1038
/*
1039
 * Make sure the object free list for sz is not empty.
1040
 * Return a pointer to the first object on the free list.
1041
 * The object MUST BE REMOVED FROM THE FREE LIST BY THE CALLER.
1042
 * Assumes we hold the allocator lock and signals are disabled.
1043
 *
1044
 */
1045
ptr_t GC_allocobj(sz, kind)
1046
word sz;
1047
int kind;
1048
{
1049
    ptr_t * flh = &(GC_obj_kinds[kind].ok_freelist[sz]);
1050
    GC_bool tried_minor = FALSE;
1051
 
1052
    if (sz == 0) return(0);
1053
 
1054
    while (*flh == 0) {
1055
      ENTER_GC();
1056
      /* Do our share of marking work */
1057
        if(TRUE_INCREMENTAL) GC_collect_a_little_inner(1);
1058
      /* Sweep blocks for objects of this size */
1059
        GC_continue_reclaim(sz, kind);
1060
      EXIT_GC();
1061
      if (*flh == 0) {
1062
        GC_new_hblk(sz, kind);
1063
      }
1064
      if (*flh == 0) {
1065
        ENTER_GC();
1066
        if (GC_incremental && GC_time_limit == GC_TIME_UNLIMITED
1067
            && ! tried_minor ) {
1068
            GC_collect_a_little_inner(1);
1069
            tried_minor = TRUE;
1070
        } else {
1071
          if (!GC_collect_or_expand((word)1,FALSE)) {
1072
            EXIT_GC();
1073
            return(0);
1074
          }
1075
        }
1076
        EXIT_GC();
1077
      }
1078
    }
1079
    /* Successful allocation; reset failure count.      */
1080
    GC_fail_count = 0;
1081
 
1082
    return(*flh);
1083
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.