blob: 73a6ca8612913c32bddf2ee695d5af7bf533aaa7 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovskyc86889d2014-04-20 11:45:16 +030027#include <assert.h>
Damiendcced922013-10-21 23:45:08 +010028#include <stdio.h>
Damiendcced922013-10-21 23:45:08 +010029#include <string.h>
30
Damien Georgeb4b10fd2015-01-01 23:30:53 +000031#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/gc.h"
33#include "py/obj.h"
34#include "py/runtime.h"
mux4f7e9f52014-04-03 23:55:12 +020035
Damien Georged3ebe482014-01-07 15:20:33 +000036#if MICROPY_ENABLE_GC
37
stijn9acb5e42014-06-18 12:29:03 +020038#if 0 // print debugging info
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020039#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020040#define DEBUG_printf DEBUG_printf
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020041#else // don't print debugging info
Damien George7860c2a2014-11-05 21:16:41 +000042#define DEBUG_PRINT (0)
Damien George41eb6082014-02-26 22:40:35 +000043#define DEBUG_printf(...) (void)0
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +020044#endif
45
Damien George516b09e2014-08-28 23:06:38 +010046// make this 1 to dump the heap each time it changes
47#define EXTENSIVE_HEAP_PROFILING (0)
48
Damiendcced922013-10-21 23:45:08 +010049#define WORDS_PER_BLOCK (4)
50#define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD)
Damiendcced922013-10-21 23:45:08 +010051
Damiendcced922013-10-21 23:45:08 +010052// ATB = allocation table byte
53// 0b00 = FREE -- free block
54// 0b01 = HEAD -- head of a chain of blocks
55// 0b10 = TAIL -- in the tail of a chain of blocks
56// 0b11 = MARK -- marked head block
57
58#define AT_FREE (0)
59#define AT_HEAD (1)
60#define AT_TAIL (2)
61#define AT_MARK (3)
62
63#define BLOCKS_PER_ATB (4)
64#define ATB_MASK_0 (0x03)
65#define ATB_MASK_1 (0x0c)
66#define ATB_MASK_2 (0x30)
67#define ATB_MASK_3 (0xc0)
68
69#define ATB_0_IS_FREE(a) (((a) & ATB_MASK_0) == 0)
70#define ATB_1_IS_FREE(a) (((a) & ATB_MASK_1) == 0)
71#define ATB_2_IS_FREE(a) (((a) & ATB_MASK_2) == 0)
72#define ATB_3_IS_FREE(a) (((a) & ATB_MASK_3) == 0)
73
74#define BLOCK_SHIFT(block) (2 * ((block) & (BLOCKS_PER_ATB - 1)))
Damien Georgeb4b10fd2015-01-01 23:30:53 +000075#define ATB_GET_KIND(block) ((MP_STATE_MEM(gc_alloc_table_start)[(block) / BLOCKS_PER_ATB] >> BLOCK_SHIFT(block)) & 3)
76#define ATB_ANY_TO_FREE(block) do { MP_STATE_MEM(gc_alloc_table_start)[(block) / BLOCKS_PER_ATB] &= (~(AT_MARK << BLOCK_SHIFT(block))); } while (0)
77#define ATB_FREE_TO_HEAD(block) do { MP_STATE_MEM(gc_alloc_table_start)[(block) / BLOCKS_PER_ATB] |= (AT_HEAD << BLOCK_SHIFT(block)); } while (0)
78#define ATB_FREE_TO_TAIL(block) do { MP_STATE_MEM(gc_alloc_table_start)[(block) / BLOCKS_PER_ATB] |= (AT_TAIL << BLOCK_SHIFT(block)); } while (0)
79#define ATB_HEAD_TO_MARK(block) do { MP_STATE_MEM(gc_alloc_table_start)[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0)
80#define ATB_MARK_TO_HEAD(block) do { MP_STATE_MEM(gc_alloc_table_start)[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0)
Damiendcced922013-10-21 23:45:08 +010081
Damien Georgeb4b10fd2015-01-01 23:30:53 +000082#define BLOCK_FROM_PTR(ptr) (((ptr) - (mp_uint_t)MP_STATE_MEM(gc_pool_start)) / BYTES_PER_BLOCK)
83#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (mp_uint_t)MP_STATE_MEM(gc_pool_start)))
Damiendcced922013-10-21 23:45:08 +010084#define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB)
85
Damien George12bab722014-04-05 20:35:48 +010086#if MICROPY_ENABLE_FINALISER
87// FTB = finaliser table byte
88// if set, then the corresponding block may have a finaliser
89
90#define BLOCKS_PER_FTB (8)
91
Damien Georgeb4b10fd2015-01-01 23:30:53 +000092#define FTB_GET(block) ((MP_STATE_MEM(gc_finaliser_table_start)[(block) / BLOCKS_PER_FTB] >> ((block) & 7)) & 1)
93#define FTB_SET(block) do { MP_STATE_MEM(gc_finaliser_table_start)[(block) / BLOCKS_PER_FTB] |= (1 << ((block) & 7)); } while (0)
94#define FTB_CLEAR(block) do { MP_STATE_MEM(gc_finaliser_table_start)[(block) / BLOCKS_PER_FTB] &= (~(1 << ((block) & 7))); } while (0)
Damien George12bab722014-04-05 20:35:48 +010095#endif
96
Damienbb5316b2013-10-22 21:12:29 +010097// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
98void gc_init(void *start, void *end) {
99 // align end pointer on block boundary
Damien George40f3c022014-07-03 13:25:24 +0100100 end = (void*)((mp_uint_t)end & (~(BYTES_PER_BLOCK - 1)));
stijndef10ce2014-06-18 10:20:41 +0200101 DEBUG_printf("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n", start, end, (byte*)end - (byte*)start);
Damienbb5316b2013-10-22 21:12:29 +0100102
Damien George12bab722014-04-05 20:35:48 +0100103 // calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes):
104 // T = A + F + P
105 // F = A * BLOCKS_PER_ATB / BLOCKS_PER_FTB
106 // P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK
107 // => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK)
Damien George40f3c022014-07-03 13:25:24 +0100108 mp_uint_t total_byte_len = (byte*)end - (byte*)start;
Damien George12bab722014-04-05 20:35:48 +0100109#if MICROPY_ENABLE_FINALISER
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000110 MP_STATE_MEM(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);
Damien George12bab722014-04-05 20:35:48 +0100111#else
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000112 MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK);
Damien George12bab722014-04-05 20:35:48 +0100113#endif
114
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000115 MP_STATE_MEM(gc_alloc_table_start) = (byte*)start;
mux4f7e9f52014-04-03 23:55:12 +0200116
Damien George12bab722014-04-05 20:35:48 +0100117#if MICROPY_ENABLE_FINALISER
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000118 mp_uint_t gc_finaliser_table_byte_len = (MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB + BLOCKS_PER_FTB - 1) / BLOCKS_PER_FTB;
119 MP_STATE_MEM(gc_finaliser_table_start) = MP_STATE_MEM(gc_alloc_table_start) + MP_STATE_MEM(gc_alloc_table_byte_len);
Damien George12bab722014-04-05 20:35:48 +0100120#endif
mux4f7e9f52014-04-03 23:55:12 +0200121
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000122 mp_uint_t gc_pool_block_len = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB;
123 MP_STATE_MEM(gc_pool_start) = (mp_uint_t*)((byte*)end - gc_pool_block_len * BYTES_PER_BLOCK);
124 MP_STATE_MEM(gc_pool_end) = (mp_uint_t*)end;
Damienbb5316b2013-10-22 21:12:29 +0100125
Damien Georgea1d3ee32014-08-08 12:33:49 +0100126#if MICROPY_ENABLE_FINALISER
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000127 assert((byte*)MP_STATE_MEM(gc_pool_start) >= MP_STATE_MEM(gc_finaliser_table_start) + gc_finaliser_table_byte_len);
Damien Georgea1d3ee32014-08-08 12:33:49 +0100128#endif
129
Damienbb5316b2013-10-22 21:12:29 +0100130 // clear ATBs
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000131 memset(MP_STATE_MEM(gc_alloc_table_start), 0, MP_STATE_MEM(gc_alloc_table_byte_len));
Damienbb5316b2013-10-22 21:12:29 +0100132
Damien George12bab722014-04-05 20:35:48 +0100133#if MICROPY_ENABLE_FINALISER
134 // clear FTBs
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000135 memset(MP_STATE_MEM(gc_finaliser_table_start), 0, gc_finaliser_table_byte_len);
Damien George12bab722014-04-05 20:35:48 +0100136#endif
mux4f7e9f52014-04-03 23:55:12 +0200137
Damien Georged5e7f6e2014-08-22 18:17:02 +0100138 // set last free ATB index to start of heap
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000139 MP_STATE_MEM(gc_last_free_atb_index) = 0;
Damien Georged5e7f6e2014-08-22 18:17:02 +0100140
Damien George12bab722014-04-05 20:35:48 +0100141 // unlock the GC
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000142 MP_STATE_MEM(gc_lock_depth) = 0;
Damien George12bab722014-04-05 20:35:48 +0100143
Damien George109c1de2014-10-31 21:30:46 +0000144 // allow auto collection
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000145 MP_STATE_MEM(gc_auto_collect_enabled) = 1;
Damien George109c1de2014-10-31 21:30:46 +0000146
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200147 DEBUG_printf("GC layout:\n");
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000148 DEBUG_printf(" alloc table at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", MP_STATE_MEM(gc_alloc_table_start), MP_STATE_MEM(gc_alloc_table_byte_len), MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB);
Damien George12bab722014-04-05 20:35:48 +0100149#if MICROPY_ENABLE_FINALISER
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000150 DEBUG_printf(" finaliser table at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", MP_STATE_MEM(gc_finaliser_table_start), gc_finaliser_table_byte_len, gc_finaliser_table_byte_len * BLOCKS_PER_FTB);
Damien George12bab722014-04-05 20:35:48 +0100151#endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000152 DEBUG_printf(" pool at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", MP_STATE_MEM(gc_pool_start), gc_pool_block_len * BYTES_PER_BLOCK, gc_pool_block_len);
Damienbb5316b2013-10-22 21:12:29 +0100153}
154
Damien George443e0182014-04-08 11:31:21 +0000155void gc_lock(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000156 MP_STATE_MEM(gc_lock_depth)++;
Damien George443e0182014-04-08 11:31:21 +0000157}
158
159void gc_unlock(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000160 MP_STATE_MEM(gc_lock_depth)--;
Damien George443e0182014-04-08 11:31:21 +0000161}
162
Dave Hylands2fe841d2014-06-30 22:49:21 -0700163bool gc_is_locked(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000164 return MP_STATE_MEM(gc_lock_depth) != 0;
Dave Hylands2fe841d2014-06-30 22:49:21 -0700165}
166
Damienfd8b6bc2013-10-22 20:26:36 +0100167#define VERIFY_PTR(ptr) ( \
168 (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000169 && ptr >= (mp_uint_t)MP_STATE_MEM(gc_pool_start) /* must be above start of pool */ \
170 && ptr < (mp_uint_t)MP_STATE_MEM(gc_pool_end) /* must be below end of pool */ \
Damienfd8b6bc2013-10-22 20:26:36 +0100171 )
172
Damiendcced922013-10-21 23:45:08 +0100173#define VERIFY_MARK_AND_PUSH(ptr) \
174 do { \
Damienfd8b6bc2013-10-22 20:26:36 +0100175 if (VERIFY_PTR(ptr)) { \
Damien George40f3c022014-07-03 13:25:24 +0100176 mp_uint_t _block = BLOCK_FROM_PTR(ptr); \
Damiendcced922013-10-21 23:45:08 +0100177 if (ATB_GET_KIND(_block) == AT_HEAD) { \
178 /* an unmarked head, mark it, and push it on gc stack */ \
179 ATB_HEAD_TO_MARK(_block); \
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000180 if (MP_STATE_MEM(gc_sp) < &MP_STATE_MEM(gc_stack)[MICROPY_ALLOC_GC_STACK_SIZE]) { \
181 *MP_STATE_MEM(gc_sp)++ = _block; \
Damiendcced922013-10-21 23:45:08 +0100182 } else { \
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000183 MP_STATE_MEM(gc_stack_overflow) = 1; \
Damiendcced922013-10-21 23:45:08 +0100184 } \
185 } \
186 } \
187 } while (0)
188
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200189STATIC void gc_drain_stack(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000190 while (MP_STATE_MEM(gc_sp) > MP_STATE_MEM(gc_stack)) {
Damiendcced922013-10-21 23:45:08 +0100191 // pop the next block off the stack
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000192 mp_uint_t block = *--MP_STATE_MEM(gc_sp);
Damiendcced922013-10-21 23:45:08 +0100193
Damieneefcc792013-10-22 15:25:25 +0100194 // work out number of consecutive blocks in the chain starting with this one
Damien George40f3c022014-07-03 13:25:24 +0100195 mp_uint_t n_blocks = 0;
Damiendcced922013-10-21 23:45:08 +0100196 do {
197 n_blocks += 1;
198 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
199
200 // check this block's children
Damien George40f3c022014-07-03 13:25:24 +0100201 mp_uint_t *scan = (mp_uint_t*)PTR_FROM_BLOCK(block);
202 for (mp_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) {
203 mp_uint_t ptr2 = *scan;
Damiendcced922013-10-21 23:45:08 +0100204 VERIFY_MARK_AND_PUSH(ptr2);
205 }
206 }
207}
208
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200209STATIC void gc_deal_with_stack_overflow(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000210 while (MP_STATE_MEM(gc_stack_overflow)) {
211 MP_STATE_MEM(gc_stack_overflow) = 0;
212 MP_STATE_MEM(gc_sp) = MP_STATE_MEM(gc_stack);
Damiendcced922013-10-21 23:45:08 +0100213
214 // scan entire memory looking for blocks which have been marked but not their children
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000215 for (mp_uint_t block = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) {
Damiendcced922013-10-21 23:45:08 +0100216 // trace (again) if mark bit set
217 if (ATB_GET_KIND(block) == AT_MARK) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000218 *MP_STATE_MEM(gc_sp)++ = block;
Damiendcced922013-10-21 23:45:08 +0100219 gc_drain_stack();
220 }
221 }
222 }
223}
224
Paul Sokolovsky755a55f2014-06-05 22:48:02 +0300225#if MICROPY_PY_GC_COLLECT_RETVAL
226uint gc_collected;
227#endif
228
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200229STATIC void gc_sweep(void) {
Paul Sokolovsky755a55f2014-06-05 22:48:02 +0300230 #if MICROPY_PY_GC_COLLECT_RETVAL
231 gc_collected = 0;
232 #endif
Damiendcced922013-10-21 23:45:08 +0100233 // free unmarked heads and their tails
234 int free_tail = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000235 for (mp_uint_t block = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) {
Damiendcced922013-10-21 23:45:08 +0100236 switch (ATB_GET_KIND(block)) {
237 case AT_HEAD:
Damien George12bab722014-04-05 20:35:48 +0100238#if MICROPY_ENABLE_FINALISER
239 if (FTB_GET(block)) {
240 mp_obj_t obj = (mp_obj_t)PTR_FROM_BLOCK(block);
241 if (((mp_obj_base_t*)obj)->type != MP_OBJ_NULL) {
242 // if the object has a type then see if it has a __del__ method
243 mp_obj_t dest[2];
244 mp_load_method_maybe(obj, MP_QSTR___del__, dest);
245 if (dest[0] != MP_OBJ_NULL) {
246 // load_method returned a method
247 mp_call_method_n_kw(0, 0, dest);
248 }
mux4f7e9f52014-04-03 23:55:12 +0200249 }
Damien George12bab722014-04-05 20:35:48 +0100250 // clear finaliser flag
251 FTB_CLEAR(block);
mux4f7e9f52014-04-03 23:55:12 +0200252 }
Damien George12bab722014-04-05 20:35:48 +0100253#endif
Damiendcced922013-10-21 23:45:08 +0100254 free_tail = 1;
Paul Sokolovsky755a55f2014-06-05 22:48:02 +0300255 #if MICROPY_PY_GC_COLLECT_RETVAL
256 gc_collected++;
257 #endif
Damiendcced922013-10-21 23:45:08 +0100258 // fall through to free the head
259
260 case AT_TAIL:
261 if (free_tail) {
stijnbbcea3f2014-06-16 10:44:29 +0200262 DEBUG_printf("gc_sweep(%p)\n",PTR_FROM_BLOCK(block));
Damiendcced922013-10-21 23:45:08 +0100263 ATB_ANY_TO_FREE(block);
264 }
265 break;
266
267 case AT_MARK:
268 ATB_MARK_TO_HEAD(block);
269 free_tail = 0;
270 break;
271 }
272 }
273}
274
Damien8b3a7c22013-10-23 20:20:17 +0100275void gc_collect_start(void) {
Damien George443e0182014-04-08 11:31:21 +0000276 gc_lock();
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000277 MP_STATE_MEM(gc_stack_overflow) = 0;
278 MP_STATE_MEM(gc_sp) = MP_STATE_MEM(gc_stack);
279 // Trace root pointers. This relies on the root pointers being organised
280 // correctly in the mp_state_ctx structure. We scan nlr_top, dict_locals,
281 // dict_globals, then the root pointer section of mp_state_vm.
282 void **ptrs = (void**)(void*)&mp_state_ctx;
283 gc_collect_root(ptrs, offsetof(mp_state_ctx_t, vm.stack_top) / sizeof(mp_uint_t));
Damiendcced922013-10-21 23:45:08 +0100284}
285
Damien George40f3c022014-07-03 13:25:24 +0100286void gc_collect_root(void **ptrs, mp_uint_t len) {
287 for (mp_uint_t i = 0; i < len; i++) {
288 mp_uint_t ptr = (mp_uint_t)ptrs[i];
Damiendcced922013-10-21 23:45:08 +0100289 VERIFY_MARK_AND_PUSH(ptr);
290 gc_drain_stack();
291 }
292}
293
Damien8b3a7c22013-10-23 20:20:17 +0100294void gc_collect_end(void) {
Damiendcced922013-10-21 23:45:08 +0100295 gc_deal_with_stack_overflow();
296 gc_sweep();
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000297 MP_STATE_MEM(gc_last_free_atb_index) = 0;
Damien George443e0182014-04-08 11:31:21 +0000298 gc_unlock();
Damieneefcc792013-10-22 15:25:25 +0100299}
Damiendcced922013-10-21 23:45:08 +0100300
Damieneefcc792013-10-22 15:25:25 +0100301void gc_info(gc_info_t *info) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000302 info->total = (MP_STATE_MEM(gc_pool_end) - MP_STATE_MEM(gc_pool_start)) * sizeof(mp_uint_t);
Damieneefcc792013-10-22 15:25:25 +0100303 info->used = 0;
304 info->free = 0;
305 info->num_1block = 0;
306 info->num_2block = 0;
307 info->max_block = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000308 for (mp_uint_t block = 0, len = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) {
Damien George40f3c022014-07-03 13:25:24 +0100309 mp_uint_t kind = ATB_GET_KIND(block);
Damieneefcc792013-10-22 15:25:25 +0100310 if (kind == AT_FREE || kind == AT_HEAD) {
311 if (len == 1) {
312 info->num_1block += 1;
313 } else if (len == 2) {
314 info->num_2block += 1;
315 }
316 if (len > info->max_block) {
317 info->max_block = len;
318 }
319 }
320 switch (kind) {
Damiendcced922013-10-21 23:45:08 +0100321 case AT_FREE:
Damieneefcc792013-10-22 15:25:25 +0100322 info->free += 1;
323 len = 0;
Damiendcced922013-10-21 23:45:08 +0100324 break;
325
326 case AT_HEAD:
Damieneefcc792013-10-22 15:25:25 +0100327 info->used += 1;
328 len = 1;
329 break;
330
Damiendcced922013-10-21 23:45:08 +0100331 case AT_TAIL:
Damieneefcc792013-10-22 15:25:25 +0100332 info->used += 1;
333 len += 1;
Damiendcced922013-10-21 23:45:08 +0100334 break;
335
336 case AT_MARK:
Damieneefcc792013-10-22 15:25:25 +0100337 // shouldn't happen
Damiendcced922013-10-21 23:45:08 +0100338 break;
339 }
340 }
341
Damieneefcc792013-10-22 15:25:25 +0100342 info->used *= BYTES_PER_BLOCK;
343 info->free *= BYTES_PER_BLOCK;
Damiendcced922013-10-21 23:45:08 +0100344}
345
Damien George40f3c022014-07-03 13:25:24 +0100346void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser) {
347 mp_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
stijndef10ce2014-06-18 10:20:41 +0200348 DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
Damiendcced922013-10-21 23:45:08 +0100349
Damien George443e0182014-04-08 11:31:21 +0000350 // check if GC is locked
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000351 if (MP_STATE_MEM(gc_lock_depth) > 0) {
Damien George443e0182014-04-08 11:31:21 +0000352 return NULL;
Damien George12bab722014-04-05 20:35:48 +0100353 }
354
Damiendcced922013-10-21 23:45:08 +0100355 // check for 0 allocation
356 if (n_blocks == 0) {
357 return NULL;
358 }
359
Damien George40f3c022014-07-03 13:25:24 +0100360 mp_uint_t i;
361 mp_uint_t end_block;
362 mp_uint_t start_block;
363 mp_uint_t n_free = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000364 int collected = !MP_STATE_MEM(gc_auto_collect_enabled);
Damiendcced922013-10-21 23:45:08 +0100365 for (;;) {
366
367 // look for a run of n_blocks available blocks
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000368 for (i = MP_STATE_MEM(gc_last_free_atb_index); i < MP_STATE_MEM(gc_alloc_table_byte_len); i++) {
369 byte a = MP_STATE_MEM(gc_alloc_table_start)[i];
Damien Georged5e7f6e2014-08-22 18:17:02 +0100370 if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; }
371 if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; }
372 if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; }
373 if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; }
374 }
Damiendcced922013-10-21 23:45:08 +0100375
376 // nothing found!
377 if (collected) {
378 return NULL;
379 }
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200380 DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes);
Damiendcced922013-10-21 23:45:08 +0100381 gc_collect();
382 collected = 1;
383 }
384
385 // found, ending at block i inclusive
386found:
387 // get starting and end blocks, both inclusive
388 end_block = i;
389 start_block = i - n_free + 1;
390
Damien Georgeb796e3d2014-08-28 10:18:40 +0100391 // Set last free ATB index to block after last block we found, for start of
392 // next scan. To reduce fragmentation, we only do this if we were looking
393 // for a single free block, which guarantees that there are no free blocks
Damien George516b09e2014-08-28 23:06:38 +0100394 // before this one. Also, whenever we free or shink a block we must check
395 // if this index needs adjusting (see gc_realloc and gc_free).
Damien Georgeb796e3d2014-08-28 10:18:40 +0100396 if (n_free == 1) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000397 MP_STATE_MEM(gc_last_free_atb_index) = (i + 1) / BLOCKS_PER_ATB;
Damien Georgeb796e3d2014-08-28 10:18:40 +0100398 }
Damien Georged5e7f6e2014-08-22 18:17:02 +0100399
Damiendcced922013-10-21 23:45:08 +0100400 // mark first block as used head
401 ATB_FREE_TO_HEAD(start_block);
402
403 // mark rest of blocks as used tail
404 // TODO for a run of many blocks can make this more efficient
Damien George40f3c022014-07-03 13:25:24 +0100405 for (mp_uint_t bl = start_block + 1; bl <= end_block; bl++) {
Damiendcced922013-10-21 23:45:08 +0100406 ATB_FREE_TO_TAIL(bl);
407 }
408
Damien George12bab722014-04-05 20:35:48 +0100409 // get pointer to first block
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000410 void *ret_ptr = (void*)(MP_STATE_MEM(gc_pool_start) + start_block * WORDS_PER_BLOCK);
stijndef10ce2014-06-18 10:20:41 +0200411 DEBUG_printf("gc_alloc(%p)\n", ret_ptr);
muxcc849f72014-04-05 15:49:03 +0200412
Damien George32bef312014-04-26 22:23:42 +0100413 // zero out the additional bytes of the newly allocated blocks
Damien Georgedaab6512014-04-25 23:37:55 +0100414 // This is needed because the blocks may have previously held pointers
415 // to the heap and will not be set to something else if the caller
416 // doesn't actually use the entire block. As such they will continue
417 // to point to the heap and may prevent other blocks from being reclaimed.
Damien Georgec0376942014-06-13 22:33:31 +0100418 memset((byte*)ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes);
Damien Georgedaab6512014-04-25 23:37:55 +0100419
Damien George12bab722014-04-05 20:35:48 +0100420#if MICROPY_ENABLE_FINALISER
421 if (has_finaliser) {
Damien George32bef312014-04-26 22:23:42 +0100422 // clear type pointer in case it is never set
423 ((mp_obj_base_t*)ret_ptr)->type = MP_OBJ_NULL;
Damien George12bab722014-04-05 20:35:48 +0100424 // set mp_obj flag only if it has a finaliser
425 FTB_SET(start_block);
426 }
427#endif
428
Damien George516b09e2014-08-28 23:06:38 +0100429 #if EXTENSIVE_HEAP_PROFILING
430 gc_dump_alloc_table();
431 #endif
432
Damien George12bab722014-04-05 20:35:48 +0100433 return ret_ptr;
Damiendcced922013-10-21 23:45:08 +0100434}
435
Damien George12bab722014-04-05 20:35:48 +0100436/*
Damien George40f3c022014-07-03 13:25:24 +0100437void *gc_alloc(mp_uint_t n_bytes) {
mux4f7e9f52014-04-03 23:55:12 +0200438 return _gc_alloc(n_bytes, false);
439}
440
Damien George40f3c022014-07-03 13:25:24 +0100441void *gc_alloc_with_finaliser(mp_uint_t n_bytes) {
mux4f7e9f52014-04-03 23:55:12 +0200442 return _gc_alloc(n_bytes, true);
443}
Damien George12bab722014-04-05 20:35:48 +0100444*/
mux4f7e9f52014-04-03 23:55:12 +0200445
Damienfd8b6bc2013-10-22 20:26:36 +0100446// force the freeing of a piece of memory
447void gc_free(void *ptr_in) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000448 if (MP_STATE_MEM(gc_lock_depth) > 0) {
Damien George443e0182014-04-08 11:31:21 +0000449 // TODO how to deal with this error?
450 return;
Damien George12bab722014-04-05 20:35:48 +0100451 }
452
Damien George40f3c022014-07-03 13:25:24 +0100453 mp_uint_t ptr = (mp_uint_t)ptr_in;
stijnbbcea3f2014-06-16 10:44:29 +0200454 DEBUG_printf("gc_free(%p)\n", ptr);
Damienfd8b6bc2013-10-22 20:26:36 +0100455
456 if (VERIFY_PTR(ptr)) {
Damien George40f3c022014-07-03 13:25:24 +0100457 mp_uint_t block = BLOCK_FROM_PTR(ptr);
Damienfd8b6bc2013-10-22 20:26:36 +0100458 if (ATB_GET_KIND(block) == AT_HEAD) {
Damien George516b09e2014-08-28 23:06:38 +0100459 // set the last_free pointer to this block if it's earlier in the heap
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000460 if (block / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) {
461 MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB;
Damien George516b09e2014-08-28 23:06:38 +0100462 }
463
Damienfd8b6bc2013-10-22 20:26:36 +0100464 // free head and all of its tail blocks
465 do {
466 ATB_ANY_TO_FREE(block);
467 block += 1;
468 } while (ATB_GET_KIND(block) == AT_TAIL);
Damien George516b09e2014-08-28 23:06:38 +0100469
470 #if EXTENSIVE_HEAP_PROFILING
471 gc_dump_alloc_table();
472 #endif
Damien Georgee7bb0442014-10-23 14:13:05 +0100473 } else {
474 assert(!"bad free");
Damienfd8b6bc2013-10-22 20:26:36 +0100475 }
Damien Georgee7bb0442014-10-23 14:13:05 +0100476 } else if (ptr_in != NULL) {
477 assert(!"bad free");
Damienfd8b6bc2013-10-22 20:26:36 +0100478 }
479}
480
Damien George0b13f3e2014-10-24 23:12:25 +0100481mp_uint_t gc_nbytes(const void *ptr_in) {
Damien George40f3c022014-07-03 13:25:24 +0100482 mp_uint_t ptr = (mp_uint_t)ptr_in;
Damiendcced922013-10-21 23:45:08 +0100483
Damienfd8b6bc2013-10-22 20:26:36 +0100484 if (VERIFY_PTR(ptr)) {
Damien George40f3c022014-07-03 13:25:24 +0100485 mp_uint_t block = BLOCK_FROM_PTR(ptr);
Damiendcced922013-10-21 23:45:08 +0100486 if (ATB_GET_KIND(block) == AT_HEAD) {
487 // work out number of consecutive blocks in the chain starting with this on
Damien George40f3c022014-07-03 13:25:24 +0100488 mp_uint_t n_blocks = 0;
Damiendcced922013-10-21 23:45:08 +0100489 do {
490 n_blocks += 1;
491 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
492 return n_blocks * BYTES_PER_BLOCK;
493 }
494 }
495
496 // invalid pointer
497 return 0;
498}
499
mux87826762014-03-12 21:00:23 +0200500#if 0
Damien George443e0182014-04-08 11:31:21 +0000501// old, simple realloc that didn't expand memory in place
Damien George40f3c022014-07-03 13:25:24 +0100502void *gc_realloc(void *ptr, mp_uint_t n_bytes) {
503 mp_uint_t n_existing = gc_nbytes(ptr);
Damien George6fc765c2014-03-07 00:21:51 +0000504 if (n_bytes <= n_existing) {
505 return ptr;
506 } else {
Damien George410f3072014-04-25 11:44:53 +0000507 bool has_finaliser;
508 if (ptr == NULL) {
509 has_finaliser = false;
510 } else {
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300511#if MICROPY_ENABLE_FINALISER
Damien George40f3c022014-07-03 13:25:24 +0100512 has_finaliser = FTB_GET(BLOCK_FROM_PTR((mp_uint_t)ptr));
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300513#else
Damien George410f3072014-04-25 11:44:53 +0000514 has_finaliser = false;
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300515#endif
Damien George410f3072014-04-25 11:44:53 +0000516 }
517 void *ptr2 = gc_alloc(n_bytes, has_finaliser);
Damien George6fc765c2014-03-07 00:21:51 +0000518 if (ptr2 == NULL) {
519 return ptr2;
520 }
521 memcpy(ptr2, ptr, n_existing);
522 gc_free(ptr);
523 return ptr2;
524 }
525}
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300526
527#else // Alternative gc_realloc impl
Damien George443e0182014-04-08 11:31:21 +0000528
Damien George40f3c022014-07-03 13:25:24 +0100529void *gc_realloc(void *ptr_in, mp_uint_t n_bytes) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000530 if (MP_STATE_MEM(gc_lock_depth) > 0) {
Damien George443e0182014-04-08 11:31:21 +0000531 return NULL;
Damien George12bab722014-04-05 20:35:48 +0100532 }
533
Damien Georgedde739d2014-04-20 18:16:25 +0100534 // check for pure allocation
muxfbaa1472014-03-05 23:23:04 +0200535 if (ptr_in == NULL) {
Damien George12bab722014-04-05 20:35:48 +0100536 return gc_alloc(n_bytes, false);
Damiendcced922013-10-21 23:45:08 +0100537 }
muxfbaa1472014-03-05 23:23:04 +0200538
Damien George37378f82014-10-23 12:02:00 +0100539 // check for pure free
540 if (n_bytes == 0) {
541 gc_free(ptr_in);
542 return NULL;
543 }
544
Damien George40f3c022014-07-03 13:25:24 +0100545 mp_uint_t ptr = (mp_uint_t)ptr_in;
Damien Georgedde739d2014-04-20 18:16:25 +0100546
547 // sanity check the ptr
548 if (!VERIFY_PTR(ptr)) {
549 return NULL;
550 }
551
Paul Sokolovskyc86889d2014-04-20 11:45:16 +0300552 // get first block
Damien George40f3c022014-07-03 13:25:24 +0100553 mp_uint_t block = BLOCK_FROM_PTR(ptr);
Paul Sokolovskyc86889d2014-04-20 11:45:16 +0300554
Damien Georgedde739d2014-04-20 18:16:25 +0100555 // sanity check the ptr is pointing to the head of a block
556 if (ATB_GET_KIND(block) != AT_HEAD) {
557 return NULL;
muxfbaa1472014-03-05 23:23:04 +0200558 }
559
Damien Georgedde739d2014-04-20 18:16:25 +0100560 // compute number of new blocks that are requested
Damien George40f3c022014-07-03 13:25:24 +0100561 mp_uint_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK;
Damien Georgedde739d2014-04-20 18:16:25 +0100562
Damien George9b0b3732014-10-15 18:24:47 +0000563 // Get the total number of consecutive blocks that are already allocated to
564 // this chunk of memory, and then count the number of free blocks following
565 // it. Stop if we reach the end of the heap, or if we find enough extra
566 // free blocks to satisfy the realloc. Note that we need to compute the
567 // total size of the existing memory chunk so we can correctly and
568 // efficiently shrink it (see below for shrinking code).
Damien George40f3c022014-07-03 13:25:24 +0100569 mp_uint_t n_free = 0;
570 mp_uint_t n_blocks = 1; // counting HEAD block
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000571 mp_uint_t max_block = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB;
Damien George9b0b3732014-10-15 18:24:47 +0000572 for (mp_uint_t bl = block + n_blocks; bl < max_block; bl++) {
573 byte block_type = ATB_GET_KIND(bl);
574 if (block_type == AT_TAIL) {
575 n_blocks++;
576 continue;
Damien Georgedde739d2014-04-20 18:16:25 +0100577 }
Damien George9b0b3732014-10-15 18:24:47 +0000578 if (block_type == AT_FREE) {
579 n_free++;
580 if (n_blocks + n_free >= new_blocks) {
581 // stop as soon as we find enough blocks for n_bytes
582 break;
583 }
584 continue;
Damien Georgedde739d2014-04-20 18:16:25 +0100585 }
586 break;
587 }
588
589 // return original ptr if it already has the requested number of blocks
590 if (new_blocks == n_blocks) {
591 return ptr_in;
592 }
593
594 // check if we can shrink the allocated area
595 if (new_blocks < n_blocks) {
596 // free unneeded tail blocks
Damien George9b0b3732014-10-15 18:24:47 +0000597 for (mp_uint_t bl = block + new_blocks, count = n_blocks - new_blocks; count > 0; bl++, count--) {
Damien Georgedde739d2014-04-20 18:16:25 +0100598 ATB_ANY_TO_FREE(bl);
599 }
Damien George516b09e2014-08-28 23:06:38 +0100600
601 // set the last_free pointer to end of this block if it's earlier in the heap
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000602 if ((block + new_blocks) / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) {
603 MP_STATE_MEM(gc_last_free_atb_index) = (block + new_blocks) / BLOCKS_PER_ATB;
Damien George516b09e2014-08-28 23:06:38 +0100604 }
605
606 #if EXTENSIVE_HEAP_PROFILING
607 gc_dump_alloc_table();
608 #endif
609
Damien Georgedde739d2014-04-20 18:16:25 +0100610 return ptr_in;
611 }
612
613 // check if we can expand in place
614 if (new_blocks <= n_blocks + n_free) {
615 // mark few more blocks as used tail
Damien George40f3c022014-07-03 13:25:24 +0100616 for (mp_uint_t bl = block + n_blocks; bl < block + new_blocks; bl++) {
Damien Georgedde739d2014-04-20 18:16:25 +0100617 assert(ATB_GET_KIND(bl) == AT_FREE);
618 ATB_FREE_TO_TAIL(bl);
619 }
Damien Georgedaab6512014-04-25 23:37:55 +0100620
Damien George32bef312014-04-26 22:23:42 +0100621 // zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc)
stijnf33385f2014-06-12 17:42:20 +0200622 memset((byte*)ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes);
Damien Georgedaab6512014-04-25 23:37:55 +0100623
Damien George516b09e2014-08-28 23:06:38 +0100624 #if EXTENSIVE_HEAP_PROFILING
625 gc_dump_alloc_table();
626 #endif
627
Damien Georgedde739d2014-04-20 18:16:25 +0100628 return ptr_in;
629 }
630
631 // can't resize inplace; try to find a new contiguous chain
632 void *ptr_out = gc_alloc(n_bytes,
633#if MICROPY_ENABLE_FINALISER
634 FTB_GET(block)
635#else
636 false
637#endif
638 );
639
640 // check that the alloc succeeded
641 if (ptr_out == NULL) {
642 return NULL;
643 }
644
stijnbbcea3f2014-06-16 10:44:29 +0200645 DEBUG_printf("gc_realloc(%p -> %p)\n", ptr_in, ptr_out);
Damien Georgedde739d2014-04-20 18:16:25 +0100646 memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK);
647 gc_free(ptr_in);
648 return ptr_out;
Damiendcced922013-10-21 23:45:08 +0100649}
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300650#endif // Alternative gc_realloc impl
mux87826762014-03-12 21:00:23 +0200651
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200652void gc_dump_info() {
653 gc_info_t info;
654 gc_info(&info);
655 printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free);
656 printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n",
657 info.num_1block, info.num_2block, info.max_block);
658}
659
Damien Georgece1162a2014-02-26 22:55:59 +0000660void gc_dump_alloc_table(void) {
Damien George516b09e2014-08-28 23:06:38 +0100661 static const mp_uint_t DUMP_BYTES_PER_LINE = 64;
662 #if !EXTENSIVE_HEAP_PROFILING
663 // When comparing heap output we don't want to print the starting
664 // pointer of the heap because it changes from run to run.
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000665 printf("GC memory layout; from %p:", MP_STATE_MEM(gc_pool_start));
Damien George516b09e2014-08-28 23:06:38 +0100666 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000667 for (mp_uint_t bl = 0; bl < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; bl++) {
Damien George516b09e2014-08-28 23:06:38 +0100668 if (bl % DUMP_BYTES_PER_LINE == 0) {
669 // a new line of blocks
Damien George516b09e2014-08-28 23:06:38 +0100670 {
671 // check if this line contains only free blocks
Damien George0b13f3e2014-10-24 23:12:25 +0100672 mp_uint_t bl2 = bl;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000673 while (bl2 < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB && ATB_GET_KIND(bl2) == AT_FREE) {
Damien George0b13f3e2014-10-24 23:12:25 +0100674 bl2++;
675 }
676 if (bl2 - bl >= 2 * DUMP_BYTES_PER_LINE) {
677 // there are at least 2 lines containing only free blocks, so abbreviate their printing
678 printf("\n (" UINT_FMT " lines all free)", (bl2 - bl) / DUMP_BYTES_PER_LINE);
679 bl = bl2 & (~(DUMP_BYTES_PER_LINE - 1));
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000680 if (bl >= MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB) {
Damien George0b13f3e2014-10-24 23:12:25 +0100681 // got to end of heap
Damien George516b09e2014-08-28 23:06:38 +0100682 break;
683 }
684 }
Damien George516b09e2014-08-28 23:06:38 +0100685 }
Damien George516b09e2014-08-28 23:06:38 +0100686 // print header for new line of blocks
Damien George0b13f3e2014-10-24 23:12:25 +0100687 #if EXTENSIVE_HEAP_PROFILING
688 printf("\n%05x: ", (uint)(bl * BYTES_PER_BLOCK) & 0xfffff);
689 #else
690 printf("\n%05x: ", (uint)PTR_FROM_BLOCK(bl) & 0xfffff);
691 #endif
Damieneefcc792013-10-22 15:25:25 +0100692 }
Damien Georgece1162a2014-02-26 22:55:59 +0000693 int c = ' ';
694 switch (ATB_GET_KIND(bl)) {
695 case AT_FREE: c = '.'; break;
Damien Georgec30595e2014-10-17 14:12:57 +0000696 /* this prints out if the object is reachable from BSS or STACK (for unix only)
697 case AT_HEAD: {
698 extern char __bss_start, _end;
699 extern char *stack_top;
700 c = 'h';
701 void **ptrs = (void**)&__bss_start;
702 mp_uint_t len = ((mp_uint_t)&_end - (mp_uint_t)&__bss_start) / sizeof(mp_uint_t);
703 for (mp_uint_t i = 0; i < len; i++) {
704 mp_uint_t ptr = (mp_uint_t)ptrs[i];
705 if (VERIFY_PTR(ptr) && BLOCK_FROM_PTR(ptr) == bl) {
706 c = 'B';
707 break;
708 }
709 }
710 if (c == 'h') {
711 ptrs = (void**)&c;
712 len = ((mp_uint_t)stack_top - (mp_uint_t)&c) / sizeof(mp_uint_t);
713 for (mp_uint_t i = 0; i < len; i++) {
714 mp_uint_t ptr = (mp_uint_t)ptrs[i];
715 if (VERIFY_PTR(ptr) && BLOCK_FROM_PTR(ptr) == bl) {
716 c = 'S';
717 break;
718 }
719 }
720 }
721 break;
722 }
723 */
Damien George0b13f3e2014-10-24 23:12:25 +0100724 /* this prints the uPy object type of the head block */
Damien Georgedaab6512014-04-25 23:37:55 +0100725 case AT_HEAD: {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000726 mp_uint_t *ptr = MP_STATE_MEM(gc_pool_start) + bl * WORDS_PER_BLOCK;
Damien George40f3c022014-07-03 13:25:24 +0100727 if (*ptr == (mp_uint_t)&mp_type_tuple) { c = 'T'; }
728 else if (*ptr == (mp_uint_t)&mp_type_list) { c = 'L'; }
729 else if (*ptr == (mp_uint_t)&mp_type_dict) { c = 'D'; }
Damien George7860c2a2014-11-05 21:16:41 +0000730 #if MICROPY_PY_BUILTINS_FLOAT
Damien George40f3c022014-07-03 13:25:24 +0100731 else if (*ptr == (mp_uint_t)&mp_type_float) { c = 'F'; }
Damien George7860c2a2014-11-05 21:16:41 +0000732 #endif
Damien George40f3c022014-07-03 13:25:24 +0100733 else if (*ptr == (mp_uint_t)&mp_type_fun_bc) { c = 'B'; }
Damien George0b13f3e2014-10-24 23:12:25 +0100734 else if (*ptr == (mp_uint_t)&mp_type_module) { c = 'M'; }
Damien Georgedaab6512014-04-25 23:37:55 +0100735 else { c = 'h'; }
736 break;
737 }
Damien Georgece1162a2014-02-26 22:55:59 +0000738 case AT_TAIL: c = 't'; break;
739 case AT_MARK: c = 'm'; break;
740 }
741 printf("%c", c);
Damieneefcc792013-10-22 15:25:25 +0100742 }
Damien Georgece1162a2014-02-26 22:55:59 +0000743 printf("\n");
Damieneefcc792013-10-22 15:25:25 +0100744}
745
Damien Georgece1162a2014-02-26 22:55:59 +0000746#if DEBUG_PRINT
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200747void gc_test(void) {
Damien George40f3c022014-07-03 13:25:24 +0100748 mp_uint_t len = 500;
749 mp_uint_t *heap = malloc(len);
750 gc_init(heap, heap + len / sizeof(mp_uint_t));
Damiendcced922013-10-21 23:45:08 +0100751 void *ptrs[100];
752 {
Damien George40f3c022014-07-03 13:25:24 +0100753 mp_uint_t **p = gc_alloc(16, false);
Damien George12bab722014-04-05 20:35:48 +0100754 p[0] = gc_alloc(64, false);
755 p[1] = gc_alloc(1, false);
756 p[2] = gc_alloc(1, false);
757 p[3] = gc_alloc(1, false);
Damien George40f3c022014-07-03 13:25:24 +0100758 mp_uint_t ***p2 = gc_alloc(16, false);
Damiendcced922013-10-21 23:45:08 +0100759 p2[0] = p;
760 p2[1] = p;
761 ptrs[0] = p2;
762 }
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200763 for (int i = 0; i < 25; i+=2) {
Damien George40f3c022014-07-03 13:25:24 +0100764 mp_uint_t *p = gc_alloc(i, false);
Damiendcced922013-10-21 23:45:08 +0100765 printf("p=%p\n", p);
766 if (i & 3) {
767 //ptrs[i] = p;
768 }
769 }
770
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200771 printf("Before GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000772 gc_dump_alloc_table();
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200773 printf("Starting GC...\n");
774 gc_collect_start();
775 gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*));
776 gc_collect_end();
777 printf("After GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000778 gc_dump_alloc_table();
Damiendcced922013-10-21 23:45:08 +0100779}
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200780#endif
Damien Georged3ebe482014-01-07 15:20:33 +0000781
782#endif // MICROPY_ENABLE_GC