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 | 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) |
| 51 | #define STACK_SIZE (64) // tunable; minimum is 1 |
| 52 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 53 | STATIC byte *gc_alloc_table_start; |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 54 | STATIC mp_uint_t gc_alloc_table_byte_len; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 55 | #if MICROPY_ENABLE_FINALISER |
| 56 | STATIC byte *gc_finaliser_table_start; |
| 57 | #endif |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 58 | STATIC mp_uint_t *gc_pool_start; |
| 59 | STATIC mp_uint_t *gc_pool_end; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 60 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 61 | STATIC int gc_stack_overflow; |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 62 | STATIC mp_uint_t gc_stack[STACK_SIZE]; |
| 63 | STATIC mp_uint_t *gc_sp; |
| 64 | STATIC mp_uint_t gc_lock_depth; |
Damien George | d5e7f6e | 2014-08-22 18:17:02 +0100 | [diff] [blame] | 65 | STATIC mp_uint_t gc_last_free_atb_index; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 66 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 67 | // ATB = allocation table byte |
| 68 | // 0b00 = FREE -- free block |
| 69 | // 0b01 = HEAD -- head of a chain of blocks |
| 70 | // 0b10 = TAIL -- in the tail of a chain of blocks |
| 71 | // 0b11 = MARK -- marked head block |
| 72 | |
| 73 | #define AT_FREE (0) |
| 74 | #define AT_HEAD (1) |
| 75 | #define AT_TAIL (2) |
| 76 | #define AT_MARK (3) |
| 77 | |
| 78 | #define BLOCKS_PER_ATB (4) |
| 79 | #define ATB_MASK_0 (0x03) |
| 80 | #define ATB_MASK_1 (0x0c) |
| 81 | #define ATB_MASK_2 (0x30) |
| 82 | #define ATB_MASK_3 (0xc0) |
| 83 | |
| 84 | #define ATB_0_IS_FREE(a) (((a) & ATB_MASK_0) == 0) |
| 85 | #define ATB_1_IS_FREE(a) (((a) & ATB_MASK_1) == 0) |
| 86 | #define ATB_2_IS_FREE(a) (((a) & ATB_MASK_2) == 0) |
| 87 | #define ATB_3_IS_FREE(a) (((a) & ATB_MASK_3) == 0) |
| 88 | |
| 89 | #define BLOCK_SHIFT(block) (2 * ((block) & (BLOCKS_PER_ATB - 1))) |
| 90 | #define ATB_GET_KIND(block) ((gc_alloc_table_start[(block) / BLOCKS_PER_ATB] >> BLOCK_SHIFT(block)) & 3) |
| 91 | #define ATB_ANY_TO_FREE(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_MARK << BLOCK_SHIFT(block))); } while (0) |
| 92 | #define ATB_FREE_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_HEAD << BLOCK_SHIFT(block)); } while (0) |
| 93 | #define ATB_FREE_TO_TAIL(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_TAIL << BLOCK_SHIFT(block)); } while (0) |
| 94 | #define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0) |
| 95 | #define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0) |
| 96 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 97 | #define BLOCK_FROM_PTR(ptr) (((ptr) - (mp_uint_t)gc_pool_start) / BYTES_PER_BLOCK) |
| 98 | #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] | 99 | #define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB) |
| 100 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 101 | #if MICROPY_ENABLE_FINALISER |
| 102 | // FTB = finaliser table byte |
| 103 | // if set, then the corresponding block may have a finaliser |
| 104 | |
| 105 | #define BLOCKS_PER_FTB (8) |
| 106 | |
| 107 | #define FTB_GET(block) ((gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] >> ((block) & 7)) & 1) |
| 108 | #define FTB_SET(block) do { gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] |= (1 << ((block) & 7)); } while (0) |
| 109 | #define FTB_CLEAR(block) do { gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] &= (~(1 << ((block) & 7))); } while (0) |
| 110 | #endif |
| 111 | |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 112 | // TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool |
| 113 | void gc_init(void *start, void *end) { |
| 114 | // align end pointer on block boundary |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 115 | end = (void*)((mp_uint_t)end & (~(BYTES_PER_BLOCK - 1))); |
stijn | def10ce | 2014-06-18 10:20:41 +0200 | [diff] [blame] | 116 | 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] | 117 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 118 | // calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes): |
| 119 | // T = A + F + P |
| 120 | // F = A * BLOCKS_PER_ATB / BLOCKS_PER_FTB |
| 121 | // P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK |
| 122 | // => 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] | 123 | mp_uint_t total_byte_len = (byte*)end - (byte*)start; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 124 | #if MICROPY_ENABLE_FINALISER |
| 125 | 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); |
| 126 | #else |
| 127 | gc_alloc_table_byte_len = total_byte_len / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK); |
| 128 | #endif |
| 129 | |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 130 | gc_alloc_table_start = (byte*)start; |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 131 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 132 | #if MICROPY_ENABLE_FINALISER |
Damien George | a1d3ee3 | 2014-08-08 12:33:49 +0100 | [diff] [blame] | 133 | 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] | 134 | gc_finaliser_table_start = gc_alloc_table_start + gc_alloc_table_byte_len; |
| 135 | #endif |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 136 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 137 | mp_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BLOCKS_PER_ATB; |
| 138 | gc_pool_start = (mp_uint_t*)((byte*)end - gc_pool_block_len * BYTES_PER_BLOCK); |
| 139 | gc_pool_end = (mp_uint_t*)end; |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 140 | |
Damien George | a1d3ee3 | 2014-08-08 12:33:49 +0100 | [diff] [blame] | 141 | #if MICROPY_ENABLE_FINALISER |
| 142 | assert((byte*)gc_pool_start >= gc_finaliser_table_start + gc_finaliser_table_byte_len); |
| 143 | #endif |
| 144 | |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 145 | // clear ATBs |
| 146 | memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len); |
| 147 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 148 | #if MICROPY_ENABLE_FINALISER |
| 149 | // clear FTBs |
| 150 | memset(gc_finaliser_table_start, 0, gc_finaliser_table_byte_len); |
| 151 | #endif |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 152 | |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 153 | // allocate first block because gc_pool_start points there and it will never |
| 154 | // be freed, so allocating 1 block with null pointers will minimise memory loss |
| 155 | ATB_FREE_TO_HEAD(0); |
| 156 | for (int i = 0; i < WORDS_PER_BLOCK; i++) { |
| 157 | gc_pool_start[i] = 0; |
| 158 | } |
| 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 | } |
| 389 | for (i = 0; i < gc_last_free_atb_index; i++) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 390 | byte a = gc_alloc_table_start[i]; |
| 391 | if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; } |
| 392 | if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; } |
| 393 | if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; } |
| 394 | if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; } |
| 395 | } |
| 396 | |
| 397 | // nothing found! |
| 398 | if (collected) { |
| 399 | return NULL; |
| 400 | } |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame] | 401 | 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] | 402 | gc_collect(); |
| 403 | collected = 1; |
| 404 | } |
| 405 | |
| 406 | // found, ending at block i inclusive |
| 407 | found: |
| 408 | // get starting and end blocks, both inclusive |
| 409 | end_block = i; |
| 410 | start_block = i - n_free + 1; |
| 411 | |
Damien George | d5e7f6e | 2014-08-22 18:17:02 +0100 | [diff] [blame] | 412 | // set last free ATB index to last block we found, for start of next scan |
| 413 | gc_last_free_atb_index = i / BLOCKS_PER_ATB; |
| 414 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 415 | // mark first block as used head |
| 416 | ATB_FREE_TO_HEAD(start_block); |
| 417 | |
| 418 | // mark rest of blocks as used tail |
| 419 | // TODO for a run of many blocks can make this more efficient |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 420 | for (mp_uint_t bl = start_block + 1; bl <= end_block; bl++) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 421 | ATB_FREE_TO_TAIL(bl); |
| 422 | } |
| 423 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 424 | // get pointer to first block |
Damien George | c037694 | 2014-06-13 22:33:31 +0100 | [diff] [blame] | 425 | void *ret_ptr = (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK); |
stijn | def10ce | 2014-06-18 10:20:41 +0200 | [diff] [blame] | 426 | DEBUG_printf("gc_alloc(%p)\n", ret_ptr); |
mux | cc849f7 | 2014-04-05 15:49:03 +0200 | [diff] [blame] | 427 | |
Damien George | 32bef31 | 2014-04-26 22:23:42 +0100 | [diff] [blame] | 428 | // zero out the additional bytes of the newly allocated blocks |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 429 | // This is needed because the blocks may have previously held pointers |
| 430 | // to the heap and will not be set to something else if the caller |
| 431 | // doesn't actually use the entire block. As such they will continue |
| 432 | // 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] | 433 | 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] | 434 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 435 | #if MICROPY_ENABLE_FINALISER |
| 436 | if (has_finaliser) { |
Damien George | 32bef31 | 2014-04-26 22:23:42 +0100 | [diff] [blame] | 437 | // clear type pointer in case it is never set |
| 438 | ((mp_obj_base_t*)ret_ptr)->type = MP_OBJ_NULL; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 439 | // set mp_obj flag only if it has a finaliser |
| 440 | FTB_SET(start_block); |
| 441 | } |
| 442 | #endif |
| 443 | |
| 444 | return ret_ptr; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 447 | /* |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 448 | void *gc_alloc(mp_uint_t n_bytes) { |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 449 | return _gc_alloc(n_bytes, false); |
| 450 | } |
| 451 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 452 | void *gc_alloc_with_finaliser(mp_uint_t n_bytes) { |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 453 | return _gc_alloc(n_bytes, true); |
| 454 | } |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 455 | */ |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 456 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 457 | // force the freeing of a piece of memory |
| 458 | void gc_free(void *ptr_in) { |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 459 | if (gc_lock_depth > 0) { |
| 460 | // TODO how to deal with this error? |
| 461 | return; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 464 | mp_uint_t ptr = (mp_uint_t)ptr_in; |
stijn | bbcea3f | 2014-06-16 10:44:29 +0200 | [diff] [blame] | 465 | DEBUG_printf("gc_free(%p)\n", ptr); |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 466 | |
| 467 | if (VERIFY_PTR(ptr)) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 468 | mp_uint_t block = BLOCK_FROM_PTR(ptr); |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 469 | if (ATB_GET_KIND(block) == AT_HEAD) { |
| 470 | // free head and all of its tail blocks |
| 471 | do { |
| 472 | ATB_ANY_TO_FREE(block); |
| 473 | block += 1; |
| 474 | } while (ATB_GET_KIND(block) == AT_TAIL); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 479 | mp_uint_t gc_nbytes(void *ptr_in) { |
| 480 | mp_uint_t ptr = (mp_uint_t)ptr_in; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 481 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 482 | if (VERIFY_PTR(ptr)) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 483 | mp_uint_t block = BLOCK_FROM_PTR(ptr); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 484 | if (ATB_GET_KIND(block) == AT_HEAD) { |
| 485 | // 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] | 486 | mp_uint_t n_blocks = 0; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 487 | do { |
| 488 | n_blocks += 1; |
| 489 | } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL); |
| 490 | return n_blocks * BYTES_PER_BLOCK; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | // invalid pointer |
| 495 | return 0; |
| 496 | } |
| 497 | |
mux | 8782676 | 2014-03-12 21:00:23 +0200 | [diff] [blame] | 498 | #if 0 |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 499 | // old, simple realloc that didn't expand memory in place |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 500 | void *gc_realloc(void *ptr, mp_uint_t n_bytes) { |
| 501 | mp_uint_t n_existing = gc_nbytes(ptr); |
Damien George | 6fc765c | 2014-03-07 00:21:51 +0000 | [diff] [blame] | 502 | if (n_bytes <= n_existing) { |
| 503 | return ptr; |
| 504 | } else { |
Damien George | 410f307 | 2014-04-25 11:44:53 +0000 | [diff] [blame] | 505 | bool has_finaliser; |
| 506 | if (ptr == NULL) { |
| 507 | has_finaliser = false; |
| 508 | } else { |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 509 | #if MICROPY_ENABLE_FINALISER |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 510 | has_finaliser = FTB_GET(BLOCK_FROM_PTR((mp_uint_t)ptr)); |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 511 | #else |
Damien George | 410f307 | 2014-04-25 11:44:53 +0000 | [diff] [blame] | 512 | has_finaliser = false; |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 513 | #endif |
Damien George | 410f307 | 2014-04-25 11:44:53 +0000 | [diff] [blame] | 514 | } |
| 515 | void *ptr2 = gc_alloc(n_bytes, has_finaliser); |
Damien George | 6fc765c | 2014-03-07 00:21:51 +0000 | [diff] [blame] | 516 | if (ptr2 == NULL) { |
| 517 | return ptr2; |
| 518 | } |
| 519 | memcpy(ptr2, ptr, n_existing); |
| 520 | gc_free(ptr); |
| 521 | return ptr2; |
| 522 | } |
| 523 | } |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 524 | |
| 525 | #else // Alternative gc_realloc impl |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 526 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 527 | void *gc_realloc(void *ptr_in, mp_uint_t n_bytes) { |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 528 | if (gc_lock_depth > 0) { |
| 529 | return NULL; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 532 | // check for pure allocation |
mux | fbaa147 | 2014-03-05 23:23:04 +0200 | [diff] [blame] | 533 | if (ptr_in == NULL) { |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 534 | return gc_alloc(n_bytes, false); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 535 | } |
mux | fbaa147 | 2014-03-05 23:23:04 +0200 | [diff] [blame] | 536 | |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 537 | mp_uint_t ptr = (mp_uint_t)ptr_in; |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 538 | |
| 539 | // sanity check the ptr |
| 540 | if (!VERIFY_PTR(ptr)) { |
| 541 | return NULL; |
| 542 | } |
| 543 | |
Paul Sokolovsky | c86889d | 2014-04-20 11:45:16 +0300 | [diff] [blame] | 544 | // get first block |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 545 | mp_uint_t block = BLOCK_FROM_PTR(ptr); |
Paul Sokolovsky | c86889d | 2014-04-20 11:45:16 +0300 | [diff] [blame] | 546 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 547 | // sanity check the ptr is pointing to the head of a block |
| 548 | if (ATB_GET_KIND(block) != AT_HEAD) { |
| 549 | return NULL; |
mux | fbaa147 | 2014-03-05 23:23:04 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 552 | // compute number of new blocks that are requested |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 553 | 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] | 554 | |
| 555 | // get the number of consecutive tail blocks and |
| 556 | // the number of free blocks after last tail block |
| 557 | // stop if we reach (or are at) end of heap |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 558 | mp_uint_t n_free = 0; |
| 559 | mp_uint_t n_blocks = 1; // counting HEAD block |
| 560 | mp_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB; |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 561 | while (block + n_blocks + n_free < max_block) { |
| 562 | if (n_blocks + n_free >= new_blocks) { |
| 563 | // stop as soon as we find enough blocks for n_bytes |
| 564 | break; |
| 565 | } |
| 566 | byte block_type = ATB_GET_KIND(block + n_blocks + n_free); |
| 567 | switch (block_type) { |
| 568 | case AT_FREE: n_free++; continue; |
| 569 | case AT_TAIL: n_blocks++; continue; |
| 570 | case AT_MARK: assert(0); |
| 571 | } |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | // return original ptr if it already has the requested number of blocks |
| 576 | if (new_blocks == n_blocks) { |
| 577 | return ptr_in; |
| 578 | } |
| 579 | |
| 580 | // check if we can shrink the allocated area |
| 581 | if (new_blocks < n_blocks) { |
| 582 | // free unneeded tail blocks |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 583 | for (mp_uint_t bl = block + new_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) { |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 584 | ATB_ANY_TO_FREE(bl); |
| 585 | } |
| 586 | return ptr_in; |
| 587 | } |
| 588 | |
| 589 | // check if we can expand in place |
| 590 | if (new_blocks <= n_blocks + n_free) { |
| 591 | // mark few more blocks as used tail |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 592 | 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] | 593 | assert(ATB_GET_KIND(bl) == AT_FREE); |
| 594 | ATB_FREE_TO_TAIL(bl); |
| 595 | } |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 596 | |
Damien George | 32bef31 | 2014-04-26 22:23:42 +0100 | [diff] [blame] | 597 | // 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] | 598 | 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] | 599 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 600 | return ptr_in; |
| 601 | } |
| 602 | |
| 603 | // can't resize inplace; try to find a new contiguous chain |
| 604 | void *ptr_out = gc_alloc(n_bytes, |
| 605 | #if MICROPY_ENABLE_FINALISER |
| 606 | FTB_GET(block) |
| 607 | #else |
| 608 | false |
| 609 | #endif |
| 610 | ); |
| 611 | |
| 612 | // check that the alloc succeeded |
| 613 | if (ptr_out == NULL) { |
| 614 | return NULL; |
| 615 | } |
| 616 | |
stijn | bbcea3f | 2014-06-16 10:44:29 +0200 | [diff] [blame] | 617 | DEBUG_printf("gc_realloc(%p -> %p)\n", ptr_in, ptr_out); |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame] | 618 | memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK); |
| 619 | gc_free(ptr_in); |
| 620 | return ptr_out; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 621 | } |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 622 | #endif // Alternative gc_realloc impl |
mux | 8782676 | 2014-03-12 21:00:23 +0200 | [diff] [blame] | 623 | |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame] | 624 | void gc_dump_info() { |
| 625 | gc_info_t info; |
| 626 | gc_info(&info); |
| 627 | printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free); |
| 628 | printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n", |
| 629 | info.num_1block, info.num_2block, info.max_block); |
| 630 | } |
| 631 | |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 632 | void gc_dump_alloc_table(void) { |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 633 | printf("GC memory layout; from %p:", gc_pool_start); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 634 | for (mp_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) { |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 635 | if (bl % 64 == 0) { |
| 636 | printf("\n%04x: ", (uint)bl); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 637 | } |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 638 | int c = ' '; |
| 639 | switch (ATB_GET_KIND(bl)) { |
| 640 | case AT_FREE: c = '.'; break; |
| 641 | case AT_HEAD: c = 'h'; break; |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 642 | /* this prints the uPy object type of the head block |
| 643 | case AT_HEAD: { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 644 | mp_uint_t *ptr = gc_pool_start + bl * WORDS_PER_BLOCK; |
| 645 | if (*ptr == (mp_uint_t)&mp_type_tuple) { c = 'T'; } |
| 646 | else if (*ptr == (mp_uint_t)&mp_type_list) { c = 'L'; } |
| 647 | else if (*ptr == (mp_uint_t)&mp_type_dict) { c = 'D'; } |
| 648 | else if (*ptr == (mp_uint_t)&mp_type_float) { c = 'F'; } |
| 649 | else if (*ptr == (mp_uint_t)&mp_type_fun_bc) { c = 'B'; } |
Damien George | daab651 | 2014-04-25 23:37:55 +0100 | [diff] [blame] | 650 | else { c = 'h'; } |
| 651 | break; |
| 652 | } |
| 653 | */ |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 654 | case AT_TAIL: c = 't'; break; |
| 655 | case AT_MARK: c = 'm'; break; |
| 656 | } |
| 657 | printf("%c", c); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 658 | } |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 659 | printf("\n"); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 660 | } |
| 661 | |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 662 | #if DEBUG_PRINT |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 663 | void gc_test(void) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 664 | mp_uint_t len = 500; |
| 665 | mp_uint_t *heap = malloc(len); |
| 666 | gc_init(heap, heap + len / sizeof(mp_uint_t)); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 667 | void *ptrs[100]; |
| 668 | { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 669 | mp_uint_t **p = gc_alloc(16, false); |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 670 | p[0] = gc_alloc(64, false); |
| 671 | p[1] = gc_alloc(1, false); |
| 672 | p[2] = gc_alloc(1, false); |
| 673 | p[3] = gc_alloc(1, false); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 674 | mp_uint_t ***p2 = gc_alloc(16, false); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 675 | p2[0] = p; |
| 676 | p2[1] = p; |
| 677 | ptrs[0] = p2; |
| 678 | } |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 679 | for (int i = 0; i < 25; i+=2) { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 680 | mp_uint_t *p = gc_alloc(i, false); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 681 | printf("p=%p\n", p); |
| 682 | if (i & 3) { |
| 683 | //ptrs[i] = p; |
| 684 | } |
| 685 | } |
| 686 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 687 | printf("Before GC:\n"); |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 688 | gc_dump_alloc_table(); |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 689 | printf("Starting GC...\n"); |
| 690 | gc_collect_start(); |
| 691 | gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*)); |
| 692 | gc_collect_end(); |
| 693 | printf("After GC:\n"); |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 694 | gc_dump_alloc_table(); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 695 | } |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 696 | #endif |
Damien George | d3ebe48 | 2014-01-07 15:20:33 +0000 | [diff] [blame] | 697 | |
| 698 | #endif // MICROPY_ENABLE_GC |