Back to index

plt-scheme  4.2.1
obj_map.c
Go to the documentation of this file.
00001 /* 
00002  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
00003  * Copyright (c) 1991, 1992 by Xerox Corporation.  All rights reserved.
00004  * Copyright (c) 1999-2001 by Hewlett-Packard Company. All rights reserved.
00005  *
00006  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00007  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00008  *
00009  * Permission is hereby granted to use or copy this program
00010  * for any purpose,  provided the above notices are retained on all copies.
00011  * Permission to modify the code and to distribute modified code is granted,
00012  * provided the above notices are retained, and a notice that the code was
00013  * modified is included with the above copyright notice.
00014  */
00015   
00016 /* Routines for maintaining maps describing heap block
00017  * layouts for various object sizes.  Allows fast pointer validity checks
00018  * and fast location of object start locations on machines (such as SPARC)
00019  * with slow division.
00020  */
00021  
00022 # include "private/gc_priv.h"
00023 
00024 map_entry_type * GC_invalid_map = 0;
00025 
00026 /* Invalidate the object map associated with a block.   Free blocks   */
00027 /* are identified by invalid maps.                             */
00028 void GC_invalidate_map(hhdr)
00029 hdr *hhdr;
00030 {
00031     register int displ;
00032     
00033     if (GC_invalid_map == 0) {
00034         GC_invalid_map = (map_entry_type *)GC_scratch_alloc(MAP_SIZE);
00035         if (GC_invalid_map == 0) {
00036             GC_err_printf0(
00037               "Cant initialize GC_invalid_map: insufficient memory\n");
00038             EXIT();
00039         }
00040         for (displ = 0; displ < HBLKSIZE; displ++) {
00041             MAP_ENTRY(GC_invalid_map, displ) = OBJ_INVALID;
00042         }
00043     }
00044     hhdr -> hb_map = GC_invalid_map;
00045 }
00046 
00047 /* Consider pointers that are offset bytes displaced from the beginning */
00048 /* of an object to be valid.                                            */
00049 
00050 # if defined(__STDC__) || defined(__cplusplus)
00051     void GC_register_displacement(GC_word offset)
00052 # else
00053     void GC_register_displacement(offset) 
00054     GC_word offset;
00055 # endif
00056 {
00057     DCL_LOCK_STATE;
00058     
00059     DISABLE_SIGNALS();
00060     LOCK();
00061     GC_register_displacement_inner(offset);
00062     UNLOCK();
00063     ENABLE_SIGNALS();
00064 }
00065 
00066 void GC_register_displacement_inner(offset) 
00067 word offset;
00068 {
00069     register unsigned i;
00070     word map_entry = BYTES_TO_WORDS(offset);
00071     
00072     if (offset >= VALID_OFFSET_SZ) {
00073         ABORT("Bad argument to GC_register_displacement");
00074     }
00075     if (map_entry > MAX_OFFSET) map_entry = OFFSET_TOO_BIG;
00076     if (!GC_valid_offsets[offset]) {
00077       GC_valid_offsets[offset] = TRUE;
00078       GC_modws_valid_offsets[offset % sizeof(word)] = TRUE;
00079       if (!GC_all_interior_pointers) {
00080         for (i = 0; i <= MAXOBJSZ; i++) {
00081           if (GC_obj_map[i] != 0) {
00082              if (i == 0) {
00083                GC_obj_map[i][offset] = (map_entry_type)map_entry;
00084              } else {
00085                register unsigned j;
00086                register unsigned lb = WORDS_TO_BYTES(i);
00087                
00088                if (offset < lb) {
00089                  for (j = offset; j < HBLKSIZE; j += lb) {
00090                    GC_obj_map[i][j] = (map_entry_type)map_entry;
00091                  }
00092                }
00093              }
00094           }
00095        }
00096       }
00097     }
00098 }
00099 
00100 
00101 /* Add a heap block map for objects of size sz to obj_map.     */
00102 /* Return FALSE on failure.                             */
00103 GC_bool GC_add_map_entry(sz)
00104 word sz;
00105 {
00106     register unsigned obj_start;
00107     register unsigned displ;
00108     register map_entry_type * new_map;
00109     word map_entry;
00110     
00111     if (sz > MAXOBJSZ) sz = 0;
00112     if (GC_obj_map[sz] != 0) {
00113         return(TRUE);
00114     }
00115     new_map = (map_entry_type *)GC_scratch_alloc(MAP_SIZE);
00116     if (new_map == 0) return(FALSE);
00117 #   ifdef PRINTSTATS
00118         GC_printf1("Adding block map for size %lu\n", (unsigned long)sz);
00119 #   endif
00120     for (displ = 0; displ < HBLKSIZE; displ++) {
00121         MAP_ENTRY(new_map,displ) = OBJ_INVALID;
00122     }
00123     if (sz == 0) {
00124         for(displ = 0; displ <= HBLKSIZE; displ++) {
00125             if (OFFSET_VALID(displ)) {
00126               map_entry = BYTES_TO_WORDS(displ);
00127               if (map_entry > MAX_OFFSET) map_entry = OFFSET_TOO_BIG;
00128                 MAP_ENTRY(new_map,displ) = (map_entry_type)map_entry;
00129             }
00130         }
00131     } else {
00132         for (obj_start = 0;
00133              obj_start + WORDS_TO_BYTES(sz) <= HBLKSIZE;
00134              obj_start += WORDS_TO_BYTES(sz)) {
00135              for (displ = 0; displ < WORDS_TO_BYTES(sz); displ++) {
00136                  if (OFFSET_VALID(displ)) {
00137                    map_entry = BYTES_TO_WORDS(displ);
00138                    if (map_entry > MAX_OFFSET) map_entry = OFFSET_TOO_BIG;
00139                      MAP_ENTRY(new_map, obj_start + displ) =
00140                                           (map_entry_type)map_entry;
00141                  }
00142              }
00143         }
00144     }
00145     GC_obj_map[sz] = new_map;
00146     return(TRUE);
00147 }