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

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

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