blob: e330e5c4a74b95a05c5d59dceeffe37ed9d4c1e8 [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"
muxcc849f72014-04-05 15:49:03 +020011#include "runtime.h"
mux4f7e9f52014-04-03 23:55:12 +020012
Damien Georged3ebe482014-01-07 15:20:33 +000013#if MICROPY_ENABLE_GC
14
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020015#if 0 // print debugging info
16#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020017#define DEBUG_printf DEBUG_printf
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020018#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000019#define DEBUG_printf(...) (void)0
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020020#endif
21
Damiendcced922013-10-21 23:45:08 +010022typedef unsigned char byte;
23
Damiendcced922013-10-21 23:45:08 +010024#define WORDS_PER_BLOCK (4)
25#define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD)
26#define STACK_SIZE (64) // tunable; minimum is 1
27
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020028STATIC byte *gc_alloc_table_start;
mux4f7e9f52014-04-03 23:55:12 +020029STATIC byte *gc_mpobj_table_start;
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020030STATIC machine_uint_t gc_alloc_table_byte_len;
mux4f7e9f52014-04-03 23:55:12 +020031STATIC machine_uint_t gc_mpobj_table_byte_len;
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020032STATIC machine_uint_t *gc_pool_start;
33STATIC machine_uint_t *gc_pool_end;
Damiendcced922013-10-21 23:45:08 +010034
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020035STATIC int gc_stack_overflow;
36STATIC machine_uint_t gc_stack[STACK_SIZE];
37STATIC machine_uint_t *gc_sp;
Damiendcced922013-10-21 23:45:08 +010038
Damiendcced922013-10-21 23:45:08 +010039// ATB = allocation table byte
40// 0b00 = FREE -- free block
41// 0b01 = HEAD -- head of a chain of blocks
42// 0b10 = TAIL -- in the tail of a chain of blocks
43// 0b11 = MARK -- marked head block
44
45#define AT_FREE (0)
46#define AT_HEAD (1)
47#define AT_TAIL (2)
48#define AT_MARK (3)
49
50#define BLOCKS_PER_ATB (4)
51#define ATB_MASK_0 (0x03)
52#define ATB_MASK_1 (0x0c)
53#define ATB_MASK_2 (0x30)
54#define ATB_MASK_3 (0xc0)
55
56#define ATB_0_IS_FREE(a) (((a) & ATB_MASK_0) == 0)
57#define ATB_1_IS_FREE(a) (((a) & ATB_MASK_1) == 0)
58#define ATB_2_IS_FREE(a) (((a) & ATB_MASK_2) == 0)
59#define ATB_3_IS_FREE(a) (((a) & ATB_MASK_3) == 0)
60
61#define BLOCK_SHIFT(block) (2 * ((block) & (BLOCKS_PER_ATB - 1)))
62#define ATB_GET_KIND(block) ((gc_alloc_table_start[(block) / BLOCKS_PER_ATB] >> BLOCK_SHIFT(block)) & 3)
63#define ATB_ANY_TO_FREE(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_MARK << BLOCK_SHIFT(block))); } while (0)
64#define ATB_FREE_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_HEAD << BLOCK_SHIFT(block)); } while (0)
65#define ATB_FREE_TO_TAIL(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_TAIL << BLOCK_SHIFT(block)); } while (0)
66#define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
67#define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
68
mux4f7e9f52014-04-03 23:55:12 +020069#define ATB_SET_MPOBJ(block) do { gc_mpobj_table_start[(block) / 8] |= (1<<(block%8)); } while (0)
70#define ATB_CLR_MPOBJ(block) do { gc_mpobj_table_start[(block) / 8] &= (~(1<<(block%8))); } while (0)
71#define ATB_IS_MPOBJ(block) ((gc_mpobj_table_start[(block) / 8]>>(block%8))&0x01)
72
Damiendcced922013-10-21 23:45:08 +010073#define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
74#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start))
75#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
76
Damienbb5316b2013-10-22 21:12:29 +010077// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
78void gc_init(void *start, void *end) {
79 // align end pointer on block boundary
80 end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020081 DEBUG_printf("Initializing GC heap: %p-%p\n", start, end);
Damienbb5316b2013-10-22 21:12:29 +010082
83 // calculate parameters for GC
84 machine_uint_t total_word_len = (machine_uint_t*)end - (machine_uint_t*)start;
85 gc_alloc_table_byte_len = total_word_len * BYTES_PER_WORD / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
86 gc_alloc_table_start = (byte*)start;
mux4f7e9f52014-04-03 23:55:12 +020087
88 gc_mpobj_table_byte_len = (gc_alloc_table_byte_len * BITS_PER_BYTE / 2)/8;
89 gc_mpobj_table_start = gc_alloc_table_start+gc_alloc_table_byte_len;
90
91 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 +010092 machine_uint_t gc_pool_word_len = gc_pool_block_len * WORDS_PER_BLOCK;
93 gc_pool_start = (machine_uint_t*)end - gc_pool_word_len;
94 gc_pool_end = end;
95
96 // clear ATBs
97 memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len);
98
mux4f7e9f52014-04-03 23:55:12 +020099 // clear MPOBJ flags
100 memset(gc_mpobj_table_start, 0, gc_mpobj_table_byte_len);
101
Damienbb5316b2013-10-22 21:12:29 +0100102 // allocate first block because gc_pool_start points there and it will never
103 // be freed, so allocating 1 block with null pointers will minimise memory loss
104 ATB_FREE_TO_HEAD(0);
105 for (int i = 0; i < WORDS_PER_BLOCK; i++) {
106 gc_pool_start[i] = 0;
107 }
108
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200109 DEBUG_printf("GC layout:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000110 DEBUG_printf(" alloc table at %p, length " UINT_FMT " bytes\n", gc_alloc_table_start, gc_alloc_table_byte_len);
111 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 +0100112}
113
Damienfd8b6bc2013-10-22 20:26:36 +0100114#define VERIFY_PTR(ptr) ( \
115 (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
116 && ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \
117 && ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \
118 )
119
Damiendcced922013-10-21 23:45:08 +0100120#define VERIFY_MARK_AND_PUSH(ptr) \
121 do { \
Damienfd8b6bc2013-10-22 20:26:36 +0100122 if (VERIFY_PTR(ptr)) { \
Damiendcced922013-10-21 23:45:08 +0100123 machine_uint_t _block = BLOCK_FROM_PTR(ptr); \
124 if (ATB_GET_KIND(_block) == AT_HEAD) { \
125 /* an unmarked head, mark it, and push it on gc stack */ \
126 ATB_HEAD_TO_MARK(_block); \
127 if (gc_sp < &gc_stack[STACK_SIZE]) { \
128 *gc_sp++ = _block; \
129 } else { \
130 gc_stack_overflow = 1; \
131 } \
132 } \
133 } \
134 } while (0)
135
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200136STATIC void gc_drain_stack(void) {
Damiendcced922013-10-21 23:45:08 +0100137 while (gc_sp > gc_stack) {
138 // pop the next block off the stack
139 machine_uint_t block = *--gc_sp;
140
Damieneefcc792013-10-22 15:25:25 +0100141 // work out number of consecutive blocks in the chain starting with this one
Damiendcced922013-10-21 23:45:08 +0100142 machine_uint_t n_blocks = 0;
143 do {
144 n_blocks += 1;
145 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
146
147 // check this block's children
148 machine_uint_t *scan = (machine_uint_t*)PTR_FROM_BLOCK(block);
149 for (machine_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) {
150 machine_uint_t ptr2 = *scan;
151 VERIFY_MARK_AND_PUSH(ptr2);
152 }
153 }
154}
155
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200156STATIC void gc_deal_with_stack_overflow(void) {
Damiendcced922013-10-21 23:45:08 +0100157 while (gc_stack_overflow) {
158 gc_stack_overflow = 0;
159 gc_sp = gc_stack;
160
161 // scan entire memory looking for blocks which have been marked but not their children
162 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
163 // trace (again) if mark bit set
164 if (ATB_GET_KIND(block) == AT_MARK) {
165 *gc_sp++ = block;
166 gc_drain_stack();
167 }
168 }
169 }
170}
171
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200172STATIC void gc_sweep(void) {
Damiendcced922013-10-21 23:45:08 +0100173 // free unmarked heads and their tails
174 int free_tail = 0;
175 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
176 switch (ATB_GET_KIND(block)) {
177 case AT_HEAD:
mux4f7e9f52014-04-03 23:55:12 +0200178 if (ATB_IS_MPOBJ(block)) {
muxcc849f72014-04-05 15:49:03 +0200179 mp_obj_t dest[2];
180 mp_load_method((mp_obj_t*)PTR_FROM_BLOCK(block), MP_QSTR___del__, dest);
181 // load_method returned a method
182 if (dest[1] != MP_OBJ_NULL) {
183 mp_call_method_n_kw(0, 0, dest);
mux4f7e9f52014-04-03 23:55:12 +0200184 }
muxcc849f72014-04-05 15:49:03 +0200185 // clear mpobj flag
186 ATB_CLR_MPOBJ(block);
mux4f7e9f52014-04-03 23:55:12 +0200187 }
Damiendcced922013-10-21 23:45:08 +0100188 free_tail = 1;
189 // fall through to free the head
190
191 case AT_TAIL:
192 if (free_tail) {
193 ATB_ANY_TO_FREE(block);
194 }
195 break;
196
197 case AT_MARK:
198 ATB_MARK_TO_HEAD(block);
199 free_tail = 0;
200 break;
201 }
202 }
203}
204
Damien8b3a7c22013-10-23 20:20:17 +0100205void gc_collect_start(void) {
Damiendcced922013-10-21 23:45:08 +0100206 gc_stack_overflow = 0;
207 gc_sp = gc_stack;
208}
209
210void gc_collect_root(void **ptrs, machine_uint_t len) {
211 for (machine_uint_t i = 0; i < len; i++) {
212 machine_uint_t ptr = (machine_uint_t)ptrs[i];
213 VERIFY_MARK_AND_PUSH(ptr);
214 gc_drain_stack();
215 }
216}
217
Damien8b3a7c22013-10-23 20:20:17 +0100218void gc_collect_end(void) {
Damiendcced922013-10-21 23:45:08 +0100219 gc_deal_with_stack_overflow();
220 gc_sweep();
Damieneefcc792013-10-22 15:25:25 +0100221}
Damiendcced922013-10-21 23:45:08 +0100222
Damieneefcc792013-10-22 15:25:25 +0100223void gc_info(gc_info_t *info) {
224 info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t);
225 info->used = 0;
226 info->free = 0;
227 info->num_1block = 0;
228 info->num_2block = 0;
229 info->max_block = 0;
230 for (machine_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
231 machine_uint_t kind = ATB_GET_KIND(block);
232 if (kind == AT_FREE || kind == AT_HEAD) {
233 if (len == 1) {
234 info->num_1block += 1;
235 } else if (len == 2) {
236 info->num_2block += 1;
237 }
238 if (len > info->max_block) {
239 info->max_block = len;
240 }
241 }
242 switch (kind) {
Damiendcced922013-10-21 23:45:08 +0100243 case AT_FREE:
Damieneefcc792013-10-22 15:25:25 +0100244 info->free += 1;
245 len = 0;
Damiendcced922013-10-21 23:45:08 +0100246 break;
247
248 case AT_HEAD:
Damieneefcc792013-10-22 15:25:25 +0100249 info->used += 1;
250 len = 1;
251 break;
252
Damiendcced922013-10-21 23:45:08 +0100253 case AT_TAIL:
Damieneefcc792013-10-22 15:25:25 +0100254 info->used += 1;
255 len += 1;
Damiendcced922013-10-21 23:45:08 +0100256 break;
257
258 case AT_MARK:
Damieneefcc792013-10-22 15:25:25 +0100259 // shouldn't happen
Damiendcced922013-10-21 23:45:08 +0100260 break;
261 }
262 }
263
Damieneefcc792013-10-22 15:25:25 +0100264 info->used *= BYTES_PER_BLOCK;
265 info->free *= BYTES_PER_BLOCK;
Damiendcced922013-10-21 23:45:08 +0100266}
267
mux4f7e9f52014-04-03 23:55:12 +0200268void *_gc_alloc(machine_uint_t n_bytes, bool is_mpobj) {
Damiendcced922013-10-21 23:45:08 +0100269 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 +0000270 DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
Damiendcced922013-10-21 23:45:08 +0100271
272 // check for 0 allocation
273 if (n_blocks == 0) {
274 return NULL;
275 }
276
277 machine_uint_t i;
278 machine_uint_t end_block;
279 machine_uint_t start_block;
280 machine_uint_t n_free = 0;
281 int collected = 0;
282 for (;;) {
283
284 // look for a run of n_blocks available blocks
285 for (i = 0; i < gc_alloc_table_byte_len; i++) {
286 byte a = gc_alloc_table_start[i];
287 if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; }
288 if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; }
289 if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; }
290 if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; }
291 }
292
293 // nothing found!
294 if (collected) {
295 return NULL;
296 }
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200297 DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes);
Damiendcced922013-10-21 23:45:08 +0100298 gc_collect();
299 collected = 1;
300 }
301
302 // found, ending at block i inclusive
303found:
304 // get starting and end blocks, both inclusive
305 end_block = i;
306 start_block = i - n_free + 1;
307
308 // mark first block as used head
309 ATB_FREE_TO_HEAD(start_block);
310
311 // mark rest of blocks as used tail
312 // TODO for a run of many blocks can make this more efficient
313 for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) {
314 ATB_FREE_TO_TAIL(bl);
315 }
316
muxcc849f72014-04-05 15:49:03 +0200317 if (is_mpobj) {
318 // set mp_obj flag only if it has del
319 ATB_SET_MPOBJ(start_block);
320 }
321
Damiendcced922013-10-21 23:45:08 +0100322 // return pointer to first block
323 return (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
324}
325
mux4f7e9f52014-04-03 23:55:12 +0200326void *gc_alloc(machine_uint_t n_bytes) {
327 return _gc_alloc(n_bytes, false);
328}
329
330void *gc_alloc_mp_obj(machine_uint_t n_bytes) {
331 return _gc_alloc(n_bytes, true);
332}
333
Damienfd8b6bc2013-10-22 20:26:36 +0100334// force the freeing of a piece of memory
335void gc_free(void *ptr_in) {
336 machine_uint_t ptr = (machine_uint_t)ptr_in;
337
338 if (VERIFY_PTR(ptr)) {
339 machine_uint_t block = BLOCK_FROM_PTR(ptr);
340 if (ATB_GET_KIND(block) == AT_HEAD) {
341 // free head and all of its tail blocks
342 do {
343 ATB_ANY_TO_FREE(block);
344 block += 1;
345 } while (ATB_GET_KIND(block) == AT_TAIL);
346 }
347 }
348}
349
Damiendcced922013-10-21 23:45:08 +0100350machine_uint_t gc_nbytes(void *ptr_in) {
351 machine_uint_t ptr = (machine_uint_t)ptr_in;
352
Damienfd8b6bc2013-10-22 20:26:36 +0100353 if (VERIFY_PTR(ptr)) {
Damiendcced922013-10-21 23:45:08 +0100354 machine_uint_t block = BLOCK_FROM_PTR(ptr);
355 if (ATB_GET_KIND(block) == AT_HEAD) {
356 // work out number of consecutive blocks in the chain starting with this on
357 machine_uint_t n_blocks = 0;
358 do {
359 n_blocks += 1;
360 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
361 return n_blocks * BYTES_PER_BLOCK;
362 }
363 }
364
365 // invalid pointer
366 return 0;
367}
368
mux87826762014-03-12 21:00:23 +0200369#if 0
Damien George6fc765c2014-03-07 00:21:51 +0000370// use this realloc for now, one below is broken
371void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
372 machine_uint_t n_existing = gc_nbytes(ptr);
373 if (n_bytes <= n_existing) {
374 return ptr;
375 } else {
376 // TODO check if we can grow inplace
377 void *ptr2 = gc_alloc(n_bytes);
378 if (ptr2 == NULL) {
379 return ptr2;
380 }
381 memcpy(ptr2, ptr, n_existing);
382 gc_free(ptr);
383 return ptr2;
384 }
385}
mux87826762014-03-12 21:00:23 +0200386#else
muxfbaa1472014-03-05 23:23:04 +0200387void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
388 void *ptr_out = NULL;
Damien George73d57932014-03-06 00:02:16 +0000389 machine_uint_t block = 0;
muxfbaa1472014-03-05 23:23:04 +0200390 machine_uint_t ptr = (machine_uint_t)ptr_in;
391
392 if (ptr_in == NULL) {
393 return gc_alloc(n_bytes);
Damiendcced922013-10-21 23:45:08 +0100394 }
muxfbaa1472014-03-05 23:23:04 +0200395
Damien George470184e2014-03-12 22:44:11 +0000396 if (VERIFY_PTR(ptr) // verify pointer
397 && (block = BLOCK_FROM_PTR(ptr)) // get first block
398 && ATB_GET_KIND(block) == AT_HEAD) { // make sure it's a HEAD block
muxfbaa1472014-03-05 23:23:04 +0200399
400 byte block_type;
401 machine_uint_t n_free = 0;
Damien George470184e2014-03-12 22:44:11 +0000402 machine_uint_t n_blocks = 1; // counting HEAD block
mux87826762014-03-12 21:00:23 +0200403 machine_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
muxfbaa1472014-03-05 23:23:04 +0200404
Damien George470184e2014-03-12 22:44:11 +0000405 // get the number of consecutive tail blocks and
406 // the number of free blocks after last tail block
407 // stop if we reach (or are at) end of heap
mux87826762014-03-12 21:00:23 +0200408 while ((block + n_blocks + n_free) < max_block
Damien George470184e2014-03-12 22:44:11 +0000409 // stop as soon as we find enough blocks for n_bytes
mux87826762014-03-12 21:00:23 +0200410 && (n_bytes > ((n_blocks+n_free) * BYTES_PER_BLOCK))
Damien George470184e2014-03-12 22:44:11 +0000411 // stop if block is HEAD
mux87826762014-03-12 21:00:23 +0200412 && (block_type = ATB_GET_KIND(block + n_blocks + n_free)) != AT_HEAD) {
muxfbaa1472014-03-05 23:23:04 +0200413 switch (block_type) {
414 case AT_FREE: n_free++; break;
415 case AT_TAIL: n_blocks++; break;
416 default: break;
417 }
mux87826762014-03-12 21:00:23 +0200418 }
Damien George470184e2014-03-12 22:44:11 +0000419 // number of allocated bytes
muxfbaa1472014-03-05 23:23:04 +0200420 machine_uint_t n_existing = n_blocks * BYTES_PER_BLOCK;
421
Damien George470184e2014-03-12 22:44:11 +0000422 // check if realloc'ing to a smaller size
muxfbaa1472014-03-05 23:23:04 +0200423 if (n_bytes <= n_existing) {
424 ptr_out = ptr_in;
Damien George470184e2014-03-12 22:44:11 +0000425 // free unneeded tail blocks
Damien George73d57932014-03-06 00:02:16 +0000426 for (machine_uint_t bl = block + n_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) {
427 ATB_ANY_TO_FREE(bl);
muxfbaa1472014-03-05 23:23:04 +0200428 }
429
Damien George470184e2014-03-12 22:44:11 +0000430 // check if we can expand in place
mux87826762014-03-12 21:00:23 +0200431 } else if (n_bytes <= (n_existing + (n_free * BYTES_PER_BLOCK))) {
Damien George470184e2014-03-12 22:44:11 +0000432 // number of blocks needed to expand +1 if there's a remainder
muxfbaa1472014-03-05 23:23:04 +0200433 machine_uint_t n_diff = ( n_bytes - n_existing)/BYTES_PER_BLOCK+
434 ((n_bytes - n_existing)%BYTES_PER_BLOCK!=0);
435
436 DEBUG_printf("gc_realloc: expanding " UINT_FMT " blocks (" UINT_FMT " bytes) to " UINT_FMT " blocks (" UINT_FMT " bytes)\n",
437 n_existing/BYTES_PER_BLOCK, n_existing, n_existing/BYTES_PER_BLOCK+n_diff, n_existing + n_diff*BYTES_PER_BLOCK);
438
Damien George470184e2014-03-12 22:44:11 +0000439 // mark rest of blocks as used tail
Damien George73d57932014-03-06 00:02:16 +0000440 for (machine_uint_t bl = block + n_blocks; bl < (block + n_blocks + n_diff); bl++) {
muxfbaa1472014-03-05 23:23:04 +0200441 ATB_FREE_TO_TAIL(bl);
442 }
443 ptr_out = ptr_in;
444
Damien George470184e2014-03-12 22:44:11 +0000445 // try to find a new contiguous chain
muxfbaa1472014-03-05 23:23:04 +0200446 } else if ((ptr_out = gc_alloc(n_bytes)) != NULL) {
Damien George470184e2014-03-12 22:44:11 +0000447 DEBUG_printf("gc_realloc: allocating new block\n");
muxfbaa1472014-03-05 23:23:04 +0200448 memcpy(ptr_out, ptr_in, n_existing);
449 gc_free(ptr_in);
450 }
451 }
452
453 return ptr_out;
Damiendcced922013-10-21 23:45:08 +0100454}
mux87826762014-03-12 21:00:23 +0200455
Damien George6fc765c2014-03-07 00:21:51 +0000456#endif
Damiendcced922013-10-21 23:45:08 +0100457
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200458void gc_dump_info() {
459 gc_info_t info;
460 gc_info(&info);
461 printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free);
462 printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n",
463 info.num_1block, info.num_2block, info.max_block);
464}
465
Damien Georgece1162a2014-02-26 22:55:59 +0000466void gc_dump_alloc_table(void) {
467 printf("GC memory layout:");
Damieneefcc792013-10-22 15:25:25 +0100468 for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
Damien Georgece1162a2014-02-26 22:55:59 +0000469 if (bl % 64 == 0) {
470 printf("\n%04x: ", (uint)bl);
Damieneefcc792013-10-22 15:25:25 +0100471 }
Damien Georgece1162a2014-02-26 22:55:59 +0000472 int c = ' ';
473 switch (ATB_GET_KIND(bl)) {
474 case AT_FREE: c = '.'; break;
475 case AT_HEAD: c = 'h'; break;
476 case AT_TAIL: c = 't'; break;
477 case AT_MARK: c = 'm'; break;
478 }
479 printf("%c", c);
Damieneefcc792013-10-22 15:25:25 +0100480 }
Damien Georgece1162a2014-02-26 22:55:59 +0000481 printf("\n");
Damieneefcc792013-10-22 15:25:25 +0100482}
483
Damien Georgece1162a2014-02-26 22:55:59 +0000484#if DEBUG_PRINT
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200485void gc_test(void) {
486 machine_uint_t len = 500;
Damiendcced922013-10-21 23:45:08 +0100487 machine_uint_t *heap = malloc(len);
488 gc_init(heap, heap + len / sizeof(machine_uint_t));
489 void *ptrs[100];
490 {
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200491 machine_uint_t **p = gc_alloc(16);
Damiendcced922013-10-21 23:45:08 +0100492 p[0] = gc_alloc(64);
493 p[1] = gc_alloc(1);
494 p[2] = gc_alloc(1);
495 p[3] = gc_alloc(1);
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200496 machine_uint_t ***p2 = gc_alloc(16);
Damiendcced922013-10-21 23:45:08 +0100497 p2[0] = p;
498 p2[1] = p;
499 ptrs[0] = p2;
500 }
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200501 for (int i = 0; i < 25; i+=2) {
Damiendcced922013-10-21 23:45:08 +0100502 machine_uint_t *p = gc_alloc(i);
503 printf("p=%p\n", p);
504 if (i & 3) {
505 //ptrs[i] = p;
506 }
507 }
508
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200509 printf("Before GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000510 gc_dump_alloc_table();
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200511 printf("Starting GC...\n");
512 gc_collect_start();
513 gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*));
514 gc_collect_end();
515 printf("After GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000516 gc_dump_alloc_table();
Damiendcced922013-10-21 23:45:08 +0100517}
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200518#endif
Damien Georged3ebe482014-01-07 15:20:33 +0000519
520#endif // MICROPY_ENABLE_GC