Line data Source code
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 : #include "private/gc_priv.h"
17 :
18 : /*
19 : * This implements:
20 : * 1. allocation of heap block headers
21 : * 2. A map from addresses to heap block addresses to heap block headers
22 : *
23 : * Access speed is crucial. We implement an index structure based on a 2
24 : * level tree.
25 : */
26 :
27 : STATIC bottom_index * GC_all_bottom_indices = 0;
28 : /* Pointer to first (lowest addr) */
29 : /* bottom_index. */
30 :
31 : STATIC bottom_index * GC_all_bottom_indices_end = 0;
32 : /* Pointer to last (highest addr) */
33 : /* bottom_index. */
34 :
35 : /* Non-macro version of header location routine */
36 1085669 : GC_INNER hdr * GC_find_header(ptr_t h)
37 : {
38 : # ifdef HASH_TL
39 : hdr * result;
40 1085669 : GET_HDR(h, result);
41 1085669 : return(result);
42 : # else
43 : return(HDR_INNER(h));
44 : # endif
45 : }
46 :
47 : /* Handle a header cache miss. Returns a pointer to the */
48 : /* header corresponding to p, if p can possibly be a valid */
49 : /* object pointer, and 0 otherwise. */
50 : /* GUARANTEED to return 0 for a pointer past the first page */
51 : /* of an object unless both GC_all_interior_pointers is set */
52 : /* and p is in fact a valid object pointer. */
53 : /* Never returns a pointer to a free hblk. */
54 : GC_INNER hdr *
55 : #ifdef PRINT_BLACK_LIST
56 : GC_header_cache_miss(ptr_t p, hdr_cache_entry *hce, ptr_t source)
57 : #else
58 1009971 : GC_header_cache_miss(ptr_t p, hdr_cache_entry *hce)
59 : #endif
60 : {
61 : hdr *hhdr;
62 : HC_MISS();
63 1009971 : GET_HDR(p, hhdr);
64 1009971 : if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
65 631135 : if (GC_all_interior_pointers) {
66 0 : if (hhdr != 0) {
67 0 : ptr_t current = p;
68 :
69 0 : current = (ptr_t)HBLKPTR(current);
70 : do {
71 0 : current = current - HBLKSIZE*(word)hhdr;
72 0 : hhdr = HDR(current);
73 0 : } while(IS_FORWARDING_ADDR_OR_NIL(hhdr));
74 : /* current points to near the start of the large object */
75 0 : if (hhdr -> hb_flags & IGNORE_OFF_PAGE)
76 0 : return 0;
77 0 : if (HBLK_IS_FREE(hhdr)
78 0 : || p - current >= (ptrdiff_t)(hhdr->hb_sz)) {
79 0 : GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
80 : /* Pointer past the end of the block */
81 0 : return 0;
82 : }
83 : } else {
84 0 : GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
85 : /* And return zero: */
86 : }
87 : GC_ASSERT(hhdr == 0 || !HBLK_IS_FREE(hhdr));
88 0 : return hhdr;
89 : /* Pointers past the first page are probably too rare */
90 : /* to add them to the cache. We don't. */
91 : /* And correctness relies on the fact that we don't. */
92 : } else {
93 631135 : if (hhdr == 0) {
94 612521 : GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
95 : }
96 603375 : return 0;
97 : }
98 : } else {
99 378836 : if (HBLK_IS_FREE(hhdr)) {
100 529 : GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
101 529 : return 0;
102 : } else {
103 378307 : hce -> block_addr = (word)(p) >> LOG_HBLKSIZE;
104 378307 : hce -> hce_hdr = hhdr;
105 378307 : return hhdr;
106 : }
107 : }
108 : }
109 :
110 : /* Routines to dynamically allocate collector data structures that will */
111 : /* never be freed. */
112 :
113 : static ptr_t scratch_free_ptr = 0;
114 :
115 : /* GC_scratch_last_end_ptr is end point of last obtained scratch area. */
116 : /* GC_scratch_end_ptr is end point of current scratch area. */
117 :
118 45684 : GC_INNER ptr_t GC_scratch_alloc(size_t bytes)
119 : {
120 45684 : register ptr_t result = scratch_free_ptr;
121 :
122 45684 : bytes += GRANULE_BYTES-1;
123 45684 : bytes &= ~(GRANULE_BYTES-1);
124 45684 : scratch_free_ptr += bytes;
125 45684 : if ((word)scratch_free_ptr <= (word)GC_scratch_end_ptr) {
126 44752 : return(result);
127 : }
128 : {
129 932 : word bytes_to_get = MINHINCR * HBLKSIZE;
130 :
131 932 : if (bytes_to_get <= bytes) {
132 : /* Undo the damage, and get memory directly */
133 168 : bytes_to_get = ROUNDUP_PAGESIZE_IF_MMAP(bytes);
134 168 : result = (ptr_t)GET_MEM(bytes_to_get);
135 : GC_add_to_our_memory(result, bytes_to_get);
136 168 : scratch_free_ptr -= bytes;
137 168 : if (result != NULL) {
138 168 : GC_scratch_last_end_ptr = result + bytes;
139 : }
140 168 : return(result);
141 : }
142 :
143 764 : bytes_to_get = ROUNDUP_PAGESIZE_IF_MMAP(bytes_to_get); /* for safety */
144 764 : result = (ptr_t)GET_MEM(bytes_to_get);
145 : GC_add_to_our_memory(result, bytes_to_get);
146 764 : if (result == 0) {
147 0 : WARN("Out of memory - trying to allocate less\n", 0);
148 0 : scratch_free_ptr -= bytes;
149 0 : bytes_to_get = ROUNDUP_PAGESIZE_IF_MMAP(bytes);
150 0 : result = (ptr_t)GET_MEM(bytes_to_get);
151 : GC_add_to_our_memory(result, bytes_to_get);
152 0 : return result;
153 : }
154 764 : scratch_free_ptr = result;
155 764 : GC_scratch_end_ptr = scratch_free_ptr + bytes_to_get;
156 764 : GC_scratch_last_end_ptr = GC_scratch_end_ptr;
157 764 : return(GC_scratch_alloc(bytes));
158 : }
159 : }
160 :
161 : static hdr * hdr_free_list = 0;
162 :
163 : /* Return an uninitialized header */
164 50122 : static hdr * alloc_hdr(void)
165 : {
166 : register hdr * result;
167 :
168 50122 : if (hdr_free_list == 0) {
169 39320 : result = (hdr *) GC_scratch_alloc((word)(sizeof(hdr)));
170 : } else {
171 10802 : result = hdr_free_list;
172 10802 : hdr_free_list = (hdr *) (result -> hb_next);
173 : }
174 50122 : return(result);
175 : }
176 :
177 29143 : GC_INLINE void free_hdr(hdr * hhdr)
178 : {
179 29143 : hhdr -> hb_next = (struct hblk *) hdr_free_list;
180 29143 : hdr_free_list = hhdr;
181 29143 : }
182 :
183 : #ifdef COUNT_HDR_CACHE_HITS
184 : /* Used for debugging/profiling (the symbols are externally visible). */
185 : word GC_hdr_cache_hits = 0;
186 : word GC_hdr_cache_misses = 0;
187 : #endif
188 :
189 163 : GC_INNER void GC_init_headers(void)
190 : {
191 : register unsigned i;
192 :
193 163 : GC_all_nils = (bottom_index *)GC_scratch_alloc((word)sizeof(bottom_index));
194 163 : if (GC_all_nils == NULL) {
195 0 : GC_err_printf("Insufficient memory for GC_all_nils\n");
196 0 : EXIT();
197 : }
198 163 : BZERO(GC_all_nils, sizeof(bottom_index));
199 333987 : for (i = 0; i < TOP_SZ; i++) {
200 333824 : GC_top_index[i] = GC_all_nils;
201 : }
202 163 : }
203 :
204 : /* Make sure that there is a bottom level index block for address addr */
205 : /* Return FALSE on failure. */
206 156214 : static GC_bool get_index(word addr)
207 : {
208 156214 : word hi = (word)(addr) >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
209 : bottom_index * r;
210 : bottom_index * p;
211 : bottom_index ** prev;
212 : bottom_index *pi;
213 :
214 : # ifdef HASH_TL
215 156214 : word i = TL_HASH(hi);
216 : bottom_index * old;
217 :
218 156214 : old = p = GC_top_index[i];
219 312428 : while(p != GC_all_nils) {
220 155978 : if (p -> key == hi) return(TRUE);
221 0 : p = p -> hash_link;
222 : }
223 236 : r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
224 236 : if (r == 0) return(FALSE);
225 236 : BZERO(r, sizeof (bottom_index));
226 236 : r -> hash_link = old;
227 236 : GC_top_index[i] = r;
228 : # else
229 : if (GC_top_index[hi] != GC_all_nils) return(TRUE);
230 : r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
231 : if (r == 0) return(FALSE);
232 : GC_top_index[hi] = r;
233 : BZERO(r, sizeof (bottom_index));
234 : # endif
235 236 : r -> key = hi;
236 : /* Add it to the list of bottom indices */
237 236 : prev = &GC_all_bottom_indices; /* pointer to p */
238 236 : pi = 0; /* bottom_index preceding p */
239 940 : while ((p = *prev) != 0 && p -> key < hi) {
240 468 : pi = p;
241 468 : prev = &(p -> asc_link);
242 : }
243 236 : r -> desc_link = pi;
244 236 : if (0 == p) {
245 221 : GC_all_bottom_indices_end = r;
246 : } else {
247 15 : p -> desc_link = r;
248 : }
249 236 : r -> asc_link = p;
250 236 : *prev = r;
251 236 : return(TRUE);
252 : }
253 :
254 : /* Install a header for block h. */
255 : /* The header is uninitialized. */
256 : /* Returns the header or 0 on failure. */
257 50122 : GC_INNER struct hblkhdr * GC_install_header(struct hblk *h)
258 : {
259 : hdr * result;
260 :
261 50122 : if (!get_index((word) h)) return(0);
262 50122 : result = alloc_hdr();
263 50122 : if (result) {
264 50122 : SET_HDR(h, result);
265 : # ifdef USE_MUNMAP
266 : result -> hb_last_reclaimed = (unsigned short)GC_gc_no;
267 : # endif
268 : }
269 50122 : return(result);
270 : }
271 :
272 : /* Set up forwarding counts for block h of size sz */
273 53038 : GC_INNER GC_bool GC_install_counts(struct hblk *h, size_t sz/* bytes */)
274 : {
275 : struct hblk * hbp;
276 : word i;
277 :
278 106092 : for (hbp = h; (word)hbp < (word)h + sz; hbp += BOTTOM_SZ) {
279 53054 : if (!get_index((word) hbp)) return(FALSE);
280 : }
281 53038 : if (!get_index((word)h + sz - 1)) return(FALSE);
282 78468 : for (hbp = h + 1; (word)hbp < (word)h + sz; hbp += 1) {
283 25430 : i = HBLK_PTR_DIFF(hbp, h);
284 25430 : SET_HDR(hbp, (hdr *)(i > MAX_JUMP? MAX_JUMP : i));
285 : }
286 53038 : return(TRUE);
287 : }
288 :
289 : /* Remove the header for block h */
290 29143 : GC_INNER void GC_remove_header(struct hblk *h)
291 : {
292 : hdr **ha;
293 29143 : GET_HDR_ADDR(h, ha);
294 29143 : free_hdr(*ha);
295 29143 : *ha = 0;
296 29143 : }
297 :
298 : /* Remove forwarding counts for h */
299 32964 : GC_INNER void GC_remove_counts(struct hblk *h, size_t sz/* bytes */)
300 : {
301 : register struct hblk * hbp;
302 164281 : for (hbp = h+1; (word)hbp < (word)h + sz; hbp += 1) {
303 131317 : SET_HDR(hbp, 0);
304 : }
305 32964 : }
306 :
307 : /* Apply fn to all allocated blocks */
308 : /*VARARGS1*/
309 488 : void GC_apply_to_all_blocks(void (*fn)(struct hblk *h, word client_data),
310 : word client_data)
311 : {
312 : signed_word j;
313 : bottom_index * index_p;
314 :
315 1806 : for (index_p = GC_all_bottom_indices; index_p != 0;
316 830 : index_p = index_p -> asc_link) {
317 768684 : for (j = BOTTOM_SZ-1; j >= 0;) {
318 767024 : if (!IS_FORWARDING_ADDR_OR_NIL(index_p->index[j])) {
319 158702 : if (!HBLK_IS_FREE(index_p->index[j])) {
320 157192 : (*fn)(((struct hblk *)
321 157192 : (((index_p->key << LOG_BOTTOM_SZ) + (word)j)
322 : << LOG_HBLKSIZE)),
323 : client_data);
324 : }
325 158702 : j--;
326 608322 : } else if (index_p->index[j] == 0) {
327 605546 : j--;
328 : } else {
329 2776 : j -= (signed_word)(index_p->index[j]);
330 : }
331 : }
332 : }
333 488 : }
334 :
335 : /* Get the next valid block whose address is at least h */
336 : /* Return 0 if there is none. */
337 947 : GC_INNER struct hblk * GC_next_used_block(struct hblk *h)
338 : {
339 : register bottom_index * bi;
340 947 : register word j = ((word)h >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
341 :
342 947 : GET_BI(h, bi);
343 947 : if (bi == GC_all_nils) {
344 244 : register word hi = (word)h >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
345 244 : bi = GC_all_bottom_indices;
346 244 : while (bi != 0 && bi -> key < hi) bi = bi -> asc_link;
347 244 : j = 0;
348 : }
349 2191 : while(bi != 0) {
350 287236 : while (j < BOTTOM_SZ) {
351 285939 : hdr * hhdr = bi -> index[j];
352 285939 : if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
353 284483 : j++;
354 : } else {
355 1456 : if (!HBLK_IS_FREE(hhdr)) {
356 703 : return((struct hblk *)
357 703 : (((bi -> key << LOG_BOTTOM_SZ) + j)
358 : << LOG_HBLKSIZE));
359 : } else {
360 753 : j += divHBLKSZ(hhdr -> hb_sz);
361 : }
362 : }
363 : }
364 297 : j = 0;
365 297 : bi = bi -> asc_link;
366 : }
367 244 : return(0);
368 : }
369 :
370 : /* Get the last (highest address) block whose address is */
371 : /* at most h. Return 0 if there is none. */
372 : /* Unlike the above, this may return a free block. */
373 491 : GC_INNER struct hblk * GC_prev_block(struct hblk *h)
374 : {
375 : register bottom_index * bi;
376 491 : register signed_word j = ((word)h >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
377 :
378 491 : GET_BI(h, bi);
379 491 : if (bi == GC_all_nils) {
380 0 : register word hi = (word)h >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
381 0 : bi = GC_all_bottom_indices_end;
382 0 : while (bi != 0 && bi -> key > hi) bi = bi -> desc_link;
383 0 : j = BOTTOM_SZ - 1;
384 : }
385 1235 : while(bi != 0) {
386 156002 : while (j >= 0) {
387 155180 : hdr * hhdr = bi -> index[j];
388 155180 : if (0 == hhdr) {
389 154863 : --j;
390 317 : } else if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
391 1 : j -= (signed_word)hhdr;
392 : } else {
393 316 : return((struct hblk *)
394 316 : (((bi -> key << LOG_BOTTOM_SZ) + j)
395 : << LOG_HBLKSIZE));
396 : }
397 : }
398 253 : j = BOTTOM_SZ - 1;
399 253 : bi = bi -> desc_link;
400 : }
401 175 : return(0);
402 : }
|