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

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

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