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 bottom_index
* GC_all_bottom_indices
= 0;
28 /* Pointer to first (lowest addr) */
31 bottom_index
* GC_all_bottom_indices_end
= 0;
32 /* Pointer to last (highest addr) */
35 /* Non-macro version of header location routine */
36 hdr
* GC_find_header(h
)
40 register hdr
* result
;
48 /* Routines to dynamically allocate collector data structures that will */
51 static ptr_t scratch_free_ptr
= 0;
53 ptr_t GC_scratch_end_ptr
= 0;
55 ptr_t GC_scratch_last_end_ptr
= 0;
56 /* End point of last obtained scratch area */
58 ptr_t
GC_scratch_alloc(bytes
)
61 register ptr_t result
= scratch_free_ptr
;
64 # define GRANULARITY (2 * sizeof(word))
66 # define GRANULARITY sizeof(word)
68 bytes
+= GRANULARITY
-1;
69 bytes
&= ~(GRANULARITY
-1);
70 scratch_free_ptr
+= bytes
;
71 if (scratch_free_ptr
<= GC_scratch_end_ptr
) {
75 word bytes_to_get
= MINHINCR
* HBLKSIZE
;
77 if (bytes_to_get
<= bytes
) {
78 /* Undo the damage, and get memory directly */
81 bytes_to_get
+= GC_page_size
- 1;
82 bytes_to_get
&= ~(GC_page_size
- 1);
84 result
= (ptr_t
)GET_MEM(bytes_to_get
);
85 scratch_free_ptr
-= bytes
;
86 GC_scratch_last_end_ptr
= result
+ bytes
;
89 result
= (ptr_t
)GET_MEM(bytes_to_get
);
92 GC_printf0("Out of memory - trying to allocate less\n");
94 scratch_free_ptr
-= bytes
;
97 bytes_to_get
+= GC_page_size
- 1;
98 bytes_to_get
&= ~(GC_page_size
- 1);
100 return((ptr_t
)GET_MEM(bytes_to_get
));
102 scratch_free_ptr
= result
;
103 GC_scratch_end_ptr
= scratch_free_ptr
+ bytes_to_get
;
104 GC_scratch_last_end_ptr
= GC_scratch_end_ptr
;
105 return(GC_scratch_alloc(bytes
));
109 static hdr
* hdr_free_list
= 0;
111 /* Return an uninitialized header */
112 static hdr
* alloc_hdr()
114 register hdr
* result
;
116 if (hdr_free_list
== 0) {
117 result
= (hdr
*) GC_scratch_alloc((word
)(sizeof(hdr
)));
119 result
= hdr_free_list
;
120 hdr_free_list
= (hdr
*) (result
-> hb_next
);
125 static void free_hdr(hhdr
)
128 hhdr
-> hb_next
= (struct hblk
*) hdr_free_list
;
129 hdr_free_list
= hhdr
;
132 void GC_init_headers()
136 GC_all_nils
= (bottom_index
*)GC_scratch_alloc((word
)sizeof(bottom_index
));
137 BZERO(GC_all_nils
, sizeof(bottom_index
));
138 for (i
= 0; i
< TOP_SZ
; i
++) {
139 GC_top_index
[i
] = GC_all_nils
;
143 /* Make sure that there is a bottom level index block for address addr */
144 /* Return FALSE on failure. */
145 static GC_bool
get_index(addr
)
148 word hi
= (word
)(addr
) >> (LOG_BOTTOM_SZ
+ LOG_HBLKSIZE
);
151 bottom_index
** prev
;
155 unsigned i
= TL_HASH(hi
);
158 old
= p
= GC_top_index
[i
];
159 while(p
!= GC_all_nils
) {
160 if (p
-> key
== hi
) return(TRUE
);
163 r
= (bottom_index
*)GC_scratch_alloc((word
)(sizeof (bottom_index
)));
164 if (r
== 0) return(FALSE
);
165 BZERO(r
, sizeof (bottom_index
));
166 r
-> hash_link
= old
;
169 if (GC_top_index
[hi
] != GC_all_nils
) return(TRUE
);
170 r
= (bottom_index
*)GC_scratch_alloc((word
)(sizeof (bottom_index
)));
171 if (r
== 0) return(FALSE
);
172 GC_top_index
[hi
] = r
;
173 BZERO(r
, sizeof (bottom_index
));
176 /* Add it to the list of bottom indices */
177 prev
= &GC_all_bottom_indices
; /* pointer to p */
178 pi
= 0; /* bottom_index preceding p */
179 while ((p
= *prev
) != 0 && p
-> key
< hi
) {
181 prev
= &(p
-> asc_link
);
185 GC_all_bottom_indices_end
= r
;
194 /* Install a header for block h. */
195 /* The header is uninitialized. */
196 /* Returns FALSE on failure. */
197 GC_bool
GC_install_header(h
)
198 register struct hblk
* h
;
202 if (!get_index((word
) h
)) return(FALSE
);
203 result
= alloc_hdr();
206 result
-> hb_last_reclaimed
= GC_gc_no
;
211 /* Set up forwarding counts for block h of size sz */
212 GC_bool
GC_install_counts(h
, sz
)
213 register struct hblk
* h
;
214 register word sz
; /* bytes */
216 register struct hblk
* hbp
;
219 for (hbp
= h
; (char *)hbp
< (char *)h
+ sz
; hbp
+= BOTTOM_SZ
) {
220 if (!get_index((word
) hbp
)) return(FALSE
);
222 if (!get_index((word
)h
+ sz
- 1)) return(FALSE
);
223 for (hbp
= h
+ 1; (char *)hbp
< (char *)h
+ sz
; hbp
+= 1) {
224 i
= HBLK_PTR_DIFF(hbp
, h
);
225 SET_HDR(hbp
, (hdr
*)(i
> MAX_JUMP
? MAX_JUMP
: i
));
230 /* Remove the header for block h */
231 void GC_remove_header(h
)
232 register struct hblk
* h
;
241 /* Remove forwarding counts for h */
242 void GC_remove_counts(h
, sz
)
243 register struct hblk
* h
;
244 register word sz
; /* bytes */
246 register struct hblk
* hbp
;
248 for (hbp
= h
+1; (char *)hbp
< (char *)h
+ sz
; hbp
+= 1) {
253 /* Apply fn to all allocated blocks */
255 void GC_apply_to_all_blocks(fn
, client_data
)
256 void (*fn
)(/* struct hblk *h, word client_data */);
260 register bottom_index
* index_p
;
262 for (index_p
= GC_all_bottom_indices
; index_p
!= 0;
263 index_p
= index_p
-> asc_link
) {
264 for (j
= BOTTOM_SZ
-1; j
>= 0;) {
265 if (!IS_FORWARDING_ADDR_OR_NIL(index_p
->index
[j
])) {
266 if (index_p
->index
[j
]->hb_map
!= GC_invalid_map
) {
267 (*fn
)(((struct hblk
*)
268 (((index_p
->key
<< LOG_BOTTOM_SZ
) + (word
)j
)
273 } else if (index_p
->index
[j
] == 0) {
276 j
-= (word
)(index_p
->index
[j
]);
282 /* Get the next valid block whose address is at least h */
283 /* Return 0 if there is none. */
284 struct hblk
* GC_next_used_block(h
)
287 register bottom_index
* bi
;
288 register word j
= ((word
)h
>> LOG_HBLKSIZE
) & (BOTTOM_SZ
-1);
291 if (bi
== GC_all_nils
) {
292 register word hi
= (word
)h
>> (LOG_BOTTOM_SZ
+ LOG_HBLKSIZE
);
293 bi
= GC_all_bottom_indices
;
294 while (bi
!= 0 && bi
-> key
< hi
) bi
= bi
-> asc_link
;
298 while (j
< BOTTOM_SZ
) {
299 hdr
* hhdr
= bi
-> index
[j
];
300 if (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
303 if (hhdr
->hb_map
!= GC_invalid_map
) {
304 return((struct hblk
*)
305 (((bi
-> key
<< LOG_BOTTOM_SZ
) + j
)
308 j
+= divHBLKSZ(hhdr
-> hb_sz
);
318 /* Get the last (highest address) block whose address is */
319 /* at most h. Return 0 if there is none. */
320 /* Unlike the above, this may return a free block. */
321 struct hblk
* GC_prev_block(h
)
324 register bottom_index
* bi
;
325 register signed_word j
= ((word
)h
>> LOG_HBLKSIZE
) & (BOTTOM_SZ
-1);
328 if (bi
== GC_all_nils
) {
329 register word hi
= (word
)h
>> (LOG_BOTTOM_SZ
+ LOG_HBLKSIZE
);
330 bi
= GC_all_bottom_indices_end
;
331 while (bi
!= 0 && bi
-> key
> hi
) bi
= bi
-> desc_link
;
336 hdr
* hhdr
= bi
-> index
[j
];
339 } else if (IS_FORWARDING_ADDR_OR_NIL(hhdr
)) {
340 j
-= (signed_word
)hhdr
;
342 return((struct hblk
*)
343 (((bi
-> key
<< LOG_BOTTOM_SZ
) + j
)
348 bi
= bi
-> desc_link
;