[BACK]Return to headers.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gc

Annotation of OpenXM_contrib/gc/headers.c, Revision 1.1

1.1     ! maekawa     1: /*
        !             2:  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
        !             3:  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
        !             4:  * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
        !             5:  *
        !             6:  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
        !             7:  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
        !             8:  *
        !             9:  * Permission is hereby granted to use or copy this program
        !            10:  * for any purpose,  provided the above notices are retained on all copies.
        !            11:  * Permission to modify the code and to distribute modified code is granted,
        !            12:  * provided the above notices are retained, and a notice that the code was
        !            13:  * modified is included with the above copyright notice.
        !            14:  */
        !            15:
        !            16: /*
        !            17:  * This implements:
        !            18:  * 1. allocation of heap block headers
        !            19:  * 2. A map from addresses to heap block addresses to heap block headers
        !            20:  *
        !            21:  * Access speed is crucial.  We implement an index structure based on a 2
        !            22:  * level tree.
        !            23:  */
        !            24:
        !            25: # include "gc_priv.h"
        !            26:
        !            27: bottom_index * GC_all_bottom_indices = 0;
        !            28:
        !            29: /* Non-macro version of header location routine */
        !            30: hdr * GC_find_header(h)
        !            31: ptr_t h;
        !            32: {
        !            33: #   ifdef HASH_TL
        !            34:        register hdr * result;
        !            35:        GET_HDR(h, result);
        !            36:        return(result);
        !            37: #   else
        !            38:        return(HDR_INNER(h));
        !            39: #   endif
        !            40: }
        !            41:
        !            42: /* Routines to dynamically allocate collector data structures that will */
        !            43: /* never be freed.                                                      */
        !            44:
        !            45: static ptr_t scratch_free_ptr = 0;
        !            46:
        !            47: ptr_t GC_scratch_end_ptr = 0;
        !            48:
        !            49: ptr_t GC_scratch_last_end_ptr = 0;
        !            50:                /* End point of last obtained scratch area */
        !            51:
        !            52: ptr_t GC_scratch_alloc(bytes)
        !            53: register word bytes;
        !            54: {
        !            55:     register ptr_t result = scratch_free_ptr;
        !            56:
        !            57: #   ifdef ALIGN_DOUBLE
        !            58: #      define GRANULARITY (2 * sizeof(word))
        !            59: #   else
        !            60: #      define GRANULARITY sizeof(word)
        !            61: #   endif
        !            62:     bytes += GRANULARITY-1;
        !            63:     bytes &= ~(GRANULARITY-1);
        !            64:     scratch_free_ptr += bytes;
        !            65:     if (scratch_free_ptr <= GC_scratch_end_ptr) {
        !            66:         return(result);
        !            67:     }
        !            68:     {
        !            69:         word bytes_to_get = MINHINCR * HBLKSIZE;
        !            70:
        !            71:         if (bytes_to_get <= bytes) {
        !            72:           /* Undo the damage, and get memory directly */
        !            73:            bytes_to_get = bytes;
        !            74: #          ifdef USE_MMAP
        !            75:                bytes_to_get += GC_page_size - 1;
        !            76:                bytes_to_get &= ~(GC_page_size - 1);
        !            77: #          endif
        !            78:            result = (ptr_t)GET_MEM(bytes_to_get);
        !            79:             scratch_free_ptr -= bytes;
        !            80:            GC_scratch_last_end_ptr = result + bytes;
        !            81:             return(result);
        !            82:         }
        !            83:         result = (ptr_t)GET_MEM(bytes_to_get);
        !            84:         if (result == 0) {
        !            85: #          ifdef PRINTSTATS
        !            86:                 GC_printf0("Out of memory - trying to allocate less\n");
        !            87: #          endif
        !            88:             scratch_free_ptr -= bytes;
        !            89:            bytes_to_get = bytes;
        !            90: #          ifdef USE_MMAP
        !            91:                bytes_to_get += GC_page_size - 1;
        !            92:                bytes_to_get &= ~(GC_page_size - 1);
        !            93: #          endif
        !            94:             return((ptr_t)GET_MEM(bytes_to_get));
        !            95:         }
        !            96:         scratch_free_ptr = result;
        !            97:         GC_scratch_end_ptr = scratch_free_ptr + bytes_to_get;
        !            98:         GC_scratch_last_end_ptr = GC_scratch_end_ptr;
        !            99:         return(GC_scratch_alloc(bytes));
        !           100:     }
        !           101: }
        !           102:
        !           103: static hdr * hdr_free_list = 0;
        !           104:
        !           105: /* Return an uninitialized header */
        !           106: static hdr * alloc_hdr()
        !           107: {
        !           108:     register hdr * result;
        !           109:
        !           110:     if (hdr_free_list == 0) {
        !           111:         result = (hdr *) GC_scratch_alloc((word)(sizeof(hdr)));
        !           112:     } else {
        !           113:         result = hdr_free_list;
        !           114:         hdr_free_list = (hdr *) (result -> hb_next);
        !           115:     }
        !           116:     return(result);
        !           117: }
        !           118:
        !           119: static void free_hdr(hhdr)
        !           120: hdr * hhdr;
        !           121: {
        !           122:     hhdr -> hb_next = (struct hblk *) hdr_free_list;
        !           123:     hdr_free_list = hhdr;
        !           124: }
        !           125:
        !           126: void GC_init_headers()
        !           127: {
        !           128:     register unsigned i;
        !           129:
        !           130:     GC_all_nils = (bottom_index *)GC_scratch_alloc((word)sizeof(bottom_index));
        !           131:     BZERO(GC_all_nils, sizeof(bottom_index));
        !           132:     for (i = 0; i < TOP_SZ; i++) {
        !           133:         GC_top_index[i] = GC_all_nils;
        !           134:     }
        !           135: }
        !           136:
        !           137: /* Make sure that there is a bottom level index block for address addr  */
        !           138: /* Return FALSE on failure.                                            */
        !           139: static GC_bool get_index(addr)
        !           140: register word addr;
        !           141: {
        !           142:     register word hi =
        !           143:                (word)(addr) >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
        !           144:     register bottom_index * r;
        !           145:     register bottom_index * p;
        !           146:     register bottom_index ** prev;
        !           147: #   ifdef HASH_TL
        !           148:       register unsigned i = TL_HASH(hi);
        !           149:       register bottom_index * old;
        !           150:
        !           151:       old = p = GC_top_index[i];
        !           152:       while(p != GC_all_nils) {
        !           153:           if (p -> key == hi) return(TRUE);
        !           154:           p = p -> hash_link;
        !           155:       }
        !           156:       r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
        !           157:       if (r == 0) return(FALSE);
        !           158:       BZERO(r, sizeof (bottom_index));
        !           159:       r -> hash_link = old;
        !           160:       GC_top_index[i] = r;
        !           161: #   else
        !           162:       if (GC_top_index[hi] != GC_all_nils) return(TRUE);
        !           163:       r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
        !           164:       if (r == 0) return(FALSE);
        !           165:       GC_top_index[hi] = r;
        !           166:       BZERO(r, sizeof (bottom_index));
        !           167: # endif
        !           168:     r -> key = hi;
        !           169:     /* Add it to the list of bottom indices */
        !           170:       prev = &GC_all_bottom_indices;
        !           171:       while ((p = *prev) != 0 && p -> key < hi) prev = &(p -> asc_link);
        !           172:       r -> asc_link = p;
        !           173:       *prev = r;
        !           174:     return(TRUE);
        !           175: }
        !           176:
        !           177: /* Install a header for block h.  */
        !           178: /* The header is uninitialized.          */
        !           179: /* Returns FALSE on failure.     */
        !           180: GC_bool GC_install_header(h)
        !           181: register struct hblk * h;
        !           182: {
        !           183:     hdr * result;
        !           184:
        !           185:     if (!get_index((word) h)) return(FALSE);
        !           186:     result = alloc_hdr();
        !           187:     SET_HDR(h, result);
        !           188:     return(result != 0);
        !           189: }
        !           190:
        !           191: /* Set up forwarding counts for block h of size sz */
        !           192: GC_bool GC_install_counts(h, sz)
        !           193: register struct hblk * h;
        !           194: register word sz; /* bytes */
        !           195: {
        !           196:     register struct hblk * hbp;
        !           197:     register int i;
        !           198:
        !           199:     for (hbp = h; (char *)hbp < (char *)h + sz; hbp += BOTTOM_SZ) {
        !           200:         if (!get_index((word) hbp)) return(FALSE);
        !           201:     }
        !           202:     if (!get_index((word)h + sz - 1)) return(FALSE);
        !           203:     for (hbp = h + 1; (char *)hbp < (char *)h + sz; hbp += 1) {
        !           204:         i = HBLK_PTR_DIFF(hbp, h);
        !           205:         SET_HDR(hbp, (hdr *)(i > MAX_JUMP? MAX_JUMP : i));
        !           206:     }
        !           207:     return(TRUE);
        !           208: }
        !           209:
        !           210: /* Remove the header for block h */
        !           211: void GC_remove_header(h)
        !           212: register struct hblk * h;
        !           213: {
        !           214:     hdr ** ha;
        !           215:
        !           216:     GET_HDR_ADDR(h, ha);
        !           217:     free_hdr(*ha);
        !           218:     *ha = 0;
        !           219: }
        !           220:
        !           221: /* Remove forwarding counts for h */
        !           222: void GC_remove_counts(h, sz)
        !           223: register struct hblk * h;
        !           224: register word sz; /* bytes */
        !           225: {
        !           226:     register struct hblk * hbp;
        !           227:
        !           228:     for (hbp = h+1; (char *)hbp < (char *)h + sz; hbp += 1) {
        !           229:         SET_HDR(hbp, 0);
        !           230:     }
        !           231: }
        !           232:
        !           233: /* Apply fn to all allocated blocks */
        !           234: /*VARARGS1*/
        !           235: void GC_apply_to_all_blocks(fn, client_data)
        !           236: void (*fn)(/* struct hblk *h, word client_data */);
        !           237: word client_data;
        !           238: {
        !           239:     register int j;
        !           240:     register bottom_index * index_p;
        !           241:
        !           242:     for (index_p = GC_all_bottom_indices; index_p != 0;
        !           243:          index_p = index_p -> asc_link) {
        !           244:         for (j = BOTTOM_SZ-1; j >= 0;) {
        !           245:             if (!IS_FORWARDING_ADDR_OR_NIL(index_p->index[j])) {
        !           246:                 if (index_p->index[j]->hb_map != GC_invalid_map) {
        !           247:                     (*fn)(((struct hblk *)
        !           248:                              (((index_p->key << LOG_BOTTOM_SZ) + (word)j)
        !           249:                               << LOG_HBLKSIZE)),
        !           250:                           client_data);
        !           251:                 }
        !           252:                 j--;
        !           253:              } else if (index_p->index[j] == 0) {
        !           254:                 j--;
        !           255:              } else {
        !           256:                 j -= (word)(index_p->index[j]);
        !           257:              }
        !           258:          }
        !           259:      }
        !           260: }
        !           261:
        !           262: /* Get the next valid block whose address is at least h        */
        !           263: /* Return 0 if there is none.                          */
        !           264: struct hblk * GC_next_block(h)
        !           265: struct hblk * h;
        !           266: {
        !           267:     register bottom_index * bi;
        !           268:     register word j = ((word)h >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
        !           269:
        !           270:     GET_BI(h, bi);
        !           271:     if (bi == GC_all_nils) {
        !           272:         register word hi = (word)h >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
        !           273:         bi = GC_all_bottom_indices;
        !           274:         while (bi != 0 && bi -> key < hi) bi = bi -> asc_link;
        !           275:         j = 0;
        !           276:     }
        !           277:     while(bi != 0) {
        !           278:         while (j < BOTTOM_SZ) {
        !           279:             if (IS_FORWARDING_ADDR_OR_NIL(bi -> index[j])) {
        !           280:                 j++;
        !           281:             } else {
        !           282:                 if (bi->index[j]->hb_map != GC_invalid_map) {
        !           283:                     return((struct hblk *)
        !           284:                              (((bi -> key << LOG_BOTTOM_SZ) + j)
        !           285:                               << LOG_HBLKSIZE));
        !           286:                 } else {
        !           287:                     j += divHBLKSZ(bi->index[j] -> hb_sz);
        !           288:                 }
        !           289:             }
        !           290:         }
        !           291:         j = 0;
        !           292:         bi = bi -> asc_link;
        !           293:     }
        !           294:     return(0);
        !           295: }

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