blob: 1eb859cbf7176c4b968ebf57c767711669bd041d [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"
Paul Sokolovskye807fa82014-04-02 20:36:32 +03006#include "misc.h"
Damiendcced922013-10-21 23:45:08 +01007#include "gc.h"
8
mux4f7e9f52014-04-03 23:55:12 +02009#include "misc.h"
10#include "qstr.h"
11#include "obj.h"
muxcc849f72014-04-05 15:49:03 +020012#include "runtime.h"
mux4f7e9f52014-04-03 23:55:12 +020013
Damien Georged3ebe482014-01-07 15:20:33 +000014#if MICROPY_ENABLE_GC
15
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020016#if 0 // print debugging info
17#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020018#define DEBUG_printf DEBUG_printf
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020019#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000020#define DEBUG_printf(...) (void)0
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020021#endif
22
Damiendcced922013-10-21 23:45:08 +010023typedef unsigned char byte;
24
Damiendcced922013-10-21 23:45:08 +010025#define WORDS_PER_BLOCK (4)
26#define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD)
27#define STACK_SIZE (64) // tunable; minimum is 1
28
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020029STATIC byte *gc_alloc_table_start;
mux4f7e9f52014-04-03 23:55:12 +020030STATIC byte *gc_mpobj_table_start;
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020031STATIC machine_uint_t gc_alloc_table_byte_len;
mux4f7e9f52014-04-03 23:55:12 +020032STATIC machine_uint_t gc_mpobj_table_byte_len;
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020033STATIC machine_uint_t *gc_pool_start;
34STATIC machine_uint_t *gc_pool_end;
Damiendcced922013-10-21 23:45:08 +010035
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020036STATIC int gc_stack_overflow;
37STATIC machine_uint_t gc_stack[STACK_SIZE];
38STATIC machine_uint_t *gc_sp;
Damiendcced922013-10-21 23:45:08 +010039
Damiendcced922013-10-21 23:45:08 +010040// ATB = allocation table byte
41// 0b00 = FREE -- free block
42// 0b01 = HEAD -- head of a chain of blocks
43// 0b10 = TAIL -- in the tail of a chain of blocks
44// 0b11 = MARK -- marked head block
45
46#define AT_FREE (0)
47#define AT_HEAD (1)
48#define AT_TAIL (2)
49#define AT_MARK (3)
50
51#define BLOCKS_PER_ATB (4)
52#define ATB_MASK_0 (0x03)
53#define ATB_MASK_1 (0x0c)
54#define ATB_MASK_2 (0x30)
55#define ATB_MASK_3 (0xc0)
56
57#define ATB_0_IS_FREE(a) (((a) & ATB_MASK_0) == 0)
58#define ATB_1_IS_FREE(a) (((a) & ATB_MASK_1) == 0)
59#define ATB_2_IS_FREE(a) (((a) & ATB_MASK_2) == 0)
60#define ATB_3_IS_FREE(a) (((a) & ATB_MASK_3) == 0)
61
62#define BLOCK_SHIFT(block) (2 * ((block) & (BLOCKS_PER_ATB - 1)))
63#define ATB_GET_KIND(block) ((gc_alloc_table_start[(block) / BLOCKS_PER_ATB] >> BLOCK_SHIFT(block)) & 3)
64#define ATB_ANY_TO_FREE(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_MARK << BLOCK_SHIFT(block))); } while (0)
65#define ATB_FREE_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_HEAD << BLOCK_SHIFT(block)); } while (0)
66#define ATB_FREE_TO_TAIL(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_TAIL << BLOCK_SHIFT(block)); } while (0)
67#define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
68#define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
69
mux4f7e9f52014-04-03 23:55:12 +020070#define ATB_SET_MPOBJ(block) do { gc_mpobj_table_start[(block) / 8] |= (1<<(block%8)); } while (0)
71#define ATB_CLR_MPOBJ(block) do { gc_mpobj_table_start[(block) / 8] &= (~(1<<(block%8))); } while (0)
72#define ATB_IS_MPOBJ(block) ((gc_mpobj_table_start[(block) / 8]>>(block%8))&0x01)
73
Damiendcced922013-10-21 23:45:08 +010074#define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
75#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start))
76#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
77
Damienbb5316b2013-10-22 21:12:29 +010078// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
79void gc_init(void *start, void *end) {
80 // align end pointer on block boundary
81 end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020082 DEBUG_printf("Initializing GC heap: %p-%p\n", start, end);
Damienbb5316b2013-10-22 21:12:29 +010083
84 // calculate parameters for GC
85 machine_uint_t total_word_len = (machine_uint_t*)end - (machine_uint_t*)start;
86 gc_alloc_table_byte_len = total_word_len * BYTES_PER_WORD / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
87 gc_alloc_table_start = (byte*)start;
mux4f7e9f52014-04-03 23:55:12 +020088
89 gc_mpobj_table_byte_len = (gc_alloc_table_byte_len * BITS_PER_BYTE / 2)/8;
90 gc_mpobj_table_start = gc_alloc_table_start+gc_alloc_table_byte_len;
91
92 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 +010093 machine_uint_t gc_pool_word_len = gc_pool_block_len * WORDS_PER_BLOCK;
94 gc_pool_start = (machine_uint_t*)end - gc_pool_word_len;
95 gc_pool_end = end;
96
97 // clear ATBs
98 memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len);
99
mux4f7e9f52014-04-03 23:55:12 +0200100 // clear MPOBJ flags
101 memset(gc_mpobj_table_start, 0, gc_mpobj_table_byte_len);
102
Damienbb5316b2013-10-22 21:12:29 +0100103 // allocate first block because gc_pool_start points there and it will never
104 // be freed, so allocating 1 block with null pointers will minimise memory loss
105 ATB_FREE_TO_HEAD(0);
106 for (int i = 0; i < WORDS_PER_BLOCK; i++) {
107 gc_pool_start[i] = 0;
108 }
109
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200110 DEBUG_printf("GC layout:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000111 DEBUG_printf(" alloc table at %p, length " UINT_FMT " bytes\n", gc_alloc_table_start, gc_alloc_table_byte_len);
112 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 +0100113}
114
Damienfd8b6bc2013-10-22 20:26:36 +0100115#define VERIFY_PTR(ptr) ( \
116 (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
117 && ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \
118 && ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \
119 )
120
Damiendcced922013-10-21 23:45:08 +0100121#define VERIFY_MARK_AND_PUSH(ptr) \
122 do { \
Damienfd8b6bc2013-10-22 20:26:36 +0100123 if (VERIFY_PTR(ptr)) { \
Damiendcced922013-10-21 23:45:08 +0100124 machine_uint_t _block = BLOCK_FROM_PTR(ptr); \
125 if (ATB_GET_KIND(_block) == AT_HEAD) { \
126 /* an unmarked head, mark it, and push it on gc stack */ \
127 ATB_HEAD_TO_MARK(_block); \
128 if (gc_sp < &gc_stack[STACK_SIZE]) { \
129 *gc_sp++ = _block; \
130 } else { \
131 gc_stack_overflow = 1; \
132 } \
133 } \
134 } \
135 } while (0)
136
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200137STATIC void gc_drain_stack(void) {
Damiendcced922013-10-21 23:45:08 +0100138 while (gc_sp > gc_stack) {
139 // pop the next block off the stack
140 machine_uint_t block = *--gc_sp;
141
Damieneefcc792013-10-22 15:25:25 +0100142 // work out number of consecutive blocks in the chain starting with this one
Damiendcced922013-10-21 23:45:08 +0100143 machine_uint_t n_blocks = 0;
144 do {
145 n_blocks += 1;
146 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
147
148 // check this block's children
149 machine_uint_t *scan = (machine_uint_t*)PTR_FROM_BLOCK(block);
150 for (machine_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) {
151 machine_uint_t ptr2 = *scan;
152 VERIFY_MARK_AND_PUSH(ptr2);
153 }
154 }
155}
156
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200157STATIC void gc_deal_with_stack_overflow(void) {
Damiendcced922013-10-21 23:45:08 +0100158 while (gc_stack_overflow) {
159 gc_stack_overflow = 0;
160 gc_sp = gc_stack;
161
162 // scan entire memory looking for blocks which have been marked but not their children
163 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
164 // trace (again) if mark bit set
165 if (ATB_GET_KIND(block) == AT_MARK) {
166 *gc_sp++ = block;
167 gc_drain_stack();
168 }
169 }
170 }
171}
172
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200173STATIC void gc_sweep(void) {
Damiendcced922013-10-21 23:45:08 +0100174 // free unmarked heads and their tails
175 int free_tail = 0;
176 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
177 switch (ATB_GET_KIND(block)) {
178 case AT_HEAD:
mux4f7e9f52014-04-03 23:55:12 +0200179 if (ATB_IS_MPOBJ(block)) {
muxcc849f72014-04-05 15:49:03 +0200180 mp_obj_t dest[2];
181 mp_load_method((mp_obj_t*)PTR_FROM_BLOCK(block), MP_QSTR___del__, dest);
182 // load_method returned a method
183 if (dest[1] != MP_OBJ_NULL) {
184 mp_call_method_n_kw(0, 0, dest);
mux4f7e9f52014-04-03 23:55:12 +0200185 }
muxcc849f72014-04-05 15:49:03 +0200186 // clear mpobj flag
187 ATB_CLR_MPOBJ(block);
mux4f7e9f52014-04-03 23:55:12 +0200188 }
Damiendcced922013-10-21 23:45:08 +0100189 free_tail = 1;
190 // fall through to free the head
191
192 case AT_TAIL:
193 if (free_tail) {
194 ATB_ANY_TO_FREE(block);
195 }
196 break;
197
198 case AT_MARK:
199 ATB_MARK_TO_HEAD(block);
200 free_tail = 0;
201 break;
202 }
203 }
204}
205
Damien8b3a7c22013-10-23 20:20:17 +0100206void gc_collect_start(void) {
Damiendcced922013-10-21 23:45:08 +0100207 gc_stack_overflow = 0;
208 gc_sp = gc_stack;
209}
210
211void gc_collect_root(void **ptrs, machine_uint_t len) {
212 for (machine_uint_t i = 0; i < len; i++) {
213 machine_uint_t ptr = (machine_uint_t)ptrs[i];
214 VERIFY_MARK_AND_PUSH(ptr);
215 gc_drain_stack();
216 }
217}
218
Damien8b3a7c22013-10-23 20:20:17 +0100219void gc_collect_end(void) {
Damiendcced922013-10-21 23:45:08 +0100220 gc_deal_with_stack_overflow();
221 gc_sweep();
Damieneefcc792013-10-22 15:25:25 +0100222}
Damiendcced922013-10-21 23:45:08 +0100223
Damieneefcc792013-10-22 15:25:25 +0100224void gc_info(gc_info_t *info) {
225 info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t);
226 info->used = 0;
227 info->free = 0;
228 info->num_1block = 0;
229 info->num_2block = 0;
230 info->max_block = 0;
231 for (machine_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
232 machine_uint_t kind = ATB_GET_KIND(block);
233 if (kind == AT_FREE || kind == AT_HEAD) {
234 if (len == 1) {
235 info->num_1block += 1;
236 } else if (len == 2) {
237 info->num_2block += 1;
238 }
239 if (len > info->max_block) {
240 info->max_block = len;
241 }
242 }
243 switch (kind) {
Damiendcced922013-10-21 23:45:08 +0100244 case AT_FREE:
Damieneefcc792013-10-22 15:25:25 +0100245 info->free += 1;
246 len = 0;
Damiendcced922013-10-21 23:45:08 +0100247 break;
248
249 case AT_HEAD:
Damieneefcc792013-10-22 15:25:25 +0100250 info->used += 1;
251 len = 1;
252 break;
253
Damiendcced922013-10-21 23:45:08 +0100254 case AT_TAIL:
Damieneefcc792013-10-22 15:25:25 +0100255 info->used += 1;
256 len += 1;
Damiendcced922013-10-21 23:45:08 +0100257 break;
258
259 case AT_MARK:
Damieneefcc792013-10-22 15:25:25 +0100260 // shouldn't happen
Damiendcced922013-10-21 23:45:08 +0100261 break;
262 }
263 }
264
Damieneefcc792013-10-22 15:25:25 +0100265 info->used *= BYTES_PER_BLOCK;
266 info->free *= BYTES_PER_BLOCK;
Damiendcced922013-10-21 23:45:08 +0100267}
268
mux4f7e9f52014-04-03 23:55:12 +0200269void *_gc_alloc(machine_uint_t n_bytes, bool is_mpobj) {
Damiendcced922013-10-21 23:45:08 +0100270 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 +0000271 DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
Damiendcced922013-10-21 23:45:08 +0100272
273 // check for 0 allocation
274 if (n_blocks == 0) {
275 return NULL;
276 }
277
278 machine_uint_t i;
279 machine_uint_t end_block;
280 machine_uint_t start_block;
281 machine_uint_t n_free = 0;
282 int collected = 0;
283 for (;;) {
284
285 // look for a run of n_blocks available blocks
286 for (i = 0; i < gc_alloc_table_byte_len; i++) {
287 byte a = gc_alloc_table_start[i];
288 if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; }
289 if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; }
290 if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; }
291 if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; }
292 }
293
294 // nothing found!
295 if (collected) {
296 return NULL;
297 }
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200298 DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes);
Damiendcced922013-10-21 23:45:08 +0100299 gc_collect();
300 collected = 1;
301 }
302
303 // found, ending at block i inclusive
304found:
305 // get starting and end blocks, both inclusive
306 end_block = i;
307 start_block = i - n_free + 1;
308
309 // mark first block as used head
310 ATB_FREE_TO_HEAD(start_block);
311
312 // 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
muxcc849f72014-04-05 15:49:03 +0200318 if (is_mpobj) {
319 // set mp_obj flag only if it has del
320 ATB_SET_MPOBJ(start_block);
321 }
322
Damiendcced922013-10-21 23:45:08 +0100323 // return pointer to first block
324 return (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
325}
326
mux4f7e9f52014-04-03 23:55:12 +0200327void *gc_alloc(machine_uint_t n_bytes) {
328 return _gc_alloc(n_bytes, false);
329}
330
331void *gc_alloc_mp_obj(machine_uint_t n_bytes) {
332 return _gc_alloc(n_bytes, true);
333}
334
Damienfd8b6bc2013-10-22 20:26:36 +0100335// force the freeing of a piece of memory
336void gc_free(void *ptr_in) {
337 machine_uint_t ptr = (machine_uint_t)ptr_in;
338
339 if (VERIFY_PTR(ptr)) {
340 machine_uint_t block = BLOCK_FROM_PTR(ptr);
341 if (ATB_GET_KIND(block) == AT_HEAD) {
342 // free head and all of its tail blocks
343 do {
344 ATB_ANY_TO_FREE(block);
345 block += 1;
346 } while (ATB_GET_KIND(block) == AT_TAIL);
347 }
348 }
349}
350
Damiendcced922013-10-21 23:45:08 +0100351machine_uint_t gc_nbytes(void *ptr_in) {
352 machine_uint_t ptr = (machine_uint_t)ptr_in;
353
Damienfd8b6bc2013-10-22 20:26:36 +0100354 if (VERIFY_PTR(ptr)) {
Damiendcced922013-10-21 23:45:08 +0100355 machine_uint_t block = BLOCK_FROM_PTR(ptr);
356 if (ATB_GET_KIND(block) == AT_HEAD) {
357 // work out number of consecutive blocks in the chain starting with this on
358 machine_uint_t n_blocks = 0;
359 do {
360 n_blocks += 1;
361 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
362 return n_blocks * BYTES_PER_BLOCK;
363 }
364 }
365
366 // invalid pointer
367 return 0;
368}
369
mux87826762014-03-12 21:00:23 +0200370#if 0
Damien George6fc765c2014-03-07 00:21:51 +0000371// use this realloc for now, one below is broken
372void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
373 machine_uint_t n_existing = gc_nbytes(ptr);
374 if (n_bytes <= n_existing) {
375 return ptr;
376 } else {
377 // TODO check if we can grow inplace
378 void *ptr2 = gc_alloc(n_bytes);
379 if (ptr2 == NULL) {
380 return ptr2;
381 }
382 memcpy(ptr2, ptr, n_existing);
383 gc_free(ptr);
384 return ptr2;
385 }
386}
mux87826762014-03-12 21:00:23 +0200387#else
muxfbaa1472014-03-05 23:23:04 +0200388void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
389 void *ptr_out = NULL;
Damien George73d57932014-03-06 00:02:16 +0000390 machine_uint_t block = 0;
muxfbaa1472014-03-05 23:23:04 +0200391 machine_uint_t ptr = (machine_uint_t)ptr_in;
392
393 if (ptr_in == NULL) {
394 return gc_alloc(n_bytes);
Damiendcced922013-10-21 23:45:08 +0100395 }
muxfbaa1472014-03-05 23:23:04 +0200396
Damien George470184e2014-03-12 22:44:11 +0000397 if (VERIFY_PTR(ptr) // verify pointer
398 && (block = BLOCK_FROM_PTR(ptr)) // get first block
399 && ATB_GET_KIND(block) == AT_HEAD) { // make sure it's a HEAD block
muxfbaa1472014-03-05 23:23:04 +0200400
401 byte block_type;
402 machine_uint_t n_free = 0;
Damien George470184e2014-03-12 22:44:11 +0000403 machine_uint_t n_blocks = 1; // counting HEAD block
mux87826762014-03-12 21:00:23 +0200404 machine_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
muxfbaa1472014-03-05 23:23:04 +0200405
Damien George470184e2014-03-12 22:44:11 +0000406 // get the number of consecutive tail blocks and
407 // the number of free blocks after last tail block
408 // stop if we reach (or are at) end of heap
mux87826762014-03-12 21:00:23 +0200409 while ((block + n_blocks + n_free) < max_block
Damien George470184e2014-03-12 22:44:11 +0000410 // stop as soon as we find enough blocks for n_bytes
mux87826762014-03-12 21:00:23 +0200411 && (n_bytes > ((n_blocks+n_free) * BYTES_PER_BLOCK))
Damien George470184e2014-03-12 22:44:11 +0000412 // stop if block is HEAD
mux87826762014-03-12 21:00:23 +0200413 && (block_type = ATB_GET_KIND(block + n_blocks + n_free)) != AT_HEAD) {
muxfbaa1472014-03-05 23:23:04 +0200414 switch (block_type) {
415 case AT_FREE: n_free++; break;
416 case AT_TAIL: n_blocks++; break;
417 default: break;
418 }
mux87826762014-03-12 21:00:23 +0200419 }
Damien George470184e2014-03-12 22:44:11 +0000420 // number of allocated bytes
muxfbaa1472014-03-05 23:23:04 +0200421 machine_uint_t n_existing = n_blocks * BYTES_PER_BLOCK;
422
Damien George470184e2014-03-12 22:44:11 +0000423 // check if realloc'ing to a smaller size
muxfbaa1472014-03-05 23:23:04 +0200424 if (n_bytes <= n_existing) {
425 ptr_out = ptr_in;
Damien George470184e2014-03-12 22:44:11 +0000426 // free unneeded tail blocks
Damien George73d57932014-03-06 00:02:16 +0000427 for (machine_uint_t bl = block + n_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) {
428 ATB_ANY_TO_FREE(bl);
muxfbaa1472014-03-05 23:23:04 +0200429 }
430
Damien George470184e2014-03-12 22:44:11 +0000431 // check if we can expand in place
mux87826762014-03-12 21:00:23 +0200432 } else if (n_bytes <= (n_existing + (n_free * BYTES_PER_BLOCK))) {
Damien George470184e2014-03-12 22:44:11 +0000433 // number of blocks needed to expand +1 if there's a remainder
muxfbaa1472014-03-05 23:23:04 +0200434 machine_uint_t n_diff = ( n_bytes - n_existing)/BYTES_PER_BLOCK+
435 ((n_bytes - n_existing)%BYTES_PER_BLOCK!=0);
436
437 DEBUG_printf("gc_realloc: expanding " UINT_FMT " blocks (" UINT_FMT " bytes) to " UINT_FMT " blocks (" UINT_FMT " bytes)\n",
438 n_existing/BYTES_PER_BLOCK, n_existing, n_existing/BYTES_PER_BLOCK+n_diff, n_existing + n_diff*BYTES_PER_BLOCK);
439
Damien George470184e2014-03-12 22:44:11 +0000440 // mark rest of blocks as used tail
Damien George73d57932014-03-06 00:02:16 +0000441 for (machine_uint_t bl = block + n_blocks; bl < (block + n_blocks + n_diff); bl++) {
muxfbaa1472014-03-05 23:23:04 +0200442 ATB_FREE_TO_TAIL(bl);
443 }
444 ptr_out = ptr_in;
445
Damien George470184e2014-03-12 22:44:11 +0000446 // try to find a new contiguous chain
muxfbaa1472014-03-05 23:23:04 +0200447 } else if ((ptr_out = gc_alloc(n_bytes)) != NULL) {
Damien George470184e2014-03-12 22:44:11 +0000448 DEBUG_printf("gc_realloc: allocating new block\n");
muxfbaa1472014-03-05 23:23:04 +0200449 memcpy(ptr_out, ptr_in, n_existing);
450 gc_free(ptr_in);
451 }
452 }
453
454 return ptr_out;
Damiendcced922013-10-21 23:45:08 +0100455}
mux87826762014-03-12 21:00:23 +0200456
Damien George6fc765c2014-03-07 00:21:51 +0000457#endif
Damiendcced922013-10-21 23:45:08 +0100458
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200459void gc_dump_info() {
460 gc_info_t info;
461 gc_info(&info);
462 printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free);
463 printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n",
464 info.num_1block, info.num_2block, info.max_block);
465}
466
Damien Georgece1162a2014-02-26 22:55:59 +0000467void gc_dump_alloc_table(void) {
468 printf("GC memory layout:");
Damieneefcc792013-10-22 15:25:25 +0100469 for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
Damien Georgece1162a2014-02-26 22:55:59 +0000470 if (bl % 64 == 0) {
471 printf("\n%04x: ", (uint)bl);
Damieneefcc792013-10-22 15:25:25 +0100472 }
Damien Georgece1162a2014-02-26 22:55:59 +0000473 int c = ' ';
474 switch (ATB_GET_KIND(bl)) {
475 case AT_FREE: c = '.'; break;
476 case AT_HEAD: c = 'h'; break;
477 case AT_TAIL: c = 't'; break;
478 case AT_MARK: c = 'm'; break;
479 }
480 printf("%c", c);
Damieneefcc792013-10-22 15:25:25 +0100481 }
Damien Georgece1162a2014-02-26 22:55:59 +0000482 printf("\n");
Damieneefcc792013-10-22 15:25:25 +0100483}
484
Damien Georgece1162a2014-02-26 22:55:59 +0000485#if DEBUG_PRINT
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200486void gc_test(void) {
487 machine_uint_t len = 500;
Damiendcced922013-10-21 23:45:08 +0100488 machine_uint_t *heap = malloc(len);
489 gc_init(heap, heap + len / sizeof(machine_uint_t));
490 void *ptrs[100];
491 {
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200492 machine_uint_t **p = gc_alloc(16);
Damiendcced922013-10-21 23:45:08 +0100493 p[0] = gc_alloc(64);
494 p[1] = gc_alloc(1);
495 p[2] = gc_alloc(1);
496 p[3] = gc_alloc(1);
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200497 machine_uint_t ***p2 = gc_alloc(16);
Damiendcced922013-10-21 23:45:08 +0100498 p2[0] = p;
499 p2[1] = p;
500 ptrs[0] = p2;
501 }
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200502 for (int i = 0; i < 25; i+=2) {
Damiendcced922013-10-21 23:45:08 +0100503 machine_uint_t *p = gc_alloc(i);
504 printf("p=%p\n", p);
505 if (i & 3) {
506 //ptrs[i] = p;
507 }
508 }
509
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200510 printf("Before GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000511 gc_dump_alloc_table();
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200512 printf("Starting GC...\n");
513 gc_collect_start();
514 gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*));
515 gc_collect_end();
516 printf("After GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000517 gc_dump_alloc_table();
Damiendcced922013-10-21 23:45:08 +0100518}
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200519#endif
Damien Georged3ebe482014-01-07 15:20:33 +0000520
521#endif // MICROPY_ENABLE_GC