Back to index

plt-scheme  4.2.1
reclaim.c
Go to the documentation of this file.
00001 /* 
00002  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
00003  * Copyright (c) 1991-1996 by Xerox Corporation.  All rights reserved.
00004  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
00005  * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
00006  *
00007  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00008  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00009  *
00010  * Permission is hereby granted to use or copy this program
00011  * for any purpose,  provided the above notices are retained on all copies.
00012  * Permission to modify the code and to distribute modified code is granted,
00013  * provided the above notices are retained, and a notice that the code was
00014  * modified is included with the above copyright notice.
00015  */
00016 
00017 #include <stdio.h>
00018 #include "private/gc_priv.h"
00019 
00020 signed_word GC_mem_found = 0;
00021                      /* Number of words of memory reclaimed     */
00022 
00023 #if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
00024   word GC_fl_builder_count = 0;
00025        /* Number of threads currently building free lists without     */
00026        /* holding GC lock.  It is not safe to collect if this is      */
00027        /* nonzero.                                             */
00028 #endif /* PARALLEL_MARK */
00029 
00030 /* We defer printing of leaked objects until we're done with the GC   */
00031 /* cycle, since the routine for printing objects needs to run outside */
00032 /* the collector, e.g. without the allocation lock.                   */
00033 #define MAX_LEAKED 40
00034 ptr_t GC_leaked[MAX_LEAKED];
00035 unsigned GC_n_leaked = 0;
00036 
00037 GC_bool GC_have_errors = FALSE;
00038 
00039 void GC_add_leaked(leaked)
00040 ptr_t leaked;
00041 {
00042     if (GC_n_leaked < MAX_LEAKED) {
00043       GC_have_errors = TRUE;
00044       GC_leaked[GC_n_leaked++] = leaked;
00045       /* Make sure it's not reclaimed this cycle */
00046         GC_set_mark_bit(leaked);
00047     }
00048 }
00049 
00050 static GC_bool printing_errors = FALSE;
00051 /* Print all objects on the list after printing any smashed objs.     */
00052 /* Clear both lists.                                           */
00053 void GC_print_all_errors ()
00054 {
00055     unsigned i;
00056 
00057     LOCK();
00058     if (printing_errors) {
00059        UNLOCK();
00060        return;
00061     }
00062     printing_errors = TRUE;
00063     UNLOCK();
00064     if (GC_debugging_started) GC_print_all_smashed();
00065     for (i = 0; i < GC_n_leaked; ++i) {
00066        ptr_t p = GC_leaked[i];
00067        if (HDR(p) -> hb_obj_kind == PTRFREE) {
00068            GC_err_printf0("Leaked atomic object at ");
00069        } else {
00070            GC_err_printf0("Leaked composite object at ");
00071        }
00072        GC_print_heap_obj(p);
00073        GC_err_printf0("\n");
00074        GC_free(p);
00075        GC_leaked[i] = 0;
00076     }
00077     GC_n_leaked = 0;
00078     printing_errors = FALSE;
00079 }
00080 
00081 
00082 #   define FOUND_FREE(hblk, word_no) \
00083       { \
00084          GC_add_leaked((ptr_t)hblk + WORDS_TO_BYTES(word_no)); \
00085       }
00086 
00087 /*
00088  * reclaim phase
00089  *
00090  */
00091 
00092 
00093 /*
00094  * Test whether a block is completely empty, i.e. contains no marked
00095  * objects.  This does not require the block to be in physical
00096  * memory.
00097  */
00098  
00099 GC_bool GC_block_empty(hhdr)
00100 register hdr * hhdr;
00101 {
00102     /* We treat hb_marks as an array of words here, even if it is     */
00103     /* actually an array of bytes.  Since we only check for zero, there      */
00104     /* are no endian-ness issues.                              */
00105     register word *p = (word *)(&(hhdr -> hb_marks[0]));
00106     register word * plim =
00107            (word *)(&(hhdr -> hb_marks[MARK_BITS_SZ]));
00108     while (p < plim) {
00109        if (*p++) return(FALSE);
00110     }
00111     return(TRUE);
00112 }
00113 
00114 /* The following functions sometimes return a DONT_KNOW value. */
00115 #define DONT_KNOW  2
00116 
00117 #ifdef SMALL_CONFIG
00118 # define GC_block_nearly_full1(hhdr, pat1) DONT_KNOW
00119 # define GC_block_nearly_full3(hhdr, pat1, pat2) DONT_KNOW
00120 # define GC_block_nearly_full(hhdr) DONT_KNOW
00121 #endif
00122 
00123 #if !defined(SMALL_CONFIG) && defined(USE_MARK_BYTES)
00124 
00125 # define GC_block_nearly_full1(hhdr, pat1) GC_block_nearly_full(hhdr)
00126 # define GC_block_nearly_full3(hhdr, pat1, pat2) GC_block_nearly_full(hhdr)
00127 
00128  
00129 GC_bool GC_block_nearly_full(hhdr)
00130 register hdr * hhdr;
00131 {
00132     /* We again treat hb_marks as an array of words, even though it   */
00133     /* isn't.  We first sum up all the words, resulting in a word     */
00134     /* containing 4 or 8 separate partial sums.                */
00135     /* We then sum the bytes in the word of partial sums.             */
00136     /* This is still endian independant.  This fails if the partial   */
00137     /* sums can overflow.                                      */
00138 #   if (BYTES_TO_WORDS(MARK_BITS_SZ)) >= 256
00139        --> potential overflow; fix the code
00140 #   endif
00141     register word *p = (word *)(&(hhdr -> hb_marks[0]));
00142     register word * plim =
00143            (word *)(&(hhdr -> hb_marks[MARK_BITS_SZ]));
00144     word sum_vector = 0;
00145     unsigned sum;
00146     while (p < plim) {
00147        sum_vector += *p;
00148        ++p;
00149     }
00150     sum = 0;
00151     while (sum_vector > 0) {
00152        sum += sum_vector & 0xff;
00153        sum_vector >>= 8;
00154     }
00155     return (sum > BYTES_TO_WORDS(7*HBLKSIZE/8)/(hhdr -> hb_sz));
00156 }
00157 #endif  /* USE_MARK_BYTES */
00158 
00159 #if !defined(SMALL_CONFIG) && !defined(USE_MARK_BYTES)
00160 
00161 /*
00162  * Test whether nearly all of the mark words consist of the same
00163  * repeating pattern.
00164  */
00165 #define FULL_THRESHOLD (MARK_BITS_SZ/16)
00166 
00167 GC_bool GC_block_nearly_full1(hhdr, pat1)
00168 hdr *hhdr;
00169 word pat1;
00170 {
00171     unsigned i;
00172     unsigned misses = 0;
00173     GC_ASSERT((MARK_BITS_SZ & 1) == 0);
00174     for (i = 0; i < MARK_BITS_SZ; ++i) {
00175        if ((hhdr -> hb_marks[i] | ~pat1) != ONES) {
00176            if (++misses > FULL_THRESHOLD) return FALSE;
00177        }
00178     }
00179     return TRUE;
00180 }
00181 
00182 /*
00183  * Test whether the same repeating 3 word pattern occurs in nearly
00184  * all the mark bit slots.
00185  * This is used as a heuristic, so we're a bit sloppy and ignore
00186  * the last one or two words.
00187  */
00188 GC_bool GC_block_nearly_full3(hhdr, pat1, pat2, pat3)
00189 hdr *hhdr;
00190 word pat1, pat2, pat3;
00191 {
00192     unsigned i;
00193     unsigned misses = 0;
00194 
00195     if (MARK_BITS_SZ < 4) {
00196       return DONT_KNOW;
00197     }
00198     for (i = 0; i < MARK_BITS_SZ - 2; i += 3) {
00199        if ((hhdr -> hb_marks[i] | ~pat1) != ONES) {
00200            if (++misses > FULL_THRESHOLD) return FALSE;
00201        }
00202        if ((hhdr -> hb_marks[i+1] | ~pat2) != ONES) {
00203            if (++misses > FULL_THRESHOLD) return FALSE;
00204        }
00205        if ((hhdr -> hb_marks[i+2] | ~pat3) != ONES) {
00206            if (++misses > FULL_THRESHOLD) return FALSE;
00207        }
00208     }
00209     return TRUE;
00210 }
00211 
00212 /* Check whether a small object block is nearly full by looking at only */
00213 /* the mark bits.                                              */
00214 /* We manually precomputed the mark bit patterns that need to be      */
00215 /* checked for, and we give up on the ones that are unlikely to occur,       */
00216 /* or have period > 3.                                                */
00217 /* This would be a lot easier with a mark bit per object instead of per      */
00218 /* word, but that would rewuire computing object numbers in the mark  */
00219 /* loop, which would require different data structures ...            */
00220 GC_bool GC_block_nearly_full(hhdr)
00221 hdr *hhdr;
00222 {
00223     int sz = hhdr -> hb_sz;
00224 
00225 #   if CPP_WORDSZ != 32 && CPP_WORDSZ != 64
00226       return DONT_KNOW;     /* Shouldn't be used in any standard config.     */
00227 #   endif
00228 #   if CPP_WORDSZ == 32
00229       switch(sz) {
00230         case 1:
00231          return GC_block_nearly_full1(hhdr, 0xffffffffl);
00232        case 2:
00233          return GC_block_nearly_full1(hhdr, 0x55555555l);
00234        case 4:
00235          return GC_block_nearly_full1(hhdr, 0x11111111l);
00236        case 6:
00237          return GC_block_nearly_full3(hhdr, 0x41041041l,
00238                                          0x10410410l,
00239                                           0x04104104l);
00240        case 8:
00241          return GC_block_nearly_full1(hhdr, 0x01010101l);
00242        case 12:
00243          return GC_block_nearly_full3(hhdr, 0x01001001l,
00244                                          0x10010010l,
00245                                           0x00100100l);
00246        case 16:
00247          return GC_block_nearly_full1(hhdr, 0x00010001l);
00248        case 32:
00249          return GC_block_nearly_full1(hhdr, 0x00000001l);
00250        default:
00251          return DONT_KNOW;
00252       }
00253 #   endif
00254 #   if CPP_WORDSZ == 64
00255       switch(sz) {
00256         case 1:
00257          return GC_block_nearly_full1(hhdr, 0xffffffffffffffffl);
00258        case 2:
00259          return GC_block_nearly_full1(hhdr, 0x5555555555555555l);
00260        case 4:
00261          return GC_block_nearly_full1(hhdr, 0x1111111111111111l);
00262        case 6:
00263          return GC_block_nearly_full3(hhdr, 0x1041041041041041l,
00264                                           0x4104104104104104l,
00265                                             0x0410410410410410l);
00266        case 8:
00267          return GC_block_nearly_full1(hhdr, 0x0101010101010101l);
00268        case 12:
00269          return GC_block_nearly_full3(hhdr, 0x1001001001001001l,
00270                                           0x0100100100100100l,
00271                                             0x0010010010010010l);
00272        case 16:
00273          return GC_block_nearly_full1(hhdr, 0x0001000100010001l);
00274        case 32:
00275          return GC_block_nearly_full1(hhdr, 0x0000000100000001l);
00276        default:
00277          return DONT_KNOW;
00278       }
00279 #   endif
00280 }
00281 #endif /* !SMALL_CONFIG  && !USE_MARK_BYTES */
00282 
00283 /* We keep track of reclaimed memory if we are either asked to, or    */
00284 /* we are using the parallel marker.  In the latter case, we assume   */
00285 /* that most allocation goes through GC_malloc_many for scalability.  */
00286 /* GC_malloc_many needs the count anyway.                      */
00287 # if defined(GATHERSTATS) || defined(PARALLEL_MARK)
00288 #   define INCR_WORDS(sz) n_words_found += (sz)
00289 #   define COUNT_PARAM , count
00290 #   define COUNT_ARG , count
00291 #   define COUNT_DECL signed_word * count;
00292 #   define NWORDS_DECL signed_word n_words_found = 0;
00293 #   define COUNT_UPDATE *count += n_words_found;
00294 #   define MEM_FOUND_ADDR , &GC_mem_found
00295 # else
00296 #   define INCR_WORDS(sz)
00297 #   define COUNT_PARAM
00298 #   define COUNT_ARG
00299 #   define COUNT_DECL
00300 #   define NWORDS_DECL
00301 #   define COUNT_UPDATE
00302 #   define MEM_FOUND_ADDR
00303 # endif
00304 /*
00305  * Restore unmarked small objects in h of size sz to the object
00306  * free list.  Returns the new list.
00307  * Clears unmarked objects.
00308  */
00309 /*ARGSUSED*/
00310 ptr_t GC_reclaim_clear(hbp, hhdr, sz, list COUNT_PARAM)
00311 register struct hblk *hbp;  /* ptr to current heap block              */
00312 register hdr * hhdr;
00313 register ptr_t list;
00314 register word sz;
00315 COUNT_DECL
00316 {
00317     register int word_no;
00318     register word *p, *q, *plim;
00319     NWORDS_DECL
00320     
00321     GC_ASSERT(hhdr == GC_find_header((ptr_t)hbp));
00322     p = (word *)(hbp->hb_body);
00323     word_no = 0;
00324     plim = (word *)((((word)hbp) + HBLKSIZE)
00325                  - WORDS_TO_BYTES(sz));
00326 
00327     /* go through all words in block */
00328        while( p <= plim )  {
00329            if( mark_bit_from_hdr(hhdr, word_no) ) {
00330               p += sz;
00331            } else {
00332               INCR_WORDS(sz);
00333               /* object is available - put on list */
00334                   obj_link(p) = list;
00335                   list = ((ptr_t)p);
00336               /* Clear object, advance p to next object in the process */
00337                   q = p + sz;
00338 #                 ifdef USE_MARK_BYTES
00339                     GC_ASSERT(!(sz & 1)
00340                             && !((word)p & (2 * sizeof(word) - 1)));
00341                     p[1] = 0;
00342                       p += 2;
00343                       while (p < q) {
00344                      CLEAR_DOUBLE(p);
00345                      p += 2;
00346                     }
00347 #                 else
00348                       p++; /* Skip link field */
00349                       while (p < q) {
00350                      *p++ = 0;
00351                     }
00352 #                 endif
00353            }
00354            word_no += sz;
00355        }
00356     COUNT_UPDATE
00357     return(list);
00358 }
00359 
00360 #if !defined(SMALL_CONFIG) && !defined(USE_MARK_BYTES)
00361 
00362 /*
00363  * A special case for 2 word composite objects (e.g. cons cells):
00364  */
00365 /*ARGSUSED*/
00366 ptr_t GC_reclaim_clear2(hbp, hhdr, list COUNT_PARAM)
00367 register struct hblk *hbp;  /* ptr to current heap block              */
00368 hdr * hhdr;
00369 register ptr_t list;
00370 COUNT_DECL
00371 {
00372     register word * mark_word_addr = &(hhdr->hb_marks[0]);
00373     register word *p, *plim;
00374     register word mark_word;
00375     register int i;
00376     NWORDS_DECL
00377 #   define DO_OBJ(start_displ) \
00378        if (!(mark_word & ((word)1 << start_displ))) { \
00379            p[start_displ] = (word)list; \
00380            list = (ptr_t)(p+start_displ); \
00381            p[start_displ+1] = 0; \
00382            INCR_WORDS(2); \
00383        }
00384     
00385     p = (word *)(hbp->hb_body);
00386     plim = (word *)(((word)hbp) + HBLKSIZE);
00387 
00388     /* go through all words in block */
00389        while( p < plim )  {
00390            mark_word = *mark_word_addr++;
00391            for (i = 0; i < WORDSZ; i += 8) {
00392               DO_OBJ(0);
00393               DO_OBJ(2);
00394               DO_OBJ(4);
00395               DO_OBJ(6);
00396               p += 8;
00397               mark_word >>= 8;
00398            }
00399        }              
00400     COUNT_UPDATE
00401     return(list);
00402 #   undef DO_OBJ
00403 }
00404 
00405 /*
00406  * Another special case for 4 word composite objects:
00407  */
00408 /*ARGSUSED*/
00409 ptr_t GC_reclaim_clear4(hbp, hhdr, list COUNT_PARAM)
00410 register struct hblk *hbp;  /* ptr to current heap block              */
00411 hdr * hhdr;
00412 register ptr_t list;
00413 COUNT_DECL
00414 {
00415     register word * mark_word_addr = &(hhdr->hb_marks[0]);
00416     register word *p, *plim;
00417     register word mark_word;
00418     NWORDS_DECL
00419 #   define DO_OBJ(start_displ) \
00420        if (!(mark_word & ((word)1 << start_displ))) { \
00421            p[start_displ] = (word)list; \
00422            list = (ptr_t)(p+start_displ); \
00423            p[start_displ+1] = 0; \
00424            CLEAR_DOUBLE(p + start_displ + 2); \
00425            INCR_WORDS(4); \
00426        }
00427     
00428     p = (word *)(hbp->hb_body);
00429     plim = (word *)(((word)hbp) + HBLKSIZE);
00430 
00431     /* go through all words in block */
00432        while( p < plim )  {
00433            mark_word = *mark_word_addr++;
00434            DO_OBJ(0);
00435            DO_OBJ(4);
00436            DO_OBJ(8);
00437            DO_OBJ(12);
00438            DO_OBJ(16);
00439            DO_OBJ(20);
00440            DO_OBJ(24);
00441            DO_OBJ(28);
00442 #          if CPP_WORDSZ == 64
00443              DO_OBJ(32);
00444              DO_OBJ(36);
00445              DO_OBJ(40);
00446              DO_OBJ(44);
00447              DO_OBJ(48);
00448              DO_OBJ(52);
00449              DO_OBJ(56);
00450              DO_OBJ(60);
00451 #          endif
00452            p += WORDSZ;
00453        }              
00454     COUNT_UPDATE
00455     return(list);
00456 #   undef DO_OBJ
00457 }
00458 
00459 #endif /* !SMALL_CONFIG && !USE_MARK_BYTES */
00460 
00461 /* The same thing, but don't clear objects: */
00462 /*ARGSUSED*/
00463 ptr_t GC_reclaim_uninit(hbp, hhdr, sz, list COUNT_PARAM)
00464 register struct hblk *hbp;  /* ptr to current heap block              */
00465 register hdr * hhdr;
00466 register ptr_t list;
00467 register word sz;
00468 COUNT_DECL
00469 {
00470     register int word_no = 0;
00471     register word *p, *plim;
00472     NWORDS_DECL
00473     
00474     p = (word *)(hbp->hb_body);
00475     plim = (word *)((((word)hbp) + HBLKSIZE)
00476                  - WORDS_TO_BYTES(sz));
00477 
00478     /* go through all words in block */
00479        while( p <= plim )  {
00480            if( !mark_bit_from_hdr(hhdr, word_no) ) {
00481               INCR_WORDS(sz);
00482               /* object is available - put on list */
00483                   obj_link(p) = list;
00484                   list = ((ptr_t)p);
00485            }
00486            p += sz;
00487            word_no += sz;
00488        }
00489     COUNT_UPDATE
00490     return(list);
00491 }
00492 
00493 /* Don't really reclaim objects, just check for unmarked ones: */
00494 /*ARGSUSED*/
00495 void GC_reclaim_check(hbp, hhdr, sz)
00496 register struct hblk *hbp;  /* ptr to current heap block              */
00497 register hdr * hhdr;
00498 register word sz;
00499 {
00500     register int word_no = 0;
00501     register word *p, *plim;
00502 #   ifdef GATHERSTATS
00503         register int n_words_found = 0;
00504 #   endif
00505     
00506     p = (word *)(hbp->hb_body);
00507     plim = (word *)((((word)hbp) + HBLKSIZE)
00508                  - WORDS_TO_BYTES(sz));
00509 
00510     /* go through all words in block */
00511        while( p <= plim )  {
00512            if( !mark_bit_from_hdr(hhdr, word_no) ) {
00513               FOUND_FREE(hbp, word_no);
00514            }
00515            p += sz;
00516            word_no += sz;
00517        }
00518 }
00519 
00520 #if !defined(SMALL_CONFIG) && !defined(USE_MARK_BYTES)
00521 /*
00522  * Another special case for 2 word atomic objects:
00523  */
00524 /*ARGSUSED*/
00525 ptr_t GC_reclaim_uninit2(hbp, hhdr, list COUNT_PARAM)
00526 register struct hblk *hbp;  /* ptr to current heap block              */
00527 hdr * hhdr;
00528 register ptr_t list;
00529 COUNT_DECL
00530 {
00531     register word * mark_word_addr = &(hhdr->hb_marks[0]);
00532     register word *p, *plim;
00533     register word mark_word;
00534     register int i;
00535     NWORDS_DECL
00536 #   define DO_OBJ(start_displ) \
00537        if (!(mark_word & ((word)1 << start_displ))) { \
00538            p[start_displ] = (word)list; \
00539            list = (ptr_t)(p+start_displ); \
00540            INCR_WORDS(2); \
00541        }
00542     
00543     p = (word *)(hbp->hb_body);
00544     plim = (word *)(((word)hbp) + HBLKSIZE);
00545 
00546     /* go through all words in block */
00547        while( p < plim )  {
00548            mark_word = *mark_word_addr++;
00549            for (i = 0; i < WORDSZ; i += 8) {
00550               DO_OBJ(0);
00551               DO_OBJ(2);
00552               DO_OBJ(4);
00553               DO_OBJ(6);
00554               p += 8;
00555               mark_word >>= 8;
00556            }
00557        }              
00558     COUNT_UPDATE
00559     return(list);
00560 #   undef DO_OBJ
00561 }
00562 
00563 /*
00564  * Another special case for 4 word atomic objects:
00565  */
00566 /*ARGSUSED*/
00567 ptr_t GC_reclaim_uninit4(hbp, hhdr, list COUNT_PARAM)
00568 register struct hblk *hbp;  /* ptr to current heap block              */
00569 hdr * hhdr;
00570 register ptr_t list;
00571 COUNT_DECL
00572 {
00573     register word * mark_word_addr = &(hhdr->hb_marks[0]);
00574     register word *p, *plim;
00575     register word mark_word;
00576     NWORDS_DECL
00577 #   define DO_OBJ(start_displ) \
00578        if (!(mark_word & ((word)1 << start_displ))) { \
00579            p[start_displ] = (word)list; \
00580            list = (ptr_t)(p+start_displ); \
00581            INCR_WORDS(4); \
00582        }
00583     
00584     p = (word *)(hbp->hb_body);
00585     plim = (word *)(((word)hbp) + HBLKSIZE);
00586 
00587     /* go through all words in block */
00588        while( p < plim )  {
00589            mark_word = *mark_word_addr++;
00590            DO_OBJ(0);
00591            DO_OBJ(4);
00592            DO_OBJ(8);
00593            DO_OBJ(12);
00594            DO_OBJ(16);
00595            DO_OBJ(20);
00596            DO_OBJ(24);
00597            DO_OBJ(28);
00598 #          if CPP_WORDSZ == 64
00599              DO_OBJ(32);
00600              DO_OBJ(36);
00601              DO_OBJ(40);
00602              DO_OBJ(44);
00603              DO_OBJ(48);
00604              DO_OBJ(52);
00605              DO_OBJ(56);
00606              DO_OBJ(60);
00607 #          endif
00608            p += WORDSZ;
00609        }              
00610     COUNT_UPDATE
00611     return(list);
00612 #   undef DO_OBJ
00613 }
00614 
00615 /* Finally the one word case, which never requires any clearing: */
00616 /*ARGSUSED*/
00617 ptr_t GC_reclaim1(hbp, hhdr, list COUNT_PARAM)
00618 register struct hblk *hbp;  /* ptr to current heap block              */
00619 hdr * hhdr;
00620 register ptr_t list;
00621 COUNT_DECL
00622 {
00623     register word * mark_word_addr = &(hhdr->hb_marks[0]);
00624     register word *p, *plim;
00625     register word mark_word;
00626     register int i;
00627     NWORDS_DECL
00628 #   define DO_OBJ(start_displ) \
00629        if (!(mark_word & ((word)1 << start_displ))) { \
00630            p[start_displ] = (word)list; \
00631            list = (ptr_t)(p+start_displ); \
00632            INCR_WORDS(1); \
00633        }
00634     
00635     p = (word *)(hbp->hb_body);
00636     plim = (word *)(((word)hbp) + HBLKSIZE);
00637 
00638     /* go through all words in block */
00639        while( p < plim )  {
00640            mark_word = *mark_word_addr++;
00641            for (i = 0; i < WORDSZ; i += 4) {
00642               DO_OBJ(0);
00643               DO_OBJ(1);
00644               DO_OBJ(2);
00645               DO_OBJ(3);
00646               p += 4;
00647               mark_word >>= 4;
00648            }
00649        }              
00650     COUNT_UPDATE
00651     return(list);
00652 #   undef DO_OBJ
00653 }
00654 
00655 #endif /* !SMALL_CONFIG && !USE_MARK_BYTES */
00656 
00657 /*
00658  * Generic procedure to rebuild a free list in hbp.
00659  * Also called directly from GC_malloc_many.
00660  */
00661 ptr_t GC_reclaim_generic(hbp, hhdr, sz, init, list COUNT_PARAM)
00662 struct hblk *hbp;    /* ptr to current heap block              */
00663 hdr * hhdr;
00664 GC_bool init;
00665 ptr_t list;
00666 word sz;
00667 COUNT_DECL
00668 {
00669     ptr_t result = list;
00670 
00671     GC_ASSERT(GC_find_header((ptr_t)hbp) == hhdr);
00672     GC_remove_protection(hbp, 1, (hhdr)->hb_descr == 0 /* Pointer-free? */);
00673     if (init) {
00674       switch(sz) {
00675 #      if !defined(SMALL_CONFIG) && !defined(USE_MARK_BYTES)
00676         case 1:
00677            /* We now issue the hint even if GC_nearly_full returned   */
00678            /* DONT_KNOW.                                       */
00679             result = GC_reclaim1(hbp, hhdr, list COUNT_ARG);
00680             break;
00681         case 2:
00682             result = GC_reclaim_clear2(hbp, hhdr, list COUNT_ARG);
00683             break;
00684         case 4:
00685             result = GC_reclaim_clear4(hbp, hhdr, list COUNT_ARG);
00686             break;
00687 #      endif /* !SMALL_CONFIG && !USE_MARK_BYTES */
00688         default:
00689             result = GC_reclaim_clear(hbp, hhdr, sz, list COUNT_ARG);
00690             break;
00691       }
00692     } else {
00693       GC_ASSERT((hhdr)->hb_descr == 0 /* Pointer-free block */);
00694       switch(sz) {
00695 #      if !defined(SMALL_CONFIG) && !defined(USE_MARK_BYTES)
00696         case 1:
00697             result = GC_reclaim1(hbp, hhdr, list COUNT_ARG);
00698             break;
00699         case 2:
00700             result = GC_reclaim_uninit2(hbp, hhdr, list COUNT_ARG);
00701             break;
00702         case 4:
00703             result = GC_reclaim_uninit4(hbp, hhdr, list COUNT_ARG);
00704             break;
00705 #      endif /* !SMALL_CONFIG && !USE_MARK_BYTES */
00706         default:
00707             result = GC_reclaim_uninit(hbp, hhdr, sz, list COUNT_ARG);
00708             break;
00709       }
00710     } 
00711     if (IS_UNCOLLECTABLE(hhdr -> hb_obj_kind)) GC_set_hdr_marks(hhdr);
00712     return result;
00713 }
00714 
00715 /*
00716  * Restore unmarked small objects in the block pointed to by hbp
00717  * to the appropriate object free list.
00718  * If entirely empty blocks are to be completely deallocated, then
00719  * caller should perform that check.
00720  */
00721 void GC_reclaim_small_nonempty_block(hbp, report_if_found COUNT_PARAM)
00722 register struct hblk *hbp;  /* ptr to current heap block              */
00723 int report_if_found;        /* Abort if a reclaimable object is found */
00724 COUNT_DECL
00725 {
00726     hdr *hhdr = HDR(hbp);
00727     word sz = hhdr -> hb_sz;
00728     int kind = hhdr -> hb_obj_kind;
00729     struct obj_kind * ok = &GC_obj_kinds[kind];
00730     ptr_t * flh = &(ok -> ok_freelist[sz]);
00731     
00732     hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
00733 
00734     if (report_if_found) {
00735        GC_reclaim_check(hbp, hhdr, sz);
00736     } else {
00737         *flh = GC_reclaim_generic(hbp, hhdr, sz,
00738                               (ok -> ok_init || GC_debugging_started),
00739                               *flh MEM_FOUND_ADDR);
00740     }
00741 }
00742 
00743 /*
00744  * Restore an unmarked large object or an entirely empty blocks of small objects
00745  * to the heap block free list.
00746  * Otherwise enqueue the block for later processing
00747  * by GC_reclaim_small_nonempty_block.
00748  * If report_if_found is TRUE, then process any block immediately, and
00749  * simply report free objects; do not actually reclaim them.
00750  */
00751 # if defined(__STDC__) || defined(__cplusplus)
00752     void GC_reclaim_block(register struct hblk *hbp, word report_if_found)
00753 # else
00754     void GC_reclaim_block(hbp, report_if_found)
00755     register struct hblk *hbp;     /* ptr to current heap block              */
00756     word report_if_found;   /* Abort if a reclaimable object is found */
00757 # endif
00758 {
00759     register hdr * hhdr;
00760     register word sz;              /* size of objects in current block       */
00761     register struct obj_kind * ok;
00762     struct hblk ** rlh;
00763 
00764     hhdr = HDR(hbp);
00765     sz = hhdr -> hb_sz;
00766     ok = &GC_obj_kinds[hhdr -> hb_obj_kind];
00767 
00768     if( sz > MAXOBJSZ ) {  /* 1 big object */
00769         if( !mark_bit_from_hdr(hhdr, 0) ) {
00770            if (report_if_found) {
00771              FOUND_FREE(hbp, 0);
00772            } else {
00773              word blocks = OBJ_SZ_TO_BLOCKS(sz);
00774              if (blocks > 1) {
00775                GC_large_allocd_bytes -= blocks * HBLKSIZE;
00776              }
00777 #            ifdef GATHERSTATS
00778                GC_mem_found += sz;
00779 #            endif
00780              GC_freehblk(hbp);
00781            }
00782        }
00783     } else {
00784         GC_bool empty = GC_block_empty(hhdr);
00785         if (report_if_found) {
00786          GC_reclaim_small_nonempty_block(hbp, (int)report_if_found
00787                                      MEM_FOUND_ADDR);
00788         } else if (empty) {
00789 #        ifdef GATHERSTATS
00790             GC_mem_found += BYTES_TO_WORDS(HBLKSIZE);
00791 #        endif
00792           GC_freehblk(hbp);
00793         } else if (TRUE != GC_block_nearly_full(hhdr)){
00794           /* group of smaller objects, enqueue the real work */
00795           rlh = &(ok -> ok_reclaim_list[sz]);
00796           hhdr -> hb_next = *rlh;
00797           *rlh = hbp;
00798         } /* else not worth salvaging. */
00799        /* We used to do the nearly_full check later, but we    */
00800        /* already have the right cache context here.  Also     */
00801        /* doing it here avoids some silly lock contention in   */
00802        /* GC_malloc_many.                               */
00803     }
00804 }
00805 
00806 #if !defined(NO_DEBUGGING)
00807 /* Routines to gather and print heap block info  */
00808 /* intended for debugging.  Otherwise should be called  */
00809 /* with lock.                                    */
00810 
00811 struct Print_stats
00812 {
00813        size_t number_of_blocks;
00814        size_t total_bytes;
00815 };
00816 
00817 #ifdef USE_MARK_BYTES
00818 
00819 /* Return the number of set mark bits in the given header      */
00820 int GC_n_set_marks(hhdr)
00821 hdr * hhdr;
00822 {
00823     register int result = 0;
00824     register int i;
00825     
00826     for (i = 0; i < MARK_BITS_SZ; i++) {
00827         result += hhdr -> hb_marks[i];
00828     }
00829     return(result);
00830 }
00831 
00832 #else
00833 
00834 /* Number of set bits in a word.  Not performance critical.    */
00835 static int set_bits(n)
00836 word n;
00837 {
00838     register word m = n;
00839     register int result = 0;
00840     
00841     while (m > 0) {
00842        if (m & 1) result++;
00843        m >>= 1;
00844     }
00845     return(result);
00846 }
00847 
00848 /* Return the number of set mark bits in the given header      */
00849 int GC_n_set_marks(hhdr)
00850 hdr * hhdr;
00851 {
00852     register int result = 0;
00853     register int i;
00854     
00855     for (i = 0; i < MARK_BITS_SZ; i++) {
00856         result += set_bits(hhdr -> hb_marks[i]);
00857     }
00858     return(result);
00859 }
00860 
00861 #endif /* !USE_MARK_BYTES  */
00862 
00863 /*ARGSUSED*/
00864 # if defined(__STDC__) || defined(__cplusplus)
00865     void GC_print_block_descr(struct hblk *h, word dummy)
00866 # else
00867     void GC_print_block_descr(h, dummy)
00868     struct hblk *h;
00869     word dummy;
00870 # endif
00871 {
00872     register hdr * hhdr = HDR(h);
00873     register size_t bytes = WORDS_TO_BYTES(hhdr -> hb_sz);
00874     struct Print_stats *ps;
00875     
00876     GC_printf3("(%lu:%lu,%lu)", (unsigned long)(hhdr -> hb_obj_kind),
00877                              (unsigned long)bytes,
00878                              (unsigned long)(GC_n_set_marks(hhdr)));
00879     bytes += HBLKSIZE-1;
00880     bytes &= ~(HBLKSIZE-1);
00881 
00882     ps = (struct Print_stats *)dummy;
00883     ps->total_bytes += bytes;
00884     ps->number_of_blocks++;
00885 }
00886 
00887 void GC_print_block_list()
00888 {
00889     struct Print_stats pstats;
00890 
00891     GC_printf1("(kind(0=ptrfree,1=normal,2=unc.,%lu=stubborn):size_in_bytes, #_marks_set)\n", STUBBORN);
00892     pstats.number_of_blocks = 0;
00893     pstats.total_bytes = 0;
00894     GC_apply_to_all_blocks(GC_print_block_descr, (word)&pstats);
00895     GC_printf2("\nblocks = %lu, bytes = %lu\n",
00896               (unsigned long)pstats.number_of_blocks,
00897               (unsigned long)pstats.total_bytes);
00898 }
00899 
00900 #endif /* NO_DEBUGGING */
00901 
00902 /*
00903  * Clear all obj_link pointers in the list of free objects *flp.
00904  * Clear *flp.
00905  * This must be done before dropping a list of free gcj-style objects,
00906  * since may otherwise end up with dangling "descriptor" pointers.
00907  * It may help for other pointer-containing objects.
00908  */
00909 void GC_clear_fl_links(flp)
00910 ptr_t *flp;
00911 {
00912     ptr_t next = *flp;
00913 
00914     while (0 != next) {
00915        *flp = 0;
00916        flp = &(obj_link(next));
00917        next = *flp;
00918     }
00919 }
00920 
00921 /*
00922  * Perform GC_reclaim_block on the entire heap, after first clearing
00923  * small object free lists (if we are not just looking for leaks).
00924  */
00925 void GC_start_reclaim(report_if_found)
00926 int report_if_found;        /* Abort if a GC_reclaimable object is found */
00927 {
00928     int kind;
00929     
00930 #   if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
00931       GC_ASSERT(0 == GC_fl_builder_count);
00932 #   endif
00933     /* Clear reclaim- and free-lists */
00934       for (kind = 0; kind < GC_n_kinds; kind++) {
00935         ptr_t *fop;
00936         ptr_t *lim;
00937         struct hblk ** rlp;
00938         struct hblk ** rlim;
00939         struct hblk ** rlist = GC_obj_kinds[kind].ok_reclaim_list;
00940        GC_bool should_clobber = (GC_obj_kinds[kind].ok_descriptor != 0);
00941         
00942         if (rlist == 0) continue;  /* This kind not used.      */
00943         if (!report_if_found) {
00944             lim = &(GC_obj_kinds[kind].ok_freelist[MAXOBJSZ+1]);
00945            for( fop = GC_obj_kinds[kind].ok_freelist; fop < lim; fop++ ) {
00946              if (*fop != 0) {
00947               if (should_clobber) {
00948                 GC_clear_fl_links(fop);
00949               } else {
00950                  *fop = 0;
00951               }
00952              }
00953            }
00954        } /* otherwise free list objects are marked,     */
00955          /* and its safe to leave them                  */
00956        rlim = rlist + MAXOBJSZ+1;
00957        for( rlp = rlist; rlp < rlim; rlp++ ) {
00958            *rlp = 0;
00959        }
00960       }
00961     
00962 #   ifdef PRINTBLOCKS
00963         GC_printf0("GC_reclaim: current block sizes:\n");
00964         GC_print_block_list();
00965 #   endif
00966 
00967   /* Go through all heap blocks (in hblklist) and reclaim unmarked objects */
00968   /* or enqueue the block for later processing.                          */
00969     GC_apply_to_all_blocks(GC_reclaim_block, (word)report_if_found);
00970 
00971 # ifdef EAGER_SWEEP
00972     /* This is a very stupid thing to do.  We make it possible anyway,       */
00973     /* so that you can convince yourself that it really is very stupid.      */
00974     GC_reclaim_all((GC_stop_func)0, FALSE);
00975 # endif
00976 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
00977     GC_ASSERT(0 == GC_fl_builder_count);
00978 # endif
00979     
00980 }
00981 
00982 /*
00983  * Sweep blocks of the indicated object size and kind until either the
00984  * appropriate free list is nonempty, or there are no more blocks to
00985  * sweep.
00986  */
00987 void GC_continue_reclaim(sz, kind)
00988 word sz;      /* words */
00989 int kind;
00990 {
00991     register hdr * hhdr;
00992     register struct hblk * hbp;
00993     register struct obj_kind * ok = &(GC_obj_kinds[kind]);
00994     struct hblk ** rlh = ok -> ok_reclaim_list;
00995     ptr_t *flh = &(ok -> ok_freelist[sz]);
00996     
00997     if (rlh == 0) return;   /* No blocks of this kind.  */
00998     rlh += sz;
00999     while ((hbp = *rlh) != 0) {
01000         hhdr = HDR(hbp);
01001         *rlh = hhdr -> hb_next;
01002         GC_reclaim_small_nonempty_block(hbp, FALSE MEM_FOUND_ADDR);
01003         if (*flh != 0) break;
01004     }
01005 }
01006 
01007 /*
01008  * Reclaim all small blocks waiting to be reclaimed.
01009  * Abort and return FALSE when/if (*stop_func)() returns TRUE.
01010  * If this returns TRUE, then it's safe to restart the world
01011  * with incorrectly cleared mark bits.
01012  * If ignore_old is TRUE, then reclaim only blocks that have been 
01013  * recently reclaimed, and discard the rest.
01014  * Stop_func may be 0.
01015  */
01016 GC_bool GC_reclaim_all(stop_func, ignore_old)
01017 GC_stop_func stop_func;
01018 GC_bool ignore_old;
01019 {
01020     register word sz;
01021     register int kind;
01022     register hdr * hhdr;
01023     register struct hblk * hbp;
01024     register struct obj_kind * ok;
01025     struct hblk ** rlp;
01026     struct hblk ** rlh;
01027 #   ifdef PRINTTIMES
01028        CLOCK_TYPE start_time;
01029        CLOCK_TYPE done_time;
01030        
01031        GET_TIME(start_time);
01032 #   endif
01033     
01034     for (kind = 0; kind < GC_n_kinds; kind++) {
01035        ok = &(GC_obj_kinds[kind]);
01036        rlp = ok -> ok_reclaim_list;
01037        if (rlp == 0) continue;
01038        for (sz = 1; sz <= MAXOBJSZ; sz++) {
01039            rlh = rlp + sz;
01040            while ((hbp = *rlh) != 0) {
01041                if (stop_func != (GC_stop_func)0 && (*stop_func)()) {
01042                    return(FALSE);
01043                }
01044               hhdr = HDR(hbp);
01045               *rlh = hhdr -> hb_next;
01046               if (!ignore_old || hhdr -> hb_last_reclaimed == GC_gc_no - 1) {
01047                   /* It's likely we'll need it this time, too  */
01048                   /* It's been touched recently, so this       */
01049                   /* shouldn't trigger paging.          */
01050                   GC_reclaim_small_nonempty_block(hbp, FALSE MEM_FOUND_ADDR);
01051               }
01052             }
01053         }
01054     }
01055 #   ifdef PRINTTIMES
01056        GET_TIME(done_time);
01057        GC_printf1("Disposing of reclaim lists took %lu msecs\n",
01058                   MS_TIME_DIFF(done_time,start_time));
01059 #   endif
01060     return(TRUE);
01061 }