OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / boehm-gc / headers.c
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     register word bytes_needed = bytes;
57
58 #   ifdef ALIGN_DOUBLE
59 #       define GRANULARITY (2 * sizeof(word))
60 #   else
61 #       define GRANULARITY sizeof(word)
62 #   endif
63     bytes += GRANULARITY-1;
64     bytes &= ~(GRANULARITY-1);
65     scratch_free_ptr += bytes;
66     if (scratch_free_ptr <= GC_scratch_end_ptr) {
67         return(result);
68     }
69     {
70         word bytes_to_get = MINHINCR * HBLKSIZE;
71          
72         if (bytes_to_get <= bytes) {
73           /* Undo the damage, and get memory directly */
74             bytes_to_get = bytes;
75 #           ifdef USE_MMAP
76                 bytes_to_get += GC_page_size - 1;
77                 bytes_to_get &= ~(GC_page_size - 1);
78 #           endif
79             result = (ptr_t)GET_MEM(bytes_to_get);
80             scratch_free_ptr -= bytes;
81             GC_scratch_last_end_ptr = result + bytes;
82             return(result);
83         }
84         result = (ptr_t)GET_MEM(bytes_to_get);
85         if (result == 0) {
86 #           ifdef PRINTSTATS
87                 GC_printf0("Out of memory - trying to allocate less\n");
88 #           endif
89             scratch_free_ptr -= bytes;
90             bytes_to_get = bytes;
91 #           ifdef USE_MMAP
92                 bytes_to_get += GC_page_size - 1;
93                 bytes_to_get &= (GC_page_size - 1);
94 #           endif
95             return((ptr_t)GET_MEM(bytes_to_get));
96         }
97         scratch_free_ptr = result;
98         GC_scratch_end_ptr = scratch_free_ptr + bytes_to_get;
99         GC_scratch_last_end_ptr = GC_scratch_end_ptr;
100         return(GC_scratch_alloc(bytes));
101     }
102 }
103
104 static hdr * hdr_free_list = 0;
105
106 /* Return an uninitialized header */
107 static hdr * alloc_hdr()
108 {
109     register hdr * result;
110     
111     if (hdr_free_list == 0) {
112         result = (hdr *) GC_scratch_alloc((word)(sizeof(hdr)));
113     } else {
114         result = hdr_free_list;
115         hdr_free_list = (hdr *) (result -> hb_next);
116     }
117     return(result);
118 }
119
120 static void free_hdr(hhdr)
121 hdr * hhdr;
122 {
123     hhdr -> hb_next = (struct hblk *) hdr_free_list;
124     hdr_free_list = hhdr;
125 }
126  
127 void GC_init_headers()
128 {
129     register int i;
130     
131     GC_all_nils = (bottom_index *)GC_scratch_alloc((word)sizeof(bottom_index));
132     BZERO(GC_all_nils, sizeof(bottom_index));
133     for (i = 0; i < TOP_SZ; i++) {
134         GC_top_index[i] = GC_all_nils;
135     }
136 }
137
138 /* Make sure that there is a bottom level index block for address addr  */
139 /* Return FALSE on failure.                                             */
140 static GC_bool get_index(addr)
141 register word addr;
142 {
143     register word hi =
144                 (word)(addr) >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
145     register bottom_index * r;
146     register bottom_index * p;
147     register bottom_index ** prev;
148 #   ifdef HASH_TL
149       register unsigned i = TL_HASH(hi);
150       register bottom_index * old;
151       
152       old = p = GC_top_index[i];
153       while(p != GC_all_nils) {
154           if (p -> key == hi) return(TRUE);
155           p = p -> hash_link;
156       }
157       r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
158       if (r == 0) return(FALSE);
159       BZERO(r, sizeof (bottom_index));
160       r -> hash_link = old;
161       GC_top_index[i] = r;
162 #   else
163       if (GC_top_index[hi] != GC_all_nils) return(TRUE);
164       r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
165       if (r == 0) return(FALSE);
166       GC_top_index[hi] = r;
167       BZERO(r, sizeof (bottom_index));
168 # endif
169     r -> key = hi;
170     /* Add it to the list of bottom indices */
171       prev = &GC_all_bottom_indices;
172       while ((p = *prev) != 0 && p -> key < hi) prev = &(p -> asc_link);
173       r -> asc_link = p;
174       *prev = r;
175     return(TRUE);
176 }
177
178 /* Install a header for block h.  */
179 /* The header is uninitialized.   */
180 /* Returns FALSE on failure.      */
181 GC_bool GC_install_header(h)
182 register struct hblk * h;
183 {
184     hdr * result;
185     
186     if (!get_index((word) h)) return(FALSE);
187     result = alloc_hdr();
188     SET_HDR(h, result);
189     return(result != 0);
190 }
191
192 /* Set up forwarding counts for block h of size sz */
193 GC_bool GC_install_counts(h, sz)
194 register struct hblk * h;
195 register word sz; /* bytes */
196 {
197     register struct hblk * hbp;
198     register int i;
199     
200     for (hbp = h; (char *)hbp < (char *)h + sz; hbp += BOTTOM_SZ) {
201         if (!get_index((word) hbp)) return(FALSE);
202     }
203     if (!get_index((word)h + sz - 1)) return(FALSE);
204     for (hbp = h + 1; (char *)hbp < (char *)h + sz; hbp += 1) {
205         i = HBLK_PTR_DIFF(hbp, h);
206         SET_HDR(hbp, (hdr *)(i > MAX_JUMP? MAX_JUMP : i));
207     }
208     return(TRUE);
209 }
210
211 /* Remove the header for block h */
212 void GC_remove_header(h)
213 register struct hblk * h;
214 {
215     hdr ** ha;
216     
217     GET_HDR_ADDR(h, ha);
218     free_hdr(*ha);
219     *ha = 0;
220 }
221
222 /* Remove forwarding counts for h */
223 void GC_remove_counts(h, sz)
224 register struct hblk * h;
225 register word sz; /* bytes */
226 {
227     register struct hblk * hbp;
228     
229     for (hbp = h+1; (char *)hbp < (char *)h + sz; hbp += 1) {
230         SET_HDR(hbp, 0);
231     }
232 }
233
234 /* Apply fn to all allocated blocks */
235 /*VARARGS1*/
236 void GC_apply_to_all_blocks(fn, client_data)
237 void (*fn)(/* struct hblk *h, word client_data */);
238 word client_data;
239 {
240     register int j;
241     register bottom_index * index_p;
242     
243     for (index_p = GC_all_bottom_indices; index_p != 0;
244          index_p = index_p -> asc_link) {
245         for (j = BOTTOM_SZ-1; j >= 0;) {
246             if (!IS_FORWARDING_ADDR_OR_NIL(index_p->index[j])) {
247                 if (index_p->index[j]->hb_map != GC_invalid_map) {
248                     (*fn)(((struct hblk *)
249                               (((index_p->key << LOG_BOTTOM_SZ) + (word)j)
250                                << LOG_HBLKSIZE)),
251                           client_data);
252                 }
253                 j--;
254              } else if (index_p->index[j] == 0) {
255                 j--;
256              } else {
257                 j -= (word)(index_p->index[j]);
258              }
259          }
260      }
261 }
262
263 /* Get the next valid block whose address is at least h */
264 /* Return 0 if there is none.                           */
265 struct hblk * GC_next_block(h)
266 struct hblk * h;
267 {
268     register bottom_index * bi;
269     register word j = ((word)h >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
270     
271     GET_BI(h, bi);
272     if (bi == GC_all_nils) {
273         register word hi = (word)h >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
274         bi = GC_all_bottom_indices;
275         while (bi != 0 && bi -> key < hi) bi = bi -> asc_link;
276         j = 0;
277     }
278     while(bi != 0) {
279         while (j < BOTTOM_SZ) {
280             if (IS_FORWARDING_ADDR_OR_NIL(bi -> index[j])) {
281                 j++;
282             } else {
283                 if (bi->index[j]->hb_map != GC_invalid_map) {
284                     return((struct hblk *)
285                               (((bi -> key << LOG_BOTTOM_SZ) + j)
286                                << LOG_HBLKSIZE));
287                 } else {
288                     j += divHBLKSZ(bi->index[j] -> hb_sz);
289                 }
290             }
291         }
292         j = 0;
293         bi = bi -> asc_link;
294     }
295     return(0);
296 }