blob: 2930e9011071e890f8ebb9c47c3eb7f5ac4013ec [file] [log] [blame]
Paul Sokolovskyc86889d2014-04-20 11:45:16 +03001#include <assert.h>
Damiendcced922013-10-21 23:45:08 +01002#include <stdio.h>
Damiendcced922013-10-21 23:45:08 +01003#include <string.h>
mux4f7e9f52014-04-03 23:55:12 +02004#include <stdbool.h>
Damiendcced922013-10-21 23:45:08 +01005
Damiend99b0522013-12-21 18:17:45 +00006#include "mpconfig.h"
Paul Sokolovskye807fa82014-04-02 20:36:32 +03007#include "misc.h"
Damiendcced922013-10-21 23:45:08 +01008#include "gc.h"
9
mux4f7e9f52014-04-03 23:55:12 +020010#include "misc.h"
11#include "qstr.h"
12#include "obj.h"
muxcc849f72014-04-05 15:49:03 +020013#include "runtime.h"
mux4f7e9f52014-04-03 23:55:12 +020014
Damien Georged3ebe482014-01-07 15:20:33 +000015#if MICROPY_ENABLE_GC
16
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020017#if 0 // print debugging info
18#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020019#define DEBUG_printf DEBUG_printf
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020020#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000021#define DEBUG_printf(...) (void)0
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020022#endif
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;
29STATIC machine_uint_t gc_alloc_table_byte_len;
Damien George12bab722014-04-05 20:35:48 +010030#if MICROPY_ENABLE_FINALISER
31STATIC byte *gc_finaliser_table_start;
32#endif
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;
Damien George443e0182014-04-08 11:31:21 +000039STATIC machine_uint_t gc_lock_depth;
Damiendcced922013-10-21 23:45:08 +010040
Damiendcced922013-10-21 23:45:08 +010041// ATB = allocation table byte
42// 0b00 = FREE -- free block
43// 0b01 = HEAD -- head of a chain of blocks
44// 0b10 = TAIL -- in the tail of a chain of blocks
45// 0b11 = MARK -- marked head block
46
47#define AT_FREE (0)
48#define AT_HEAD (1)
49#define AT_TAIL (2)
50#define AT_MARK (3)
51
52#define BLOCKS_PER_ATB (4)
53#define ATB_MASK_0 (0x03)
54#define ATB_MASK_1 (0x0c)
55#define ATB_MASK_2 (0x30)
56#define ATB_MASK_3 (0xc0)
57
58#define ATB_0_IS_FREE(a) (((a) & ATB_MASK_0) == 0)
59#define ATB_1_IS_FREE(a) (((a) & ATB_MASK_1) == 0)
60#define ATB_2_IS_FREE(a) (((a) & ATB_MASK_2) == 0)
61#define ATB_3_IS_FREE(a) (((a) & ATB_MASK_3) == 0)
62
63#define BLOCK_SHIFT(block) (2 * ((block) & (BLOCKS_PER_ATB - 1)))
64#define ATB_GET_KIND(block) ((gc_alloc_table_start[(block) / BLOCKS_PER_ATB] >> BLOCK_SHIFT(block)) & 3)
65#define ATB_ANY_TO_FREE(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_MARK << BLOCK_SHIFT(block))); } while (0)
66#define ATB_FREE_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_HEAD << BLOCK_SHIFT(block)); } while (0)
67#define ATB_FREE_TO_TAIL(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_TAIL << BLOCK_SHIFT(block)); } while (0)
68#define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
69#define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
70
Damiendcced922013-10-21 23:45:08 +010071#define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK)
72#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start))
73#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
74
Damien George12bab722014-04-05 20:35:48 +010075#if MICROPY_ENABLE_FINALISER
76// FTB = finaliser table byte
77// if set, then the corresponding block may have a finaliser
78
79#define BLOCKS_PER_FTB (8)
80
81#define FTB_GET(block) ((gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] >> ((block) & 7)) & 1)
82#define FTB_SET(block) do { gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] |= (1 << ((block) & 7)); } while (0)
83#define FTB_CLEAR(block) do { gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] &= (~(1 << ((block) & 7))); } while (0)
84#endif
85
Damienbb5316b2013-10-22 21:12:29 +010086// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
87void gc_init(void *start, void *end) {
88 // align end pointer on block boundary
89 end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
Damien George12bab722014-04-05 20:35:48 +010090 DEBUG_printf("Initializing GC heap: %p..%p = %ld bytes\n", start, end, end - start);
Damienbb5316b2013-10-22 21:12:29 +010091
Damien George12bab722014-04-05 20:35:48 +010092 // calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes):
93 // T = A + F + P
94 // F = A * BLOCKS_PER_ATB / BLOCKS_PER_FTB
95 // P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK
96 // => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK)
97 machine_uint_t total_byte_len = end - start;
98#if MICROPY_ENABLE_FINALISER
99 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);
100#else
101 gc_alloc_table_byte_len = total_byte_len / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
102#endif
103
104
Damienbb5316b2013-10-22 21:12:29 +0100105 gc_alloc_table_start = (byte*)start;
mux4f7e9f52014-04-03 23:55:12 +0200106
Damien George12bab722014-04-05 20:35:48 +0100107#if MICROPY_ENABLE_FINALISER
108 machine_uint_t gc_finaliser_table_byte_len = (gc_alloc_table_byte_len * BLOCKS_PER_ATB) / BLOCKS_PER_FTB;
109 gc_finaliser_table_start = gc_alloc_table_start + gc_alloc_table_byte_len;
110#endif
mux4f7e9f52014-04-03 23:55:12 +0200111
Damien George12bab722014-04-05 20:35:48 +0100112 machine_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
113 gc_pool_start = end - gc_pool_block_len * BYTES_PER_BLOCK;
Damienbb5316b2013-10-22 21:12:29 +0100114 gc_pool_end = end;
115
116 // clear ATBs
117 memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len);
118
Damien George12bab722014-04-05 20:35:48 +0100119#if MICROPY_ENABLE_FINALISER
120 // clear FTBs
121 memset(gc_finaliser_table_start, 0, gc_finaliser_table_byte_len);
122#endif
mux4f7e9f52014-04-03 23:55:12 +0200123
Damienbb5316b2013-10-22 21:12:29 +0100124 // allocate first block because gc_pool_start points there and it will never
125 // be freed, so allocating 1 block with null pointers will minimise memory loss
126 ATB_FREE_TO_HEAD(0);
127 for (int i = 0; i < WORDS_PER_BLOCK; i++) {
128 gc_pool_start[i] = 0;
129 }
130
Damien George12bab722014-04-05 20:35:48 +0100131 // unlock the GC
Damien George443e0182014-04-08 11:31:21 +0000132 gc_lock_depth = 0;
Damien George12bab722014-04-05 20:35:48 +0100133
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200134 DEBUG_printf("GC layout:\n");
Damien George12bab722014-04-05 20:35:48 +0100135 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);
136#if MICROPY_ENABLE_FINALISER
137 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);
138#endif
139 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 +0100140}
141
Damien George443e0182014-04-08 11:31:21 +0000142void gc_lock(void) {
143 gc_lock_depth++;
144}
145
146void gc_unlock(void) {
147 gc_lock_depth--;
148}
149
Damienfd8b6bc2013-10-22 20:26:36 +0100150#define VERIFY_PTR(ptr) ( \
151 (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
152 && ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \
153 && ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \
154 )
155
Damiendcced922013-10-21 23:45:08 +0100156#define VERIFY_MARK_AND_PUSH(ptr) \
157 do { \
Damienfd8b6bc2013-10-22 20:26:36 +0100158 if (VERIFY_PTR(ptr)) { \
Damiendcced922013-10-21 23:45:08 +0100159 machine_uint_t _block = BLOCK_FROM_PTR(ptr); \
160 if (ATB_GET_KIND(_block) == AT_HEAD) { \
161 /* an unmarked head, mark it, and push it on gc stack */ \
162 ATB_HEAD_TO_MARK(_block); \
163 if (gc_sp < &gc_stack[STACK_SIZE]) { \
164 *gc_sp++ = _block; \
165 } else { \
166 gc_stack_overflow = 1; \
167 } \
168 } \
169 } \
170 } while (0)
171
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200172STATIC void gc_drain_stack(void) {
Damiendcced922013-10-21 23:45:08 +0100173 while (gc_sp > gc_stack) {
174 // pop the next block off the stack
175 machine_uint_t block = *--gc_sp;
176
Damieneefcc792013-10-22 15:25:25 +0100177 // work out number of consecutive blocks in the chain starting with this one
Damiendcced922013-10-21 23:45:08 +0100178 machine_uint_t n_blocks = 0;
179 do {
180 n_blocks += 1;
181 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
182
183 // check this block's children
184 machine_uint_t *scan = (machine_uint_t*)PTR_FROM_BLOCK(block);
185 for (machine_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) {
186 machine_uint_t ptr2 = *scan;
187 VERIFY_MARK_AND_PUSH(ptr2);
188 }
189 }
190}
191
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200192STATIC void gc_deal_with_stack_overflow(void) {
Damiendcced922013-10-21 23:45:08 +0100193 while (gc_stack_overflow) {
194 gc_stack_overflow = 0;
195 gc_sp = gc_stack;
196
197 // scan entire memory looking for blocks which have been marked but not their children
198 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
199 // trace (again) if mark bit set
200 if (ATB_GET_KIND(block) == AT_MARK) {
201 *gc_sp++ = block;
202 gc_drain_stack();
203 }
204 }
205 }
206}
207
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200208STATIC void gc_sweep(void) {
Damiendcced922013-10-21 23:45:08 +0100209 // free unmarked heads and their tails
210 int free_tail = 0;
211 for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
212 switch (ATB_GET_KIND(block)) {
213 case AT_HEAD:
Damien George12bab722014-04-05 20:35:48 +0100214#if MICROPY_ENABLE_FINALISER
215 if (FTB_GET(block)) {
216 mp_obj_t obj = (mp_obj_t)PTR_FROM_BLOCK(block);
217 if (((mp_obj_base_t*)obj)->type != MP_OBJ_NULL) {
218 // if the object has a type then see if it has a __del__ method
219 mp_obj_t dest[2];
220 mp_load_method_maybe(obj, MP_QSTR___del__, dest);
221 if (dest[0] != MP_OBJ_NULL) {
222 // load_method returned a method
223 mp_call_method_n_kw(0, 0, dest);
224 }
mux4f7e9f52014-04-03 23:55:12 +0200225 }
Damien George12bab722014-04-05 20:35:48 +0100226 // clear finaliser flag
227 FTB_CLEAR(block);
mux4f7e9f52014-04-03 23:55:12 +0200228 }
Damien George12bab722014-04-05 20:35:48 +0100229#endif
Damiendcced922013-10-21 23:45:08 +0100230 free_tail = 1;
231 // fall through to free the head
232
233 case AT_TAIL:
234 if (free_tail) {
235 ATB_ANY_TO_FREE(block);
236 }
237 break;
238
239 case AT_MARK:
240 ATB_MARK_TO_HEAD(block);
241 free_tail = 0;
242 break;
243 }
244 }
245}
246
Damien8b3a7c22013-10-23 20:20:17 +0100247void gc_collect_start(void) {
Damien George443e0182014-04-08 11:31:21 +0000248 gc_lock();
Damiendcced922013-10-21 23:45:08 +0100249 gc_stack_overflow = 0;
250 gc_sp = gc_stack;
251}
252
253void gc_collect_root(void **ptrs, machine_uint_t len) {
254 for (machine_uint_t i = 0; i < len; i++) {
255 machine_uint_t ptr = (machine_uint_t)ptrs[i];
256 VERIFY_MARK_AND_PUSH(ptr);
257 gc_drain_stack();
258 }
259}
260
Damien8b3a7c22013-10-23 20:20:17 +0100261void gc_collect_end(void) {
Damiendcced922013-10-21 23:45:08 +0100262 gc_deal_with_stack_overflow();
263 gc_sweep();
Damien George443e0182014-04-08 11:31:21 +0000264 gc_unlock();
Damieneefcc792013-10-22 15:25:25 +0100265}
Damiendcced922013-10-21 23:45:08 +0100266
Damieneefcc792013-10-22 15:25:25 +0100267void gc_info(gc_info_t *info) {
268 info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t);
269 info->used = 0;
270 info->free = 0;
271 info->num_1block = 0;
272 info->num_2block = 0;
273 info->max_block = 0;
274 for (machine_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) {
275 machine_uint_t kind = ATB_GET_KIND(block);
276 if (kind == AT_FREE || kind == AT_HEAD) {
277 if (len == 1) {
278 info->num_1block += 1;
279 } else if (len == 2) {
280 info->num_2block += 1;
281 }
282 if (len > info->max_block) {
283 info->max_block = len;
284 }
285 }
286 switch (kind) {
Damiendcced922013-10-21 23:45:08 +0100287 case AT_FREE:
Damieneefcc792013-10-22 15:25:25 +0100288 info->free += 1;
289 len = 0;
Damiendcced922013-10-21 23:45:08 +0100290 break;
291
292 case AT_HEAD:
Damieneefcc792013-10-22 15:25:25 +0100293 info->used += 1;
294 len = 1;
295 break;
296
Damiendcced922013-10-21 23:45:08 +0100297 case AT_TAIL:
Damieneefcc792013-10-22 15:25:25 +0100298 info->used += 1;
299 len += 1;
Damiendcced922013-10-21 23:45:08 +0100300 break;
301
302 case AT_MARK:
Damieneefcc792013-10-22 15:25:25 +0100303 // shouldn't happen
Damiendcced922013-10-21 23:45:08 +0100304 break;
305 }
306 }
307
Damieneefcc792013-10-22 15:25:25 +0100308 info->used *= BYTES_PER_BLOCK;
309 info->free *= BYTES_PER_BLOCK;
Damiendcced922013-10-21 23:45:08 +0100310}
311
Damien George12bab722014-04-05 20:35:48 +0100312void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser) {
Damiendcced922013-10-21 23:45:08 +0100313 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 +0000314 DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
Damiendcced922013-10-21 23:45:08 +0100315
Damien George443e0182014-04-08 11:31:21 +0000316 // check if GC is locked
317 if (gc_lock_depth > 0) {
318 return NULL;
Damien George12bab722014-04-05 20:35:48 +0100319 }
320
Damiendcced922013-10-21 23:45:08 +0100321 // check for 0 allocation
322 if (n_blocks == 0) {
323 return NULL;
324 }
325
326 machine_uint_t i;
327 machine_uint_t end_block;
328 machine_uint_t start_block;
329 machine_uint_t n_free = 0;
330 int collected = 0;
331 for (;;) {
332
333 // look for a run of n_blocks available blocks
334 for (i = 0; i < gc_alloc_table_byte_len; i++) {
335 byte a = gc_alloc_table_start[i];
336 if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; }
337 if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; }
338 if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; }
339 if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; }
340 }
341
342 // nothing found!
343 if (collected) {
344 return NULL;
345 }
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200346 DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes);
Damiendcced922013-10-21 23:45:08 +0100347 gc_collect();
348 collected = 1;
349 }
350
351 // found, ending at block i inclusive
352found:
353 // get starting and end blocks, both inclusive
354 end_block = i;
355 start_block = i - n_free + 1;
356
357 // mark first block as used head
358 ATB_FREE_TO_HEAD(start_block);
359
360 // mark rest of blocks as used tail
361 // TODO for a run of many blocks can make this more efficient
362 for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) {
363 ATB_FREE_TO_TAIL(bl);
364 }
365
Damien George12bab722014-04-05 20:35:48 +0100366 // get pointer to first block
367 void *ret_ptr = (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
muxcc849f72014-04-05 15:49:03 +0200368
Damien George12bab722014-04-05 20:35:48 +0100369#if MICROPY_ENABLE_FINALISER
370 if (has_finaliser) {
371 // clear type pointer in case it is never set
372 ((mp_obj_base_t*)ret_ptr)->type = MP_OBJ_NULL;
373 // set mp_obj flag only if it has a finaliser
374 FTB_SET(start_block);
375 }
376#endif
377
378 return ret_ptr;
Damiendcced922013-10-21 23:45:08 +0100379}
380
Damien George12bab722014-04-05 20:35:48 +0100381/*
mux4f7e9f52014-04-03 23:55:12 +0200382void *gc_alloc(machine_uint_t n_bytes) {
383 return _gc_alloc(n_bytes, false);
384}
385
Damien George12bab722014-04-05 20:35:48 +0100386void *gc_alloc_with_finaliser(machine_uint_t n_bytes) {
mux4f7e9f52014-04-03 23:55:12 +0200387 return _gc_alloc(n_bytes, true);
388}
Damien George12bab722014-04-05 20:35:48 +0100389*/
mux4f7e9f52014-04-03 23:55:12 +0200390
Damienfd8b6bc2013-10-22 20:26:36 +0100391// force the freeing of a piece of memory
392void gc_free(void *ptr_in) {
Damien George443e0182014-04-08 11:31:21 +0000393 if (gc_lock_depth > 0) {
394 // TODO how to deal with this error?
395 return;
Damien George12bab722014-04-05 20:35:48 +0100396 }
397
Damienfd8b6bc2013-10-22 20:26:36 +0100398 machine_uint_t ptr = (machine_uint_t)ptr_in;
399
400 if (VERIFY_PTR(ptr)) {
401 machine_uint_t block = BLOCK_FROM_PTR(ptr);
402 if (ATB_GET_KIND(block) == AT_HEAD) {
403 // free head and all of its tail blocks
404 do {
405 ATB_ANY_TO_FREE(block);
406 block += 1;
407 } while (ATB_GET_KIND(block) == AT_TAIL);
408 }
409 }
410}
411
Damiendcced922013-10-21 23:45:08 +0100412machine_uint_t gc_nbytes(void *ptr_in) {
413 machine_uint_t ptr = (machine_uint_t)ptr_in;
414
Damienfd8b6bc2013-10-22 20:26:36 +0100415 if (VERIFY_PTR(ptr)) {
Damiendcced922013-10-21 23:45:08 +0100416 machine_uint_t block = BLOCK_FROM_PTR(ptr);
417 if (ATB_GET_KIND(block) == AT_HEAD) {
418 // work out number of consecutive blocks in the chain starting with this on
419 machine_uint_t n_blocks = 0;
420 do {
421 n_blocks += 1;
422 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
423 return n_blocks * BYTES_PER_BLOCK;
424 }
425 }
426
427 // invalid pointer
428 return 0;
429}
430
mux87826762014-03-12 21:00:23 +0200431#if 0
Damien George443e0182014-04-08 11:31:21 +0000432// old, simple realloc that didn't expand memory in place
Damien George6fc765c2014-03-07 00:21:51 +0000433void *gc_realloc(void *ptr, machine_uint_t n_bytes) {
434 machine_uint_t n_existing = gc_nbytes(ptr);
435 if (n_bytes <= n_existing) {
436 return ptr;
437 } else {
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300438 // TODO false is incorrect! Should get value from current block!
439 void *ptr2 = gc_alloc(n_bytes,
440#if MICROPY_ENABLE_FINALISER
441 FTB_GET(BLOCK_FROM_PTR((machine_uint_t)ptr))
442#else
443 false
444#endif
445 );
Damien George6fc765c2014-03-07 00:21:51 +0000446 if (ptr2 == NULL) {
447 return ptr2;
448 }
449 memcpy(ptr2, ptr, n_existing);
450 gc_free(ptr);
451 return ptr2;
452 }
453}
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300454
455#else // Alternative gc_realloc impl
Damien George443e0182014-04-08 11:31:21 +0000456
muxfbaa1472014-03-05 23:23:04 +0200457void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
Damien George443e0182014-04-08 11:31:21 +0000458 if (gc_lock_depth > 0) {
459 return NULL;
Damien George12bab722014-04-05 20:35:48 +0100460 }
461
Damien Georgedde739d2014-04-20 18:16:25 +0100462 // check for pure allocation
muxfbaa1472014-03-05 23:23:04 +0200463 if (ptr_in == NULL) {
Damien George12bab722014-04-05 20:35:48 +0100464 return gc_alloc(n_bytes, false);
Damiendcced922013-10-21 23:45:08 +0100465 }
muxfbaa1472014-03-05 23:23:04 +0200466
Damien Georgedde739d2014-04-20 18:16:25 +0100467 machine_uint_t ptr = (machine_uint_t)ptr_in;
468
469 // sanity check the ptr
470 if (!VERIFY_PTR(ptr)) {
471 return NULL;
472 }
473
Paul Sokolovskyc86889d2014-04-20 11:45:16 +0300474 // get first block
475 machine_uint_t block = BLOCK_FROM_PTR(ptr);
476
Damien Georgedde739d2014-04-20 18:16:25 +0100477 // sanity check the ptr is pointing to the head of a block
478 if (ATB_GET_KIND(block) != AT_HEAD) {
479 return NULL;
muxfbaa1472014-03-05 23:23:04 +0200480 }
481
Damien Georgedde739d2014-04-20 18:16:25 +0100482 // compute number of new blocks that are requested
483 machine_uint_t new_blocks = (n_bytes + BYTES_PER_BLOCK) / BYTES_PER_BLOCK;
484
485 // get the number of consecutive tail blocks and
486 // the number of free blocks after last tail block
487 // stop if we reach (or are at) end of heap
488 machine_uint_t n_free = 0;
489 machine_uint_t n_blocks = 1; // counting HEAD block
490 machine_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB;
491 while (block + n_blocks + n_free < max_block) {
492 if (n_blocks + n_free >= new_blocks) {
493 // stop as soon as we find enough blocks for n_bytes
494 break;
495 }
496 byte block_type = ATB_GET_KIND(block + n_blocks + n_free);
497 switch (block_type) {
498 case AT_FREE: n_free++; continue;
499 case AT_TAIL: n_blocks++; continue;
500 case AT_MARK: assert(0);
501 }
502 break;
503 }
504
505 // return original ptr if it already has the requested number of blocks
506 if (new_blocks == n_blocks) {
507 return ptr_in;
508 }
509
510 // check if we can shrink the allocated area
511 if (new_blocks < n_blocks) {
512 // free unneeded tail blocks
513 for (machine_uint_t bl = block + new_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) {
514 ATB_ANY_TO_FREE(bl);
515 }
516 return ptr_in;
517 }
518
519 // check if we can expand in place
520 if (new_blocks <= n_blocks + n_free) {
521 // mark few more blocks as used tail
522 for (machine_uint_t bl = block + n_blocks; bl < block + new_blocks; bl++) {
523 assert(ATB_GET_KIND(bl) == AT_FREE);
524 ATB_FREE_TO_TAIL(bl);
525 }
526 return ptr_in;
527 }
528
529 // can't resize inplace; try to find a new contiguous chain
530 void *ptr_out = gc_alloc(n_bytes,
531#if MICROPY_ENABLE_FINALISER
532 FTB_GET(block)
533#else
534 false
535#endif
536 );
537
538 // check that the alloc succeeded
539 if (ptr_out == NULL) {
540 return NULL;
541 }
542
543 DEBUG_printf("gc_realloc: allocating new block\n");
544 memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK);
545 gc_free(ptr_in);
546 return ptr_out;
Damiendcced922013-10-21 23:45:08 +0100547}
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300548#endif // Alternative gc_realloc impl
mux87826762014-03-12 21:00:23 +0200549
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200550void gc_dump_info() {
551 gc_info_t info;
552 gc_info(&info);
553 printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free);
554 printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n",
555 info.num_1block, info.num_2block, info.max_block);
556}
557
Damien Georgece1162a2014-02-26 22:55:59 +0000558void gc_dump_alloc_table(void) {
559 printf("GC memory layout:");
Damieneefcc792013-10-22 15:25:25 +0100560 for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) {
Damien Georgece1162a2014-02-26 22:55:59 +0000561 if (bl % 64 == 0) {
562 printf("\n%04x: ", (uint)bl);
Damieneefcc792013-10-22 15:25:25 +0100563 }
Damien Georgece1162a2014-02-26 22:55:59 +0000564 int c = ' ';
565 switch (ATB_GET_KIND(bl)) {
566 case AT_FREE: c = '.'; break;
567 case AT_HEAD: c = 'h'; break;
568 case AT_TAIL: c = 't'; break;
569 case AT_MARK: c = 'm'; break;
570 }
571 printf("%c", c);
Damieneefcc792013-10-22 15:25:25 +0100572 }
Damien Georgece1162a2014-02-26 22:55:59 +0000573 printf("\n");
Damieneefcc792013-10-22 15:25:25 +0100574}
575
Damien Georgece1162a2014-02-26 22:55:59 +0000576#if DEBUG_PRINT
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200577void gc_test(void) {
578 machine_uint_t len = 500;
Damiendcced922013-10-21 23:45:08 +0100579 machine_uint_t *heap = malloc(len);
580 gc_init(heap, heap + len / sizeof(machine_uint_t));
581 void *ptrs[100];
582 {
Damien George12bab722014-04-05 20:35:48 +0100583 machine_uint_t **p = gc_alloc(16, false);
584 p[0] = gc_alloc(64, false);
585 p[1] = gc_alloc(1, false);
586 p[2] = gc_alloc(1, false);
587 p[3] = gc_alloc(1, false);
588 machine_uint_t ***p2 = gc_alloc(16, false);
Damiendcced922013-10-21 23:45:08 +0100589 p2[0] = p;
590 p2[1] = p;
591 ptrs[0] = p2;
592 }
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200593 for (int i = 0; i < 25; i+=2) {
Damien George12bab722014-04-05 20:35:48 +0100594 machine_uint_t *p = gc_alloc(i, false);
Damiendcced922013-10-21 23:45:08 +0100595 printf("p=%p\n", p);
596 if (i & 3) {
597 //ptrs[i] = p;
598 }
599 }
600
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200601 printf("Before GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000602 gc_dump_alloc_table();
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200603 printf("Starting GC...\n");
604 gc_collect_start();
605 gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*));
606 gc_collect_end();
607 printf("After GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000608 gc_dump_alloc_table();
Damiendcced922013-10-21 23:45:08 +0100609}
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200610#endif
Damien Georged3ebe482014-01-07 15:20:33 +0000611
612#endif // MICROPY_ENABLE_GC