[BACK]Return to alloc.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / gc

Annotation of OpenXM_contrib2/asir2000/gc/alloc.c, Revision 1.6

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>