Back to index

lightning-sunbird  0.9+nobinonly
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  *
00005  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00006  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00007  *
00008  * Permission is hereby granted to use or copy this program
00009  * for any purpose,  provided the above notices are retained on all copies.
00010  * Permission to modify the code and to distribute modified code is granted,
00011  * provided the above notices are retained, and a notice that the code was
00012  * modified is included with the above copyright notice.
00013  */
00014 /* Boehm, October 9, 1995 1:09 pm PDT */
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 "gc_priv.h"
00023 
00024 char * 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 = 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 # ifndef ALL_INTERIOR_POINTERS
00058     DCL_LOCK_STATE;
00059     
00060     DISABLE_SIGNALS();
00061     LOCK();
00062     GC_register_displacement_inner(offset);
00063     UNLOCK();
00064     ENABLE_SIGNALS();
00065 # endif
00066 }
00067 
00068 void GC_register_displacement_inner(offset) 
00069 word offset;
00070 {
00071 # ifndef ALL_INTERIOR_POINTERS
00072     register unsigned i;
00073     
00074     if (offset > MAX_OFFSET) {
00075         ABORT("Bad argument to GC_register_displacement");
00076     }
00077     if (!GC_valid_offsets[offset]) {
00078       GC_valid_offsets[offset] = TRUE;
00079       GC_modws_valid_offsets[offset % sizeof(word)] = TRUE;
00080       for (i = 0; i <= MAXOBJSZ; i++) {
00081           if (GC_obj_map[i] != 0) {
00082              if (i == 0) {
00083                GC_obj_map[i][offset + HDR_BYTES] = (char)BYTES_TO_WORDS(offset);
00084              } else {
00085                register unsigned j;
00086                register unsigned lb = WORDS_TO_BYTES(i);
00087                
00088                if (offset < lb) {
00089                  for (j = offset + HDR_BYTES; j < HBLKSIZE; j += lb) {
00090                    GC_obj_map[i][j] = (char)BYTES_TO_WORDS(offset);
00091                  }
00092                }
00093              }
00094           }
00095       }
00096     }
00097 # endif
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 char * new_map;
00109     
00110     if (sz > MAXOBJSZ) sz = 0;
00111     if (GC_obj_map[sz] != 0) {
00112         return(TRUE);
00113     }
00114     new_map = GC_scratch_alloc(MAP_SIZE);
00115     if (new_map == 0) return(FALSE);
00116 #   ifdef PRINTSTATS
00117         GC_printf1("Adding block map for size %lu\n", (unsigned long)sz);
00118 #   endif
00119     for (displ = 0; displ < HBLKSIZE; displ++) {
00120         MAP_ENTRY(new_map,displ) = OBJ_INVALID;
00121     }
00122     if (sz == 0) {
00123         for(displ = 0; displ <= MAX_OFFSET; displ++) {
00124             if (OFFSET_VALID(displ)) {
00125                 MAP_ENTRY(new_map,displ+HDR_BYTES) = BYTES_TO_WORDS(displ);
00126             }
00127         }
00128     } else {
00129         for (obj_start = HDR_BYTES;
00130              obj_start + WORDS_TO_BYTES(sz) <= HBLKSIZE;
00131              obj_start += WORDS_TO_BYTES(sz)) {
00132              for (displ = 0; displ < WORDS_TO_BYTES(sz); displ++) {
00133                  if (OFFSET_VALID(displ)) {
00134                      MAP_ENTRY(new_map, obj_start + displ) =
00135                                                  BYTES_TO_WORDS(displ);
00136                  }
00137              }
00138         }
00139     }
00140     GC_obj_map[sz] = new_map;
00141     return(TRUE);
00142 }