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.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
18 * 1. allocation of heap block headers
19 * 2. A map from addresses to heap block addresses to heap block headers
21 * Access speed is crucial. We implement an index structure based on a 2
27 # include "private/gc_priv.h"
29 bottom_index * GC_all_bottom_indices = 0;
30 /* Pointer to first (lowest addr) */
33 bottom_index * GC_all_bottom_indices_end = 0;
34 /* Pointer to last (highest addr) */
37 /* Non-macro version of header location routine */
38 hdr * GC_find_header(ptr_t h)
49 /* Handle a header cache miss. Returns a pointer to the */
50 /* header corresponding to p, if p can possibly be a valid */
51 /* object pointer, and 0 otherwise. */
52 /* GUARANTEED to return 0 for a pointer past the first page */
53 /* of an object unless both GC_all_interior_pointers is set */
54 /* and p is in fact a valid object pointer. */
55 #ifdef PRINT_BLACK_LIST
56 hdr * GC_header_cache_miss(ptr_t p, hdr_cache_entry *hce, ptr_t source)
58 hdr * GC_header_cache_miss(ptr_t p, hdr_cache_entry *hce)
64 if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
65 if (GC_all_interior_pointers) {
69 current = (ptr_t)HBLKPTR(current);
71 current = current - HBLKSIZE*(word)hhdr;
73 } while(IS_FORWARDING_ADDR_OR_NIL(hhdr));
74 /* current points to near the start of the large object */
75 if (hhdr -> hb_flags & IGNORE_OFF_PAGE
76 || HBLK_IS_FREE(hhdr))
78 if (p - current >= (ptrdiff_t)(hhdr->hb_sz)) {
79 GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
80 /* Pointer past the end of the block */
84 GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
87 /* Pointers past the first page are probably too rare */
88 /* to add them to the cache. We don't. */
89 /* And correctness relies on the fact that we don't. */
92 GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
97 if (HBLK_IS_FREE(hhdr)) {
98 GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
101 hce -> block_addr = (word)(p) >> LOG_HBLKSIZE;
102 hce -> hce_hdr = hhdr;
108 /* Routines to dynamically allocate collector data structures that will */
109 /* never be freed. */
111 static ptr_t scratch_free_ptr = 0;
113 /* GC_scratch_last_end_ptr is end point of last obtained scratch area. */
114 /* GC_scratch_end_ptr is end point of current scratch area. */
116 ptr_t GC_scratch_alloc(size_t bytes)
118 register ptr_t result = scratch_free_ptr;
120 bytes += GRANULE_BYTES-1;
121 bytes &= ~(GRANULE_BYTES-1);
122 scratch_free_ptr += bytes;
123 if (scratch_free_ptr <= GC_scratch_end_ptr) {
127 word bytes_to_get = MINHINCR * HBLKSIZE;
129 if (bytes_to_get <= bytes) {
130 /* Undo the damage, and get memory directly */
131 bytes_to_get = bytes;
133 bytes_to_get += GC_page_size - 1;
134 bytes_to_get &= ~(GC_page_size - 1);
136 result = (ptr_t)GET_MEM(bytes_to_get);
137 GC_add_to_our_memory(result, bytes_to_get);
138 scratch_free_ptr -= bytes;
139 GC_scratch_last_end_ptr = result + bytes;
142 result = (ptr_t)GET_MEM(bytes_to_get);
143 GC_add_to_our_memory(result, bytes_to_get);
146 GC_printf("Out of memory - trying to allocate less\n");
147 scratch_free_ptr -= bytes;
148 bytes_to_get = bytes;
150 bytes_to_get += GC_page_size - 1;
151 bytes_to_get &= ~(GC_page_size - 1);
153 result = (ptr_t)GET_MEM(bytes_to_get);
154 GC_add_to_our_memory(result, bytes_to_get);
157 scratch_free_ptr = result;
158 GC_scratch_end_ptr = scratch_free_ptr + bytes_to_get;
159 GC_scratch_last_end_ptr = GC_scratch_end_ptr;
160 return(GC_scratch_alloc(bytes));
164 static hdr * hdr_free_list = 0;
166 /* Return an uninitialized header */
167 static hdr * alloc_hdr(void)
169 register hdr * result;
171 if (hdr_free_list == 0) {
172 result = (hdr *) GC_scratch_alloc((word)(sizeof(hdr)));
174 result = hdr_free_list;
175 hdr_free_list = (hdr *) (result -> hb_next);
180 static void free_hdr(hdr * hhdr)
182 hhdr -> hb_next = (struct hblk *) hdr_free_list;
183 hdr_free_list = hhdr;
187 word GC_hdr_cache_hits = 0;
188 word GC_hdr_cache_misses = 0;
191 void GC_init_headers(void)
195 GC_all_nils = (bottom_index *)GC_scratch_alloc((word)sizeof(bottom_index));
196 BZERO(GC_all_nils, sizeof(bottom_index));
197 for (i = 0; i < TOP_SZ; i++) {
198 GC_top_index[i] = GC_all_nils;
202 /* Make sure that there is a bottom level index block for address addr */
203 /* Return FALSE on failure. */
204 static GC_bool get_index(word addr)
206 word hi = (word)(addr) >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
209 bottom_index ** prev;
213 word i = TL_HASH(hi);
216 old = p = GC_top_index[i];
217 while(p != GC_all_nils) {
218 if (p -> key == hi) return(TRUE);
221 r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
222 if (r == 0) return(FALSE);
223 BZERO(r, sizeof (bottom_index));
224 r -> hash_link = old;
227 if (GC_top_index[hi] != GC_all_nils) return(TRUE);
228 r = (bottom_index*)GC_scratch_alloc((word)(sizeof (bottom_index)));
229 if (r == 0) return(FALSE);
230 GC_top_index[hi] = r;
231 BZERO(r, sizeof (bottom_index));
234 /* Add it to the list of bottom indices */
235 prev = &GC_all_bottom_indices; /* pointer to p */
236 pi = 0; /* bottom_index preceding p */
237 while ((p = *prev) != 0 && p -> key < hi) {
239 prev = &(p -> asc_link);
243 GC_all_bottom_indices_end = r;
252 /* Install a header for block h. */
253 /* The header is uninitialized. */
254 /* Returns the header or 0 on failure. */
255 struct hblkhdr * GC_install_header(struct hblk *h)
259 if (!get_index((word) h)) return(0);
260 result = alloc_hdr();
263 result -> hb_last_reclaimed = (unsigned short)GC_gc_no;
268 /* Set up forwarding counts for block h of size sz */
269 GC_bool GC_install_counts(struct hblk *h, size_t sz/* bytes */)
274 for (hbp = h; (char *)hbp < (char *)h + sz; hbp += BOTTOM_SZ) {
275 if (!get_index((word) hbp)) return(FALSE);
277 if (!get_index((word)h + sz - 1)) return(FALSE);
278 for (hbp = h + 1; (char *)hbp < (char *)h + sz; hbp += 1) {
279 i = HBLK_PTR_DIFF(hbp, h);
280 SET_HDR(hbp, (hdr *)(i > MAX_JUMP? MAX_JUMP : i));
285 /* Remove the header for block h */
286 void GC_remove_header(struct hblk *h)
295 /* Remove forwarding counts for h */
296 void GC_remove_counts(struct hblk *h, size_t sz/* bytes */)
298 register struct hblk * hbp;
300 for (hbp = h+1; (char *)hbp < (char *)h + sz; hbp += 1) {
305 /* Apply fn to all allocated blocks */
307 void GC_apply_to_all_blocks(void (*fn)(struct hblk *h, word client_data),
311 bottom_index * index_p;
313 for (index_p = GC_all_bottom_indices; index_p != 0;
314 index_p = index_p -> asc_link) {
315 for (j = BOTTOM_SZ-1; j >= 0;) {
316 if (!IS_FORWARDING_ADDR_OR_NIL(index_p->index[j])) {
317 if (!HBLK_IS_FREE(index_p->index[j])) {
318 (*fn)(((struct hblk *)
319 (((index_p->key << LOG_BOTTOM_SZ) + (word)j)
324 } else if (index_p->index[j] == 0) {
327 j -= (signed_word)(index_p->index[j]);
333 /* Get the next valid block whose address is at least h */
334 /* Return 0 if there is none. */
335 struct hblk * GC_next_used_block(struct hblk *h)
337 register bottom_index * bi;
338 register word j = ((word)h >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
341 if (bi == GC_all_nils) {
342 register word hi = (word)h >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
343 bi = GC_all_bottom_indices;
344 while (bi != 0 && bi -> key < hi) bi = bi -> asc_link;
348 while (j < BOTTOM_SZ) {
349 hdr * hhdr = bi -> index[j];
350 if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
353 if (!HBLK_IS_FREE(hhdr)) {
354 return((struct hblk *)
355 (((bi -> key << LOG_BOTTOM_SZ) + j)
358 j += divHBLKSZ(hhdr -> hb_sz);
368 /* Get the last (highest address) block whose address is */
369 /* at most h. Return 0 if there is none. */
370 /* Unlike the above, this may return a free block. */
371 struct hblk * GC_prev_block(struct hblk *h)
373 register bottom_index * bi;
374 register signed_word j = ((word)h >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
377 if (bi == GC_all_nils) {
378 register word hi = (word)h >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
379 bi = GC_all_bottom_indices_end;
380 while (bi != 0 && bi -> key > hi) bi = bi -> desc_link;
385 hdr * hhdr = bi -> index[j];
388 } else if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
389 j -= (signed_word)hhdr;
391 return((struct hblk *)
392 (((bi -> key << LOG_BOTTOM_SZ) + j)
397 bi = bi -> desc_link;