blob: 0d28828c0d084a541b67f832a520ea470f5357ef [file] [log] [blame]
Damiendcced922013-10-21 23:45:08 +01001#include <stdio.h>
Damiendcced922013-10-21 23:45:08 +01002#include <string.h>
mux4f7e9f52014-04-03 23:55:12 +02003#include <stdbool.h>
Damiendcced922013-10-21 23:45:08 +01004
Damiend99b0522013-12-21 18:17:45 +00005#include "mpconfig.h"
Damiendcced922013-10-21 23:45:08 +01006#include "gc.h"
7
mux4f7e9f52014-04-03 23:55:12 +02008#include "misc.h"
9#include "qstr.h"
10#include "obj.h"
11
Damien Georged3ebe482014-01-07 15:20:33 +000012#if MICROPY_ENABLE_GC
13
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020014#if 0 // print debugging info
15#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020016#define DEBUG_printf DEBUG_printf
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020017#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000018#define DEBUG_printf(...) (void)0
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020019#endif
20
Damiendcced922013-10-21 23:45:08 +010021typedef unsigned char byte;
22
Damiendcced922013-10-21 23:45:08 +010023#define WORDS_PER_BLOCK (4)
24#define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD)
25#define STACK_SIZE (64) // tunable; minimum is 1
26
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020027STATIC byte *gc_alloc_table_start;
mux4f7e9f52014-04-03 23:55:12 +020028STATIC byte *gc_mpobj_table_start;
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020029STATIC machine_uint_t gc_alloc_table_byte_len;
mux4f7e9f52014-04-03 23:55:12 +020030STATIC machine_uint_t gc_mpobj_table_byte_len;
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020031STATIC machine_uint_t *gc_pool_start;
32STATIC machine_uint_t *gc_pool_end;
Damiendcced922013-10-21 23:45:08 +010033
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020034STATIC int gc_stack_overflow;
35STATIC machine_uint_t gc_stack[STACK_SIZE];
36STATIC machine_uint_t *gc_sp;
Damiendcced922013-10-21 23:45:08 +010037
Damiendcced922013-10-21 23:45:08 +010038// ATB = allocation table byte
39// 0b00 = FREE -- free block
40// 0b01 = HEAD -- head of a chain of blocks
41// 0b10 = TAIL -- in the tail of a chain of blocks
42// 0b11 = MARK -- marked head block
43
44#define AT_FREE (0)
45#define AT_HEAD (1)
46#define AT_TAIL (2)
47#define AT_MARK (3)
48
49#define BLOCKS_PER_ATB (4)
50#define ATB_MASK_0 (0x03)
51#define ATB_MASK_1 (0x0c)
52#define ATB_MASK_2 (0x30)
53#define ATB_MASK_3 (0xc0)
54
55#define ATB_0_IS_FREE(a) (((a) & ATB_MASK_0) == 0)
56#define ATB_1_IS_FREE(a) (((a) & ATB_MASK_1) == 0)
57#define ATB_2_IS_FREE(a) (((a) & ATB_MASK_2) == 0)
58#define ATB_3_IS_FREE(a) (((a) & ATB_MASK_3) == 0)
59
60#define BLOCK_SHIFT(block) (2 * ((block) & (BLOCKS_PER_ATB - 1)))
61#define ATB_GET_KIND(block) ((gc_alloc_table_start[(block) / BLOCKS_PER_ATB] >> BLOCK_SHIFT(block)) & 3)
62#define ATB_ANY_TO_FREE(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_MARK << BLOCK_SHIFT(block))); } while (0)
63#define ATB_FREE_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_HEAD << BLOCK_SHIFT(block)); } while (0)
64#define ATB_FREE_TO_TAIL(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_TAIL << BLOCK_SHIFT(block)); } while (0)
65#define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
66#define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
67
mux4f7e9f52014-04-03 23:55:12 +020068#define ATB_SET_MPOBJ(block) do { gc_mpobj_table_start[(block) / 8] |= (1<<(block%8)); } while (0)
69#define ATB_CLR_MPOBJ(block) do { gc_mpobj_table_start[(block) / 8] &= (~(1<<(block%8))); } while (0)
70#define ATB_IS_MPOBJ(block) ((gc_mpobj_table_start[(block) / 8]>>(block%8))&0x01)
71
Damiendcced922013-10-21 23:45:08 +010072#define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
73#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start))
74#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
75
Damienbb5316b2013-10-22 21:12:29 +010076// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
77void gc_init(void *start, void *end) {
78 // align end pointer on block boundary
79 end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020080 DEBUG_printf("Initializing GC heap: %p-%p\n", start, end);
Damienbb5316b2013-10-22 21:12:29 +010081
82 // calculate parameters for GC
83 machine_uint_t total_word_len = (machine_uint_t*)end - (machine_uint_t*)start;
84 gc_alloc_table_byte_len = total_word_len * BYTES_PER_WORD / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
85 gc_alloc_table_start = (byte*)start;
mux4f7e9f52014-04-03 23:55:12 +020086
87 gc_mpobj_table_byte_len = (gc_alloc_table_byte_len * BITS_PER_BYTE / 2)/8;
88 gc_mpobj_table_start = gc_alloc_table_start+gc_alloc_table_byte_len;
89
90 machine_uint_t gc_pool_block_len = (gc_alloc_table_byte_len * BITS_PER_BYTE / 2) -(gc_mpobj_table_byte_len / BYTES_PER_BLOCK);
Damienbb5316b2013-10-22 21:12:29 +010091 machine_uint_t gc_pool_word_len = gc_pool_block_len * WORDS_PER_BLOCK;
92 gc_pool_start = (machine_uint_t*)end - gc_pool_word_len;
93 gc_pool_end = end;
94
95 // clear ATBs
96 memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len);
97
mux4f7e9f52014-04-03 23:55:12 +020098 // clear MPOBJ flags
99 memset(gc_mpobj_table_start, 0, gc_mpobj_table_byte_len);
100
Damienbb5316b2013-10-22 21:12:29 +0100101 // allocate first block because gc_pool_start points there and it will never
102 // be freed, so allocating 1 block with null pointers will minimise memory loss
103 ATB_FREE_TO_HEAD(0);
104 for (int i = 0; i < WORDS_PER_BLOCK; i++) {
105 gc_pool_start[i] = 0;
106 }
107
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200108 DEBUG_printf("GC layout:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000109 DEBUG_printf(" alloc table at %p, length " UINT_FMT " bytes\n", gc_alloc_table_start, gc_alloc_table_byte_len);
110 DEBUG_printf(" pool at %p, length " UINT_FMT " blocks = " UINT_FMT " words = " UINT_FMT " 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 +0100111}
112
Damienfd8b6bc2013-10-22 20:26:36 +0100113#define VERIFY_PTR(ptr) ( \
114 (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
115 && ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \
116 && ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \
117 )
118
Damiendcced922013-10-21 23:45:08 +0100119#define VERIFY_MARK_AND_PUSH(ptr) \
120 do { \
Damienfd8b6bc2013-10-22 20:26:36 +0100121 if (VERIFY_PTR(ptr)) { \
Damiendcced922013-10-21 23:45:08 +0100122 machine_uint_t _block = BLOCK_FROM_PTR(ptr); \
123 if (ATB_GET_KIND(_block) == AT_HEAD) { \
124 /* an unmarked head, mark it, and push it on gc stack */ \
125 ATB_HEAD_TO_MARK(_block); \
126 if (gc_sp < &gc_stack[STACK_SIZE]) { \
127 *gc_sp++ = _block; \
128 } else { \
129 gc_stack_overflow = 1; \
130 } \
131 } \
132 } \
133 } while (0)
134
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200135STATIC void gc_drain_stack(void) {
Damiendcced922013-10-21 23:45:08 +0100136 while (gc_sp > gc_stack) {
137 // pop the next block off the stack
138 machine_uint_t block = *--gc_sp;
139
Damieneefcc792013-10-22 15:25:25 +0100140 // work out number of consecutive blocks in the chain starting with this one
Damiendcced922013-10-21 23:45:08 +0100141 machine_uint_t n_blocks = 0;
142 do {
143 n_blocks += 1;
144 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
145
146 // check this block's children
147 machine_uint_t *scan = (machine_uint_t*)PTR_FROM_BLOCK(block);
148 for (machine_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) {
149 machine_uint_t ptr2 = *scan;
150 VERIFY_MARK_AND_PUSH(ptr2);
151 }
152 }
153}
154
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200155STATIC void gc_deal_with_stack_overflow(void) {
Damiendcced922013-10-21 23:45:08 +0100156 while (gc_stack_overflow) {
157 gc_stack_overflow = 0;
158 gc_sp = gc_stack;
159
160 // scan entire memory looking for blocks which have been marked but not their children
161 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
162 // trace (again) if mark bit set
163 if (ATB_GET_KIND(block) == AT_MARK) {
164 *gc_sp++ = block;
165 gc_drain_stack();
166 }
167 }
168 }
169}
170
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200171STATIC void gc_sweep(void) {
Damiendcced922013-10-21 23:45:08 +0100172 // free unmarked heads and their tails
173 int free_tail = 0;
174 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
175 switch (ATB_GET_KIND(block)) {
176 case AT_HEAD:
mux4f7e9f52014-04-03 23:55:12 +0200177 if (ATB_IS_MPOBJ(block)) {
178 ATB_CLR_MPOBJ(block); // clear mpobj flag
179 mp_obj_t *self = (mp_obj_t*)PTR_FROM_BLOCK(block);
180 mp_obj_type_t *type= mp_obj_get_type(self);
181 if (type->del != NULL) {
182 type->del(self);
183 }
184 }
Damiendcced922013-10-21 23:45:08 +0100185 free_tail = 1;
186 // fall through to free the head
187
188 case AT_TAIL:
189 if (free_tail) {
190 ATB_ANY_TO_FREE(block);
191 }
192 break;
193
194 case AT_MARK:
195 ATB_MARK_TO_HEAD(block);
196 free_tail = 0;
197 break;
198 }
199 }
200}
201
Damien8b3a7c22013-10-23 20:20:17 +0100202void gc_collect_start(void) {
Damiendcced922013-10-21 23:45:08 +0100203 gc_stack_overflow = 0;
204 gc_sp = gc_stack;
205}
206
207void gc_collect_root(void **ptrs, machine_uint_t len) {
208 for (machine_uint_t i = 0; i < len; i++) {
209 machine_uint_t ptr = (machine_uint_t)ptrs[i];
210 VERIFY_MARK_AND_PUSH(ptr);
211 gc_drain_stack();
212 }
213}
214
Damien8b3a7c22013-10-23 20:20:17 +0100215void gc_collect_end(void) {
Damiendcced922013-10-21 23:45:08 +0100216 gc_deal_with_stack_overflow();
217 gc_sweep();
Damieneefcc792013-10-22 15:25:25 +0100218}
Damiendcced922013-10-21 23:45:08 +0100219
Damieneefcc792013-10-22 15:25:25 +0100220void gc_info(gc_info_t *info) {
221 info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t);
222 info->used = 0;
223 info->free = 0;
224 info->num_1block = 0;
225 info->num_2block = 0;
226 info->max_block = 0;
227 for (machine_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
228 machine_uint_t kind = ATB_GET_KIND(block);
229 if (kind == AT_FREE || kind == AT_HEAD) {
230 if (len == 1) {
231 info->num_1block += 1;
232 } else if (len == 2) {
233 info->num_2block += 1;
234 }
235 if (len > info->max_block) {
236 info->max_block = len;
237 }
238 }
239 switch (kind) {
Damiendcced922013-10-21 23:45:08 +0100240 case AT_FREE:
Damieneefcc792013-10-22 15:25:25 +0100241 info->free += 1;
242 len = 0;
Damiendcced922013-10-21 23:45:08 +0100243 break;
244
245 case AT_HEAD:
Damieneefcc792013-10-22 15:25:25 +0100246 info->used += 1;
247 len = 1;
248 break;
249
Damiendcced922013-10-21 23:45:08 +0100250 case AT_TAIL:
Damieneefcc792013-10-22 15:25:25 +0100251 info->used += 1;
252 len += 1;
Damiendcced922013-10-21 23:45:08 +0100253 break;
254
255 case AT_MARK:
Damieneefcc792013-10-22 15:25:25 +0100256 // shouldn't happen
Damiendcced922013-10-21 23:45:08 +0100257 break;
258 }
259 }
260
Damieneefcc792013-10-22 15:25:25 +0100261 info->used *= BYTES_PER_BLOCK;
262 info->free *= BYTES_PER_BLOCK;
Damiendcced922013-10-21 23:45:08 +0100263}
264
mux4f7e9f52014-04-03 23:55:12 +0200265void *_gc_alloc(machine_uint_t n_bytes, bool is_mpobj) {
Damiendcced922013-10-21 23:45:08 +0100266 machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
Damien Georgece1162a2014-02-26 22:55:59 +0000267 DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
Damiendcced922013-10-21 23:45:08 +0100268
269 // check for 0 allocation
270 if (n_blocks == 0) {
271 return NULL;
272 }
273
274 machine_uint_t i;
275 machine_uint_t end_block;
276 machine_uint_t start_block;
277 machine_uint_t n_free = 0;
278 int collected = 0;
279 for (;;) {
280
281 // look for a run of n_blocks available blocks
282 for (i = 0; i < gc_alloc_table_byte_len; i++) {
283 byte a = gc_alloc_table_start[i];
284 if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; }
285 if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; }
286 if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; }
287 if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; }
288 }
289
290 // nothing found!
291 if (collected) {
292 return NULL;
293 }
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200294 DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes);
Damiendcced922013-10-21 23:45:08 +0100295 gc_collect();
296 collected = 1;
297 }
298
299 // found, ending at block i inclusive
300found:
301 // get starting and end blocks, both inclusive
302 end_block = i;
303 start_block = i - n_free + 1;
304
305 // mark first block as used head
306 ATB_FREE_TO_HEAD(start_block);
307
mux4f7e9f52014-04-03 23:55:12 +0200308 if (is_mpobj) {
309 ATB_SET_MPOBJ(start_block);
310 }
311
Damiendcced922013-10-21 23:45:08 +0100312 // mark rest of blocks as used tail
313 // TODO for a run of many blocks can make this more efficient
314 for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) {
315 ATB_FREE_TO_TAIL(bl);
316 }
317
318 // return pointer to first block
319 return (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
320}
321
mux4f7e9f52014-04-03 23:55:12 +0200322void *gc_alloc(machine_uint_t n_bytes) {
323 return _gc_alloc(n_bytes, false);
324}
325
326void *gc_alloc_mp_obj(machine_uint_t n_bytes) {
327 return _gc_alloc(n_bytes, true);
328}
329
Damienfd8b6bc2013-10-22 20:26:36 +0100330// force the freeing of a piece of memory
331void gc_free(void *ptr_in) {
332 machine_uint_t ptr = (machine_uint_t)ptr_in;
333
334 if (VERIFY_PTR(ptr)) {
335 machine_uint_t block = BLOCK_FROM_PTR(ptr);
336 if (ATB_GET_KIND(block) == AT_HEAD) {
337 // free head and all of its tail blocks
338 do {
339 ATB_ANY_TO_FREE(block);
340 block += 1;
341 } while (ATB_GET_KIND(block) == AT_TAIL);
342 }
343 }
344}
345
Damiendcced922013-10-21 23:45:08 +0100346machine_uint_t gc_nbytes(void *ptr_in) {
347 machine_uint_t ptr = (machine_uint_t)ptr_in;
348
Damienfd8b6bc2013-10-22 20:26:36 +0100349 if (VERIFY_PTR(ptr)) {
Damiendcced922013-10-21 23:45:08 +0100350 machine_uint_t block = BLOCK_FROM_PTR(ptr);
351 if (ATB_GET_KIND(block) == AT_HEAD) {
352 // work out number of consecutive blocks in the chain starting with this on
353 machine_uint_t n_blocks = 0;
354 do {
355 n_blocks += 1;
356 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
357 return n_blocks * BYTES_PER_BLOCK;
358 }
359 }
360
361 // invalid pointer
362 return 0;
363}
364
mux87826762014-03-12 21:00:23 +0200365#if 0
Damien George6fc765c2014-03-07 00:21:51 +0000366// use this realloc for now, one below is broken
367void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
368 machine_uint_t n_existing = gc_nbytes(ptr);
369 if (n_bytes <= n_existing) {
370 return ptr;
371 } else {
372 // TODO check if we can grow inplace
373 void *ptr2 = gc_alloc(n_bytes);
374 if (ptr2 == NULL) {
375 return ptr2;
376 }
377 memcpy(ptr2, ptr, n_existing);
378 gc_free(ptr);
379 return ptr2;
380 }
381}
mux87826762014-03-12 21:00:23 +0200382#else
muxfbaa1472014-03-05 23:23:04 +0200383void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
384 void *ptr_out = NULL;
Damien George73d57932014-03-06 00:02:16 +0000385 machine_uint_t block = 0;
muxfbaa1472014-03-05 23:23:04 +0200386 machine_uint_t ptr = (machine_uint_t)ptr_in;
387
388 if (ptr_in == NULL) {
389 return gc_alloc(n_bytes);
Damiendcced922013-10-21 23:45:08 +0100390 }
muxfbaa1472014-03-05 23:23:04 +0200391
Damien George470184e2014-03-12 22:44:11 +0000392 if (VERIFY_PTR(ptr) // verify pointer
393 && (block = BLOCK_FROM_PTR(ptr)) // get first block
394 && ATB_GET_KIND(block) == AT_HEAD) { // make sure it's a HEAD block
muxfbaa1472014-03-05 23:23:04 +0200395
396 byte block_type;
397 machine_uint_t n_free = 0;
Damien George470184e2014-03-12 22:44:11 +0000398 machine_uint_t n_blocks = 1; // counting HEAD block
mux87826762014-03-12 21:00:23 +0200399 machine_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
muxfbaa1472014-03-05 23:23:04 +0200400
Damien George470184e2014-03-12 22:44:11 +0000401 // get the number of consecutive tail blocks and
402 // the number of free blocks after last tail block
403 // stop if we reach (or are at) end of heap
mux87826762014-03-12 21:00:23 +0200404 while ((block + n_blocks + n_free) < max_block
Damien George470184e2014-03-12 22:44:11 +0000405 // stop as soon as we find enough blocks for n_bytes
mux87826762014-03-12 21:00:23 +0200406 && (n_bytes > ((n_blocks+n_free) * BYTES_PER_BLOCK))
Damien George470184e2014-03-12 22:44:11 +0000407 // stop if block is HEAD
mux87826762014-03-12 21:00:23 +0200408 && (block_type = ATB_GET_KIND(block + n_blocks + n_free)) != AT_HEAD) {
muxfbaa1472014-03-05 23:23:04 +0200409 switch (block_type) {
410 case AT_FREE: n_free++; break;
411 case AT_TAIL: n_blocks++; break;
412 default: break;
413 }
mux87826762014-03-12 21:00:23 +0200414 }
Damien George470184e2014-03-12 22:44:11 +0000415 // number of allocated bytes
muxfbaa1472014-03-05 23:23:04 +0200416 machine_uint_t n_existing = n_blocks * BYTES_PER_BLOCK;
417
Damien George470184e2014-03-12 22:44:11 +0000418 // check if realloc'ing to a smaller size
muxfbaa1472014-03-05 23:23:04 +0200419 if (n_bytes <= n_existing) {
420 ptr_out = ptr_in;
Damien George470184e2014-03-12 22:44:11 +0000421 // free unneeded tail blocks
Damien George73d57932014-03-06 00:02:16 +0000422 for (machine_uint_t bl = block + n_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) {
423 ATB_ANY_TO_FREE(bl);
muxfbaa1472014-03-05 23:23:04 +0200424 }
425
Damien George470184e2014-03-12 22:44:11 +0000426 // check if we can expand in place
mux87826762014-03-12 21:00:23 +0200427 } else if (n_bytes <= (n_existing + (n_free * BYTES_PER_BLOCK))) {
Damien George470184e2014-03-12 22:44:11 +0000428 // number of blocks needed to expand +1 if there's a remainder
muxfbaa1472014-03-05 23:23:04 +0200429 machine_uint_t n_diff = ( n_bytes - n_existing)/BYTES_PER_BLOCK+
430 ((n_bytes - n_existing)%BYTES_PER_BLOCK!=0);
431
432 DEBUG_printf("gc_realloc: expanding " UINT_FMT " blocks (" UINT_FMT " bytes) to " UINT_FMT " blocks (" UINT_FMT " bytes)\n",
433 n_existing/BYTES_PER_BLOCK, n_existing, n_existing/BYTES_PER_BLOCK+n_diff, n_existing + n_diff*BYTES_PER_BLOCK);
434
Damien George470184e2014-03-12 22:44:11 +0000435 // mark rest of blocks as used tail
Damien George73d57932014-03-06 00:02:16 +0000436 for (machine_uint_t bl = block + n_blocks; bl < (block + n_blocks + n_diff); bl++) {
muxfbaa1472014-03-05 23:23:04 +0200437 ATB_FREE_TO_TAIL(bl);
438 }
439 ptr_out = ptr_in;
440
Damien George470184e2014-03-12 22:44:11 +0000441 // try to find a new contiguous chain
muxfbaa1472014-03-05 23:23:04 +0200442 } else if ((ptr_out = gc_alloc(n_bytes)) != NULL) {
Damien George470184e2014-03-12 22:44:11 +0000443 DEBUG_printf("gc_realloc: allocating new block\n");
muxfbaa1472014-03-05 23:23:04 +0200444 memcpy(ptr_out, ptr_in, n_existing);
445 gc_free(ptr_in);
446 }
447 }
448
449 return ptr_out;
Damiendcced922013-10-21 23:45:08 +0100450}
mux87826762014-03-12 21:00:23 +0200451
Damien George6fc765c2014-03-07 00:21:51 +0000452#endif
Damiendcced922013-10-21 23:45:08 +0100453
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200454void gc_dump_info() {
455 gc_info_t info;
456 gc_info(&info);
457 printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free);
458 printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n",
459 info.num_1block, info.num_2block, info.max_block);
460}
461
Damien Georgece1162a2014-02-26 22:55:59 +0000462void gc_dump_alloc_table(void) {
463 printf("GC memory layout:");
Damieneefcc792013-10-22 15:25:25 +0100464 for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
Damien Georgece1162a2014-02-26 22:55:59 +0000465 if (bl % 64 == 0) {
466 printf("\n%04x: ", (uint)bl);
Damieneefcc792013-10-22 15:25:25 +0100467 }
Damien Georgece1162a2014-02-26 22:55:59 +0000468 int c = ' ';
469 switch (ATB_GET_KIND(bl)) {
470 case AT_FREE: c = '.'; break;
471 case AT_HEAD: c = 'h'; break;
472 case AT_TAIL: c = 't'; break;
473 case AT_MARK: c = 'm'; break;
474 }
475 printf("%c", c);
Damieneefcc792013-10-22 15:25:25 +0100476 }
Damien Georgece1162a2014-02-26 22:55:59 +0000477 printf("\n");
Damieneefcc792013-10-22 15:25:25 +0100478}
479
Damien Georgece1162a2014-02-26 22:55:59 +0000480#if DEBUG_PRINT
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200481void gc_test(void) {
482 machine_uint_t len = 500;
Damiendcced922013-10-21 23:45:08 +0100483 machine_uint_t *heap = malloc(len);
484 gc_init(heap, heap + len / sizeof(machine_uint_t));
485 void *ptrs[100];
486 {
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200487 machine_uint_t **p = gc_alloc(16);
Damiendcced922013-10-21 23:45:08 +0100488 p[0] = gc_alloc(64);
489 p[1] = gc_alloc(1);
490 p[2] = gc_alloc(1);
491 p[3] = gc_alloc(1);
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200492 machine_uint_t ***p2 = gc_alloc(16);
Damiendcced922013-10-21 23:45:08 +0100493 p2[0] = p;
494 p2[1] = p;
495 ptrs[0] = p2;
496 }
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200497 for (int i = 0; i < 25; i+=2) {
Damiendcced922013-10-21 23:45:08 +0100498 machine_uint_t *p = gc_alloc(i);
499 printf("p=%p\n", p);
500 if (i & 3) {
501 //ptrs[i] = p;
502 }
503 }
504
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200505 printf("Before GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000506 gc_dump_alloc_table();
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200507 printf("Starting GC...\n");
508 gc_collect_start();
509 gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*));
510 gc_collect_end();
511 printf("After GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000512 gc_dump_alloc_table();
Damiendcced922013-10-21 23:45:08 +0100513}
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200514#endif
Damien Georged3ebe482014-01-07 15:20:33 +0000515
516#endif // MICROPY_ENABLE_GC