Skip to content

Commit dcced92

Browse files
committed
Add mark-sweep garbage collector.
1 parent f48cf67 commit dcced92

2 files changed

Lines changed: 340 additions & 0 deletions

File tree

py/gc.c

Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,332 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdint.h>
4+
#include <string.h>
5+
6+
#include "mpyconfig.h"
7+
#include "gc.h"
8+
9+
// a machine word is big enough to hold a pointer
10+
/*
11+
#define BYTES_PER_WORD (8)
12+
typedef unsigned long machine_uint_t;
13+
*/
14+
typedef unsigned char byte;
15+
16+
#define BITS_PER_BYTE (8)
17+
#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
18+
#define WORDS_PER_BLOCK (4)
19+
#define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD)
20+
#define STACK_SIZE (64) // tunable; minimum is 1
21+
22+
static byte *gc_alloc_table_start;
23+
static byte *gc_alloc_table_end;
24+
static machine_uint_t gc_alloc_table_byte_len;
25+
static machine_uint_t *gc_pool_start;
26+
static machine_uint_t *gc_pool_end;
27+
28+
static int gc_stack_overflow;
29+
static machine_uint_t gc_stack[STACK_SIZE];
30+
static machine_uint_t *gc_sp;
31+
32+
// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
33+
void gc_init(void *start, void *end) {
34+
// align end pointer on block boundary
35+
end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
36+
machine_uint_t total_word_len = (machine_uint_t*)end - (machine_uint_t*)start;
37+
gc_alloc_table_byte_len = total_word_len * BYTES_PER_WORD / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
38+
gc_alloc_table_start = (byte*)start;
39+
gc_alloc_table_end = gc_alloc_table_start + gc_alloc_table_byte_len;
40+
machine_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BITS_PER_BYTE / 2;
41+
machine_uint_t gc_pool_word_len = gc_pool_block_len * WORDS_PER_BLOCK;
42+
gc_pool_start = (machine_uint_t*)end - gc_pool_word_len;
43+
gc_pool_end = end;
44+
45+
/*
46+
printf("GC layout:\n");
47+
printf(" alloc table at %p, length %u bytes\n", gc_alloc_table_start, gc_alloc_table_byte_len);
48+
printf(" pool at %p, length %u blocks = %u words = %u bytes\n", gc_pool_start, gc_pool_block_len, gc_pool_word_len, gc_pool_word_len * BYTES_PER_WORD);
49+
*/
50+
printf("GC: %u bytes\n", gc_pool_word_len * BYTES_PER_WORD);
51+
}
52+
53+
// ATB = allocation table byte
54+
// 0b00 = FREE -- free block
55+
// 0b01 = HEAD -- head of a chain of blocks
56+
// 0b10 = TAIL -- in the tail of a chain of blocks
57+
// 0b11 = MARK -- marked head block
58+
59+
#define AT_FREE (0)
60+
#define AT_HEAD (1)
61+
#define AT_TAIL (2)
62+
#define AT_MARK (3)
63+
64+
#define BLOCKS_PER_ATB (4)
65+
#define ATB_MASK_0 (0x03)
66+
#define ATB_MASK_1 (0x0c)
67+
#define ATB_MASK_2 (0x30)
68+
#define ATB_MASK_3 (0xc0)
69+
70+
#define ATB_0_IS_FREE(a) (((a) & ATB_MASK_0) == 0)
71+
#define ATB_1_IS_FREE(a) (((a) & ATB_MASK_1) == 0)
72+
#define ATB_2_IS_FREE(a) (((a) & ATB_MASK_2) == 0)
73+
#define ATB_3_IS_FREE(a) (((a) & ATB_MASK_3) == 0)
74+
75+
#define BLOCK_SHIFT(block) (2 * ((block) & (BLOCKS_PER_ATB - 1)))
76+
#define ATB_GET_KIND(block) ((gc_alloc_table_start[(block) / BLOCKS_PER_ATB] >> BLOCK_SHIFT(block)) & 3)
77+
#define ATB_ANY_TO_FREE(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_MARK << BLOCK_SHIFT(block))); } while (0)
78+
#define ATB_FREE_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_HEAD << BLOCK_SHIFT(block)); } while (0)
79+
#define ATB_FREE_TO_TAIL(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_TAIL << BLOCK_SHIFT(block)); } while (0)
80+
#define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
81+
#define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
82+
83+
void gc_dump_at() {
84+
for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
85+
printf("block % 6u ", bl);
86+
switch (ATB_GET_KIND(bl)) {
87+
case AT_FREE: printf("FREE"); break;
88+
case AT_HEAD: printf("HEAD"); break;
89+
case AT_TAIL: printf("TAIL"); break;
90+
default: printf("MARK"); break;
91+
}
92+
printf("\n");
93+
}
94+
}
95+
96+
#define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
97+
#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start))
98+
#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
99+
100+
#define VERIFY_MARK_AND_PUSH(ptr) \
101+
do { \
102+
if ( \
103+
(ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
104+
&& ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \
105+
&& ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \
106+
) { \
107+
machine_uint_t _block = BLOCK_FROM_PTR(ptr); \
108+
if (ATB_GET_KIND(_block) == AT_HEAD) { \
109+
/* an unmarked head, mark it, and push it on gc stack */ \
110+
ATB_HEAD_TO_MARK(_block); \
111+
if (gc_sp < &gc_stack[STACK_SIZE]) { \
112+
*gc_sp++ = _block; \
113+
} else { \
114+
gc_stack_overflow = 1; \
115+
} \
116+
} \
117+
} \
118+
} while (0)
119+
120+
static void gc_drain_stack() {
121+
while (gc_sp > gc_stack) {
122+
// pop the next block off the stack
123+
machine_uint_t block = *--gc_sp;
124+
125+
// work out number of consecutive blocks in the chain starting with this on
126+
machine_uint_t n_blocks = 0;
127+
do {
128+
n_blocks += 1;
129+
} while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
130+
131+
// check this block's children
132+
machine_uint_t *scan = (machine_uint_t*)PTR_FROM_BLOCK(block);
133+
for (machine_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) {
134+
machine_uint_t ptr2 = *scan;
135+
VERIFY_MARK_AND_PUSH(ptr2);
136+
}
137+
}
138+
}
139+
140+
static void gc_deal_with_stack_overflow() {
141+
while (gc_stack_overflow) {
142+
gc_stack_overflow = 0;
143+
gc_sp = gc_stack;
144+
145+
// scan entire memory looking for blocks which have been marked but not their children
146+
for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
147+
// trace (again) if mark bit set
148+
if (ATB_GET_KIND(block) == AT_MARK) {
149+
*gc_sp++ = block;
150+
gc_drain_stack();
151+
}
152+
}
153+
}
154+
}
155+
156+
static void gc_sweep() {
157+
// free unmarked heads and their tails
158+
int free_tail = 0;
159+
for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
160+
switch (ATB_GET_KIND(block)) {
161+
case AT_HEAD:
162+
free_tail = 1;
163+
// fall through to free the head
164+
165+
case AT_TAIL:
166+
if (free_tail) {
167+
ATB_ANY_TO_FREE(block);
168+
}
169+
break;
170+
171+
case AT_MARK:
172+
ATB_MARK_TO_HEAD(block);
173+
free_tail = 0;
174+
break;
175+
}
176+
}
177+
}
178+
179+
void gc_collect_start() {
180+
gc_stack_overflow = 0;
181+
gc_sp = gc_stack;
182+
}
183+
184+
void gc_collect_root(void **ptrs, machine_uint_t len) {
185+
for (machine_uint_t i = 0; i < len; i++) {
186+
machine_uint_t ptr = (machine_uint_t)ptrs[i];
187+
VERIFY_MARK_AND_PUSH(ptr);
188+
gc_drain_stack();
189+
}
190+
}
191+
192+
void gc_collect_end() {
193+
gc_deal_with_stack_overflow();
194+
gc_sweep();
195+
196+
machine_uint_t n_free = 0;
197+
machine_uint_t n_used = 0;
198+
for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
199+
switch (ATB_GET_KIND(block)) {
200+
case AT_FREE:
201+
n_free += 1;
202+
break;
203+
204+
case AT_HEAD:
205+
case AT_TAIL:
206+
n_used += 1;
207+
break;
208+
209+
case AT_MARK:
210+
break;
211+
}
212+
}
213+
214+
printf("GC %u/%u\n", n_used * BYTES_PER_BLOCK, (n_free + n_used) * BYTES_PER_BLOCK);
215+
}
216+
217+
void *gc_alloc(machine_uint_t n_bytes) {
218+
machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
219+
//printf("gc_alloc(%u bytes -> %u blocks)\n", n_bytes, n_blocks);
220+
221+
// check for 0 allocation
222+
if (n_blocks == 0) {
223+
return NULL;
224+
}
225+
226+
machine_uint_t i;
227+
machine_uint_t end_block;
228+
machine_uint_t start_block;
229+
machine_uint_t n_free = 0;
230+
int collected = 0;
231+
for (;;) {
232+
233+
// look for a run of n_blocks available blocks
234+
for (i = 0; i < gc_alloc_table_byte_len; i++) {
235+
byte a = gc_alloc_table_start[i];
236+
if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; }
237+
if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; }
238+
if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; }
239+
if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; }
240+
}
241+
242+
// nothing found!
243+
if (collected) {
244+
return NULL;
245+
}
246+
gc_collect();
247+
collected = 1;
248+
}
249+
250+
// found, ending at block i inclusive
251+
found:
252+
// get starting and end blocks, both inclusive
253+
end_block = i;
254+
start_block = i - n_free + 1;
255+
256+
// mark first block as used head
257+
ATB_FREE_TO_HEAD(start_block);
258+
259+
// mark rest of blocks as used tail
260+
// TODO for a run of many blocks can make this more efficient
261+
for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) {
262+
ATB_FREE_TO_TAIL(bl);
263+
}
264+
265+
// return pointer to first block
266+
return (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
267+
}
268+
269+
machine_uint_t gc_nbytes(void *ptr_in) {
270+
machine_uint_t ptr = (machine_uint_t)ptr_in;
271+
272+
if (
273+
(ptr & (BYTES_PER_BLOCK - 1)) == 0 // must be aligned on a block
274+
&& ptr >= (machine_uint_t)gc_pool_start // must be above start of pool
275+
&& ptr < (machine_uint_t)gc_pool_end // must be below end of pool
276+
) {
277+
machine_uint_t block = BLOCK_FROM_PTR(ptr);
278+
if (ATB_GET_KIND(block) == AT_HEAD) {
279+
// work out number of consecutive blocks in the chain starting with this on
280+
machine_uint_t n_blocks = 0;
281+
do {
282+
n_blocks += 1;
283+
} while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
284+
return n_blocks * BYTES_PER_BLOCK;
285+
}
286+
}
287+
288+
// invalid pointer
289+
return 0;
290+
}
291+
292+
void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
293+
machine_uint_t n_existing = gc_nbytes(ptr);
294+
if (n_bytes <= n_existing) {
295+
return ptr;
296+
} else {
297+
void *ptr2 = gc_alloc(n_bytes);
298+
memcpy(ptr2, ptr, n_existing);
299+
return ptr2;
300+
}
301+
}
302+
303+
/*
304+
int main() {
305+
machine_uint_t len = 1000;
306+
machine_uint_t *heap = malloc(len);
307+
gc_init(heap, heap + len / sizeof(machine_uint_t));
308+
void *ptrs[100];
309+
{
310+
machine_uint_t *p = gc_alloc(16);
311+
p[0] = gc_alloc(64);
312+
p[1] = gc_alloc(1);
313+
p[2] = gc_alloc(1);
314+
p[3] = gc_alloc(1);
315+
machine_uint_t *p2 = gc_alloc(16);
316+
p2[0] = p;
317+
p2[1] = p;
318+
ptrs[0] = p2;
319+
}
320+
for (int i = 0; i < 50; i+=2) {
321+
machine_uint_t *p = gc_alloc(i);
322+
printf("p=%p\n", p);
323+
if (i & 3) {
324+
//ptrs[i] = p;
325+
}
326+
}
327+
328+
gc_dump_at();
329+
gc_collect(ptrs, sizeof(ptrs) / sizeof(void*));
330+
gc_dump_at();
331+
}
332+
*/

py/gc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
void gc_init(void *start, void *end);
2+
void gc_collect_start();
3+
void gc_collect_root(void **ptrs, machine_uint_t len);
4+
void gc_collect_end();
5+
void gc_collect();
6+
void *gc_alloc(machine_uint_t n_bytes);
7+
machine_uint_t gc_nbytes(void *ptr_in);
8+
void *gc_realloc(void *ptr, machine_uint_t n_bytes);

0 commit comments

Comments
 (0)