blob: 3a505e9c78389d19fae5291dc7bb90b70d1a6e5a [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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
Stefan Naumannace9fb52017-07-24 18:55:14 +020038#if MICROPY_DEBUG_VERBOSE // 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
Paul Sokolovsky75feece2015-12-03 01:40:52 +020049#define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / BYTES_PER_WORD)
50#define BYTES_PER_BLOCK (MICROPY_BYTES_PER_GC_BLOCK)
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 George94fe6e52015-11-27 13:07:48 +000082#define BLOCK_FROM_PTR(ptr) (((byte*)(ptr) - MP_STATE_MEM(gc_pool_start)) / BYTES_PER_BLOCK)
83#define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (uintptr_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
Damien Georgea1c93a62016-05-26 10:53:34 +000097#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
Damien Georgec93d9ca2016-04-25 15:28:57 +000098#define GC_ENTER() mp_thread_mutex_lock(&MP_STATE_MEM(gc_mutex), 1)
99#define GC_EXIT() mp_thread_mutex_unlock(&MP_STATE_MEM(gc_mutex))
100#else
101#define GC_ENTER()
102#define GC_EXIT()
103#endif
104
Damienbb5316b2013-10-22 21:12:29 +0100105// TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool
106void gc_init(void *start, void *end) {
107 // align end pointer on block boundary
Damien George94fe6e52015-11-27 13:07:48 +0000108 end = (void*)((uintptr_t)end & (~(BYTES_PER_BLOCK - 1)));
stijndef10ce2014-06-18 10:20:41 +0200109 DEBUG_printf("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n", start, end, (byte*)end - (byte*)start);
Damienbb5316b2013-10-22 21:12:29 +0100110
Damien George12bab722014-04-05 20:35:48 +0100111 // calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes):
112 // T = A + F + P
113 // F = A * BLOCKS_PER_ATB / BLOCKS_PER_FTB
114 // P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK
115 // => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK)
Damien Georged977d262015-12-16 20:09:11 -0500116 size_t total_byte_len = (byte*)end - (byte*)start;
Damien George12bab722014-04-05 20:35:48 +0100117#if MICROPY_ENABLE_FINALISER
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000118 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 +0100119#else
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000120 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 +0100121#endif
122
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000123 MP_STATE_MEM(gc_alloc_table_start) = (byte*)start;
mux4f7e9f52014-04-03 23:55:12 +0200124
Damien George12bab722014-04-05 20:35:48 +0100125#if MICROPY_ENABLE_FINALISER
Damien Georged977d262015-12-16 20:09:11 -0500126 size_t gc_finaliser_table_byte_len = (MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB + BLOCKS_PER_FTB - 1) / BLOCKS_PER_FTB;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000127 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 +0100128#endif
mux4f7e9f52014-04-03 23:55:12 +0200129
Damien Georged977d262015-12-16 20:09:11 -0500130 size_t gc_pool_block_len = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB;
Damien George94fe6e52015-11-27 13:07:48 +0000131 MP_STATE_MEM(gc_pool_start) = (byte*)end - gc_pool_block_len * BYTES_PER_BLOCK;
132 MP_STATE_MEM(gc_pool_end) = end;
Damienbb5316b2013-10-22 21:12:29 +0100133
Damien Georgea1d3ee32014-08-08 12:33:49 +0100134#if MICROPY_ENABLE_FINALISER
Damien George94fe6e52015-11-27 13:07:48 +0000135 assert(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 +0100136#endif
137
Damienbb5316b2013-10-22 21:12:29 +0100138 // clear ATBs
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000139 memset(MP_STATE_MEM(gc_alloc_table_start), 0, MP_STATE_MEM(gc_alloc_table_byte_len));
Damienbb5316b2013-10-22 21:12:29 +0100140
Damien George12bab722014-04-05 20:35:48 +0100141#if MICROPY_ENABLE_FINALISER
142 // clear FTBs
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000143 memset(MP_STATE_MEM(gc_finaliser_table_start), 0, gc_finaliser_table_byte_len);
Damien George12bab722014-04-05 20:35:48 +0100144#endif
mux4f7e9f52014-04-03 23:55:12 +0200145
Damien Georged5e7f6e2014-08-22 18:17:02 +0100146 // set last free ATB index to start of heap
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000147 MP_STATE_MEM(gc_last_free_atb_index) = 0;
Damien Georged5e7f6e2014-08-22 18:17:02 +0100148
Damien George12bab722014-04-05 20:35:48 +0100149 // unlock the GC
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000150 MP_STATE_MEM(gc_lock_depth) = 0;
Damien George12bab722014-04-05 20:35:48 +0100151
Damien George109c1de2014-10-31 21:30:46 +0000152 // allow auto collection
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000153 MP_STATE_MEM(gc_auto_collect_enabled) = 1;
Damien George109c1de2014-10-31 21:30:46 +0000154
Paul Sokolovsky93e353e2016-07-21 00:37:30 +0300155 #if MICROPY_GC_ALLOC_THRESHOLD
156 // by default, maxuint for gc threshold, effectively turning gc-by-threshold off
157 MP_STATE_MEM(gc_alloc_threshold) = (size_t)-1;
158 MP_STATE_MEM(gc_alloc_amount) = 0;
159 #endif
160
Damien Georgec93d9ca2016-04-25 15:28:57 +0000161 #if MICROPY_PY_THREAD
162 mp_thread_mutex_init(&MP_STATE_MEM(gc_mutex));
163 #endif
164
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200165 DEBUG_printf("GC layout:\n");
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000166 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 +0100167#if MICROPY_ENABLE_FINALISER
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000168 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 +0100169#endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000170 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 +0100171}
172
Damien George443e0182014-04-08 11:31:21 +0000173void gc_lock(void) {
Damien Georgec93d9ca2016-04-25 15:28:57 +0000174 GC_ENTER();
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000175 MP_STATE_MEM(gc_lock_depth)++;
Damien Georgec93d9ca2016-04-25 15:28:57 +0000176 GC_EXIT();
Damien George443e0182014-04-08 11:31:21 +0000177}
178
179void gc_unlock(void) {
Damien Georgec93d9ca2016-04-25 15:28:57 +0000180 GC_ENTER();
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000181 MP_STATE_MEM(gc_lock_depth)--;
Damien Georgec93d9ca2016-04-25 15:28:57 +0000182 GC_EXIT();
Damien George443e0182014-04-08 11:31:21 +0000183}
184
Dave Hylands2fe841d2014-06-30 22:49:21 -0700185bool gc_is_locked(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000186 return MP_STATE_MEM(gc_lock_depth) != 0;
Dave Hylands2fe841d2014-06-30 22:49:21 -0700187}
188
Damien George94fe6e52015-11-27 13:07:48 +0000189// ptr should be of type void*
Damienfd8b6bc2013-10-22 20:26:36 +0100190#define VERIFY_PTR(ptr) ( \
Damien George94fe6e52015-11-27 13:07:48 +0000191 ((uintptr_t)(ptr) & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \
192 && ptr >= (void*)MP_STATE_MEM(gc_pool_start) /* must be above start of pool */ \
193 && ptr < (void*)MP_STATE_MEM(gc_pool_end) /* must be below end of pool */ \
Damienfd8b6bc2013-10-22 20:26:36 +0100194 )
195
Damien George94fe6e52015-11-27 13:07:48 +0000196// ptr should be of type void*
Damiendcced922013-10-21 23:45:08 +0100197#define VERIFY_MARK_AND_PUSH(ptr) \
198 do { \
Damienfd8b6bc2013-10-22 20:26:36 +0100199 if (VERIFY_PTR(ptr)) { \
Damien Georged977d262015-12-16 20:09:11 -0500200 size_t _block = BLOCK_FROM_PTR(ptr); \
Damiendcced922013-10-21 23:45:08 +0100201 if (ATB_GET_KIND(_block) == AT_HEAD) { \
202 /* an unmarked head, mark it, and push it on gc stack */ \
Paul Sokolovsky3ea03a12015-12-27 19:50:34 +0200203 DEBUG_printf("gc_mark(%p)\n", ptr); \
Damiendcced922013-10-21 23:45:08 +0100204 ATB_HEAD_TO_MARK(_block); \
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000205 if (MP_STATE_MEM(gc_sp) < &MP_STATE_MEM(gc_stack)[MICROPY_ALLOC_GC_STACK_SIZE]) { \
206 *MP_STATE_MEM(gc_sp)++ = _block; \
Damiendcced922013-10-21 23:45:08 +0100207 } else { \
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000208 MP_STATE_MEM(gc_stack_overflow) = 1; \
Damiendcced922013-10-21 23:45:08 +0100209 } \
210 } \
211 } \
212 } while (0)
213
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200214STATIC void gc_drain_stack(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000215 while (MP_STATE_MEM(gc_sp) > MP_STATE_MEM(gc_stack)) {
Damiendcced922013-10-21 23:45:08 +0100216 // pop the next block off the stack
Damien George94fe6e52015-11-27 13:07:48 +0000217 size_t block = *--MP_STATE_MEM(gc_sp);
Damiendcced922013-10-21 23:45:08 +0100218
Damieneefcc792013-10-22 15:25:25 +0100219 // work out number of consecutive blocks in the chain starting with this one
Damien Georged977d262015-12-16 20:09:11 -0500220 size_t n_blocks = 0;
Damiendcced922013-10-21 23:45:08 +0100221 do {
222 n_blocks += 1;
223 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
224
225 // check this block's children
Damien George969e4bb2015-12-16 19:41:37 -0500226 void **ptrs = (void**)PTR_FROM_BLOCK(block);
227 for (size_t i = n_blocks * BYTES_PER_BLOCK / sizeof(void*); i > 0; i--, ptrs++) {
228 void *ptr = *ptrs;
229 VERIFY_MARK_AND_PUSH(ptr);
Damiendcced922013-10-21 23:45:08 +0100230 }
231 }
232}
233
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200234STATIC void gc_deal_with_stack_overflow(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000235 while (MP_STATE_MEM(gc_stack_overflow)) {
236 MP_STATE_MEM(gc_stack_overflow) = 0;
237 MP_STATE_MEM(gc_sp) = MP_STATE_MEM(gc_stack);
Damiendcced922013-10-21 23:45:08 +0100238
239 // scan entire memory looking for blocks which have been marked but not their children
Damien Georged977d262015-12-16 20:09:11 -0500240 for (size_t block = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) {
Damiendcced922013-10-21 23:45:08 +0100241 // trace (again) if mark bit set
242 if (ATB_GET_KIND(block) == AT_MARK) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000243 *MP_STATE_MEM(gc_sp)++ = block;
Damiendcced922013-10-21 23:45:08 +0100244 gc_drain_stack();
245 }
246 }
247 }
248}
249
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200250STATIC void gc_sweep(void) {
Paul Sokolovsky755a55f2014-06-05 22:48:02 +0300251 #if MICROPY_PY_GC_COLLECT_RETVAL
Damien Georgee1e359f2015-02-07 17:24:10 +0000252 MP_STATE_MEM(gc_collected) = 0;
Paul Sokolovsky755a55f2014-06-05 22:48:02 +0300253 #endif
Damiendcced922013-10-21 23:45:08 +0100254 // free unmarked heads and their tails
255 int free_tail = 0;
Damien Georgef7782f82015-12-16 19:45:42 -0500256 for (size_t block = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) {
Damiendcced922013-10-21 23:45:08 +0100257 switch (ATB_GET_KIND(block)) {
258 case AT_HEAD:
Damien George12bab722014-04-05 20:35:48 +0100259#if MICROPY_ENABLE_FINALISER
260 if (FTB_GET(block)) {
Damien Georgef7782f82015-12-16 19:45:42 -0500261 mp_obj_base_t *obj = (mp_obj_base_t*)PTR_FROM_BLOCK(block);
262 if (obj->type != NULL) {
Damien George12bab722014-04-05 20:35:48 +0100263 // if the object has a type then see if it has a __del__ method
264 mp_obj_t dest[2];
Damien Georgef7782f82015-12-16 19:45:42 -0500265 mp_load_method_maybe(MP_OBJ_FROM_PTR(obj), MP_QSTR___del__, dest);
Damien George12bab722014-04-05 20:35:48 +0100266 if (dest[0] != MP_OBJ_NULL) {
Damien Georgec7e8c6f2017-04-12 13:52:04 +1000267 // load_method returned a method, execute it in a protected environment
268 #if MICROPY_ENABLE_SCHEDULER
269 mp_sched_lock();
270 #endif
271 mp_call_function_1_protected(dest[0], dest[1]);
272 #if MICROPY_ENABLE_SCHEDULER
273 mp_sched_unlock();
274 #endif
Damien George12bab722014-04-05 20:35:48 +0100275 }
mux4f7e9f52014-04-03 23:55:12 +0200276 }
Damien George12bab722014-04-05 20:35:48 +0100277 // clear finaliser flag
278 FTB_CLEAR(block);
mux4f7e9f52014-04-03 23:55:12 +0200279 }
Damien George12bab722014-04-05 20:35:48 +0100280#endif
Damiendcced922013-10-21 23:45:08 +0100281 free_tail = 1;
Paul Sokolovsky3ea03a12015-12-27 19:50:34 +0200282 DEBUG_printf("gc_sweep(%x)\n", PTR_FROM_BLOCK(block));
Paul Sokolovsky755a55f2014-06-05 22:48:02 +0300283 #if MICROPY_PY_GC_COLLECT_RETVAL
Damien Georgee1e359f2015-02-07 17:24:10 +0000284 MP_STATE_MEM(gc_collected)++;
Paul Sokolovsky755a55f2014-06-05 22:48:02 +0300285 #endif
Damiendcced922013-10-21 23:45:08 +0100286 // fall through to free the head
287
288 case AT_TAIL:
289 if (free_tail) {
290 ATB_ANY_TO_FREE(block);
291 }
292 break;
293
294 case AT_MARK:
295 ATB_MARK_TO_HEAD(block);
296 free_tail = 0;
297 break;
298 }
299 }
300}
301
Damien8b3a7c22013-10-23 20:20:17 +0100302void gc_collect_start(void) {
Damien Georgec93d9ca2016-04-25 15:28:57 +0000303 GC_ENTER();
304 MP_STATE_MEM(gc_lock_depth)++;
Paul Sokolovsky93e353e2016-07-21 00:37:30 +0300305 #if MICROPY_GC_ALLOC_THRESHOLD
306 MP_STATE_MEM(gc_alloc_amount) = 0;
307 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000308 MP_STATE_MEM(gc_stack_overflow) = 0;
309 MP_STATE_MEM(gc_sp) = MP_STATE_MEM(gc_stack);
310 // Trace root pointers. This relies on the root pointers being organised
311 // correctly in the mp_state_ctx structure. We scan nlr_top, dict_locals,
312 // dict_globals, then the root pointer section of mp_state_vm.
313 void **ptrs = (void**)(void*)&mp_state_ctx;
Damien George330165a2016-04-22 22:44:56 +0000314 gc_collect_root(ptrs, offsetof(mp_state_ctx_t, vm.qstr_last_chunk) / sizeof(void*));
Damiendcced922013-10-21 23:45:08 +0100315}
316
Damien George94fe6e52015-11-27 13:07:48 +0000317void gc_collect_root(void **ptrs, size_t len) {
318 for (size_t i = 0; i < len; i++) {
319 void *ptr = ptrs[i];
Damiendcced922013-10-21 23:45:08 +0100320 VERIFY_MARK_AND_PUSH(ptr);
321 gc_drain_stack();
322 }
323}
324
Damien8b3a7c22013-10-23 20:20:17 +0100325void gc_collect_end(void) {
Damiendcced922013-10-21 23:45:08 +0100326 gc_deal_with_stack_overflow();
327 gc_sweep();
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000328 MP_STATE_MEM(gc_last_free_atb_index) = 0;
Damien Georgec93d9ca2016-04-25 15:28:57 +0000329 MP_STATE_MEM(gc_lock_depth)--;
330 GC_EXIT();
Damieneefcc792013-10-22 15:25:25 +0100331}
Damiendcced922013-10-21 23:45:08 +0100332
Damieneefcc792013-10-22 15:25:25 +0100333void gc_info(gc_info_t *info) {
Damien Georgec93d9ca2016-04-25 15:28:57 +0000334 GC_ENTER();
Damien George94fe6e52015-11-27 13:07:48 +0000335 info->total = MP_STATE_MEM(gc_pool_end) - MP_STATE_MEM(gc_pool_start);
Damieneefcc792013-10-22 15:25:25 +0100336 info->used = 0;
337 info->free = 0;
Paul Sokolovsky749cbac2016-07-01 00:09:55 +0300338 info->max_free = 0;
Damieneefcc792013-10-22 15:25:25 +0100339 info->num_1block = 0;
340 info->num_2block = 0;
341 info->max_block = 0;
Paul Sokolovsky6a6e0b72016-06-30 01:59:24 +0300342 bool finish = false;
Paul Sokolovsky749cbac2016-07-01 00:09:55 +0300343 for (size_t block = 0, len = 0, len_free = 0; !finish;) {
Damien Georged977d262015-12-16 20:09:11 -0500344 size_t kind = ATB_GET_KIND(block);
Damieneefcc792013-10-22 15:25:25 +0100345 switch (kind) {
Damiendcced922013-10-21 23:45:08 +0100346 case AT_FREE:
Damieneefcc792013-10-22 15:25:25 +0100347 info->free += 1;
Paul Sokolovsky749cbac2016-07-01 00:09:55 +0300348 len_free += 1;
Damieneefcc792013-10-22 15:25:25 +0100349 len = 0;
Damiendcced922013-10-21 23:45:08 +0100350 break;
351
352 case AT_HEAD:
Damieneefcc792013-10-22 15:25:25 +0100353 info->used += 1;
354 len = 1;
355 break;
356
Damiendcced922013-10-21 23:45:08 +0100357 case AT_TAIL:
Damieneefcc792013-10-22 15:25:25 +0100358 info->used += 1;
359 len += 1;
Damiendcced922013-10-21 23:45:08 +0100360 break;
361
362 case AT_MARK:
Damieneefcc792013-10-22 15:25:25 +0100363 // shouldn't happen
Damiendcced922013-10-21 23:45:08 +0100364 break;
365 }
Paul Sokolovsky6a6e0b72016-06-30 01:59:24 +0300366
367 block++;
368 finish = (block == MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB);
369 // Get next block type if possible
370 if (!finish) {
371 kind = ATB_GET_KIND(block);
372 }
373
374 if (finish || kind == AT_FREE || kind == AT_HEAD) {
375 if (len == 1) {
376 info->num_1block += 1;
377 } else if (len == 2) {
378 info->num_2block += 1;
379 }
380 if (len > info->max_block) {
381 info->max_block = len;
382 }
Paul Sokolovsky749cbac2016-07-01 00:09:55 +0300383 if (finish || kind == AT_HEAD) {
384 if (len_free > info->max_free) {
385 info->max_free = len_free;
386 }
387 len_free = 0;
388 }
Paul Sokolovsky6a6e0b72016-06-30 01:59:24 +0300389 }
Damiendcced922013-10-21 23:45:08 +0100390 }
391
Damieneefcc792013-10-22 15:25:25 +0100392 info->used *= BYTES_PER_BLOCK;
393 info->free *= BYTES_PER_BLOCK;
Damien Georgec93d9ca2016-04-25 15:28:57 +0000394 GC_EXIT();
Damiendcced922013-10-21 23:45:08 +0100395}
396
Damien George94fe6e52015-11-27 13:07:48 +0000397void *gc_alloc(size_t n_bytes, bool has_finaliser) {
Damien Georged977d262015-12-16 20:09:11 -0500398 size_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK;
stijndef10ce2014-06-18 10:20:41 +0200399 DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks);
Damiendcced922013-10-21 23:45:08 +0100400
Damien Georgec93d9ca2016-04-25 15:28:57 +0000401 // check for 0 allocation
402 if (n_blocks == 0) {
Damien George443e0182014-04-08 11:31:21 +0000403 return NULL;
Damien George12bab722014-04-05 20:35:48 +0100404 }
405
Damien Georgec93d9ca2016-04-25 15:28:57 +0000406 GC_ENTER();
407
408 // check if GC is locked
409 if (MP_STATE_MEM(gc_lock_depth) > 0) {
410 GC_EXIT();
Damiendcced922013-10-21 23:45:08 +0100411 return NULL;
412 }
413
Damien Georged977d262015-12-16 20:09:11 -0500414 size_t i;
415 size_t end_block;
416 size_t start_block;
417 size_t n_free = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000418 int collected = !MP_STATE_MEM(gc_auto_collect_enabled);
Paul Sokolovsky93e353e2016-07-21 00:37:30 +0300419
420 #if MICROPY_GC_ALLOC_THRESHOLD
421 if (!collected && MP_STATE_MEM(gc_alloc_amount) >= MP_STATE_MEM(gc_alloc_threshold)) {
422 GC_EXIT();
423 gc_collect();
424 GC_ENTER();
425 }
426 #endif
427
Damiendcced922013-10-21 23:45:08 +0100428 for (;;) {
429
430 // look for a run of n_blocks available blocks
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000431 for (i = MP_STATE_MEM(gc_last_free_atb_index); i < MP_STATE_MEM(gc_alloc_table_byte_len); i++) {
432 byte a = MP_STATE_MEM(gc_alloc_table_start)[i];
Damien Georged5e7f6e2014-08-22 18:17:02 +0100433 if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; }
434 if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; }
435 if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; }
436 if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; }
437 }
Damiendcced922013-10-21 23:45:08 +0100438
Damien Georgec93d9ca2016-04-25 15:28:57 +0000439 GC_EXIT();
Damiendcced922013-10-21 23:45:08 +0100440 // nothing found!
441 if (collected) {
442 return NULL;
443 }
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200444 DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes);
Damiendcced922013-10-21 23:45:08 +0100445 gc_collect();
446 collected = 1;
Damien Georgec93d9ca2016-04-25 15:28:57 +0000447 GC_ENTER();
Damiendcced922013-10-21 23:45:08 +0100448 }
449
450 // found, ending at block i inclusive
451found:
452 // get starting and end blocks, both inclusive
453 end_block = i;
454 start_block = i - n_free + 1;
455
Damien Georgeb796e3d2014-08-28 10:18:40 +0100456 // Set last free ATB index to block after last block we found, for start of
457 // next scan. To reduce fragmentation, we only do this if we were looking
458 // for a single free block, which guarantees that there are no free blocks
Damien George516b09e2014-08-28 23:06:38 +0100459 // before this one. Also, whenever we free or shink a block we must check
460 // if this index needs adjusting (see gc_realloc and gc_free).
Damien Georgeb796e3d2014-08-28 10:18:40 +0100461 if (n_free == 1) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000462 MP_STATE_MEM(gc_last_free_atb_index) = (i + 1) / BLOCKS_PER_ATB;
Damien Georgeb796e3d2014-08-28 10:18:40 +0100463 }
Damien Georged5e7f6e2014-08-22 18:17:02 +0100464
Damiendcced922013-10-21 23:45:08 +0100465 // mark first block as used head
466 ATB_FREE_TO_HEAD(start_block);
467
468 // mark rest of blocks as used tail
469 // TODO for a run of many blocks can make this more efficient
Damien Georged977d262015-12-16 20:09:11 -0500470 for (size_t bl = start_block + 1; bl <= end_block; bl++) {
Damiendcced922013-10-21 23:45:08 +0100471 ATB_FREE_TO_TAIL(bl);
472 }
473
Damien George12bab722014-04-05 20:35:48 +0100474 // get pointer to first block
Damien George3653f512016-05-05 10:25:08 +0000475 // we must create this pointer before unlocking the GC so a collection can find it
Damien George94fe6e52015-11-27 13:07:48 +0000476 void *ret_ptr = (void*)(MP_STATE_MEM(gc_pool_start) + start_block * BYTES_PER_BLOCK);
stijndef10ce2014-06-18 10:20:41 +0200477 DEBUG_printf("gc_alloc(%p)\n", ret_ptr);
muxcc849f72014-04-05 15:49:03 +0200478
Paul Sokolovsky93e353e2016-07-21 00:37:30 +0300479 #if MICROPY_GC_ALLOC_THRESHOLD
480 MP_STATE_MEM(gc_alloc_amount) += n_blocks;
481 #endif
482
Damien George3653f512016-05-05 10:25:08 +0000483 GC_EXIT();
484
Damien George5ffe1d82016-08-26 15:35:26 +1000485 #if MICROPY_GC_CONSERVATIVE_CLEAR
486 // be conservative and zero out all the newly allocated blocks
487 memset((byte*)ret_ptr, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK);
488 #else
Damien George32bef312014-04-26 22:23:42 +0100489 // zero out the additional bytes of the newly allocated blocks
Damien Georgedaab6512014-04-25 23:37:55 +0100490 // This is needed because the blocks may have previously held pointers
491 // to the heap and will not be set to something else if the caller
492 // doesn't actually use the entire block. As such they will continue
493 // to point to the heap and may prevent other blocks from being reclaimed.
Damien Georgec0376942014-06-13 22:33:31 +0100494 memset((byte*)ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes);
Damien George5ffe1d82016-08-26 15:35:26 +1000495 #endif
Damien Georgedaab6512014-04-25 23:37:55 +0100496
Damien George3a2171e2015-09-04 16:53:46 +0100497 #if MICROPY_ENABLE_FINALISER
Damien George12bab722014-04-05 20:35:48 +0100498 if (has_finaliser) {
Damien George32bef312014-04-26 22:23:42 +0100499 // clear type pointer in case it is never set
Damien George94fe6e52015-11-27 13:07:48 +0000500 ((mp_obj_base_t*)ret_ptr)->type = NULL;
Damien George12bab722014-04-05 20:35:48 +0100501 // set mp_obj flag only if it has a finaliser
Damien Georgec93d9ca2016-04-25 15:28:57 +0000502 GC_ENTER();
Damien George12bab722014-04-05 20:35:48 +0100503 FTB_SET(start_block);
Damien Georgec93d9ca2016-04-25 15:28:57 +0000504 GC_EXIT();
Damien George12bab722014-04-05 20:35:48 +0100505 }
Damien George3a2171e2015-09-04 16:53:46 +0100506 #else
507 (void)has_finaliser;
508 #endif
Damien George12bab722014-04-05 20:35:48 +0100509
Damien George516b09e2014-08-28 23:06:38 +0100510 #if EXTENSIVE_HEAP_PROFILING
511 gc_dump_alloc_table();
512 #endif
513
Damien George12bab722014-04-05 20:35:48 +0100514 return ret_ptr;
Damiendcced922013-10-21 23:45:08 +0100515}
516
Damien George12bab722014-04-05 20:35:48 +0100517/*
Damien George40f3c022014-07-03 13:25:24 +0100518void *gc_alloc(mp_uint_t n_bytes) {
mux4f7e9f52014-04-03 23:55:12 +0200519 return _gc_alloc(n_bytes, false);
520}
521
Damien George40f3c022014-07-03 13:25:24 +0100522void *gc_alloc_with_finaliser(mp_uint_t n_bytes) {
mux4f7e9f52014-04-03 23:55:12 +0200523 return _gc_alloc(n_bytes, true);
524}
Damien George12bab722014-04-05 20:35:48 +0100525*/
mux4f7e9f52014-04-03 23:55:12 +0200526
Damienfd8b6bc2013-10-22 20:26:36 +0100527// force the freeing of a piece of memory
Dave Hylands7f3c0d12015-11-06 14:32:47 -0800528// TODO: freeing here does not call finaliser
Damien George94fe6e52015-11-27 13:07:48 +0000529void gc_free(void *ptr) {
Damien Georgec93d9ca2016-04-25 15:28:57 +0000530 GC_ENTER();
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000531 if (MP_STATE_MEM(gc_lock_depth) > 0) {
Damien George443e0182014-04-08 11:31:21 +0000532 // TODO how to deal with this error?
Damien Georgec93d9ca2016-04-25 15:28:57 +0000533 GC_EXIT();
Damien George443e0182014-04-08 11:31:21 +0000534 return;
Damien George12bab722014-04-05 20:35:48 +0100535 }
536
stijnbbcea3f2014-06-16 10:44:29 +0200537 DEBUG_printf("gc_free(%p)\n", ptr);
Damienfd8b6bc2013-10-22 20:26:36 +0100538
Damien George12d4fa92017-07-12 12:17:38 +1000539 if (ptr == NULL) {
Damien Georgec93d9ca2016-04-25 15:28:57 +0000540 GC_EXIT();
Damien Georgec93d9ca2016-04-25 15:28:57 +0000541 } else {
Damien George12d4fa92017-07-12 12:17:38 +1000542 // get the GC block number corresponding to this pointer
543 assert(VERIFY_PTR(ptr));
544 size_t block = BLOCK_FROM_PTR(ptr);
545 assert(ATB_GET_KIND(block) == AT_HEAD);
546
547 #if MICROPY_ENABLE_FINALISER
548 FTB_CLEAR(block);
549 #endif
550
551 // set the last_free pointer to this block if it's earlier in the heap
552 if (block / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) {
553 MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB;
554 }
555
556 // free head and all of its tail blocks
557 do {
558 ATB_ANY_TO_FREE(block);
559 block += 1;
560 } while (ATB_GET_KIND(block) == AT_TAIL);
561
Damien Georgec93d9ca2016-04-25 15:28:57 +0000562 GC_EXIT();
Damien George12d4fa92017-07-12 12:17:38 +1000563
564 #if EXTENSIVE_HEAP_PROFILING
565 gc_dump_alloc_table();
566 #endif
Damienfd8b6bc2013-10-22 20:26:36 +0100567 }
568}
569
Damien George94fe6e52015-11-27 13:07:48 +0000570size_t gc_nbytes(const void *ptr) {
Damien Georgec93d9ca2016-04-25 15:28:57 +0000571 GC_ENTER();
Damienfd8b6bc2013-10-22 20:26:36 +0100572 if (VERIFY_PTR(ptr)) {
Damien Georged977d262015-12-16 20:09:11 -0500573 size_t block = BLOCK_FROM_PTR(ptr);
Damiendcced922013-10-21 23:45:08 +0100574 if (ATB_GET_KIND(block) == AT_HEAD) {
575 // work out number of consecutive blocks in the chain starting with this on
Damien Georged977d262015-12-16 20:09:11 -0500576 size_t n_blocks = 0;
Damiendcced922013-10-21 23:45:08 +0100577 do {
578 n_blocks += 1;
579 } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL);
Damien Georgec93d9ca2016-04-25 15:28:57 +0000580 GC_EXIT();
Damiendcced922013-10-21 23:45:08 +0100581 return n_blocks * BYTES_PER_BLOCK;
582 }
583 }
584
585 // invalid pointer
Damien Georgec93d9ca2016-04-25 15:28:57 +0000586 GC_EXIT();
Damiendcced922013-10-21 23:45:08 +0100587 return 0;
588}
589
mux87826762014-03-12 21:00:23 +0200590#if 0
Damien George443e0182014-04-08 11:31:21 +0000591// old, simple realloc that didn't expand memory in place
Damien George40f3c022014-07-03 13:25:24 +0100592void *gc_realloc(void *ptr, mp_uint_t n_bytes) {
593 mp_uint_t n_existing = gc_nbytes(ptr);
Damien George6fc765c2014-03-07 00:21:51 +0000594 if (n_bytes <= n_existing) {
595 return ptr;
596 } else {
Damien George410f3072014-04-25 11:44:53 +0000597 bool has_finaliser;
598 if (ptr == NULL) {
599 has_finaliser = false;
600 } else {
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300601#if MICROPY_ENABLE_FINALISER
Damien George40f3c022014-07-03 13:25:24 +0100602 has_finaliser = FTB_GET(BLOCK_FROM_PTR((mp_uint_t)ptr));
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300603#else
Damien George410f3072014-04-25 11:44:53 +0000604 has_finaliser = false;
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300605#endif
Damien George410f3072014-04-25 11:44:53 +0000606 }
607 void *ptr2 = gc_alloc(n_bytes, has_finaliser);
Damien George6fc765c2014-03-07 00:21:51 +0000608 if (ptr2 == NULL) {
609 return ptr2;
610 }
611 memcpy(ptr2, ptr, n_existing);
612 gc_free(ptr);
613 return ptr2;
614 }
615}
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300616
617#else // Alternative gc_realloc impl
Damien George443e0182014-04-08 11:31:21 +0000618
Damien George94fe6e52015-11-27 13:07:48 +0000619void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) {
Damien Georgedde739d2014-04-20 18:16:25 +0100620 // check for pure allocation
muxfbaa1472014-03-05 23:23:04 +0200621 if (ptr_in == NULL) {
Damien George12bab722014-04-05 20:35:48 +0100622 return gc_alloc(n_bytes, false);
Damiendcced922013-10-21 23:45:08 +0100623 }
muxfbaa1472014-03-05 23:23:04 +0200624
Damien George37378f82014-10-23 12:02:00 +0100625 // check for pure free
626 if (n_bytes == 0) {
627 gc_free(ptr_in);
628 return NULL;
629 }
630
Damien George94fe6e52015-11-27 13:07:48 +0000631 void *ptr = ptr_in;
Damien Georgedde739d2014-04-20 18:16:25 +0100632
633 // sanity check the ptr
634 if (!VERIFY_PTR(ptr)) {
635 return NULL;
636 }
637
Paul Sokolovskyc86889d2014-04-20 11:45:16 +0300638 // get first block
Damien Georged977d262015-12-16 20:09:11 -0500639 size_t block = BLOCK_FROM_PTR(ptr);
Paul Sokolovskyc86889d2014-04-20 11:45:16 +0300640
Damien Georgee33806a2016-05-04 09:14:43 +0000641 GC_ENTER();
642
Damien Georgedde739d2014-04-20 18:16:25 +0100643 // sanity check the ptr is pointing to the head of a block
644 if (ATB_GET_KIND(block) != AT_HEAD) {
Damien Georgee33806a2016-05-04 09:14:43 +0000645 GC_EXIT();
Damien Georgedde739d2014-04-20 18:16:25 +0100646 return NULL;
muxfbaa1472014-03-05 23:23:04 +0200647 }
648
Damien Georgec93d9ca2016-04-25 15:28:57 +0000649 if (MP_STATE_MEM(gc_lock_depth) > 0) {
650 GC_EXIT();
651 return NULL;
652 }
653
Damien Georgedde739d2014-04-20 18:16:25 +0100654 // compute number of new blocks that are requested
Damien Georged977d262015-12-16 20:09:11 -0500655 size_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK;
Damien Georgedde739d2014-04-20 18:16:25 +0100656
Damien George9b0b3732014-10-15 18:24:47 +0000657 // Get the total number of consecutive blocks that are already allocated to
658 // this chunk of memory, and then count the number of free blocks following
659 // it. Stop if we reach the end of the heap, or if we find enough extra
660 // free blocks to satisfy the realloc. Note that we need to compute the
661 // total size of the existing memory chunk so we can correctly and
662 // efficiently shrink it (see below for shrinking code).
Damien Georged977d262015-12-16 20:09:11 -0500663 size_t n_free = 0;
664 size_t n_blocks = 1; // counting HEAD block
665 size_t max_block = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB;
666 for (size_t bl = block + n_blocks; bl < max_block; bl++) {
Damien George9b0b3732014-10-15 18:24:47 +0000667 byte block_type = ATB_GET_KIND(bl);
668 if (block_type == AT_TAIL) {
669 n_blocks++;
670 continue;
Damien Georgedde739d2014-04-20 18:16:25 +0100671 }
Damien George9b0b3732014-10-15 18:24:47 +0000672 if (block_type == AT_FREE) {
673 n_free++;
674 if (n_blocks + n_free >= new_blocks) {
675 // stop as soon as we find enough blocks for n_bytes
676 break;
677 }
678 continue;
Damien Georgedde739d2014-04-20 18:16:25 +0100679 }
680 break;
681 }
682
683 // return original ptr if it already has the requested number of blocks
684 if (new_blocks == n_blocks) {
Damien Georgec93d9ca2016-04-25 15:28:57 +0000685 GC_EXIT();
Damien Georgedde739d2014-04-20 18:16:25 +0100686 return ptr_in;
687 }
688
689 // check if we can shrink the allocated area
690 if (new_blocks < n_blocks) {
691 // free unneeded tail blocks
Damien Georged977d262015-12-16 20:09:11 -0500692 for (size_t bl = block + new_blocks, count = n_blocks - new_blocks; count > 0; bl++, count--) {
Damien Georgedde739d2014-04-20 18:16:25 +0100693 ATB_ANY_TO_FREE(bl);
694 }
Damien George516b09e2014-08-28 23:06:38 +0100695
696 // set the last_free pointer to end of this block if it's earlier in the heap
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000697 if ((block + new_blocks) / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) {
698 MP_STATE_MEM(gc_last_free_atb_index) = (block + new_blocks) / BLOCKS_PER_ATB;
Damien George516b09e2014-08-28 23:06:38 +0100699 }
700
Damien Georgec93d9ca2016-04-25 15:28:57 +0000701 GC_EXIT();
702
Damien George516b09e2014-08-28 23:06:38 +0100703 #if EXTENSIVE_HEAP_PROFILING
704 gc_dump_alloc_table();
705 #endif
706
Damien Georgedde739d2014-04-20 18:16:25 +0100707 return ptr_in;
708 }
709
710 // check if we can expand in place
711 if (new_blocks <= n_blocks + n_free) {
712 // mark few more blocks as used tail
Damien Georged977d262015-12-16 20:09:11 -0500713 for (size_t bl = block + n_blocks; bl < block + new_blocks; bl++) {
Damien Georgedde739d2014-04-20 18:16:25 +0100714 assert(ATB_GET_KIND(bl) == AT_FREE);
715 ATB_FREE_TO_TAIL(bl);
716 }
Damien Georgedaab6512014-04-25 23:37:55 +0100717
Damien Georgec93d9ca2016-04-25 15:28:57 +0000718 GC_EXIT();
719
Damien George5ffe1d82016-08-26 15:35:26 +1000720 #if MICROPY_GC_CONSERVATIVE_CLEAR
721 // be conservative and zero out all the newly allocated blocks
722 memset((byte*)ptr_in + n_blocks * BYTES_PER_BLOCK, 0, (new_blocks - n_blocks) * BYTES_PER_BLOCK);
723 #else
Damien George32bef312014-04-26 22:23:42 +0100724 // zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc)
stijnf33385f2014-06-12 17:42:20 +0200725 memset((byte*)ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes);
Damien George5ffe1d82016-08-26 15:35:26 +1000726 #endif
Damien Georgedaab6512014-04-25 23:37:55 +0100727
Damien George516b09e2014-08-28 23:06:38 +0100728 #if EXTENSIVE_HEAP_PROFILING
729 gc_dump_alloc_table();
730 #endif
731
Damien Georgedde739d2014-04-20 18:16:25 +0100732 return ptr_in;
733 }
734
Damien Georgee33806a2016-05-04 09:14:43 +0000735 #if MICROPY_ENABLE_FINALISER
736 bool ftb_state = FTB_GET(block);
737 #else
738 bool ftb_state = false;
739 #endif
740
Damien Georgec93d9ca2016-04-25 15:28:57 +0000741 GC_EXIT();
742
Damien Georgeade9a052015-06-13 21:53:22 +0100743 if (!allow_move) {
744 // not allowed to move memory block so return failure
745 return NULL;
746 }
747
Damien Georgedde739d2014-04-20 18:16:25 +0100748 // can't resize inplace; try to find a new contiguous chain
Damien Georgee33806a2016-05-04 09:14:43 +0000749 void *ptr_out = gc_alloc(n_bytes, ftb_state);
Damien Georgedde739d2014-04-20 18:16:25 +0100750
751 // check that the alloc succeeded
752 if (ptr_out == NULL) {
753 return NULL;
754 }
755
stijnbbcea3f2014-06-16 10:44:29 +0200756 DEBUG_printf("gc_realloc(%p -> %p)\n", ptr_in, ptr_out);
Damien Georgedde739d2014-04-20 18:16:25 +0100757 memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK);
758 gc_free(ptr_in);
759 return ptr_out;
Damiendcced922013-10-21 23:45:08 +0100760}
Paul Sokolovskyed162b52014-04-20 11:43:38 +0300761#endif // Alternative gc_realloc impl
mux87826762014-03-12 21:00:23 +0200762
Damien Georgeabc19592015-01-12 22:34:38 +0000763void gc_dump_info(void) {
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200764 gc_info_t info;
765 gc_info(&info);
Damien Georgeacaccb32015-12-18 12:52:45 +0000766 mp_printf(&mp_plat_print, "GC: total: %u, used: %u, free: %u\n",
767 (uint)info.total, (uint)info.used, (uint)info.free);
Paul Sokolovsky749cbac2016-07-01 00:09:55 +0300768 mp_printf(&mp_plat_print, " No. of 1-blocks: %u, 2-blocks: %u, max blk sz: %u, max free sz: %u\n",
769 (uint)info.num_1block, (uint)info.num_2block, (uint)info.max_block, (uint)info.max_free);
Paul Sokolovsky723a6ed2014-02-11 18:01:38 +0200770}
771
Damien Georgece1162a2014-02-26 22:55:59 +0000772void gc_dump_alloc_table(void) {
Damien Georgec93d9ca2016-04-25 15:28:57 +0000773 GC_ENTER();
Damien Georged977d262015-12-16 20:09:11 -0500774 static const size_t DUMP_BYTES_PER_LINE = 64;
Damien George516b09e2014-08-28 23:06:38 +0100775 #if !EXTENSIVE_HEAP_PROFILING
776 // When comparing heap output we don't want to print the starting
777 // pointer of the heap because it changes from run to run.
Damien Georgee72cda92015-04-11 12:15:47 +0100778 mp_printf(&mp_plat_print, "GC memory layout; from %p:", MP_STATE_MEM(gc_pool_start));
Damien George516b09e2014-08-28 23:06:38 +0100779 #endif
Damien Georged977d262015-12-16 20:09:11 -0500780 for (size_t bl = 0; bl < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; bl++) {
Damien George516b09e2014-08-28 23:06:38 +0100781 if (bl % DUMP_BYTES_PER_LINE == 0) {
782 // a new line of blocks
Damien George516b09e2014-08-28 23:06:38 +0100783 {
784 // check if this line contains only free blocks
Damien Georged977d262015-12-16 20:09:11 -0500785 size_t bl2 = bl;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000786 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 +0100787 bl2++;
788 }
789 if (bl2 - bl >= 2 * DUMP_BYTES_PER_LINE) {
790 // there are at least 2 lines containing only free blocks, so abbreviate their printing
Damien Georgeacaccb32015-12-18 12:52:45 +0000791 mp_printf(&mp_plat_print, "\n (%u lines all free)", (uint)(bl2 - bl) / DUMP_BYTES_PER_LINE);
Damien George0b13f3e2014-10-24 23:12:25 +0100792 bl = bl2 & (~(DUMP_BYTES_PER_LINE - 1));
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000793 if (bl >= MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB) {
Damien George0b13f3e2014-10-24 23:12:25 +0100794 // got to end of heap
Damien George516b09e2014-08-28 23:06:38 +0100795 break;
796 }
797 }
Damien George516b09e2014-08-28 23:06:38 +0100798 }
Damien George516b09e2014-08-28 23:06:38 +0100799 // print header for new line of blocks
Damien George12ab9ed2015-03-31 23:07:02 +0100800 // (the cast to uint32_t is for 16-bit ports)
Paul Sokolovsky68a7a922016-05-13 00:16:38 +0300801 //mp_printf(&mp_plat_print, "\n%05x: ", (uint)(PTR_FROM_BLOCK(bl) & (uint32_t)0xfffff));
Damien Georgee72cda92015-04-11 12:15:47 +0100802 mp_printf(&mp_plat_print, "\n%05x: ", (uint)((bl * BYTES_PER_BLOCK) & (uint32_t)0xfffff));
Damieneefcc792013-10-22 15:25:25 +0100803 }
Damien Georgece1162a2014-02-26 22:55:59 +0000804 int c = ' ';
805 switch (ATB_GET_KIND(bl)) {
806 case AT_FREE: c = '.'; break;
Damien Georgec30595e2014-10-17 14:12:57 +0000807 /* this prints out if the object is reachable from BSS or STACK (for unix only)
808 case AT_HEAD: {
Damien Georgec30595e2014-10-17 14:12:57 +0000809 c = 'h';
stijnafd6c8e2015-01-08 10:32:45 +0100810 void **ptrs = (void**)(void*)&mp_state_ctx;
811 mp_uint_t len = offsetof(mp_state_ctx_t, vm.stack_top) / sizeof(mp_uint_t);
Damien Georgec30595e2014-10-17 14:12:57 +0000812 for (mp_uint_t i = 0; i < len; i++) {
813 mp_uint_t ptr = (mp_uint_t)ptrs[i];
814 if (VERIFY_PTR(ptr) && BLOCK_FROM_PTR(ptr) == bl) {
815 c = 'B';
816 break;
817 }
818 }
819 if (c == 'h') {
820 ptrs = (void**)&c;
Damien George330165a2016-04-22 22:44:56 +0000821 len = ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)&c) / sizeof(mp_uint_t);
Damien Georgec30595e2014-10-17 14:12:57 +0000822 for (mp_uint_t i = 0; i < len; i++) {
823 mp_uint_t ptr = (mp_uint_t)ptrs[i];
824 if (VERIFY_PTR(ptr) && BLOCK_FROM_PTR(ptr) == bl) {
825 c = 'S';
826 break;
827 }
828 }
829 }
830 break;
831 }
832 */
Damien George0b13f3e2014-10-24 23:12:25 +0100833 /* this prints the uPy object type of the head block */
Damien Georgedaab6512014-04-25 23:37:55 +0100834 case AT_HEAD: {
Damien George94fe6e52015-11-27 13:07:48 +0000835 void **ptr = (void**)(MP_STATE_MEM(gc_pool_start) + bl * BYTES_PER_BLOCK);
836 if (*ptr == &mp_type_tuple) { c = 'T'; }
837 else if (*ptr == &mp_type_list) { c = 'L'; }
838 else if (*ptr == &mp_type_dict) { c = 'D'; }
Paul Sokolovsky3d7f3f02016-05-11 18:52:46 +0300839 else if (*ptr == &mp_type_str || *ptr == &mp_type_bytes) { c = 'S'; }
Paul Sokolovskybc04dc22016-05-11 19:21:53 +0300840 #if MICROPY_PY_BUILTINS_BYTEARRAY
841 else if (*ptr == &mp_type_bytearray) { c = 'A'; }
842 #endif
843 #if MICROPY_PY_ARRAY
844 else if (*ptr == &mp_type_array) { c = 'A'; }
845 #endif
Damien George7860c2a2014-11-05 21:16:41 +0000846 #if MICROPY_PY_BUILTINS_FLOAT
Damien George94fe6e52015-11-27 13:07:48 +0000847 else if (*ptr == &mp_type_float) { c = 'F'; }
Damien George7860c2a2014-11-05 21:16:41 +0000848 #endif
Damien George94fe6e52015-11-27 13:07:48 +0000849 else if (*ptr == &mp_type_fun_bc) { c = 'B'; }
850 else if (*ptr == &mp_type_module) { c = 'M'; }
Damien Georgeec214052015-01-11 14:37:06 +0000851 else {
852 c = 'h';
853 #if 0
854 // This code prints "Q" for qstr-pool data, and "q" for qstr-str
855 // data. It can be useful to see how qstrs are being allocated,
856 // but is disabled by default because it is very slow.
857 for (qstr_pool_t *pool = MP_STATE_VM(last_pool); c == 'h' && pool != NULL; pool = pool->prev) {
858 if ((qstr_pool_t*)ptr == pool) {
859 c = 'Q';
860 break;
861 }
862 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
863 if ((const byte*)ptr == *q) {
864 c = 'q';
865 break;
866 }
867 }
868 }
869 #endif
870 }
Damien Georgedaab6512014-04-25 23:37:55 +0100871 break;
872 }
Paul Sokolovsky9a8751b2016-05-13 00:16:38 +0300873 case AT_TAIL: c = '='; break;
Damien Georgece1162a2014-02-26 22:55:59 +0000874 case AT_MARK: c = 'm'; break;
875 }
Damien Georgee72cda92015-04-11 12:15:47 +0100876 mp_printf(&mp_plat_print, "%c", c);
Damieneefcc792013-10-22 15:25:25 +0100877 }
Damien Georgee72cda92015-04-11 12:15:47 +0100878 mp_print_str(&mp_plat_print, "\n");
Damien Georgec93d9ca2016-04-25 15:28:57 +0000879 GC_EXIT();
Damieneefcc792013-10-22 15:25:25 +0100880}
881
Damien Georgece1162a2014-02-26 22:55:59 +0000882#if DEBUG_PRINT
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200883void gc_test(void) {
Damien George40f3c022014-07-03 13:25:24 +0100884 mp_uint_t len = 500;
885 mp_uint_t *heap = malloc(len);
886 gc_init(heap, heap + len / sizeof(mp_uint_t));
Damiendcced922013-10-21 23:45:08 +0100887 void *ptrs[100];
888 {
Damien George40f3c022014-07-03 13:25:24 +0100889 mp_uint_t **p = gc_alloc(16, false);
Damien George12bab722014-04-05 20:35:48 +0100890 p[0] = gc_alloc(64, false);
891 p[1] = gc_alloc(1, false);
892 p[2] = gc_alloc(1, false);
893 p[3] = gc_alloc(1, false);
Damien George40f3c022014-07-03 13:25:24 +0100894 mp_uint_t ***p2 = gc_alloc(16, false);
Damiendcced922013-10-21 23:45:08 +0100895 p2[0] = p;
896 p2[1] = p;
897 ptrs[0] = p2;
898 }
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200899 for (int i = 0; i < 25; i+=2) {
Damien George40f3c022014-07-03 13:25:24 +0100900 mp_uint_t *p = gc_alloc(i, false);
Damiendcced922013-10-21 23:45:08 +0100901 printf("p=%p\n", p);
902 if (i & 3) {
903 //ptrs[i] = p;
904 }
905 }
906
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200907 printf("Before GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000908 gc_dump_alloc_table();
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200909 printf("Starting GC...\n");
910 gc_collect_start();
911 gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*));
912 gc_collect_end();
913 printf("After GC:\n");
Damien Georgece1162a2014-02-26 22:55:59 +0000914 gc_dump_alloc_table();
Damiendcced922013-10-21 23:45:08 +0100915}
Paul Sokolovskyaf19cbd2014-02-10 21:45:54 +0200916#endif
Damien Georged3ebe482014-01-07 15:20:33 +0000917
918#endif // MICROPY_ENABLE_GC