-
Notifications
You must be signed in to change notification settings - Fork 401
/
headers.c
420 lines (373 loc) · 12.4 KB
/
headers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
* Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996 by Silicon Graphics. All rights reserved.
* Copyright (c) 2008-2022 Ivan Maidanski
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to use or copy this program
* for any purpose, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*/
#include "private/gc_priv.h"
#if defined(KEEP_BACK_PTRS) && defined(GC_ASSERTIONS)
# include "private/dbg_mlc.h" /* for NOT_MARKED */
#endif
/*
* This implements:
* 1. allocation of heap block headers
* 2. A map from addresses to heap block addresses to heap block headers
*
* Access speed is crucial. We implement an index structure based on a 2
* level tree.
*/
/* A non-macro version of header location routine. */
GC_INNER hdr * GC_find_header(const void * h)
{
# ifdef HASH_TL
hdr * result;
GET_HDR(h, result);
return result;
# else
return HDR_INNER(h);
# endif
}
/* Handle a header cache miss. Returns a pointer to the */
/* header corresponding to p, if p can possibly be a valid */
/* object pointer, and 0 otherwise. */
/* GUARANTEED to return 0 for a pointer past the first page */
/* of an object unless both GC_all_interior_pointers is set */
/* and p is in fact a valid object pointer. */
/* Never returns a pointer to a free hblk. */
GC_INNER hdr *
#ifdef PRINT_BLACK_LIST
GC_header_cache_miss(ptr_t p, hdr_cache_entry *hce, ptr_t source)
#else
GC_header_cache_miss(ptr_t p, hdr_cache_entry *hce)
#endif
{
hdr *hhdr;
HC_MISS();
GET_HDR(p, hhdr);
if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
if (GC_all_interior_pointers) {
if (hhdr != NULL) {
ptr_t current = (ptr_t)GC_find_starting_hblk(HBLKPTR(p), &hhdr);
/* current points to near the start of the large object */
if (hhdr -> hb_flags & IGNORE_OFF_PAGE)
return 0;
if (HBLK_IS_FREE(hhdr)
|| p - current >= (signed_word)(hhdr -> hb_sz)) {
GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
/* Pointer past the end of the block */
return 0;
}
} else {
GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
/* And return zero: */
}
GC_ASSERT(NULL == hhdr || !HBLK_IS_FREE(hhdr));
return hhdr;
/* Pointers past the first page are probably too rare */
/* to add them to the cache. We don't. */
/* And correctness relies on the fact that we don't. */
} else {
if (NULL == hhdr) {
GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
}
return 0;
}
} else {
if (HBLK_IS_FREE(hhdr)) {
GC_ADD_TO_BLACK_LIST_NORMAL(p, source);
return 0;
} else {
hce -> block_addr = ADDR(p) >> LOG_HBLKSIZE;
hce -> hce_hdr = hhdr;
return hhdr;
}
}
}
/* Routines to dynamically allocate collector data structures that will */
/* never be freed. */
GC_INNER ptr_t GC_scratch_alloc(size_t bytes)
{
ptr_t result = GC_scratch_free_ptr;
size_t bytes_to_get;
GC_ASSERT(I_HOLD_LOCK());
bytes = ROUNDUP_GRANULE_SIZE(bytes);
for (;;) {
GC_ASSERT(GC_scratch_end_addr >= ADDR(result));
if (bytes <= GC_scratch_end_addr - ADDR(result)) {
/* Unallocated space of scratch buffer has enough size. */
GC_scratch_free_ptr = result + bytes;
return result;
}
GC_ASSERT(GC_page_size != 0);
if (bytes >= MINHINCR * HBLKSIZE) {
bytes_to_get = ROUNDUP_PAGESIZE_IF_MMAP(bytes);
result = GC_os_get_mem(bytes_to_get);
if (result != NULL) {
# if defined(KEEP_BACK_PTRS) && (GC_GRANULE_BYTES < 0x10)
GC_ASSERT(ADDR(result) > (word)NOT_MARKED);
# endif
/* No update of scratch free area pointer; */
/* get memory directly. */
# ifdef USE_SCRATCH_LAST_END_PTR
/* Update end point of last obtained area (needed only */
/* by GC_register_dynamic_libraries for some targets). */
GC_scratch_last_end_addr = ADDR(result) + bytes;
# endif
}
return result;
}
bytes_to_get = ROUNDUP_PAGESIZE_IF_MMAP(MINHINCR * HBLKSIZE);
/* round up for safety */
result = GC_os_get_mem(bytes_to_get);
if (EXPECT(NULL == result, FALSE)) {
WARN("Out of memory - trying to allocate requested amount"
" (%" WARN_PRIuPTR " bytes)...\n", bytes);
bytes_to_get = ROUNDUP_PAGESIZE_IF_MMAP(bytes);
result = GC_os_get_mem(bytes_to_get);
if (result != NULL) {
# ifdef USE_SCRATCH_LAST_END_PTR
GC_scratch_last_end_addr = ADDR(result) + bytes;
# endif
}
return result;
}
/* TODO: some amount of unallocated space may remain unused forever */
/* Update scratch area pointers and retry. */
GC_scratch_free_ptr = result;
GC_scratch_end_addr = ADDR(GC_scratch_free_ptr) + bytes_to_get;
# ifdef USE_SCRATCH_LAST_END_PTR
GC_scratch_last_end_addr = GC_scratch_end_addr;
# endif
}
}
/* Return an uninitialized header */
static hdr * alloc_hdr(void)
{
hdr * result;
GC_ASSERT(I_HOLD_LOCK());
if (NULL == GC_hdr_free_list) {
result = (hdr *)GC_scratch_alloc(sizeof(hdr));
} else {
result = GC_hdr_free_list;
GC_hdr_free_list = (hdr *)(result -> hb_next);
}
return result;
}
GC_INLINE void free_hdr(hdr * hhdr)
{
hhdr -> hb_next = (struct hblk *)GC_hdr_free_list;
GC_hdr_free_list = hhdr;
}
#ifdef COUNT_HDR_CACHE_HITS
/* Used for debugging/profiling (the symbols are externally visible). */
word GC_hdr_cache_hits = 0;
word GC_hdr_cache_misses = 0;
#endif
GC_INNER void GC_init_headers(void)
{
unsigned i;
GC_ASSERT(I_HOLD_LOCK());
GC_ASSERT(NULL == GC_all_nils);
GC_all_nils = (bottom_index *)GC_scratch_alloc(sizeof(bottom_index));
if (GC_all_nils == NULL) {
GC_err_printf("Insufficient memory for GC_all_nils\n");
EXIT();
}
BZERO(GC_all_nils, sizeof(bottom_index));
for (i = 0; i < TOP_SZ; i++) {
GC_top_index[i] = GC_all_nils;
}
}
/* Make sure that there is a bottom level index block for address addr. */
/* Return FALSE on failure. */
static GC_bool get_index(word addr)
{
word hi = addr >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
bottom_index * r;
bottom_index * p;
bottom_index ** prev;
bottom_index *pi; /* old_p */
word i;
GC_ASSERT(I_HOLD_LOCK());
# ifdef HASH_TL
i = TL_HASH(hi);
pi = GC_top_index[i];
for (p = pi; p != GC_all_nils; p = p -> hash_link) {
if (p -> key == hi) return TRUE;
}
# else
if (GC_top_index[hi] != GC_all_nils)
return TRUE;
i = hi;
# endif
r = (bottom_index *)GC_scratch_alloc(sizeof(bottom_index));
if (EXPECT(NULL == r, FALSE))
return FALSE;
BZERO(r, sizeof(bottom_index));
r -> key = hi;
# ifdef HASH_TL
r -> hash_link = pi;
# endif
/* Add it to the list of bottom indices */
prev = &GC_all_bottom_indices; /* pointer to p */
pi = 0; /* bottom_index preceding p */
while ((p = *prev) != 0 && p -> key < hi) {
pi = p;
prev = &(p -> asc_link);
}
r -> desc_link = pi;
if (0 == p) {
GC_all_bottom_indices_end = r;
} else {
p -> desc_link = r;
}
r -> asc_link = p;
*prev = r;
GC_top_index[i] = r;
return TRUE;
}
GC_INNER hdr * GC_install_header(struct hblk *h)
{
hdr * result;
GC_ASSERT(I_HOLD_LOCK());
if (EXPECT(!get_index(ADDR(h)), FALSE)) return NULL;
result = alloc_hdr();
if (EXPECT(result != NULL, TRUE)) {
GC_ASSERT(!IS_FORWARDING_ADDR_OR_NIL(result));
SET_HDR(h, result);
# ifdef USE_MUNMAP
result -> hb_last_reclaimed = (unsigned short)GC_gc_no;
# endif
}
return result;
}
GC_INNER GC_bool GC_install_counts(struct hblk *h, size_t sz /* bytes */)
{
struct hblk * hbp;
for (hbp = h; ADDR_LT((ptr_t)hbp, (ptr_t)h + sz); hbp += BOTTOM_SZ) {
if (!get_index(ADDR(hbp)))
return FALSE;
if (ADDR(hbp) > GC_WORD_MAX - (word)BOTTOM_SZ * HBLKSIZE)
break; /* overflow of hbp+=BOTTOM_SZ is expected */
}
if (!get_index(ADDR(h) + sz - 1))
return FALSE;
GC_ASSERT(!IS_FORWARDING_ADDR_OR_NIL(HDR(h)));
for (hbp = h + 1; ADDR_LT((ptr_t)hbp, (ptr_t)h + sz); hbp++) {
word i = (word)HBLK_PTR_DIFF(hbp, h);
SET_HDR(hbp, (hdr *)(i > MAX_JUMP ? MAX_JUMP : i));
}
return TRUE;
}
GC_INNER void GC_remove_header(struct hblk *h)
{
hdr **ha;
GET_HDR_ADDR(h, ha);
free_hdr(*ha);
*ha = 0;
}
GC_INNER void GC_remove_counts(struct hblk *h, size_t sz /* bytes */)
{
struct hblk * hbp;
if (sz <= HBLKSIZE) return;
if (NULL == HDR(h + 1)) {
# ifdef GC_ASSERTIONS
for (hbp = h + 2; ADDR_LT((ptr_t)hbp, (ptr_t)h + sz); hbp++) {
GC_ASSERT(NULL == HDR(hbp));
}
# endif
return;
}
for (hbp = h + 1; ADDR_LT((ptr_t)hbp, (ptr_t)h + sz); hbp++) {
SET_HDR(hbp, NULL);
}
}
#define HBLK_ADDR(bi, j) \
((((bi) -> key << LOG_BOTTOM_SZ) + (word)(j)) << LOG_HBLKSIZE)
GC_API void GC_CALL GC_apply_to_all_blocks(GC_walk_hblk_fn fn,
void *client_data)
{
bottom_index * bi;
for (bi = GC_all_bottom_indices; bi != NULL; bi = bi -> asc_link) {
signed_word j;
for (j = BOTTOM_SZ-1; j >= 0;) {
hdr * hhdr = bi -> index[j];
if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
j -= (signed_word)(hhdr != NULL ? ADDR(hhdr) : 1);
} else {
if (!HBLK_IS_FREE(hhdr)) {
GC_ASSERT(HBLK_ADDR(bi, j) == ADDR(hhdr -> hb_block));
fn(hhdr -> hb_block, client_data);
}
j--;
}
}
}
}
GC_INNER struct hblk * GC_next_block(struct hblk *h, GC_bool allow_free)
{
REGISTER bottom_index * bi;
REGISTER size_t j = (size_t)(ADDR(h) >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
GC_ASSERT(I_HOLD_READER_LOCK());
GET_BI(h, bi);
if (bi == GC_all_nils) {
REGISTER word hi = ADDR(h) >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
bi = GC_all_bottom_indices;
while (bi != NULL && bi -> key < hi) bi = bi -> asc_link;
j = 0;
}
for (; bi != NULL; bi = bi -> asc_link) {
while (j < BOTTOM_SZ) {
hdr * hhdr = bi -> index[j];
if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
j++;
} else {
if (allow_free || !HBLK_IS_FREE(hhdr)) {
GC_ASSERT(HBLK_ADDR(bi, j) == ADDR(hhdr -> hb_block));
return hhdr -> hb_block;
}
j += divHBLKSZ(hhdr -> hb_sz);
}
}
j = 0;
}
return NULL;
}
GC_INNER struct hblk * GC_prev_block(struct hblk *h)
{
bottom_index * bi;
signed_word j = (ADDR(h) >> LOG_HBLKSIZE) & (BOTTOM_SZ-1);
GC_ASSERT(I_HOLD_READER_LOCK());
GET_BI(h, bi);
if (bi == GC_all_nils) {
word hi = ADDR(h) >> (LOG_BOTTOM_SZ + LOG_HBLKSIZE);
bi = GC_all_bottom_indices_end;
while (bi != NULL && bi -> key > hi)
bi = bi -> desc_link;
j = BOTTOM_SZ - 1;
}
for (; bi != NULL; bi = bi -> desc_link) {
while (j >= 0) {
hdr * hhdr = bi -> index[j];
if (NULL == hhdr) {
--j;
} else if (IS_FORWARDING_ADDR_OR_NIL(hhdr)) {
j -= (signed_word)ADDR(hhdr);
} else {
/* TODO: return hhdr -> hb_block instead */
return (struct hblk *)HBLK_ADDR(bi, j);
}
}
j = BOTTOM_SZ - 1;
}
return NULL;
}