-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgc.c
437 lines (392 loc) · 8.95 KB
/
gc.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
module : gc.c
version : 1.54
date : 11/20/24
*/
#ifdef MALLOC_DEBUG
#include "rmalloc.h"
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>
#include <setjmp.h>
#include <signal.h>
#ifdef _MSC_VER
#pragma warning(disable: 4267)
#endif
#ifdef __linux__
#include <unistd.h>
#endif
#ifdef __APPLE__
#include <mach-o/getsect.h>
#endif
#include "khash.h"
#include "gc.h"
#ifdef _MSC_VER
#define DOWN_64K ~0xFFFF
#define PEPOINTER 15
#define IMAGE_BASE 13
#define BASE_OF_CODE 11
#define SIZE_OF_CODE 7
#define SIZE_OF_DATA 8
#define SIZE_OF_BSS 9
#endif
#define GC_COLL 0
#define GC_LEAF 1
#define GC_MARK 2
#define GROW_FACTOR 2
#define BSS_ALIGN 4
#define MIN_ITEMS 170 /* initial number of items */
/*
* When pointers are aligned at 16 bytes, the lower 4 bits are always zero.
*/
#define HASH_FUNCTION(key) (khint_t)((key) >> 4)
typedef struct mem_info {
unsigned flags: 2;
unsigned size: 30;
} mem_info;
/*
* The map contains a pointer as key and mem_info as value.
*/
KHASH_INIT(Backup, uint64_t, mem_info, 1, HASH_FUNCTION, kh_int64_hash_equal)
static khash_t(Backup) *MEM; /* backup of pointers */
static khint_t max_items; /* max. items before gc */
static uint64_t lower, upper; /* heap bounds */
#ifdef COUNT_COLLECTIONS
static size_t GC_gc_no; /* number of garbage collections */
static size_t memory_use; /* amount of memory currently used */
static size_t free_bytes; /* amount of memory on the freelist */
#endif
/*
* Pointers to memory segments.
*/
#ifdef SCAN_BSS_MEMORY
static uint64_t start_of_text,
start_of_data,
start_of_bss,
start_of_heap;
#endif
/*
* fatal - Report a fatal error and end the program. The message is taken from
* yacc. The function should be present in main.c
*/
#ifdef _MSC_VER
void fatal(char *str);
#endif
/*
* Determine sections of memory. This is highly system dependent and not tested
* on __APPLE__.
*/
#ifdef SCAN_BSS_MEMORY
static void init_heap(void)
{
extern int main(int argc, char **argv);
#ifdef _MSC_VER
int *ptr;
#endif
start_of_text = (uint64_t)main;
#ifdef __CYGWIN__
extern char __data_start__, __bss_start__, __bss_end__;
#if 0
extern char __data_end__, __end__;
#endif
start_of_data = (uint64_t)&__data_start__;
start_of_bss = (uint64_t)&__bss_start__;
start_of_heap = (uint64_t)&__bss_end__;
#endif
#ifdef __linux__
extern char etext, edata, end;
start_of_data = (uint64_t)&etext;
start_of_bss = (uint64_t)&edata;
start_of_heap = (uint64_t)&end;
#endif
#ifdef __APPLE__
start_of_data = (uint64_t)get_etext();
start_of_bss = (uint64_t)get_edata();
start_of_heap = (uint64_t)get_end();
#endif
#ifdef _MSC_VER
start_of_text &= DOWN_64K;
ptr = (int *)start_of_text;
ptr += ptr[PEPOINTER] / 4;
start_of_text = ptr[IMAGE_BASE] + ptr[BASE_OF_CODE];
start_of_data = start_of_text + ptr[SIZE_OF_CODE];
start_of_bss = start_of_data + ptr[SIZE_OF_DATA];
start_of_heap = start_of_bss + ptr[SIZE_OF_BSS];
#endif
}
#endif
/*
* Report of the amount of memory allocated is delegated to valgrind.
*/
#ifdef FREE_ON_EXIT
static void mem_exit(void)
{
khint_t key;
for (key = 0; key != kh_end(MEM); key++)
if (kh_exist(MEM, key))
free((void *)kh_key(MEM, key));
kh_destroy(Backup, MEM);
}
#endif
/*
* Initialise gc memory.
*/
void GC_INIT(void)
{
#ifdef SCAN_BSS_MEMORY
init_heap();
#endif
#ifdef FREE_ON_EXIT
free(malloc(1));
atexit(mem_exit);
#endif
MEM = kh_init(Backup);
max_items = MIN_ITEMS;
}
/*
* Mark a block as in use. No optimization for this (recursive) function.
*/
static void mark_ptr(char *ptr)
{
khint_t key;
size_t i, size;
uint64_t value;
value = (uint64_t)ptr;
if (value < lower || value >= upper)
return;
if ((key = kh_get(Backup, MEM, value)) != kh_end(MEM)) {
if (kh_val(MEM, key).flags & GC_MARK)
return;
kh_val(MEM, key).flags |= GC_MARK;
if (kh_val(MEM, key).flags & GC_LEAF)
return;
size = kh_val(MEM, key).size / sizeof(char *);
for (i = 0; i < size; i++)
mark_ptr(((char **)value)[i]); /* recursion is suspicious */
}
}
/*
* Mark blocks that can be found on the stack.
*/
static void mark_stk(void)
{
uint64_t ptr = (uint64_t)&ptr;
#ifdef STACK_GROWS_UPWARD
if (ptr > bottom)
for (; ptr > (uint64_t)bottom_of_stack; ptr -= sizeof(char *))
mark_ptr(*(char **)ptr);
else
#endif
for (; ptr < (uint64_t)bottom_of_stack; ptr += sizeof(char *))
mark_ptr(*(char **)ptr);
}
/*
* Mark blocks that are pointed to from static uninitialized memory.
*/
#ifdef SCAN_BSS_MEMORY
static void mark_bss(void)
{
uint64_t ptr, end_of_bss;
end_of_bss = start_of_heap - sizeof(void *);
for (ptr = start_of_bss; ptr <= end_of_bss; ptr += BSS_ALIGN)
mark_ptr(*(char **)ptr);
}
#endif
/*
* Walk registered blocks and free those that have not been marked.
*/
static void scan(void)
{
khint_t key;
for (key = 0; key != kh_end(MEM); key++) {
if (kh_exist(MEM, key)) {
if (kh_val(MEM, key).flags & GC_MARK)
kh_val(MEM, key).flags &= ~GC_MARK;
else {
#ifdef COUNT_COLLECTIONS
free_bytes += kh_val(MEM, key).size;
#endif
free((void *)kh_key(MEM, key));
kh_del(Backup, MEM, key);
}
}
}
}
/*
* Collect garbage.
*
* Pointers that are reachable from registers or stack are marked
* as well as all pointers that are reachable from those pointers.
* In other words: roots for garbage collection are searched in
* registers, on the stack, and in the blocks themselves.
*/
void GC_gcollect(void)
{
jmp_buf env;
void (* volatile m)(void) = mark_stk;
memset(&env, 0, sizeof(jmp_buf));
setjmp(env);
(*m)();
#ifdef SCAN_BSS_MEMORY
mark_bss();
#endif
scan();
#ifdef COUNT_COLLECTIONS
GC_gc_no++;
#endif
}
/*
* Register an allocated memory block and garbage collect if there are too many
* blocks already.
*/
static void remind(char *ptr, size_t size, int flags)
{
int rv;
khint_t key;
uint64_t value;
value = (uint64_t)ptr;
if (lower > value || !lower)
lower = value;
if (upper < value + size)
upper = value + size;
key = kh_put(Backup, MEM, value, &rv);
kh_val(MEM, key).flags = flags;
kh_val(MEM, key).size = size;
#ifdef COUNT_COLLECTIONS
memory_use += size;
#endif
/*
* See if there are already too many items allocated. If yes, trigger the
* garbage collector. As the number of items that need to be remembered is
* unknown, it is set to twice the number of items that are currently being
* used. This allows a 100% growth in the number of items allocated.
*/
if (max_items < kh_size(MEM)) {
GC_gcollect();
max_items = kh_size(MEM) * GROW_FACTOR;
}
}
/*
* Register an allocated memory block. The block is cleared with zeroes.
*/
static void *mem_block(size_t size, int leaf)
{
void *ptr = 0;
ptr = malloc(size);
#ifdef _MSC_VER
if (!ptr)
fatal("memory exhausted");
#endif
memset(ptr, 0, size);
remind(ptr, size, leaf);
return ptr;
}
/*
* Register a memory block that contains no other blocks.
*/
#ifdef USE_GC_MALLOC_ATOMIC
void *GC_malloc_atomic(size_t size)
{
return mem_block(size, GC_LEAF);
}
#endif
/*
* Register a memory block that can be collected.
*/
#ifdef USE_GC_MALLOC
void *GC_malloc(size_t size)
{
return mem_block(size, GC_COLL);
}
#endif
#ifdef USE_GC_REALLOC
/*
* Forget about a memory block and return its flags.
*/
#ifdef COUNT_COLLECTIONS
static unsigned char forget(void *ptr, unsigned *size)
#else
static unsigned char forget(void *ptr)
#endif
{
khint_t key;
unsigned char flags = 0;
if ((key = kh_get(Backup, MEM, (uint64_t)ptr)) != kh_end(MEM)) {
flags = kh_val(MEM, key).flags;
#ifdef COUNT_COLLECTIONS
*size = kh_val(MEM, key).size;
#endif
kh_del(Backup, MEM, key);
}
return flags;
}
/*
* Enlarge an already allocated memory block.
*/
void *GC_realloc(void *ptr, size_t size)
{
unsigned char flags;
#ifdef COUNT_COLLECTIONS
unsigned old_size = 0;
#endif
if (!ptr)
return GC_malloc(size);
#ifdef COUNT_COLLECTIONS
flags = forget(ptr, &old_size);
memory_use -= old_size;
memory_use += size;
#else
flags = forget(ptr);
#endif
ptr = realloc(ptr, size);
#ifdef _MSC_VER
if (!ptr)
fatal("memory exhausted");
#endif
remind(ptr, size, flags);
return ptr;
}
#endif
/*
* Duplicate a string. A string does not contain internal pointers.
*/
#ifdef USE_GC_STRDUP
char *GC_strdup(const char *str)
{
char *ptr;
size_t leng;
leng = strlen(str) + 1;
if ((ptr = GC_malloc_atomic(leng)) != 0)
strcpy(ptr, str);
return ptr;
}
#endif
#ifdef COUNT_COLLECTIONS
/*
* Return the number of garbage collections.
* This is reported in the main function.
*/
size_t GC_get_gc_no(void)
{
return GC_gc_no;
}
/*
* Return the amount of memory currently in use.
*/
/* LCOV_EXCL_START */
size_t GC_get_memory_use(void)
{
return memory_use;
}
/*
* Return the amount of memory on the freelist.
*/
size_t GC_get_free_bytes(void)
{
return free_bytes;
}
/* LCOV_EXCL_STOP */
#endif