blob: c8256fb03bccba6b62c0697ce2bdc7ddd136b862 [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;
243 //printf("gc_alloc(%u bytes -> %u blocks)\n", n_bytes, n_blocks);
244
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 }
270 gc_collect();
271 collected = 1;
272 }
273
274 // found, ending at block i inclusive
275found:
276 // get starting and end blocks, both inclusive
277 end_block = i;
278 start_block = i - n_free + 1;
279
280 // mark first block as used head
281 ATB_FREE_TO_HEAD(start_block);
282
283 // mark rest of blocks as used tail
284 // TODO for a run of many blocks can make this more efficient
285 for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) {
286 ATB_FREE_TO_TAIL(bl);
287 }
288
289 // return pointer to first block
290 return (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
291}
292
Damienfd8b6bc2013-10-22 20:26:36 +0100293// force the freeing of a piece of memory
294void gc_free(void *ptr_in) {
295 machine_uint_t ptr = (machine_uint_t)ptr_in;
296
297 if (VERIFY_PTR(ptr)) {
298 machine_uint_t block = BLOCK_FROM_PTR(ptr);
299 if (ATB_GET_KIND(block) == AT_HEAD) {
300 // free head and all of its tail blocks
301 do {
302 ATB_ANY_TO_FREE(block);
303 block += 1;
304 } while (ATB_GET_KIND(block) == AT_TAIL);
305 }
306 }
307}
308
Damiendcced922013-10-21 23:45:08 +0100309machine_uint_t gc_nbytes(void *ptr_in) {
310 machine_uint_t ptr = (machine_uint_t)ptr_in;
311
Damienfd8b6bc2013-10-22 20:26:36 +0100312 if (VERIFY_PTR(ptr)) {
Damiendcced922013-10-21 23:45:08 +0100313 machine_uint_t block = BLOCK_FROM_PTR(ptr);
314 if (ATB_GET_KIND(block) == AT_HEAD) {
315 // work out number of consecutive blocks in the chain starting with this on
316 machine_uint_t n_blocks = 0;
317 do {
318 n_blocks += 1;
319 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
320 return n_blocks * BYTES_PER_BLOCK;
321 }
322 }
323
324 // invalid pointer
325 return 0;
326}
327
328void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
329 machine_uint_t n_existing = gc_nbytes(ptr);
330 if (n_bytes <= n_existing) {
331 return ptr;
332 } else {
Damiend2c1a732013-10-23 21:03:27 +0100333 // TODO check if we can grow inplace
Damiendcced922013-10-21 23:45:08 +0100334 void *ptr2 = gc_alloc(n_bytes);
Paul Sokolovskyc0a83742014-02-11 15:28:37 +0200335 if (ptr2 == NULL) {
336 return ptr2;
337 }
Damiendcced922013-10-21 23:45:08 +0100338 memcpy(ptr2, ptr, n_existing);
Damiend2c1a732013-10-23 21:03:27 +0100339 gc_free(ptr);
Damiendcced922013-10-21 23:45:08 +0100340 return ptr2;
341 }
342}
343
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200344#if DEBUG_PRINT
Damien8b3a7c22013-10-23 20:20:17 +0100345static void gc_dump_at(void) {
Damieneefcc792013-10-22 15:25:25 +0100346 for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200347 printf("block %06u ", bl);
Damieneefcc792013-10-22 15:25:25 +0100348 switch (ATB_GET_KIND(bl)) {
349 case AT_FREE: printf("FREE"); break;
350 case AT_HEAD: printf("HEAD"); break;
351 case AT_TAIL: printf("TAIL"); break;
352 default: printf("MARK"); break;
353 }
354 printf("\n");
355 }
356}
357
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200358void gc_test(void) {
359 machine_uint_t len = 500;
Damiendcced922013-10-21 23:45:08 +0100360 machine_uint_t *heap = malloc(len);
361 gc_init(heap, heap + len / sizeof(machine_uint_t));
362 void *ptrs[100];
363 {
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200364 machine_uint_t **p = gc_alloc(16);
Damiendcced922013-10-21 23:45:08 +0100365 p[0] = gc_alloc(64);
366 p[1] = gc_alloc(1);
367 p[2] = gc_alloc(1);
368 p[3] = gc_alloc(1);
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200369 machine_uint_t ***p2 = gc_alloc(16);
Damiendcced922013-10-21 23:45:08 +0100370 p2[0] = p;
371 p2[1] = p;
372 ptrs[0] = p2;
373 }
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200374 for (int i = 0; i < 25; i+=2) {
Damiendcced922013-10-21 23:45:08 +0100375 machine_uint_t *p = gc_alloc(i);
376 printf("p=%p\n", p);
377 if (i & 3) {
378 //ptrs[i] = p;
379 }
380 }
381
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200382 printf("Before GC:\n");
Damiendcced922013-10-21 23:45:08 +0100383 gc_dump_at();
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200384 printf("Starting GC...\n");
385 gc_collect_start();
386 gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*));
387 gc_collect_end();
388 printf("After GC:\n");
Damiendcced922013-10-21 23:45:08 +0100389 gc_dump_at();
390}
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200391#endif
Damien Georged3ebe482014-01-07 15:20:33 +0000392
393#endif // MICROPY_ENABLE_GC