[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.2

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

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