blob: 30f2bfbde7f3a73c257f9200ff453e28792e7f1c [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 +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;
28STATIC machine_uint_t gc_alloc_table_byte_len;
Damien George12bab722014-04-05 20:35:48 +010029#if MICROPY_ENABLE_FINALISER
30STATIC byte *gc_finaliser_table_start;
31#endif
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;
Damien George443e0182014-04-08 11:31:21 +000038STATIC machine_uint_t gc_lock_depth;
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
Damiendcced922013-10-21 23:45:08 +010070#define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
71#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start))
72#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
73
Damien George12bab722014-04-05 20:35:48 +010074#if MICROPY_ENABLE_FINALISER
75// FTB = finaliser table byte
76// if set, then the corresponding block may have a finaliser
77
78#define BLOCKS_PER_FTB (8)
79
80#define FTB_GET(block) ((gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] >> ((block) & 7)) & 1)
81#define FTB_SET(block) do { gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] |= (1 << ((block) & 7)); } while (0)
82#define FTB_CLEAR(block) do { gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] &= (~(1 << ((block) & 7))); } while (0)
83#endif
84
Damienbb5316b2013-10-22 21:12:29 +010085// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
86void gc_init(void *start, void *end) {
87 // align end pointer on block boundary
88 end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
Damien George12bab722014-04-05 20:35:48 +010089 DEBUG_printf("Initializing GC heap: %p..%p = %ld bytes\n", start, end, end - start);
Damienbb5316b2013-10-22 21:12:29 +010090
Damien George12bab722014-04-05 20:35:48 +010091 // calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes):
92 // T = A + F + P
93 // F = A * BLOCKS_PER_ATB / BLOCKS_PER_FTB
94 // P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK
95 // => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK)
96 machine_uint_t total_byte_len = end - start;
97#if MICROPY_ENABLE_FINALISER
98 gc_alloc_table_byte_len = total_byte_len * BITS_PER_BYTE / (BITS_PER_BYTE + BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK);
99#else
100 gc_alloc_table_byte_len = total_byte_len / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
101#endif
102
103
Damienbb5316b2013-10-22 21:12:29 +0100104 gc_alloc_table_start = (byte*)start;
mux4f7e9f52014-04-03 23:55:12 +0200105
Damien George12bab722014-04-05 20:35:48 +0100106#if MICROPY_ENABLE_FINALISER
107 machine_uint_t gc_finaliser_table_byte_len = (gc_alloc_table_byte_len * BLOCKS_PER_ATB) / BLOCKS_PER_FTB;
108 gc_finaliser_table_start = gc_alloc_table_start + gc_alloc_table_byte_len;
109#endif
mux4f7e9f52014-04-03 23:55:12 +0200110
Damien George12bab722014-04-05 20:35:48 +0100111 machine_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
112 gc_pool_start = end - gc_pool_block_len * BYTES_PER_BLOCK;
Damienbb5316b2013-10-22 21:12:29 +0100113 gc_pool_end = end;
114
115 // clear ATBs
116 memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len);
117
Damien George12bab722014-04-05 20:35:48 +0100118#if MICROPY_ENABLE_FINALISER
119 // clear FTBs
120 memset(gc_finaliser_table_start, 0, gc_finaliser_table_byte_len);
121#endif
mux4f7e9f52014-04-03 23:55:12 +0200122
Damienbb5316b2013-10-22 21:12:29 +0100123 // allocate first block because gc_pool_start points there and it will never
124 // be freed, so allocating 1 block with null pointers will minimise memory loss
125 ATB_FREE_TO_HEAD(0);
126 for (int i = 0; i < WORDS_PER_BLOCK; i++) {
127 gc_pool_start[i] = 0;
128 }
129
Damien George12bab722014-04-05 20:35:48 +0100130 // unlock the GC
Damien George443e0182014-04-08 11:31:21 +0000131 gc_lock_depth = 0;
Damien George12bab722014-04-05 20:35:48 +0100132
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200133 DEBUG_printf("GC layout:\n");
Damien George12bab722014-04-05 20:35:48 +0100134 DEBUG_printf(" alloc table at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", gc_alloc_table_start, gc_alloc_table_byte_len, gc_alloc_table_byte_len * BLOCKS_PER_ATB);
135#if MICROPY_ENABLE_FINALISER
136 DEBUG_printf(" finaliser table at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", gc_finaliser_table_start, gc_finaliser_table_byte_len, gc_finaliser_table_byte_len * BLOCKS_PER_FTB);
137#endif
138 DEBUG_printf(" pool at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", gc_pool_start, gc_pool_block_len * BYTES_PER_BLOCK, gc_pool_block_len);
Damienbb5316b2013-10-22 21:12:29 +0100139}
140
Damien George443e0182014-04-08 11:31:21 +0000141void gc_lock(void) {
142 gc_lock_depth++;
143}
144
145void gc_unlock(void) {
146 gc_lock_depth--;
147}
148
Damienfd8b6bc2013-10-22 20:26:36 +0100149#define VERIFY_PTR(ptr) ( \
150 (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
151 && ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \
152 && ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \
153 )
154
Damiendcced922013-10-21 23:45:08 +0100155#define VERIFY_MARK_AND_PUSH(ptr) \
156 do { \
Damienfd8b6bc2013-10-22 20:26:36 +0100157 if (VERIFY_PTR(ptr)) { \
Damiendcced922013-10-21 23:45:08 +0100158 machine_uint_t _block = BLOCK_FROM_PTR(ptr); \
159 if (ATB_GET_KIND(_block) == AT_HEAD) { \
160 /* an unmarked head, mark it, and push it on gc stack */ \
161 ATB_HEAD_TO_MARK(_block); \
162 if (gc_sp < &gc_stack[STACK_SIZE]) { \
163 *gc_sp++ = _block; \
164 } else { \
165 gc_stack_overflow = 1; \
166 } \
167 } \
168 } \
169 } while (0)
170
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200171STATIC void gc_drain_stack(void) {
Damiendcced922013-10-21 23:45:08 +0100172 while (gc_sp > gc_stack) {
173 // pop the next block off the stack
174 machine_uint_t block = *--gc_sp;
175
Damieneefcc792013-10-22 15:25:25 +0100176 // work out number of consecutive blocks in the chain starting with this one
Damiendcced922013-10-21 23:45:08 +0100177 machine_uint_t n_blocks = 0;
178 do {
179 n_blocks += 1;
180 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
181
182 // check this block's children
183 machine_uint_t *scan = (machine_uint_t*)PTR_FROM_BLOCK(block);
184 for (machine_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) {
185 machine_uint_t ptr2 = *scan;
186 VERIFY_MARK_AND_PUSH(ptr2);
187 }
188 }
189}
190
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200191STATIC void gc_deal_with_stack_overflow(void) {
Damiendcced922013-10-21 23:45:08 +0100192 while (gc_stack_overflow) {
193 gc_stack_overflow = 0;
194 gc_sp = gc_stack;
195
196 // scan entire memory looking for blocks which have been marked but not their children
197 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
198 // trace (again) if mark bit set
199 if (ATB_GET_KIND(block) == AT_MARK) {
200 *gc_sp++ = block;
201 gc_drain_stack();
202 }
203 }
204 }
205}
206
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200207STATIC void gc_sweep(void) {
Damiendcced922013-10-21 23:45:08 +0100208 // free unmarked heads and their tails
209 int free_tail = 0;
210 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
211 switch (ATB_GET_KIND(block)) {
212 case AT_HEAD:
Damien George12bab722014-04-05 20:35:48 +0100213#if MICROPY_ENABLE_FINALISER
214 if (FTB_GET(block)) {
215 mp_obj_t obj = (mp_obj_t)PTR_FROM_BLOCK(block);
216 if (((mp_obj_base_t*)obj)->type != MP_OBJ_NULL) {
217 // if the object has a type then see if it has a __del__ method
218 mp_obj_t dest[2];
219 mp_load_method_maybe(obj, MP_QSTR___del__, dest);
220 if (dest[0] != MP_OBJ_NULL) {
221 // load_method returned a method
222 mp_call_method_n_kw(0, 0, dest);
223 }
mux4f7e9f52014-04-03 23:55:12 +0200224 }
Damien George12bab722014-04-05 20:35:48 +0100225 // clear finaliser flag
226 FTB_CLEAR(block);
mux4f7e9f52014-04-03 23:55:12 +0200227 }
Damien George12bab722014-04-05 20:35:48 +0100228#endif
Damiendcced922013-10-21 23:45:08 +0100229 free_tail = 1;
230 // fall through to free the head
231
232 case AT_TAIL:
233 if (free_tail) {
234 ATB_ANY_TO_FREE(block);
235 }
236 break;
237
238 case AT_MARK:
239 ATB_MARK_TO_HEAD(block);
240 free_tail = 0;
241 break;
242 }
243 }
244}
245
Damien8b3a7c22013-10-23 20:20:17 +0100246void gc_collect_start(void) {
Damien George443e0182014-04-08 11:31:21 +0000247 gc_lock();
Damiendcced922013-10-21 23:45:08 +0100248 gc_stack_overflow = 0;
249 gc_sp = gc_stack;
250}
251
252void gc_collect_root(void **ptrs, machine_uint_t len) {
253 for (machine_uint_t i = 0; i < len; i++) {
254 machine_uint_t ptr = (machine_uint_t)ptrs[i];
255 VERIFY_MARK_AND_PUSH(ptr);
256 gc_drain_stack();
257 }
258}
259
Damien8b3a7c22013-10-23 20:20:17 +0100260void gc_collect_end(void) {
Damiendcced922013-10-21 23:45:08 +0100261 gc_deal_with_stack_overflow();
262 gc_sweep();
Damien George443e0182014-04-08 11:31:21 +0000263 gc_unlock();
Damieneefcc792013-10-22 15:25:25 +0100264}
Damiendcced922013-10-21 23:45:08 +0100265
Damieneefcc792013-10-22 15:25:25 +0100266void gc_info(gc_info_t *info) {
267 info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t);
268 info->used = 0;
269 info->free = 0;
270 info->num_1block = 0;
271 info->num_2block = 0;
272 info->max_block = 0;
273 for (machine_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
274 machine_uint_t kind = ATB_GET_KIND(block);
275 if (kind == AT_FREE || kind == AT_HEAD) {
276 if (len == 1) {
277 info->num_1block += 1;
278 } else if (len == 2) {
279 info->num_2block += 1;
280 }
281 if (len > info->max_block) {
282 info->max_block = len;
283 }
284 }
285 switch (kind) {
Damiendcced922013-10-21 23:45:08 +0100286 case AT_FREE:
Damieneefcc792013-10-22 15:25:25 +0100287 info->free += 1;
288 len = 0;
Damiendcced922013-10-21 23:45:08 +0100289 break;
290
291 case AT_HEAD:
Damieneefcc792013-10-22 15:25:25 +0100292 info->used += 1;
293 len = 1;
294 break;
295
Damiendcced922013-10-21 23:45:08 +0100296 case AT_TAIL:
Damieneefcc792013-10-22 15:25:25 +0100297 info->used += 1;
298 len += 1;
Damiendcced922013-10-21 23:45:08 +0100299 break;
300
301 case AT_MARK:
Damieneefcc792013-10-22 15:25:25 +0100302 // shouldn't happen
Damiendcced922013-10-21 23:45:08 +0100303 break;
304 }
305 }
306
Damieneefcc792013-10-22 15:25:25 +0100307 info->used *= BYTES_PER_BLOCK;
308 info->free *= BYTES_PER_BLOCK;
Damiendcced922013-10-21 23:45:08 +0100309}
310
Damien George12bab722014-04-05 20:35:48 +0100311void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser) {
Damiendcced922013-10-21 23:45:08 +0100312 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 +0000313 DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
Damiendcced922013-10-21 23:45:08 +0100314
Damien George443e0182014-04-08 11:31:21 +0000315 // check if GC is locked
316 if (gc_lock_depth > 0) {
317 return NULL;
Damien George12bab722014-04-05 20:35:48 +0100318 }
319
Damiendcced922013-10-21 23:45:08 +0100320 // check for 0 allocation
321 if (n_blocks == 0) {
322 return NULL;
323 }
324
325 machine_uint_t i;
326 machine_uint_t end_block;
327 machine_uint_t start_block;
328 machine_uint_t n_free = 0;
329 int collected = 0;
330 for (;;) {
331
332 // look for a run of n_blocks available blocks
333 for (i = 0; i < gc_alloc_table_byte_len; i++) {
334 byte a = gc_alloc_table_start[i];
335 if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; }
336 if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; }
337 if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; }
338 if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; }
339 }
340
341 // nothing found!
342 if (collected) {
343 return NULL;
344 }
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200345 DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes);
Damiendcced922013-10-21 23:45:08 +0100346 gc_collect();
347 collected = 1;
348 }
349
350 // found, ending at block i inclusive
351found:
352 // get starting and end blocks, both inclusive
353 end_block = i;
354 start_block = i - n_free + 1;
355
356 // mark first block as used head
357 ATB_FREE_TO_HEAD(start_block);
358
359 // mark rest of blocks as used tail
360 // TODO for a run of many blocks can make this more efficient
361 for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) {
362 ATB_FREE_TO_TAIL(bl);
363 }
364
Damien George12bab722014-04-05 20:35:48 +0100365 // get pointer to first block
366 void *ret_ptr = (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
muxcc849f72014-04-05 15:49:03 +0200367
Damien George12bab722014-04-05 20:35:48 +0100368#if MICROPY_ENABLE_FINALISER
369 if (has_finaliser) {
370 // clear type pointer in case it is never set
371 ((mp_obj_base_t*)ret_ptr)->type = MP_OBJ_NULL;
372 // set mp_obj flag only if it has a finaliser
373 FTB_SET(start_block);
374 }
375#endif
376
377 return ret_ptr;
Damiendcced922013-10-21 23:45:08 +0100378}
379
Damien George12bab722014-04-05 20:35:48 +0100380/*
mux4f7e9f52014-04-03 23:55:12 +0200381void *gc_alloc(machine_uint_t n_bytes) {
382 return _gc_alloc(n_bytes, false);
383}
384
Damien George12bab722014-04-05 20:35:48 +0100385void *gc_alloc_with_finaliser(machine_uint_t n_bytes) {
mux4f7e9f52014-04-03 23:55:12 +0200386 return _gc_alloc(n_bytes, true);
387}
Damien George12bab722014-04-05 20:35:48 +0100388*/
mux4f7e9f52014-04-03 23:55:12 +0200389
Damienfd8b6bc2013-10-22 20:26:36 +0100390// force the freeing of a piece of memory
391void gc_free(void *ptr_in) {
Damien George443e0182014-04-08 11:31:21 +0000392 if (gc_lock_depth > 0) {
393 // TODO how to deal with this error?
394 return;
Damien George12bab722014-04-05 20:35:48 +0100395 }
396
Damienfd8b6bc2013-10-22 20:26:36 +0100397 machine_uint_t ptr = (machine_uint_t)ptr_in;
398
399 if (VERIFY_PTR(ptr)) {
400 machine_uint_t block = BLOCK_FROM_PTR(ptr);
401 if (ATB_GET_KIND(block) == AT_HEAD) {
402 // free head and all of its tail blocks
403 do {
404 ATB_ANY_TO_FREE(block);
405 block += 1;
406 } while (ATB_GET_KIND(block) == AT_TAIL);
407 }
408 }
409}
410
Damiendcced922013-10-21 23:45:08 +0100411machine_uint_t gc_nbytes(void *ptr_in) {
412 machine_uint_t ptr = (machine_uint_t)ptr_in;
413
Damienfd8b6bc2013-10-22 20:26:36 +0100414 if (VERIFY_PTR(ptr)) {
Damiendcced922013-10-21 23:45:08 +0100415 machine_uint_t block = BLOCK_FROM_PTR(ptr);
416 if (ATB_GET_KIND(block) == AT_HEAD) {
417 // work out number of consecutive blocks in the chain starting with this on
418 machine_uint_t n_blocks = 0;
419 do {
420 n_blocks += 1;
421 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
422 return n_blocks * BYTES_PER_BLOCK;
423 }
424 }
425
426 // invalid pointer
427 return 0;
428}
429
mux87826762014-03-12 21:00:23 +0200430#if 0
Damien George443e0182014-04-08 11:31:21 +0000431// old, simple realloc that didn't expand memory in place
Damien George6fc765c2014-03-07 00:21:51 +0000432void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
433 machine_uint_t n_existing = gc_nbytes(ptr);
434 if (n_bytes <= n_existing) {
435 return ptr;
436 } else {
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300437 // TODO false is incorrect! Should get value from current block!
438 void *ptr2 = gc_alloc(n_bytes,
439#if MICROPY_ENABLE_FINALISER
440 FTB_GET(BLOCK_FROM_PTR((machine_uint_t)ptr))
441#else
442 false
443#endif
444 );
Damien George6fc765c2014-03-07 00:21:51 +0000445 if (ptr2 == NULL) {
446 return ptr2;
447 }
448 memcpy(ptr2, ptr, n_existing);
449 gc_free(ptr);
450 return ptr2;
451 }
452}
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300453
454#else // Alternative gc_realloc impl
Damien George443e0182014-04-08 11:31:21 +0000455
muxfbaa1472014-03-05 23:23:04 +0200456void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
Damien George443e0182014-04-08 11:31:21 +0000457 if (gc_lock_depth > 0) {
458 return NULL;
Damien George12bab722014-04-05 20:35:48 +0100459 }
460
muxfbaa1472014-03-05 23:23:04 +0200461 void *ptr_out = NULL;
Damien George73d57932014-03-06 00:02:16 +0000462 machine_uint_t block = 0;
muxfbaa1472014-03-05 23:23:04 +0200463 machine_uint_t ptr = (machine_uint_t)ptr_in;
464
465 if (ptr_in == NULL) {
Damien George12bab722014-04-05 20:35:48 +0100466 return gc_alloc(n_bytes, false);
Damiendcced922013-10-21 23:45:08 +0100467 }
muxfbaa1472014-03-05 23:23:04 +0200468
Damien George470184e2014-03-12 22:44:11 +0000469 if (VERIFY_PTR(ptr) // verify pointer
470 && (block = BLOCK_FROM_PTR(ptr)) // get first block
471 && ATB_GET_KIND(block) == AT_HEAD) { // make sure it's a HEAD block
muxfbaa1472014-03-05 23:23:04 +0200472
473 byte block_type;
474 machine_uint_t n_free = 0;
Damien George470184e2014-03-12 22:44:11 +0000475 machine_uint_t n_blocks = 1; // counting HEAD block
mux87826762014-03-12 21:00:23 +0200476 machine_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
muxfbaa1472014-03-05 23:23:04 +0200477
Damien George470184e2014-03-12 22:44:11 +0000478 // get the number of consecutive tail blocks and
479 // the number of free blocks after last tail block
480 // stop if we reach (or are at) end of heap
mux87826762014-03-12 21:00:23 +0200481 while ((block + n_blocks + n_free) < max_block
Damien George470184e2014-03-12 22:44:11 +0000482 // stop as soon as we find enough blocks for n_bytes
mux87826762014-03-12 21:00:23 +0200483 && (n_bytes > ((n_blocks+n_free) * BYTES_PER_BLOCK))
Damien George470184e2014-03-12 22:44:11 +0000484 // stop if block is HEAD
mux87826762014-03-12 21:00:23 +0200485 && (block_type = ATB_GET_KIND(block + n_blocks + n_free)) != AT_HEAD) {
muxfbaa1472014-03-05 23:23:04 +0200486 switch (block_type) {
487 case AT_FREE: n_free++; break;
488 case AT_TAIL: n_blocks++; break;
489 default: break;
490 }
mux87826762014-03-12 21:00:23 +0200491 }
Damien George470184e2014-03-12 22:44:11 +0000492 // number of allocated bytes
muxfbaa1472014-03-05 23:23:04 +0200493 machine_uint_t n_existing = n_blocks * BYTES_PER_BLOCK;
494
Damien George470184e2014-03-12 22:44:11 +0000495 // check if realloc'ing to a smaller size
muxfbaa1472014-03-05 23:23:04 +0200496 if (n_bytes <= n_existing) {
497 ptr_out = ptr_in;
Damien George470184e2014-03-12 22:44:11 +0000498 // free unneeded tail blocks
Damien George73d57932014-03-06 00:02:16 +0000499 for (machine_uint_t bl = block + n_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) {
500 ATB_ANY_TO_FREE(bl);
muxfbaa1472014-03-05 23:23:04 +0200501 }
502
Damien George470184e2014-03-12 22:44:11 +0000503 // check if we can expand in place
mux87826762014-03-12 21:00:23 +0200504 } else if (n_bytes <= (n_existing + (n_free * BYTES_PER_BLOCK))) {
Damien George470184e2014-03-12 22:44:11 +0000505 // number of blocks needed to expand +1 if there's a remainder
muxfbaa1472014-03-05 23:23:04 +0200506 machine_uint_t n_diff = ( n_bytes - n_existing)/BYTES_PER_BLOCK+
507 ((n_bytes - n_existing)%BYTES_PER_BLOCK!=0);
508
509 DEBUG_printf("gc_realloc: expanding " UINT_FMT " blocks (" UINT_FMT " bytes) to " UINT_FMT " blocks (" UINT_FMT " bytes)\n",
510 n_existing/BYTES_PER_BLOCK, n_existing, n_existing/BYTES_PER_BLOCK+n_diff, n_existing + n_diff*BYTES_PER_BLOCK);
511
Damien George470184e2014-03-12 22:44:11 +0000512 // mark rest of blocks as used tail
Damien George73d57932014-03-06 00:02:16 +0000513 for (machine_uint_t bl = block + n_blocks; bl < (block + n_blocks + n_diff); bl++) {
muxfbaa1472014-03-05 23:23:04 +0200514 ATB_FREE_TO_TAIL(bl);
515 }
516 ptr_out = ptr_in;
517
Damien George470184e2014-03-12 22:44:11 +0000518 // try to find a new contiguous chain
Damien George12bab722014-04-05 20:35:48 +0100519 } else if ((ptr_out = gc_alloc(n_bytes,
520#if MICROPY_ENABLE_FINALISER
521 FTB_GET(block)
522#else
523 false
524#endif
525 )) != NULL) {
Damien George470184e2014-03-12 22:44:11 +0000526 DEBUG_printf("gc_realloc: allocating new block\n");
muxfbaa1472014-03-05 23:23:04 +0200527 memcpy(ptr_out, ptr_in, n_existing);
528 gc_free(ptr_in);
529 }
530 }
531
532 return ptr_out;
Damiendcced922013-10-21 23:45:08 +0100533}
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300534#endif // Alternative gc_realloc impl
mux87826762014-03-12 21:00:23 +0200535
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200536void gc_dump_info() {
537 gc_info_t info;
538 gc_info(&info);
539 printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free);
540 printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n",
541 info.num_1block, info.num_2block, info.max_block);
542}
543
Damien Georgece1162a2014-02-26 22:55:59 +0000544void gc_dump_alloc_table(void) {
545 printf("GC memory layout:");
Damieneefcc792013-10-22 15:25:25 +0100546 for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
Damien Georgece1162a2014-02-26 22:55:59 +0000547 if (bl % 64 == 0) {
548 printf("\n%04x: ", (uint)bl);
Damieneefcc792013-10-22 15:25:25 +0100549 }
Damien Georgece1162a2014-02-26 22:55:59 +0000550 int c = ' ';
551 switch (ATB_GET_KIND(bl)) {
552 case AT_FREE: c = '.'; break;
553 case AT_HEAD: c = 'h'; break;
554 case AT_TAIL: c = 't'; break;
555 case AT_MARK: c = 'm'; break;
556 }
557 printf("%c", c);
Damieneefcc792013-10-22 15:25:25 +0100558 }
Damien Georgece1162a2014-02-26 22:55:59 +0000559 printf("\n");
Damieneefcc792013-10-22 15:25:25 +0100560}
561
Damien Georgece1162a2014-02-26 22:55:59 +0000562#if DEBUG_PRINT
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200563void gc_test(void) {
564 machine_uint_t len = 500;
Damiendcced922013-10-21 23:45:08 +0100565 machine_uint_t *heap = malloc(len);
566 gc_init(heap, heap + len / sizeof(machine_uint_t));
567 void *ptrs[100];
568 {
Damien George12bab722014-04-05 20:35:48 +0100569 machine_uint_t **p = gc_alloc(16, false);
570 p[0] = gc_alloc(64, false);
571 p[1] = gc_alloc(1, false);
572 p[2] = gc_alloc(1, false);
573 p[3] = gc_alloc(1, false);
574 machine_uint_t ***p2 = gc_alloc(16, false);
Damiendcced922013-10-21 23:45:08 +0100575 p2[0] = p;
576 p2[1] = p;
577 ptrs[0] = p2;
578 }
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200579 for (int i = 0; i < 25; i+=2) {
Damien George12bab722014-04-05 20:35:48 +0100580 machine_uint_t *p = gc_alloc(i, false);
Damiendcced922013-10-21 23:45:08 +0100581 printf("p=%p\n", p);
582 if (i & 3) {
583 //ptrs[i] = p;
584 }
585 }
586
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200587 printf("Before GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000588 gc_dump_alloc_table();
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200589 printf("Starting GC...\n");
590 gc_collect_start();
591 gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*));
592 gc_collect_end();
593 printf("After GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000594 gc_dump_alloc_table();
Damiendcced922013-10-21 23:45:08 +0100595}
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200596#endif
Damien Georged3ebe482014-01-07 15:20:33 +0000597
598#endif // MICROPY_ENABLE_GC