Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 Sokolovsky | c86889d | 2014-04-20 11:45:16 +0300 | [diff] [blame] | 27 | #include <assert.h> |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 28 | #include <stdio.h> |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 29 | #include <string.h> |
| 30 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 31 | #include "py/mpstate.h" |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 32 | #include "py/gc.h" |
| 33 | #include "py/obj.h" |
| 34 | #include "py/runtime.h" |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 35 | |
Damien George | d3ebe48 | 2014-01-07 15:20:33 +0000 | [diff] [blame] | 36 | #if MICROPY_ENABLE_GC |
| 37 | |
stijn | 9acb5e4 | 2014-06-18 12:29:03 +0200 | [diff] [blame] | 38 | #if 0 // print debugging info |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 39 | #define DEBUG_PRINT (1) |
Paul Sokolovsky | 44739e2 | 2014-02-16 18:11:42 +0200 | [diff] [blame] | 40 | #define DEBUG_printf DEBUG_printf |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 41 | #else // don't print debugging info |
Damien George | 7860c2a | 2014-11-05 21:16:41 +0000 | [diff] [blame] | 42 | #define DEBUG_PRINT (0) |
Damien George | 41eb608 | 2014-02-26 22:40:35 +0000 | [diff] [blame] | 43 | #define DEBUG_printf(...) (void)0 |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 44 | #endif |
| 45 | |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 46 | // make this 1 to dump the heap each time it changes |
| 47 | #define EXTENSIVE_HEAP_PROFILING (0) |
| 48 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 49 | #define WORDS_PER_BLOCK (4) |
| 50 | #define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD) |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 51 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 52 | // 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 George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 75 | #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) |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 81 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 82 | #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))) |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 84 | #define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB) |
| 85 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 86 | #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 George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 92 | #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 George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 95 | #endif |
| 96 | |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 97 | // TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool |
| 98 | void gc_init(void *start, void *end) { |
| 99 | // align end pointer on block boundary |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 100 | end = (void*)((mp_uint_t)end & (~(BYTES_PER_BLOCK - 1))); |
stijn | def10ce | 2014-06-18 10:20:41 +0200 | [diff] [blame] | 101 | DEBUG_printf("Initializing GC heap: %p..%p = " UINT_FMT " bytes\n", start, end, (byte*)end - (byte*)start); |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 102 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 103 | // 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 George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 108 | mp_uint_t total_byte_len = (byte*)end - (byte*)start; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 109 | #if MICROPY_ENABLE_FINALISER |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 110 | 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 George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 111 | #else |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 112 | MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK); |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 113 | #endif |
| 114 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 115 | MP_STATE_MEM(gc_alloc_table_start) = (byte*)start; |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 116 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 117 | #if MICROPY_ENABLE_FINALISER |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 118 | 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 George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 120 | #endif |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 121 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 122 | 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; |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 125 | |
Damien George | a1d3ee3 | 2014-08-08 12:33:49 +0100 | [diff] [blame] | 126 | #if MICROPY_ENABLE_FINALISER |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 127 | assert((byte*)MP_STATE_MEM(gc_pool_start) >= MP_STATE_MEM(gc_finaliser_table_start) + gc_finaliser_table_byte_len); |
Damien George | a1d3ee3 | 2014-08-08 12:33:49 +0100 | [diff] [blame] | 128 | #endif |
| 129 | |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 130 | // clear ATBs |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 131 | memset(MP_STATE_MEM(gc_alloc_table_start), 0, MP_STATE_MEM(gc_alloc_table_byte_len)); |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 132 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 133 | #if MICROPY_ENABLE_FINALISER |
| 134 | // clear FTBs |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 135 | memset(MP_STATE_MEM(gc_finaliser_table_start), 0, gc_finaliser_table_byte_len); |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 136 | #endif |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 137 | |
Damien George | d5e7f6e | 2014-08-22 18:17:02 +0100 | [diff] [blame] | 138 | // set last free ATB index to start of heap |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 139 | MP_STATE_MEM(gc_last_free_atb_index) = 0; |
Damien George | d5e7f6e | 2014-08-22 18:17:02 +0100 | [diff] [blame] | 140 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 141 | // unlock the GC |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 142 | MP_STATE_MEM(gc_lock_depth) = 0; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 143 | |
Damien George | 109c1de | 2014-10-31 21:30:46 +0000 | [diff] [blame] | 144 | // allow auto collection |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 145 | MP_STATE_MEM(gc_auto_collect_enabled) = 1; |
Damien George | 109c1de | 2014-10-31 21:30:46 +0000 | [diff] [blame] | 146 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 147 | DEBUG_printf("GC layout:\n"); |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 148 | 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 George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 149 | #if MICROPY_ENABLE_FINALISER |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 150 | 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 George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 151 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 152 | 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); |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 155 | void gc_lock(void) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 156 | MP_STATE_MEM(gc_lock_depth)++; |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void gc_unlock(void) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 160 | MP_STATE_MEM(gc_lock_depth)--; |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Dave Hylands | 2fe841d | 2014-06-30 22:49:21 -0700 | [diff] [blame] | 163 | bool gc_is_locked(void) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 164 | return MP_STATE_MEM(gc_lock_depth) != 0; |
Dave Hylands | 2fe841d | 2014-06-30 22:49:21 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 167 | #define VERIFY_PTR(ptr) ( \ |
| 168 | (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \ |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 169 | && 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 */ \ |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 171 | ) |
| 172 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 173 | #define VERIFY_MARK_AND_PUSH(ptr) \ |
| 174 | do { \ |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 175 | if (VERIFY_PTR(ptr)) { \ |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 176 | mp_uint_t _block = BLOCK_FROM_PTR(ptr); \ |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 177 | 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 George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 180 | if (MP_STATE_MEM(gc_sp) < &MP_STATE_MEM(gc_stack)[MICROPY_ALLOC_GC_STACK_SIZE]) { \ |
| 181 | *MP_STATE_MEM(gc_sp)++ = _block; \ |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 182 | } else { \ |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 183 | MP_STATE_MEM(gc_stack_overflow) = 1; \ |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 184 | } \ |
| 185 | } \ |
| 186 | } \ |
| 187 | } while (0) |
| 188 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 189 | STATIC void gc_drain_stack(void) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 190 | while (MP_STATE_MEM(gc_sp) > MP_STATE_MEM(gc_stack)) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 191 | // pop the next block off the stack |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 192 | mp_uint_t block = *--MP_STATE_MEM(gc_sp); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 193 | |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 194 | // work out number of consecutive blocks in the chain starting with this one |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 195 | mp_uint_t n_blocks = 0; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 196 | do { |
| 197 | n_blocks += 1; |
| 198 | } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL); |
| 199 | |
| 200 | // check this block's children |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 201 | 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; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 204 | VERIFY_MARK_AND_PUSH(ptr2); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 209 | STATIC void gc_deal_with_stack_overflow(void) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 210 | 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); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 213 | |
| 214 | // scan entire memory looking for blocks which have been marked but not their children |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 215 | for (mp_uint_t block = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 216 | // trace (again) if mark bit set |
| 217 | if (ATB_GET_KIND(block) == AT_MARK) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 218 | *MP_STATE_MEM(gc_sp)++ = block; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 219 | gc_drain_stack(); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 225 | STATIC void gc_sweep(void) { |
Paul Sokolovsky | 755a55f | 2014-06-05 22:48:02 +0300 | [diff] [blame] | 226 | #if MICROPY_PY_GC_COLLECT_RETVAL |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 227 | MP_STATE_MEM(gc_collected) = 0; |
Paul Sokolovsky | 755a55f | 2014-06-05 22:48:02 +0300 | [diff] [blame] | 228 | #endif |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 229 | // free unmarked heads and their tails |
| 230 | int free_tail = 0; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 231 | for (mp_uint_t block = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 232 | switch (ATB_GET_KIND(block)) { |
| 233 | case AT_HEAD: |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 234 | #if MICROPY_ENABLE_FINALISER |
| 235 | if (FTB_GET(block)) { |
| 236 | mp_obj_t obj = (mp_obj_t)PTR_FROM_BLOCK(block); |
| 237 | if (((mp_obj_base_t*)obj)->type != MP_OBJ_NULL) { |
| 238 | // if the object has a type then see if it has a __del__ method |
| 239 | mp_obj_t dest[2]; |
| 240 | mp_load_method_maybe(obj, MP_QSTR___del__, dest); |
| 241 | if (dest[0] != MP_OBJ_NULL) { |
| 242 | // load_method returned a method |
| 243 | mp_call_method_n_kw(0, 0, dest); |
| 244 | } |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 245 | } |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 246 | // clear finaliser flag |
| 247 | FTB_CLEAR(block); |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 248 | } |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 249 | #endif |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 250 | free_tail = 1; |
Paul Sokolovsky | 755a55f | 2014-06-05 22:48:02 +0300 | [diff] [blame] | 251 | #if MICROPY_PY_GC_COLLECT_RETVAL |
Damien George | e1e359f | 2015-02-07 17:24:10 +0000 | [diff] [blame] | 252 | MP_STATE_MEM(gc_collected)++; |
Paul Sokolovsky | 755a55f | 2014-06-05 22:48:02 +0300 | [diff] [blame] | 253 | #endif |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 254 | // fall through to free the head |
| 255 | |
| 256 | case AT_TAIL: |
| 257 | if (free_tail) { |
stijn | bbcea3f | 2014-06-16 10:44:29 +0200 | [diff] [blame] | 258 | DEBUG_printf("gc_sweep(%p)\n",PTR_FROM_BLOCK(block)); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 259 | ATB_ANY_TO_FREE(block); |
| 260 | } |
| 261 | break; |
| 262 | |
| 263 | case AT_MARK: |
| 264 | ATB_MARK_TO_HEAD(block); |
| 265 | free_tail = 0; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 271 | void gc_collect_start(void) { |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 272 | gc_lock(); |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 273 | MP_STATE_MEM(gc_stack_overflow) = 0; |
| 274 | MP_STATE_MEM(gc_sp) = MP_STATE_MEM(gc_stack); |
| 275 | // Trace root pointers. This relies on the root pointers being organised |
| 276 | // correctly in the mp_state_ctx structure. We scan nlr_top, dict_locals, |
| 277 | // dict_globals, then the root pointer section of mp_state_vm. |
| 278 | void **ptrs = (void**)(void*)&mp_state_ctx; |
| 279 | gc_collect_root(ptrs, offsetof(mp_state_ctx_t, vm.stack_top) / sizeof(mp_uint_t)); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 282 | void gc_collect_root(void **ptrs, mp_uint_t len) { |
| 283 | for (mp_uint_t i = 0; i < len; i++) { |
| 284 | mp_uint_t ptr = (mp_uint_t)ptrs[i]; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 285 | VERIFY_MARK_AND_PUSH(ptr); |
| 286 | gc_drain_stack(); |
| 287 | } |
| 288 | } |
| 289 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 290 | void gc_collect_end(void) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 291 | gc_deal_with_stack_overflow(); |
| 292 | gc_sweep(); |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 293 | MP_STATE_MEM(gc_last_free_atb_index) = 0; |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 294 | gc_unlock(); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 295 | } |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 296 | |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 297 | void gc_info(gc_info_t *info) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 298 | info->total = (MP_STATE_MEM(gc_pool_end) - MP_STATE_MEM(gc_pool_start)) * sizeof(mp_uint_t); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 299 | info->used = 0; |
| 300 | info->free = 0; |
| 301 | info->num_1block = 0; |
| 302 | info->num_2block = 0; |
| 303 | info->max_block = 0; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 304 | for (mp_uint_t block = 0, len = 0; block < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; block++) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 305 | mp_uint_t kind = ATB_GET_KIND(block); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 306 | if (kind == AT_FREE || kind == AT_HEAD) { |
| 307 | if (len == 1) { |
| 308 | info->num_1block += 1; |
| 309 | } else if (len == 2) { |
| 310 | info->num_2block += 1; |
| 311 | } |
| 312 | if (len > info->max_block) { |
| 313 | info->max_block = len; |
| 314 | } |
| 315 | } |
| 316 | switch (kind) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 317 | case AT_FREE: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 318 | info->free += 1; |
| 319 | len = 0; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 320 | break; |
| 321 | |
| 322 | case AT_HEAD: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 323 | info->used += 1; |
| 324 | len = 1; |
| 325 | break; |
| 326 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 327 | case AT_TAIL: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 328 | info->used += 1; |
| 329 | len += 1; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | case AT_MARK: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 333 | // shouldn't happen |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | } |
| 337 | |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 338 | info->used *= BYTES_PER_BLOCK; |
| 339 | info->free *= BYTES_PER_BLOCK; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 342 | void *gc_alloc(mp_uint_t n_bytes, bool has_finaliser) { |
| 343 | mp_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK; |
stijn | def10ce | 2014-06-18 10:20:41 +0200 | [diff] [blame] | 344 | DEBUG_printf("gc_alloc(" UINT_FMT " bytes -> " UINT_FMT " blocks)\n", n_bytes, n_blocks); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 345 | |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 346 | // check if GC is locked |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 347 | if (MP_STATE_MEM(gc_lock_depth) > 0) { |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 348 | return NULL; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 351 | // check for 0 allocation |
| 352 | if (n_blocks == 0) { |
| 353 | return NULL; |
| 354 | } |
| 355 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 356 | mp_uint_t i; |
| 357 | mp_uint_t end_block; |
| 358 | mp_uint_t start_block; |
| 359 | mp_uint_t n_free = 0; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 360 | int collected = !MP_STATE_MEM(gc_auto_collect_enabled); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 361 | for (;;) { |
| 362 | |
| 363 | // look for a run of n_blocks available blocks |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 364 | for (i = MP_STATE_MEM(gc_last_free_atb_index); i < MP_STATE_MEM(gc_alloc_table_byte_len); i++) { |
| 365 | byte a = MP_STATE_MEM(gc_alloc_table_start)[i]; |
Damien George | d5e7f6e | 2014-08-22 18:17:02 +0100 | [diff] [blame] | 366 | if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; } |
| 367 | if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; } |
| 368 | if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; } |
| 369 | if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; } |
| 370 | } |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 371 | |
| 372 | // nothing found! |
| 373 | if (collected) { |
| 374 | return NULL; |
| 375 | } |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame] | 376 | DEBUG_printf("gc_alloc(" UINT_FMT "): no free mem, triggering GC\n", n_bytes); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 377 | gc_collect(); |
| 378 | collected = 1; |
| 379 | } |
| 380 | |
| 381 | // found, ending at block i inclusive |
| 382 | found: |
| 383 | // get starting and end blocks, both inclusive |
| 384 | end_block = i; |
| 385 | start_block = i - n_free + 1; |
| 386 | |
Damien George | b796e3d | 2014-08-28 10:18:40 +0100 | [diff] [blame] | 387 | // Set last free ATB index to block after last block we found, for start of |
| 388 | // next scan. To reduce fragmentation, we only do this if we were looking |
| 389 | // for a single free block, which guarantees that there are no free blocks |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 390 | // before this one. Also, whenever we free or shink a block we must check |
| 391 | // if this index needs adjusting (see gc_realloc and gc_free). |
Damien George | b796e3d | 2014-08-28 10:18:40 +0100 | [diff] [blame] | 392 | if (n_free == 1) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 393 | MP_STATE_MEM(gc_last_free_atb_index) = (i + 1) / BLOCKS_PER_ATB; |
Damien George | b796e3d | 2014-08-28 10:18:40 +0100 | [diff] [blame] | 394 | } |
Damien George | d5e7f6e | 2014-08-22 18:17:02 +0100 | [diff] [blame] | 395 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 396 | // mark first block as used head |
| 397 | ATB_FREE_TO_HEAD(start_block); |
| 398 | |
| 399 | // mark rest of blocks as used tail |
| 400 | // TODO for a run of many blocks can make this more efficient |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 401 | for (mp_uint_t bl = start_block + 1; bl <= end_block; bl++) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 402 | ATB_FREE_TO_TAIL(bl); |
| 403 | } |
| 404 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 405 | // get pointer to first block |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 406 | void *ret_ptr = (void*)(MP_STATE_MEM(gc_pool_start) + start_block * WORDS_PER_BLOCK); |
stijn | def10ce | 2014-06-18 10:20:41 +0200 | [diff] [blame] | 407 | DEBUG_printf("gc_alloc(%p)\n", ret_ptr); |
mux | cc849f7 | 2014-04-05 15:49:03 +0200 | [diff] [blame] | 408 | |
Damien George | 32bef31 | 2014-04-26 22:23:42 +0100 | [diff] [blame] | 409 | // zero out the additional bytes of the newly allocated blocks |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 410 | // This is needed because the blocks may have previously held pointers |
| 411 | // to the heap and will not be set to something else if the caller |
| 412 | // doesn't actually use the entire block. As such they will continue |
| 413 | // to point to the heap and may prevent other blocks from being reclaimed. |
Damien George | c037694 | 2014-06-13 22:33:31 +0100 | [diff] [blame] | 414 | memset((byte*)ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes); |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 415 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 416 | #if MICROPY_ENABLE_FINALISER |
| 417 | if (has_finaliser) { |
Damien George | 32bef31 | 2014-04-26 22:23:42 +0100 | [diff] [blame] | 418 | // clear type pointer in case it is never set |
| 419 | ((mp_obj_base_t*)ret_ptr)->type = MP_OBJ_NULL; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 420 | // set mp_obj flag only if it has a finaliser |
| 421 | FTB_SET(start_block); |
| 422 | } |
| 423 | #endif |
| 424 | |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 425 | #if EXTENSIVE_HEAP_PROFILING |
| 426 | gc_dump_alloc_table(); |
| 427 | #endif |
| 428 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 429 | return ret_ptr; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 432 | /* |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 433 | void *gc_alloc(mp_uint_t n_bytes) { |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 434 | return _gc_alloc(n_bytes, false); |
| 435 | } |
| 436 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 437 | void *gc_alloc_with_finaliser(mp_uint_t n_bytes) { |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 438 | return _gc_alloc(n_bytes, true); |
| 439 | } |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 440 | */ |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 441 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 442 | // force the freeing of a piece of memory |
| 443 | void gc_free(void *ptr_in) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 444 | if (MP_STATE_MEM(gc_lock_depth) > 0) { |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 445 | // TODO how to deal with this error? |
| 446 | return; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 449 | mp_uint_t ptr = (mp_uint_t)ptr_in; |
stijn | bbcea3f | 2014-06-16 10:44:29 +0200 | [diff] [blame] | 450 | DEBUG_printf("gc_free(%p)\n", ptr); |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 451 | |
| 452 | if (VERIFY_PTR(ptr)) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 453 | mp_uint_t block = BLOCK_FROM_PTR(ptr); |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 454 | if (ATB_GET_KIND(block) == AT_HEAD) { |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 455 | // set the last_free pointer to this block if it's earlier in the heap |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 456 | if (block / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) { |
| 457 | MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB; |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 460 | // free head and all of its tail blocks |
| 461 | do { |
| 462 | ATB_ANY_TO_FREE(block); |
| 463 | block += 1; |
| 464 | } while (ATB_GET_KIND(block) == AT_TAIL); |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 465 | |
| 466 | #if EXTENSIVE_HEAP_PROFILING |
| 467 | gc_dump_alloc_table(); |
| 468 | #endif |
Damien George | e7bb044 | 2014-10-23 14:13:05 +0100 | [diff] [blame] | 469 | } else { |
| 470 | assert(!"bad free"); |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 471 | } |
Damien George | e7bb044 | 2014-10-23 14:13:05 +0100 | [diff] [blame] | 472 | } else if (ptr_in != NULL) { |
| 473 | assert(!"bad free"); |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 477 | mp_uint_t gc_nbytes(const void *ptr_in) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 478 | mp_uint_t ptr = (mp_uint_t)ptr_in; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 479 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 480 | if (VERIFY_PTR(ptr)) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 481 | mp_uint_t block = BLOCK_FROM_PTR(ptr); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 482 | if (ATB_GET_KIND(block) == AT_HEAD) { |
| 483 | // work out number of consecutive blocks in the chain starting with this on |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 484 | mp_uint_t n_blocks = 0; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 485 | do { |
| 486 | n_blocks += 1; |
| 487 | } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL); |
| 488 | return n_blocks * BYTES_PER_BLOCK; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // invalid pointer |
| 493 | return 0; |
| 494 | } |
| 495 | |
mux | 8782676 | 2014-03-12 21:00:23 +0200 | [diff] [blame] | 496 | #if 0 |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 497 | // old, simple realloc that didn't expand memory in place |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 498 | void *gc_realloc(void *ptr, mp_uint_t n_bytes) { |
| 499 | mp_uint_t n_existing = gc_nbytes(ptr); |
Damien George | 6fc765c | 2014-03-07 00:21:51 +0000 | [diff] [blame] | 500 | if (n_bytes <= n_existing) { |
| 501 | return ptr; |
| 502 | } else { |
Damien George | 410f307 | 2014-04-25 11:44:53 +0000 | [diff] [blame] | 503 | bool has_finaliser; |
| 504 | if (ptr == NULL) { |
| 505 | has_finaliser = false; |
| 506 | } else { |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 507 | #if MICROPY_ENABLE_FINALISER |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 508 | has_finaliser = FTB_GET(BLOCK_FROM_PTR((mp_uint_t)ptr)); |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 509 | #else |
Damien George | 410f307 | 2014-04-25 11:44:53 +0000 | [diff] [blame] | 510 | has_finaliser = false; |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 511 | #endif |
Damien George | 410f307 | 2014-04-25 11:44:53 +0000 | [diff] [blame] | 512 | } |
| 513 | void *ptr2 = gc_alloc(n_bytes, has_finaliser); |
Damien George | 6fc765c | 2014-03-07 00:21:51 +0000 | [diff] [blame] | 514 | if (ptr2 == NULL) { |
| 515 | return ptr2; |
| 516 | } |
| 517 | memcpy(ptr2, ptr, n_existing); |
| 518 | gc_free(ptr); |
| 519 | return ptr2; |
| 520 | } |
| 521 | } |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 522 | |
| 523 | #else // Alternative gc_realloc impl |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 524 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 525 | void *gc_realloc(void *ptr_in, mp_uint_t n_bytes) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 526 | if (MP_STATE_MEM(gc_lock_depth) > 0) { |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 527 | return NULL; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 528 | } |
| 529 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 530 | // check for pure allocation |
mux | fbaa147 | 2014-03-05 23:23:04 +0200 | [diff] [blame] | 531 | if (ptr_in == NULL) { |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 532 | return gc_alloc(n_bytes, false); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 533 | } |
mux | fbaa147 | 2014-03-05 23:23:04 +0200 | [diff] [blame] | 534 | |
Damien George | 37378f8 | 2014-10-23 12:02:00 +0100 | [diff] [blame] | 535 | // check for pure free |
| 536 | if (n_bytes == 0) { |
| 537 | gc_free(ptr_in); |
| 538 | return NULL; |
| 539 | } |
| 540 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 541 | mp_uint_t ptr = (mp_uint_t)ptr_in; |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 542 | |
| 543 | // sanity check the ptr |
| 544 | if (!VERIFY_PTR(ptr)) { |
| 545 | return NULL; |
| 546 | } |
| 547 | |
Paul Sokolovsky | c86889d | 2014-04-20 11:45:16 +0300 | [diff] [blame] | 548 | // get first block |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 549 | mp_uint_t block = BLOCK_FROM_PTR(ptr); |
Paul Sokolovsky | c86889d | 2014-04-20 11:45:16 +0300 | [diff] [blame] | 550 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 551 | // sanity check the ptr is pointing to the head of a block |
| 552 | if (ATB_GET_KIND(block) != AT_HEAD) { |
| 553 | return NULL; |
mux | fbaa147 | 2014-03-05 23:23:04 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 556 | // compute number of new blocks that are requested |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 557 | mp_uint_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK; |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 558 | |
Damien George | 9b0b373 | 2014-10-15 18:24:47 +0000 | [diff] [blame] | 559 | // Get the total number of consecutive blocks that are already allocated to |
| 560 | // this chunk of memory, and then count the number of free blocks following |
| 561 | // it. Stop if we reach the end of the heap, or if we find enough extra |
| 562 | // free blocks to satisfy the realloc. Note that we need to compute the |
| 563 | // total size of the existing memory chunk so we can correctly and |
| 564 | // efficiently shrink it (see below for shrinking code). |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 565 | mp_uint_t n_free = 0; |
| 566 | mp_uint_t n_blocks = 1; // counting HEAD block |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 567 | mp_uint_t max_block = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; |
Damien George | 9b0b373 | 2014-10-15 18:24:47 +0000 | [diff] [blame] | 568 | for (mp_uint_t bl = block + n_blocks; bl < max_block; bl++) { |
| 569 | byte block_type = ATB_GET_KIND(bl); |
| 570 | if (block_type == AT_TAIL) { |
| 571 | n_blocks++; |
| 572 | continue; |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 573 | } |
Damien George | 9b0b373 | 2014-10-15 18:24:47 +0000 | [diff] [blame] | 574 | if (block_type == AT_FREE) { |
| 575 | n_free++; |
| 576 | if (n_blocks + n_free >= new_blocks) { |
| 577 | // stop as soon as we find enough blocks for n_bytes |
| 578 | break; |
| 579 | } |
| 580 | continue; |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 581 | } |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | // return original ptr if it already has the requested number of blocks |
| 586 | if (new_blocks == n_blocks) { |
| 587 | return ptr_in; |
| 588 | } |
| 589 | |
| 590 | // check if we can shrink the allocated area |
| 591 | if (new_blocks < n_blocks) { |
| 592 | // free unneeded tail blocks |
Damien George | 9b0b373 | 2014-10-15 18:24:47 +0000 | [diff] [blame] | 593 | for (mp_uint_t bl = block + new_blocks, count = n_blocks - new_blocks; count > 0; bl++, count--) { |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 594 | ATB_ANY_TO_FREE(bl); |
| 595 | } |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 596 | |
| 597 | // set the last_free pointer to end of this block if it's earlier in the heap |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 598 | if ((block + new_blocks) / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) { |
| 599 | MP_STATE_MEM(gc_last_free_atb_index) = (block + new_blocks) / BLOCKS_PER_ATB; |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | #if EXTENSIVE_HEAP_PROFILING |
| 603 | gc_dump_alloc_table(); |
| 604 | #endif |
| 605 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 606 | return ptr_in; |
| 607 | } |
| 608 | |
| 609 | // check if we can expand in place |
| 610 | if (new_blocks <= n_blocks + n_free) { |
| 611 | // mark few more blocks as used tail |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 612 | for (mp_uint_t bl = block + n_blocks; bl < block + new_blocks; bl++) { |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 613 | assert(ATB_GET_KIND(bl) == AT_FREE); |
| 614 | ATB_FREE_TO_TAIL(bl); |
| 615 | } |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 616 | |
Damien George | 32bef31 | 2014-04-26 22:23:42 +0100 | [diff] [blame] | 617 | // zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc) |
stijn | f33385f | 2014-06-12 17:42:20 +0200 | [diff] [blame] | 618 | memset((byte*)ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes); |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 619 | |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 620 | #if EXTENSIVE_HEAP_PROFILING |
| 621 | gc_dump_alloc_table(); |
| 622 | #endif |
| 623 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 624 | return ptr_in; |
| 625 | } |
| 626 | |
| 627 | // can't resize inplace; try to find a new contiguous chain |
| 628 | void *ptr_out = gc_alloc(n_bytes, |
| 629 | #if MICROPY_ENABLE_FINALISER |
| 630 | FTB_GET(block) |
| 631 | #else |
| 632 | false |
| 633 | #endif |
| 634 | ); |
| 635 | |
| 636 | // check that the alloc succeeded |
| 637 | if (ptr_out == NULL) { |
| 638 | return NULL; |
| 639 | } |
| 640 | |
stijn | bbcea3f | 2014-06-16 10:44:29 +0200 | [diff] [blame] | 641 | DEBUG_printf("gc_realloc(%p -> %p)\n", ptr_in, ptr_out); |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 642 | memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK); |
| 643 | gc_free(ptr_in); |
| 644 | return ptr_out; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 645 | } |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 646 | #endif // Alternative gc_realloc impl |
mux | 8782676 | 2014-03-12 21:00:23 +0200 | [diff] [blame] | 647 | |
Damien George | abc1959 | 2015-01-12 22:34:38 +0000 | [diff] [blame] | 648 | void gc_dump_info(void) { |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame] | 649 | gc_info_t info; |
| 650 | gc_info(&info); |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame^] | 651 | mp_printf(&mp_plat_print, "GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", |
| 652 | info.total, info.used, info.free); |
| 653 | mp_printf(&mp_plat_print, " No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n", |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame] | 654 | info.num_1block, info.num_2block, info.max_block); |
| 655 | } |
| 656 | |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 657 | void gc_dump_alloc_table(void) { |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 658 | static const mp_uint_t DUMP_BYTES_PER_LINE = 64; |
| 659 | #if !EXTENSIVE_HEAP_PROFILING |
| 660 | // When comparing heap output we don't want to print the starting |
| 661 | // pointer of the heap because it changes from run to run. |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame^] | 662 | mp_printf(&mp_plat_print, "GC memory layout; from %p:", MP_STATE_MEM(gc_pool_start)); |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 663 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 664 | for (mp_uint_t bl = 0; bl < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; bl++) { |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 665 | if (bl % DUMP_BYTES_PER_LINE == 0) { |
| 666 | // a new line of blocks |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 667 | { |
| 668 | // check if this line contains only free blocks |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 669 | mp_uint_t bl2 = bl; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 670 | while (bl2 < MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB && ATB_GET_KIND(bl2) == AT_FREE) { |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 671 | bl2++; |
| 672 | } |
| 673 | if (bl2 - bl >= 2 * DUMP_BYTES_PER_LINE) { |
| 674 | // there are at least 2 lines containing only free blocks, so abbreviate their printing |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame^] | 675 | mp_printf(&mp_plat_print, "\n (" UINT_FMT " lines all free)", (bl2 - bl) / DUMP_BYTES_PER_LINE); |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 676 | bl = bl2 & (~(DUMP_BYTES_PER_LINE - 1)); |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 677 | if (bl >= MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB) { |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 678 | // got to end of heap |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 679 | break; |
| 680 | } |
| 681 | } |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 682 | } |
Damien George | 516b09e | 2014-08-28 23:06:38 +0100 | [diff] [blame] | 683 | // print header for new line of blocks |
Damien George | 12ab9ed | 2015-03-31 23:07:02 +0100 | [diff] [blame] | 684 | // (the cast to uint32_t is for 16-bit ports) |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 685 | #if EXTENSIVE_HEAP_PROFILING |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame^] | 686 | mp_printf(&mp_plat_print, "\n%05x: ", (uint)((bl * BYTES_PER_BLOCK) & (uint32_t)0xfffff)); |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 687 | #else |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame^] | 688 | mp_printf(&mp_plat_print, "\n%05x: ", (uint)(PTR_FROM_BLOCK(bl) & (uint32_t)0xfffff)); |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 689 | #endif |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 690 | } |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 691 | int c = ' '; |
| 692 | switch (ATB_GET_KIND(bl)) { |
| 693 | case AT_FREE: c = '.'; break; |
Damien George | c30595e | 2014-10-17 14:12:57 +0000 | [diff] [blame] | 694 | /* this prints out if the object is reachable from BSS or STACK (for unix only) |
| 695 | case AT_HEAD: { |
Damien George | c30595e | 2014-10-17 14:12:57 +0000 | [diff] [blame] | 696 | c = 'h'; |
stijn | afd6c8e | 2015-01-08 10:32:45 +0100 | [diff] [blame] | 697 | void **ptrs = (void**)(void*)&mp_state_ctx; |
| 698 | mp_uint_t len = offsetof(mp_state_ctx_t, vm.stack_top) / sizeof(mp_uint_t); |
Damien George | c30595e | 2014-10-17 14:12:57 +0000 | [diff] [blame] | 699 | for (mp_uint_t i = 0; i < len; i++) { |
| 700 | mp_uint_t ptr = (mp_uint_t)ptrs[i]; |
| 701 | if (VERIFY_PTR(ptr) && BLOCK_FROM_PTR(ptr) == bl) { |
| 702 | c = 'B'; |
| 703 | break; |
| 704 | } |
| 705 | } |
| 706 | if (c == 'h') { |
| 707 | ptrs = (void**)&c; |
stijn | afd6c8e | 2015-01-08 10:32:45 +0100 | [diff] [blame] | 708 | len = ((mp_uint_t)MP_STATE_VM(stack_top) - (mp_uint_t)&c) / sizeof(mp_uint_t); |
Damien George | c30595e | 2014-10-17 14:12:57 +0000 | [diff] [blame] | 709 | for (mp_uint_t i = 0; i < len; i++) { |
| 710 | mp_uint_t ptr = (mp_uint_t)ptrs[i]; |
| 711 | if (VERIFY_PTR(ptr) && BLOCK_FROM_PTR(ptr) == bl) { |
| 712 | c = 'S'; |
| 713 | break; |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | break; |
| 718 | } |
| 719 | */ |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 720 | /* this prints the uPy object type of the head block */ |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 721 | case AT_HEAD: { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 722 | mp_uint_t *ptr = MP_STATE_MEM(gc_pool_start) + bl * WORDS_PER_BLOCK; |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 723 | if (*ptr == (mp_uint_t)&mp_type_tuple) { c = 'T'; } |
| 724 | else if (*ptr == (mp_uint_t)&mp_type_list) { c = 'L'; } |
| 725 | else if (*ptr == (mp_uint_t)&mp_type_dict) { c = 'D'; } |
Damien George | 7860c2a | 2014-11-05 21:16:41 +0000 | [diff] [blame] | 726 | #if MICROPY_PY_BUILTINS_FLOAT |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 727 | else if (*ptr == (mp_uint_t)&mp_type_float) { c = 'F'; } |
Damien George | 7860c2a | 2014-11-05 21:16:41 +0000 | [diff] [blame] | 728 | #endif |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 729 | else if (*ptr == (mp_uint_t)&mp_type_fun_bc) { c = 'B'; } |
Damien George | 0b13f3e | 2014-10-24 23:12:25 +0100 | [diff] [blame] | 730 | else if (*ptr == (mp_uint_t)&mp_type_module) { c = 'M'; } |
Damien George | ec21405 | 2015-01-11 14:37:06 +0000 | [diff] [blame] | 731 | else { |
| 732 | c = 'h'; |
| 733 | #if 0 |
| 734 | // This code prints "Q" for qstr-pool data, and "q" for qstr-str |
| 735 | // data. It can be useful to see how qstrs are being allocated, |
| 736 | // but is disabled by default because it is very slow. |
| 737 | for (qstr_pool_t *pool = MP_STATE_VM(last_pool); c == 'h' && pool != NULL; pool = pool->prev) { |
| 738 | if ((qstr_pool_t*)ptr == pool) { |
| 739 | c = 'Q'; |
| 740 | break; |
| 741 | } |
| 742 | for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) { |
| 743 | if ((const byte*)ptr == *q) { |
| 744 | c = 'q'; |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | #endif |
| 750 | } |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 751 | break; |
| 752 | } |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 753 | case AT_TAIL: c = 't'; break; |
| 754 | case AT_MARK: c = 'm'; break; |
| 755 | } |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame^] | 756 | mp_printf(&mp_plat_print, "%c", c); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 757 | } |
Damien George | e72cda9 | 2015-04-11 12:15:47 +0100 | [diff] [blame^] | 758 | mp_print_str(&mp_plat_print, "\n"); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 759 | } |
| 760 | |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 761 | #if DEBUG_PRINT |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 762 | void gc_test(void) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 763 | mp_uint_t len = 500; |
| 764 | mp_uint_t *heap = malloc(len); |
| 765 | gc_init(heap, heap + len / sizeof(mp_uint_t)); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 766 | void *ptrs[100]; |
| 767 | { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 768 | mp_uint_t **p = gc_alloc(16, false); |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 769 | p[0] = gc_alloc(64, false); |
| 770 | p[1] = gc_alloc(1, false); |
| 771 | p[2] = gc_alloc(1, false); |
| 772 | p[3] = gc_alloc(1, false); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 773 | mp_uint_t ***p2 = gc_alloc(16, false); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 774 | p2[0] = p; |
| 775 | p2[1] = p; |
| 776 | ptrs[0] = p2; |
| 777 | } |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 778 | for (int i = 0; i < 25; i+=2) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 779 | mp_uint_t *p = gc_alloc(i, false); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 780 | printf("p=%p\n", p); |
| 781 | if (i & 3) { |
| 782 | //ptrs[i] = p; |
| 783 | } |
| 784 | } |
| 785 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 786 | printf("Before GC:\n"); |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 787 | gc_dump_alloc_table(); |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 788 | printf("Starting GC...\n"); |
| 789 | gc_collect_start(); |
| 790 | gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*)); |
| 791 | gc_collect_end(); |
| 792 | printf("After GC:\n"); |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 793 | gc_dump_alloc_table(); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 794 | } |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 795 | #endif |
Damien George | d3ebe48 | 2014-01-07 15:20:33 +0000 | [diff] [blame] | 796 | |
| 797 | #endif // MICROPY_ENABLE_GC |