blob: 9c8d203d28354c73204f952975c9e85e76097201 [file] [log] [blame]
Damiendcced922013-10-21 23:45:08 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <stdint.h>
4#include <string.h>
5
Damiend99b0522013-12-21 18:17:45 +00006#include "mpconfig.h"
Damiendcced922013-10-21 23:45:08 +01007#include "gc.h"
8
Damien Georged3ebe482014-01-07 15:20:33 +00009#if MICROPY_ENABLE_GC
10
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020011#if 0 // print debugging info
12#define DEBUG_PRINT (1)
13#define DEBUG_printf(args...) printf(args)
14#else // don't print debugging info
15#define DEBUG_printf(args...) (void)0
16#endif
17
Damiendcced922013-10-21 23:45:08 +010018typedef unsigned char byte;
19
Damiendcced922013-10-21 23:45:08 +010020#define WORDS_PER_BLOCK (4)
21#define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD)
22#define STACK_SIZE (64) // tunable; minimum is 1
23
24static byte *gc_alloc_table_start;
Damiendcced922013-10-21 23:45:08 +010025static machine_uint_t gc_alloc_table_byte_len;
26static machine_uint_t *gc_pool_start;
27static machine_uint_t *gc_pool_end;
28
29static int gc_stack_overflow;
30static machine_uint_t gc_stack[STACK_SIZE];
31static machine_uint_t *gc_sp;
32
Damiendcced922013-10-21 23:45:08 +010033// ATB = allocation table byte
34// 0b00 = FREE -- free block
35// 0b01 = HEAD -- head of a chain of blocks
36// 0b10 = TAIL -- in the tail of a chain of blocks
37// 0b11 = MARK -- marked head block
38
39#define AT_FREE (0)
40#define AT_HEAD (1)
41#define AT_TAIL (2)
42#define AT_MARK (3)
43
44#define BLOCKS_PER_ATB (4)
45#define ATB_MASK_0 (0x03)
46#define ATB_MASK_1 (0x0c)
47#define ATB_MASK_2 (0x30)
48#define ATB_MASK_3 (0xc0)
49
50#define ATB_0_IS_FREE(a) (((a) & ATB_MASK_0) == 0)
51#define ATB_1_IS_FREE(a) (((a) & ATB_MASK_1) == 0)
52#define ATB_2_IS_FREE(a) (((a) & ATB_MASK_2) == 0)
53#define ATB_3_IS_FREE(a) (((a) & ATB_MASK_3) == 0)
54
55#define BLOCK_SHIFT(block) (2 * ((block) & (BLOCKS_PER_ATB - 1)))
56#define ATB_GET_KIND(block) ((gc_alloc_table_start[(block) / BLOCKS_PER_ATB] >> BLOCK_SHIFT(block)) & 3)
57#define ATB_ANY_TO_FREE(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_MARK << BLOCK_SHIFT(block))); } while (0)
58#define ATB_FREE_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_HEAD << BLOCK_SHIFT(block)); } while (0)
59#define ATB_FREE_TO_TAIL(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_TAIL << BLOCK_SHIFT(block)); } while (0)
60#define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
61#define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
62
Damiendcced922013-10-21 23:45:08 +010063#define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
64#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start))
65#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
66
Damienbb5316b2013-10-22 21:12:29 +010067// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
68void gc_init(void *start, void *end) {
69 // align end pointer on block boundary
70 end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020071 DEBUG_printf("Initializing GC heap: %p-%p\n", start, end);
Damienbb5316b2013-10-22 21:12:29 +010072
73 // calculate parameters for GC
74 machine_uint_t total_word_len = (machine_uint_t*)end - (machine_uint_t*)start;
75 gc_alloc_table_byte_len = total_word_len * BYTES_PER_WORD / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
76 gc_alloc_table_start = (byte*)start;
77 machine_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BITS_PER_BYTE / 2;
78 machine_uint_t gc_pool_word_len = gc_pool_block_len * WORDS_PER_BLOCK;
79 gc_pool_start = (machine_uint_t*)end - gc_pool_word_len;
80 gc_pool_end = end;
81
82 // clear ATBs
83 memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len);
84
85 // allocate first block because gc_pool_start points there and it will never
86 // be freed, so allocating 1 block with null pointers will minimise memory loss
87 ATB_FREE_TO_HEAD(0);
88 for (int i = 0; i < WORDS_PER_BLOCK; i++) {
89 gc_pool_start[i] = 0;
90 }
91
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020092 DEBUG_printf("GC layout:\n");
93 DEBUG_printf(" alloc table at %p, length %u bytes\n", gc_alloc_table_start, gc_alloc_table_byte_len);
94 DEBUG_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);
Damienbb5316b2013-10-22 21:12:29 +010095}
96
Damienfd8b6bc2013-10-22 20:26:36 +010097#define VERIFY_PTR(ptr) ( \
98 (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
99 && ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \
100 && ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \
101 )
102
Damiendcced922013-10-21 23:45:08 +0100103#define VERIFY_MARK_AND_PUSH(ptr) \
104 do { \
Damienfd8b6bc2013-10-22 20:26:36 +0100105 if (VERIFY_PTR(ptr)) { \
Damiendcced922013-10-21 23:45:08 +0100106 machine_uint_t _block = BLOCK_FROM_PTR(ptr); \
107 if (ATB_GET_KIND(_block) == AT_HEAD) { \
108 /* an unmarked head, mark it, and push it on gc stack */ \
109 ATB_HEAD_TO_MARK(_block); \
110 if (gc_sp < &gc_stack[STACK_SIZE]) { \
111 *gc_sp++ = _block; \
112 } else { \
113 gc_stack_overflow = 1; \
114 } \
115 } \
116 } \
117 } while (0)
118
Damien8b3a7c22013-10-23 20:20:17 +0100119static void gc_drain_stack(void) {
Damiendcced922013-10-21 23:45:08 +0100120 while (gc_sp > gc_stack) {
121 // pop the next block off the stack
122 machine_uint_t block = *--gc_sp;
123
Damieneefcc792013-10-22 15:25:25 +0100124 // work out number of consecutive blocks in the chain starting with this one
Damiendcced922013-10-21 23:45:08 +0100125 machine_uint_t n_blocks = 0;
126 do {
127 n_blocks += 1;
128 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
129
130 // check this block's children
131 machine_uint_t *scan = (machine_uint_t*)PTR_FROM_BLOCK(block);
132 for (machine_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) {
133 machine_uint_t ptr2 = *scan;
134 VERIFY_MARK_AND_PUSH(ptr2);
135 }
136 }
137}
138
Damien8b3a7c22013-10-23 20:20:17 +0100139static void gc_deal_with_stack_overflow(void) {
Damiendcced922013-10-21 23:45:08 +0100140 while (gc_stack_overflow) {
141 gc_stack_overflow = 0;
142 gc_sp = gc_stack;
143
144 // scan entire memory looking for blocks which have been marked but not their children
145 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
146 // trace (again) if mark bit set
147 if (ATB_GET_KIND(block) == AT_MARK) {
148 *gc_sp++ = block;
149 gc_drain_stack();
150 }
151 }
152 }
153}
154
Damien8b3a7c22013-10-23 20:20:17 +0100155static void gc_sweep(void) {
Damiendcced922013-10-21 23:45:08 +0100156 // free unmarked heads and their tails
157 int free_tail = 0;
158 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
159 switch (ATB_GET_KIND(block)) {
160 case AT_HEAD:
161 free_tail = 1;
162 // fall through to free the head
163
164 case AT_TAIL:
165 if (free_tail) {
166 ATB_ANY_TO_FREE(block);
167 }
168 break;
169
170 case AT_MARK:
171 ATB_MARK_TO_HEAD(block);
172 free_tail = 0;
173 break;
174 }
175 }
176}
177
Damien8b3a7c22013-10-23 20:20:17 +0100178void gc_collect_start(void) {
Damiendcced922013-10-21 23:45:08 +0100179 gc_stack_overflow = 0;
180 gc_sp = gc_stack;
181}
182
183void gc_collect_root(void **ptrs, machine_uint_t len) {
184 for (machine_uint_t i = 0; i < len; i++) {
185 machine_uint_t ptr = (machine_uint_t)ptrs[i];
186 VERIFY_MARK_AND_PUSH(ptr);
187 gc_drain_stack();
188 }
189}
190
Damien8b3a7c22013-10-23 20:20:17 +0100191void gc_collect_end(void) {
Damiendcced922013-10-21 23:45:08 +0100192 gc_deal_with_stack_overflow();
193 gc_sweep();
Damieneefcc792013-10-22 15:25:25 +0100194}
Damiendcced922013-10-21 23:45:08 +0100195
Damieneefcc792013-10-22 15:25:25 +0100196void gc_info(gc_info_t *info) {
197 info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t);
198 info->used = 0;
199 info->free = 0;
200 info->num_1block = 0;
201 info->num_2block = 0;
202 info->max_block = 0;
203 for (machine_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
204 machine_uint_t kind = ATB_GET_KIND(block);
205 if (kind == AT_FREE || kind == AT_HEAD) {
206 if (len == 1) {
207 info->num_1block += 1;
208 } else if (len == 2) {
209 info->num_2block += 1;
210 }
211 if (len > info->max_block) {
212 info->max_block = len;
213 }
214 }
215 switch (kind) {
Damiendcced922013-10-21 23:45:08 +0100216 case AT_FREE:
Damieneefcc792013-10-22 15:25:25 +0100217 info->free += 1;
218 len = 0;
Damiendcced922013-10-21 23:45:08 +0100219 break;
220
221 case AT_HEAD:
Damieneefcc792013-10-22 15:25:25 +0100222 info->used += 1;
223 len = 1;
224 break;
225
Damiendcced922013-10-21 23:45:08 +0100226 case AT_TAIL:
Damieneefcc792013-10-22 15:25:25 +0100227 info->used += 1;
228 len += 1;
Damiendcced922013-10-21 23:45:08 +0100229 break;
230
231 case AT_MARK:
Damieneefcc792013-10-22 15:25:25 +0100232 // shouldn't happen
Damiendcced922013-10-21 23:45:08 +0100233 break;
234 }
235 }
236
Damieneefcc792013-10-22 15:25:25 +0100237 info->used *= BYTES_PER_BLOCK;
238 info->free *= BYTES_PER_BLOCK;
Damiendcced922013-10-21 23:45:08 +0100239}
240
241void *gc_alloc(machine_uint_t n_bytes) {
242 machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200243 DEBUG_printf("gc_alloc(%u bytes -> %u blocks)\n", n_bytes, n_blocks);
Damiendcced922013-10-21 23:45:08 +0100244
245 // check for 0 allocation
246 if (n_blocks == 0) {
247 return NULL;
248 }
249
250 machine_uint_t i;
251 machine_uint_t end_block;
252 machine_uint_t start_block;
253 machine_uint_t n_free = 0;
254 int collected = 0;
255 for (;;) {
256
257 // look for a run of n_blocks available blocks
258 for (i = 0; i < gc_alloc_table_byte_len; i++) {
259 byte a = gc_alloc_table_start[i];
260 if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; }
261 if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; }
262 if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; }
263 if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; }
264 }
265
266 // nothing found!
267 if (collected) {
268 return NULL;
269 }
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200270 DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes);
Damiendcced922013-10-21 23:45:08 +0100271 gc_collect();
272 collected = 1;
273 }
274
275 // found, ending at block i inclusive
276found:
277 // get starting and end blocks, both inclusive
278 end_block = i;
279 start_block = i - n_free + 1;
280
281 // mark first block as used head
282 ATB_FREE_TO_HEAD(start_block);
283
284 // mark rest of blocks as used tail
285 // TODO for a run of many blocks can make this more efficient
286 for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) {
287 ATB_FREE_TO_TAIL(bl);
288 }
289
290 // return pointer to first block
291 return (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
292}
293
Damienfd8b6bc2013-10-22 20:26:36 +0100294// force the freeing of a piece of memory
295void gc_free(void *ptr_in) {
296 machine_uint_t ptr = (machine_uint_t)ptr_in;
297
298 if (VERIFY_PTR(ptr)) {
299 machine_uint_t block = BLOCK_FROM_PTR(ptr);
300 if (ATB_GET_KIND(block) == AT_HEAD) {
301 // free head and all of its tail blocks
302 do {
303 ATB_ANY_TO_FREE(block);
304 block += 1;
305 } while (ATB_GET_KIND(block) == AT_TAIL);
306 }
307 }
308}
309
Damiendcced922013-10-21 23:45:08 +0100310machine_uint_t gc_nbytes(void *ptr_in) {
311 machine_uint_t ptr = (machine_uint_t)ptr_in;
312
Damienfd8b6bc2013-10-22 20:26:36 +0100313 if (VERIFY_PTR(ptr)) {
Damiendcced922013-10-21 23:45:08 +0100314 machine_uint_t block = BLOCK_FROM_PTR(ptr);
315 if (ATB_GET_KIND(block) == AT_HEAD) {
316 // work out number of consecutive blocks in the chain starting with this on
317 machine_uint_t n_blocks = 0;
318 do {
319 n_blocks += 1;
320 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
321 return n_blocks * BYTES_PER_BLOCK;
322 }
323 }
324
325 // invalid pointer
326 return 0;
327}
328
329void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
330 machine_uint_t n_existing = gc_nbytes(ptr);
331 if (n_bytes <= n_existing) {
332 return ptr;
333 } else {
Damiend2c1a732013-10-23 21:03:27 +0100334 // TODO check if we can grow inplace
Damiendcced922013-10-21 23:45:08 +0100335 void *ptr2 = gc_alloc(n_bytes);
Paul Sokolovskyc0a83742014-02-11 15:28:37 +0200336 if (ptr2 == NULL) {
337 return ptr2;
338 }
Damiendcced922013-10-21 23:45:08 +0100339 memcpy(ptr2, ptr, n_existing);
Damiend2c1a732013-10-23 21:03:27 +0100340 gc_free(ptr);
Damiendcced922013-10-21 23:45:08 +0100341 return ptr2;
342 }
343}
344
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200345void gc_dump_info() {
346 gc_info_t info;
347 gc_info(&info);
348 printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free);
349 printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n",
350 info.num_1block, info.num_2block, info.max_block);
351}
352
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200353#if DEBUG_PRINT
Damien8b3a7c22013-10-23 20:20:17 +0100354static void gc_dump_at(void) {
Damieneefcc792013-10-22 15:25:25 +0100355 for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200356 printf("block %06u ", bl);
Damieneefcc792013-10-22 15:25:25 +0100357 switch (ATB_GET_KIND(bl)) {
358 case AT_FREE: printf("FREE"); break;
359 case AT_HEAD: printf("HEAD"); break;
360 case AT_TAIL: printf("TAIL"); break;
361 default: printf("MARK"); break;
362 }
363 printf("\n");
364 }
365}
366
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200367void gc_test(void) {
368 machine_uint_t len = 500;
Damiendcced922013-10-21 23:45:08 +0100369 machine_uint_t *heap = malloc(len);
370 gc_init(heap, heap + len / sizeof(machine_uint_t));
371 void *ptrs[100];
372 {
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200373 machine_uint_t **p = gc_alloc(16);
Damiendcced922013-10-21 23:45:08 +0100374 p[0] = gc_alloc(64);
375 p[1] = gc_alloc(1);
376 p[2] = gc_alloc(1);
377 p[3] = gc_alloc(1);
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200378 machine_uint_t ***p2 = gc_alloc(16);
Damiendcced922013-10-21 23:45:08 +0100379 p2[0] = p;
380 p2[1] = p;
381 ptrs[0] = p2;
382 }
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200383 for (int i = 0; i < 25; i+=2) {
Damiendcced922013-10-21 23:45:08 +0100384 machine_uint_t *p = gc_alloc(i);
385 printf("p=%p\n", p);
386 if (i & 3) {
387 //ptrs[i] = p;
388 }
389 }
390
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200391 printf("Before GC:\n");
Damiendcced922013-10-21 23:45:08 +0100392 gc_dump_at();
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200393 printf("Starting GC...\n");
394 gc_collect_start();
395 gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*));
396 gc_collect_end();
397 printf("After GC:\n");
Damiendcced922013-10-21 23:45:08 +0100398 gc_dump_at();
399}
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200400#endif
Damien Georged3ebe482014-01-07 15:20:33 +0000401
402#endif // MICROPY_ENABLE_GC