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;
29 /* Non-macro version of header location routine */
30 hdr
* GC_find_header(h
)
34 register hdr
* result
;
42 /* Routines to dynamically allocate collector data structures that will */
45 static ptr_t scratch_free_ptr
= 0;
47 ptr_t GC_scratch_end_ptr
= 0;
49 ptr_t GC_scratch_last_end_ptr
= 0;
50 /* End point of last obtained scratch area */
52 ptr_t
GC_scratch_alloc(bytes
)
55 register ptr_t result
= scratch_free_ptr
;
56 register word bytes_needed
= bytes
;
59 # define GRANULARITY (2 * sizeof(word))
61 # define GRANULARITY sizeof(word)
63 bytes
+= GRANULARITY
-1;
64 bytes
&= ~(GRANULARITY
-1);
65 scratch_free_ptr
+= bytes
;
66 if (scratch_free_ptr
<= GC_scratch_end_ptr
) {
70 word bytes_to_get
= MINHINCR
* HBLKSIZE
;
72 if (bytes_to_get
<= bytes
) {
73 /* Undo the damage, and get memory directly */
76 bytes_to_get
+= GC_page_size
- 1;
77 bytes_to_get
&= ~(GC_page_size
- 1);
79 result
= (ptr_t
)GET_MEM(bytes_to_get
);
80 scratch_free_ptr
-= bytes
;
81 GC_scratch_last_end_ptr
= result
+ bytes
;
84 result
= (ptr_t
)GET_MEM(bytes_to_get
);
87 GC_printf0("Out of memory - trying to allocate less\n");
89 scratch_free_ptr
-= bytes
;
92 bytes_to_get
+= GC_page_size
- 1;
93 bytes_to_get
&= (GC_page_size
- 1);
95 return((ptr_t
)GET_MEM(bytes_to_get
));
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
));
104 static hdr
* hdr_free_list
= 0;
106 /* Return an uninitialized header */
107 static hdr
* alloc_hdr()
109 register hdr
* result
;
111 if (hdr_free_list
== 0) {
112 result
= (hdr
*) GC_scratch_alloc((word
)(sizeof(hdr
)));
114 result
= hdr_free_list
;
115 hdr_free_list
= (hdr
*) (result
-> hb_next
);
120 static void free_hdr(hhdr
)
123 hhdr
-> hb_next
= (struct hblk
*) hdr_free_list
;
124 hdr_free_list
= hhdr
;
127 void GC_init_headers()
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
;
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
)
144 (word
)(addr
) >> (LOG_BOTTOM_SZ
+ LOG_HBLKSIZE
);
145 register bottom_index
* r
;
146 register bottom_index
* p
;
147 register bottom_index
** prev
;
149 register unsigned i
= TL_HASH(hi
);
150 register bottom_index
* old
;
152 old
= p
= GC_top_index
[i
];
153 while(p
!= GC_all_nils
) {
154 if (p
-> key
== hi
) return(TRUE
);
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
;
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
));
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
);
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
;
186 if (!get_index((word
) h
)) return(FALSE
);
187 result
= alloc_hdr();
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 */
197 register struct hblk
* hbp
;
200 for (hbp
= h
; (char *)hbp
< (char *)h
+ sz
; hbp
+= BOTTOM_SZ
) {
201 if (!get_index((word
) hbp
)) return(FALSE
);
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
));
211 /* Remove the header for block h */
212 void GC_remove_header(h
)
213 register struct hblk
* h
;
222 /* Remove forwarding counts for h */
223 void GC_remove_counts(h
, sz
)
224 register struct hblk
* h
;
225 register word sz
; /* bytes */
227 register struct hblk
* hbp
;
229 for (hbp
= h
+1; (char *)hbp
< (char *)h
+ sz
; hbp
+= 1) {
234 /* Apply fn to all allocated blocks */
236 void GC_apply_to_all_blocks(fn
, client_data
)
237 void (*fn
)(/* struct hblk *h, word client_data */);
241 register bottom_index
* index_p
;
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
)
254 } else if (index_p
->index
[j
] == 0) {
257 j
-= (word
)(index_p
->index
[j
]);
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
)
268 register bottom_index
* bi
;
269 register word j
= ((word
)h
>> LOG_HBLKSIZE
) & (BOTTOM_SZ
-1);
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
;
279 while (j
< BOTTOM_SZ
) {
280 if (IS_FORWARDING_ADDR_OR_NIL(bi
-> index
[j
])) {
283 if (bi
->index
[j
]->hb_map
!= GC_invalid_map
) {
284 return((struct hblk
*)
285 (((bi
-> key
<< LOG_BOTTOM_SZ
) + j
)
288 j
+= divHBLKSZ(bi
->index
[j
] -> hb_sz
);