blob: 054a3cc3153665b0da61814c7a864a6ae19851df [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
Damiendcced922013-10-21 23:45:08 +010011// a machine word is big enough to hold a pointer
12/*
13#define BYTES_PER_WORD (8)
14typedef unsigned long machine_uint_t;
15*/
16typedef unsigned char byte;
17
18#define BITS_PER_BYTE (8)
19#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
20#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)));
71
72 // calculate parameters for GC
73 machine_uint_t total_word_len = (machine_uint_t*)end - (machine_uint_t*)start;
74 gc_alloc_table_byte_len = total_word_len * BYTES_PER_WORD / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
75 gc_alloc_table_start = (byte*)start;
76 machine_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BITS_PER_BYTE / 2;
77 machine_uint_t gc_pool_word_len = gc_pool_block_len * WORDS_PER_BLOCK;
78 gc_pool_start = (machine_uint_t*)end - gc_pool_word_len;
79 gc_pool_end = end;
80
81 // clear ATBs
82 memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len);
83
84 // allocate first block because gc_pool_start points there and it will never
85 // be freed, so allocating 1 block with null pointers will minimise memory loss
86 ATB_FREE_TO_HEAD(0);
87 for (int i = 0; i < WORDS_PER_BLOCK; i++) {
88 gc_pool_start[i] = 0;
89 }
90
91 /*
92 printf("GC layout:\n");
93 printf(" alloc table at %p, length %u bytes\n", gc_alloc_table_start, gc_alloc_table_byte_len);
94 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);
95 */
96}
97
Damienfd8b6bc2013-10-22 20:26:36 +010098#define VERIFY_PTR(ptr) ( \
99 (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
100 && ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \
101 && ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \
102 )
103
Damiendcced922013-10-21 23:45:08 +0100104#define VERIFY_MARK_AND_PUSH(ptr) \
105 do { \
Damienfd8b6bc2013-10-22 20:26:36 +0100106 if (VERIFY_PTR(ptr)) { \
Damiendcced922013-10-21 23:45:08 +0100107 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
Damien8b3a7c22013-10-23 20:20:17 +0100120static void gc_drain_stack(void) {
Damiendcced922013-10-21 23:45:08 +0100121 while (gc_sp > gc_stack) {
122 // pop the next block off the stack
123 machine_uint_t block = *--gc_sp;
124
Damieneefcc792013-10-22 15:25:25 +0100125 // work out number of consecutive blocks in the chain starting with this one
Damiendcced922013-10-21 23:45:08 +0100126 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
Damien8b3a7c22013-10-23 20:20:17 +0100140static void gc_deal_with_stack_overflow(void) {
Damiendcced922013-10-21 23:45:08 +0100141 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
Damien8b3a7c22013-10-23 20:20:17 +0100156static void gc_sweep(void) {
Damiendcced922013-10-21 23:45:08 +0100157 // 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
Damien8b3a7c22013-10-23 20:20:17 +0100179void gc_collect_start(void) {
Damiendcced922013-10-21 23:45:08 +0100180 gc_stack_overflow = 0;
181 gc_sp = gc_stack;
182}
183
184void 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
Damien8b3a7c22013-10-23 20:20:17 +0100192void gc_collect_end(void) {
Damiendcced922013-10-21 23:45:08 +0100193 gc_deal_with_stack_overflow();
194 gc_sweep();
Damieneefcc792013-10-22 15:25:25 +0100195}
Damiendcced922013-10-21 23:45:08 +0100196
Damieneefcc792013-10-22 15:25:25 +0100197void gc_info(gc_info_t *info) {
198 info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t);
199 info->used = 0;
200 info->free = 0;
201 info->num_1block = 0;
202 info->num_2block = 0;
203 info->max_block = 0;
204 for (machine_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
205 machine_uint_t kind = ATB_GET_KIND(block);
206 if (kind == AT_FREE || kind == AT_HEAD) {
207 if (len == 1) {
208 info->num_1block += 1;
209 } else if (len == 2) {
210 info->num_2block += 1;
211 }
212 if (len > info->max_block) {
213 info->max_block = len;
214 }
215 }
216 switch (kind) {
Damiendcced922013-10-21 23:45:08 +0100217 case AT_FREE:
Damieneefcc792013-10-22 15:25:25 +0100218 info->free += 1;
219 len = 0;
Damiendcced922013-10-21 23:45:08 +0100220 break;
221
222 case AT_HEAD:
Damieneefcc792013-10-22 15:25:25 +0100223 info->used += 1;
224 len = 1;
225 break;
226
Damiendcced922013-10-21 23:45:08 +0100227 case AT_TAIL:
Damieneefcc792013-10-22 15:25:25 +0100228 info->used += 1;
229 len += 1;
Damiendcced922013-10-21 23:45:08 +0100230 break;
231
232 case AT_MARK:
Damieneefcc792013-10-22 15:25:25 +0100233 // shouldn't happen
Damiendcced922013-10-21 23:45:08 +0100234 break;
235 }
236 }
237
Damieneefcc792013-10-22 15:25:25 +0100238 info->used *= BYTES_PER_BLOCK;
239 info->free *= BYTES_PER_BLOCK;
Damiendcced922013-10-21 23:45:08 +0100240}
241
242void *gc_alloc(machine_uint_t n_bytes) {
243 machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
244 //printf("gc_alloc(%u bytes -> %u blocks)\n", n_bytes, n_blocks);
245
246 // check for 0 allocation
247 if (n_blocks == 0) {
248 return NULL;
249 }
250
251 machine_uint_t i;
252 machine_uint_t end_block;
253 machine_uint_t start_block;
254 machine_uint_t n_free = 0;
255 int collected = 0;
256 for (;;) {
257
258 // look for a run of n_blocks available blocks
259 for (i = 0; i < gc_alloc_table_byte_len; i++) {
260 byte a = gc_alloc_table_start[i];
261 if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; }
262 if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; }
263 if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; }
264 if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; }
265 }
266
267 // nothing found!
268 if (collected) {
269 return NULL;
270 }
271 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);
336 memcpy(ptr2, ptr, n_existing);
Damiend2c1a732013-10-23 21:03:27 +0100337 gc_free(ptr);
Damiendcced922013-10-21 23:45:08 +0100338 return ptr2;
339 }
340}
341
342/*
Damien8b3a7c22013-10-23 20:20:17 +0100343static void gc_dump_at(void) {
Damieneefcc792013-10-22 15:25:25 +0100344 for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
345 printf("block % 6u ", bl);
346 switch (ATB_GET_KIND(bl)) {
347 case AT_FREE: printf("FREE"); break;
348 case AT_HEAD: printf("HEAD"); break;
349 case AT_TAIL: printf("TAIL"); break;
350 default: printf("MARK"); break;
351 }
352 printf("\n");
353 }
354}
355
Damien8b3a7c22013-10-23 20:20:17 +0100356int main(void) {
Damiendcced922013-10-21 23:45:08 +0100357 machine_uint_t len = 1000;
358 machine_uint_t *heap = malloc(len);
359 gc_init(heap, heap + len / sizeof(machine_uint_t));
360 void *ptrs[100];
361 {
362 machine_uint_t *p = gc_alloc(16);
363 p[0] = gc_alloc(64);
364 p[1] = gc_alloc(1);
365 p[2] = gc_alloc(1);
366 p[3] = gc_alloc(1);
367 machine_uint_t *p2 = gc_alloc(16);
368 p2[0] = p;
369 p2[1] = p;
370 ptrs[0] = p2;
371 }
372 for (int i = 0; i < 50; i+=2) {
373 machine_uint_t *p = gc_alloc(i);
374 printf("p=%p\n", p);
375 if (i & 3) {
376 //ptrs[i] = p;
377 }
378 }
379
380 gc_dump_at();
381 gc_collect(ptrs, sizeof(ptrs) / sizeof(void*));
382 gc_dump_at();
383}
384*/
Damien Georged3ebe482014-01-07 15:20:33 +0000385
386#endif // MICROPY_ENABLE_GC