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