Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / libgc / obj_map.c
1 /* 
2  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3  * Copyright (c) 1991, 1992 by Xerox Corporation.  All rights reserved.
4  * Copyright (c) 1999-2001 by Hewlett-Packard Company. 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 /* Routines for maintaining maps describing heap block
17  * layouts for various object sizes.  Allows fast pointer validity checks
18  * and fast location of object start locations on machines (such as SPARC)
19  * with slow division.
20  */
21  
22 # include "private/gc_priv.h"
23
24 map_entry_type * GC_invalid_map = 0;
25 static word max_valid_offset = 0;
26
27 /* Invalidate the object map associated with a block.   Free blocks     */
28 /* are identified by invalid maps.                                      */
29 void GC_invalidate_map(hhdr)
30 hdr *hhdr;
31 {
32     register int displ;
33     
34     if (GC_invalid_map == 0) {
35         GC_invalid_map = (map_entry_type *)GC_scratch_alloc(MAP_SIZE);
36         if (GC_invalid_map == 0) {
37             GC_err_printf0(
38                 "Cant initialize GC_invalid_map: insufficient memory\n");
39             EXIT();
40         }
41         for (displ = 0; displ < HBLKSIZE; displ++) {
42             MAP_ENTRY(GC_invalid_map, displ) = OBJ_INVALID;
43         }
44     }
45     hhdr -> hb_map = GC_invalid_map;
46 }
47
48 /* Consider pointers that are offset bytes displaced from the beginning */
49 /* of an object to be valid.                                            */
50
51 # if defined(__STDC__) || defined(__cplusplus)
52     void GC_register_displacement(GC_word offset)
53 # else
54     void GC_register_displacement(offset) 
55     GC_word offset;
56 # endif
57 {
58     DCL_LOCK_STATE;
59     
60     DISABLE_SIGNALS();
61     LOCK();
62     GC_register_displacement_inner(offset);
63     UNLOCK();
64     ENABLE_SIGNALS();
65 }
66
67 void GC_register_displacement_inner(offset) 
68 word offset;
69 {
70     register unsigned i;
71     word map_entry = BYTES_TO_WORDS(offset);
72     
73     if (offset >= VALID_OFFSET_SZ) {
74         ABORT("Bad argument to GC_register_displacement");
75     }
76     if (map_entry > MAX_OFFSET) map_entry = OFFSET_TOO_BIG;
77     if (!GC_valid_offsets[offset]) {
78       GC_valid_offsets[offset] = TRUE;
79       GC_modws_valid_offsets[offset % sizeof(word)] = TRUE;
80           if (offset > max_valid_offset)
81                   max_valid_offset = offset;
82       if (!GC_all_interior_pointers) {
83         for (i = 0; i <= MAXOBJSZ; i++) {
84           if (GC_obj_map[i] != 0) {
85              if (i == 0) {
86                GC_obj_map[i][offset] = (map_entry_type)map_entry;
87              } else {
88                register unsigned j;
89                register unsigned lb = WORDS_TO_BYTES(i);
90                
91                if (offset < lb) {
92                  for (j = offset; j < HBLKSIZE; j += lb) {
93                    GC_obj_map[i][j] = (map_entry_type)map_entry;
94                  }
95                }
96              }
97           }
98         }
99       }
100     }
101 }
102
103
104 /* Add a heap block map for objects of size sz to obj_map.      */
105 /* Return FALSE on failure.                                     */
106 GC_bool GC_add_map_entry(sz)
107 word sz;
108 {
109     register unsigned obj_start;
110     register unsigned displ;
111     register map_entry_type * new_map;
112     word map_entry;
113     
114     if (sz > MAXOBJSZ) sz = 0;
115     if (GC_obj_map[sz] != 0) {
116         return(TRUE);
117     }
118     new_map = (map_entry_type *)GC_scratch_alloc(MAP_SIZE);
119     if (new_map == 0) return(FALSE);
120 #   ifdef PRINTSTATS
121         GC_printf1("Adding block map for size %lu\n", (unsigned long)sz);
122 #   endif
123         INIT_MAP(new_map);
124     if (sz == 0) {
125         for(displ = 0; displ <= max_valid_offset; displ++) {
126             if (OFFSET_VALID(displ)) {
127                 map_entry = BYTES_TO_WORDS(displ);
128                 if (map_entry > MAX_OFFSET) map_entry = OFFSET_TOO_BIG;
129                 MAP_ENTRY(new_map,displ) = (map_entry_type)map_entry;
130             }
131         }
132     } else {
133         for (obj_start = 0;
134              obj_start + WORDS_TO_BYTES(sz) <= HBLKSIZE;
135              obj_start += WORDS_TO_BYTES(sz)) {
136              for (displ = 0; displ <= max_valid_offset; displ++) {
137                  if (OFFSET_VALID(displ)) {
138                      map_entry = BYTES_TO_WORDS(displ);
139                      if (map_entry > MAX_OFFSET) map_entry = OFFSET_TOO_BIG;
140                      MAP_ENTRY(new_map, obj_start + displ) =
141                                                 (map_entry_type)map_entry;
142                  }
143              }
144         }
145     }
146     GC_obj_map[sz] = new_map;
147     return(TRUE);
148 }