Paul Sokolovsky | c86889d | 2014-04-20 11:45:16 +0300 | [diff] [blame] | 1 | #include <assert.h> |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 2 | #include <stdio.h> |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 3 | #include <string.h> |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 4 | #include <stdbool.h> |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 5 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 6 | #include "mpconfig.h" |
Paul Sokolovsky | e807fa8 | 2014-04-02 20:36:32 +0300 | [diff] [blame] | 7 | #include "misc.h" |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 8 | #include "gc.h" |
| 9 | |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 10 | #include "misc.h" |
| 11 | #include "qstr.h" |
| 12 | #include "obj.h" |
mux | cc849f7 | 2014-04-05 15:49:03 +0200 | [diff] [blame] | 13 | #include "runtime.h" |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 14 | |
Damien George | d3ebe48 | 2014-01-07 15:20:33 +0000 | [diff] [blame] | 15 | #if MICROPY_ENABLE_GC |
| 16 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 17 | #if 0 // print debugging info |
| 18 | #define DEBUG_PRINT (1) |
Paul Sokolovsky | 44739e2 | 2014-02-16 18:11:42 +0200 | [diff] [blame] | 19 | #define DEBUG_printf DEBUG_printf |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 20 | #else // don't print debugging info |
Damien George | 41eb608 | 2014-02-26 22:40:35 +0000 | [diff] [blame] | 21 | #define DEBUG_printf(...) (void)0 |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 22 | #endif |
| 23 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 24 | #define WORDS_PER_BLOCK (4) |
| 25 | #define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD) |
| 26 | #define STACK_SIZE (64) // tunable; minimum is 1 |
| 27 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 28 | STATIC byte *gc_alloc_table_start; |
| 29 | STATIC machine_uint_t gc_alloc_table_byte_len; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 30 | #if MICROPY_ENABLE_FINALISER |
| 31 | STATIC byte *gc_finaliser_table_start; |
| 32 | #endif |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 33 | STATIC machine_uint_t *gc_pool_start; |
| 34 | STATIC machine_uint_t *gc_pool_end; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 35 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 36 | STATIC int gc_stack_overflow; |
| 37 | STATIC machine_uint_t gc_stack[STACK_SIZE]; |
| 38 | STATIC machine_uint_t *gc_sp; |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 39 | STATIC machine_uint_t gc_lock_depth; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 40 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 41 | // ATB = allocation table byte |
| 42 | // 0b00 = FREE -- free block |
| 43 | // 0b01 = HEAD -- head of a chain of blocks |
| 44 | // 0b10 = TAIL -- in the tail of a chain of blocks |
| 45 | // 0b11 = MARK -- marked head block |
| 46 | |
| 47 | #define AT_FREE (0) |
| 48 | #define AT_HEAD (1) |
| 49 | #define AT_TAIL (2) |
| 50 | #define AT_MARK (3) |
| 51 | |
| 52 | #define BLOCKS_PER_ATB (4) |
| 53 | #define ATB_MASK_0 (0x03) |
| 54 | #define ATB_MASK_1 (0x0c) |
| 55 | #define ATB_MASK_2 (0x30) |
| 56 | #define ATB_MASK_3 (0xc0) |
| 57 | |
| 58 | #define ATB_0_IS_FREE(a) (((a) & ATB_MASK_0) == 0) |
| 59 | #define ATB_1_IS_FREE(a) (((a) & ATB_MASK_1) == 0) |
| 60 | #define ATB_2_IS_FREE(a) (((a) & ATB_MASK_2) == 0) |
| 61 | #define ATB_3_IS_FREE(a) (((a) & ATB_MASK_3) == 0) |
| 62 | |
| 63 | #define BLOCK_SHIFT(block) (2 * ((block) & (BLOCKS_PER_ATB - 1))) |
| 64 | #define ATB_GET_KIND(block) ((gc_alloc_table_start[(block) / BLOCKS_PER_ATB] >> BLOCK_SHIFT(block)) & 3) |
| 65 | #define ATB_ANY_TO_FREE(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_MARK << BLOCK_SHIFT(block))); } while (0) |
| 66 | #define ATB_FREE_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_HEAD << BLOCK_SHIFT(block)); } while (0) |
| 67 | #define ATB_FREE_TO_TAIL(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_TAIL << BLOCK_SHIFT(block)); } while (0) |
| 68 | #define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0) |
| 69 | #define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0) |
| 70 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 71 | #define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK) |
| 72 | #define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start)) |
| 73 | #define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB) |
| 74 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 75 | #if MICROPY_ENABLE_FINALISER |
| 76 | // FTB = finaliser table byte |
| 77 | // if set, then the corresponding block may have a finaliser |
| 78 | |
| 79 | #define BLOCKS_PER_FTB (8) |
| 80 | |
| 81 | #define FTB_GET(block) ((gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] >> ((block) & 7)) & 1) |
| 82 | #define FTB_SET(block) do { gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] |= (1 << ((block) & 7)); } while (0) |
| 83 | #define FTB_CLEAR(block) do { gc_finaliser_table_start[(block) / BLOCKS_PER_FTB] &= (~(1 << ((block) & 7))); } while (0) |
| 84 | #endif |
| 85 | |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 86 | // TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool |
| 87 | void gc_init(void *start, void *end) { |
| 88 | // align end pointer on block boundary |
| 89 | end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1))); |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 90 | DEBUG_printf("Initializing GC heap: %p..%p = %ld bytes\n", start, end, end - start); |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 91 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 92 | // calculate parameters for GC (T=total, A=alloc table, F=finaliser table, P=pool; all in bytes): |
| 93 | // T = A + F + P |
| 94 | // F = A * BLOCKS_PER_ATB / BLOCKS_PER_FTB |
| 95 | // P = A * BLOCKS_PER_ATB * BYTES_PER_BLOCK |
| 96 | // => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK) |
| 97 | machine_uint_t total_byte_len = end - start; |
| 98 | #if MICROPY_ENABLE_FINALISER |
| 99 | 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); |
| 100 | #else |
| 101 | gc_alloc_table_byte_len = total_byte_len / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK); |
| 102 | #endif |
| 103 | |
| 104 | |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 105 | gc_alloc_table_start = (byte*)start; |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 106 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 107 | #if MICROPY_ENABLE_FINALISER |
| 108 | machine_uint_t gc_finaliser_table_byte_len = (gc_alloc_table_byte_len * BLOCKS_PER_ATB) / BLOCKS_PER_FTB; |
| 109 | gc_finaliser_table_start = gc_alloc_table_start + gc_alloc_table_byte_len; |
| 110 | #endif |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 111 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 112 | machine_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BLOCKS_PER_ATB; |
| 113 | gc_pool_start = end - gc_pool_block_len * BYTES_PER_BLOCK; |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 114 | gc_pool_end = end; |
| 115 | |
| 116 | // clear ATBs |
| 117 | memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len); |
| 118 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 119 | #if MICROPY_ENABLE_FINALISER |
| 120 | // clear FTBs |
| 121 | memset(gc_finaliser_table_start, 0, gc_finaliser_table_byte_len); |
| 122 | #endif |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 123 | |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 124 | // allocate first block because gc_pool_start points there and it will never |
| 125 | // be freed, so allocating 1 block with null pointers will minimise memory loss |
| 126 | ATB_FREE_TO_HEAD(0); |
| 127 | for (int i = 0; i < WORDS_PER_BLOCK; i++) { |
| 128 | gc_pool_start[i] = 0; |
| 129 | } |
| 130 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 131 | // unlock the GC |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 132 | gc_lock_depth = 0; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 133 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 134 | DEBUG_printf("GC layout:\n"); |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 135 | 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); |
| 136 | #if MICROPY_ENABLE_FINALISER |
| 137 | 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); |
| 138 | #endif |
| 139 | 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] | 140 | } |
| 141 | |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 142 | void gc_lock(void) { |
| 143 | gc_lock_depth++; |
| 144 | } |
| 145 | |
| 146 | void gc_unlock(void) { |
| 147 | gc_lock_depth--; |
| 148 | } |
| 149 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 150 | #define VERIFY_PTR(ptr) ( \ |
| 151 | (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \ |
| 152 | && ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \ |
| 153 | && ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \ |
| 154 | ) |
| 155 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 156 | #define VERIFY_MARK_AND_PUSH(ptr) \ |
| 157 | do { \ |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 158 | if (VERIFY_PTR(ptr)) { \ |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 159 | machine_uint_t _block = BLOCK_FROM_PTR(ptr); \ |
| 160 | if (ATB_GET_KIND(_block) == AT_HEAD) { \ |
| 161 | /* an unmarked head, mark it, and push it on gc stack */ \ |
| 162 | ATB_HEAD_TO_MARK(_block); \ |
| 163 | if (gc_sp < &gc_stack[STACK_SIZE]) { \ |
| 164 | *gc_sp++ = _block; \ |
| 165 | } else { \ |
| 166 | gc_stack_overflow = 1; \ |
| 167 | } \ |
| 168 | } \ |
| 169 | } \ |
| 170 | } while (0) |
| 171 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 172 | STATIC void gc_drain_stack(void) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 173 | while (gc_sp > gc_stack) { |
| 174 | // pop the next block off the stack |
| 175 | machine_uint_t block = *--gc_sp; |
| 176 | |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 177 | // work out number of consecutive blocks in the chain starting with this one |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 178 | machine_uint_t n_blocks = 0; |
| 179 | do { |
| 180 | n_blocks += 1; |
| 181 | } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL); |
| 182 | |
| 183 | // check this block's children |
| 184 | machine_uint_t *scan = (machine_uint_t*)PTR_FROM_BLOCK(block); |
| 185 | for (machine_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) { |
| 186 | machine_uint_t ptr2 = *scan; |
| 187 | VERIFY_MARK_AND_PUSH(ptr2); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 192 | STATIC void gc_deal_with_stack_overflow(void) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 193 | while (gc_stack_overflow) { |
| 194 | gc_stack_overflow = 0; |
| 195 | gc_sp = gc_stack; |
| 196 | |
| 197 | // scan entire memory looking for blocks which have been marked but not their children |
| 198 | for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) { |
| 199 | // trace (again) if mark bit set |
| 200 | if (ATB_GET_KIND(block) == AT_MARK) { |
| 201 | *gc_sp++ = block; |
| 202 | gc_drain_stack(); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 208 | STATIC void gc_sweep(void) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 209 | // free unmarked heads and their tails |
| 210 | int free_tail = 0; |
| 211 | for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) { |
| 212 | switch (ATB_GET_KIND(block)) { |
| 213 | case AT_HEAD: |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 214 | #if MICROPY_ENABLE_FINALISER |
| 215 | if (FTB_GET(block)) { |
| 216 | mp_obj_t obj = (mp_obj_t)PTR_FROM_BLOCK(block); |
| 217 | if (((mp_obj_base_t*)obj)->type != MP_OBJ_NULL) { |
| 218 | // if the object has a type then see if it has a __del__ method |
| 219 | mp_obj_t dest[2]; |
| 220 | mp_load_method_maybe(obj, MP_QSTR___del__, dest); |
| 221 | if (dest[0] != MP_OBJ_NULL) { |
| 222 | // load_method returned a method |
| 223 | mp_call_method_n_kw(0, 0, dest); |
| 224 | } |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 225 | } |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 226 | // clear finaliser flag |
| 227 | FTB_CLEAR(block); |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 228 | } |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 229 | #endif |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 230 | free_tail = 1; |
| 231 | // fall through to free the head |
| 232 | |
| 233 | case AT_TAIL: |
| 234 | if (free_tail) { |
| 235 | ATB_ANY_TO_FREE(block); |
| 236 | } |
| 237 | break; |
| 238 | |
| 239 | case AT_MARK: |
| 240 | ATB_MARK_TO_HEAD(block); |
| 241 | free_tail = 0; |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 247 | void gc_collect_start(void) { |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 248 | gc_lock(); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 249 | gc_stack_overflow = 0; |
| 250 | gc_sp = gc_stack; |
| 251 | } |
| 252 | |
| 253 | void gc_collect_root(void **ptrs, machine_uint_t len) { |
| 254 | for (machine_uint_t i = 0; i < len; i++) { |
| 255 | machine_uint_t ptr = (machine_uint_t)ptrs[i]; |
| 256 | VERIFY_MARK_AND_PUSH(ptr); |
| 257 | gc_drain_stack(); |
| 258 | } |
| 259 | } |
| 260 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 261 | void gc_collect_end(void) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 262 | gc_deal_with_stack_overflow(); |
| 263 | gc_sweep(); |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 264 | gc_unlock(); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 265 | } |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 266 | |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 267 | void gc_info(gc_info_t *info) { |
| 268 | info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t); |
| 269 | info->used = 0; |
| 270 | info->free = 0; |
| 271 | info->num_1block = 0; |
| 272 | info->num_2block = 0; |
| 273 | info->max_block = 0; |
| 274 | for (machine_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) { |
| 275 | machine_uint_t kind = ATB_GET_KIND(block); |
| 276 | if (kind == AT_FREE || kind == AT_HEAD) { |
| 277 | if (len == 1) { |
| 278 | info->num_1block += 1; |
| 279 | } else if (len == 2) { |
| 280 | info->num_2block += 1; |
| 281 | } |
| 282 | if (len > info->max_block) { |
| 283 | info->max_block = len; |
| 284 | } |
| 285 | } |
| 286 | switch (kind) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 287 | case AT_FREE: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 288 | info->free += 1; |
| 289 | len = 0; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 290 | break; |
| 291 | |
| 292 | case AT_HEAD: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 293 | info->used += 1; |
| 294 | len = 1; |
| 295 | break; |
| 296 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 297 | case AT_TAIL: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 298 | info->used += 1; |
| 299 | len += 1; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 300 | break; |
| 301 | |
| 302 | case AT_MARK: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 303 | // shouldn't happen |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 308 | info->used *= BYTES_PER_BLOCK; |
| 309 | info->free *= BYTES_PER_BLOCK; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 312 | void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 313 | machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK; |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 314 | 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] | 315 | |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 316 | // check if GC is locked |
| 317 | if (gc_lock_depth > 0) { |
| 318 | return NULL; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 321 | // check for 0 allocation |
| 322 | if (n_blocks == 0) { |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | machine_uint_t i; |
| 327 | machine_uint_t end_block; |
| 328 | machine_uint_t start_block; |
| 329 | machine_uint_t n_free = 0; |
| 330 | int collected = 0; |
| 331 | for (;;) { |
| 332 | |
| 333 | // look for a run of n_blocks available blocks |
| 334 | for (i = 0; i < gc_alloc_table_byte_len; i++) { |
| 335 | byte a = gc_alloc_table_start[i]; |
| 336 | if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; } |
| 337 | if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; } |
| 338 | if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; } |
| 339 | if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; } |
| 340 | } |
| 341 | |
| 342 | // nothing found! |
| 343 | if (collected) { |
| 344 | return NULL; |
| 345 | } |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame] | 346 | 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] | 347 | gc_collect(); |
| 348 | collected = 1; |
| 349 | } |
| 350 | |
| 351 | // found, ending at block i inclusive |
| 352 | found: |
| 353 | // get starting and end blocks, both inclusive |
| 354 | end_block = i; |
| 355 | start_block = i - n_free + 1; |
| 356 | |
| 357 | // mark first block as used head |
| 358 | ATB_FREE_TO_HEAD(start_block); |
| 359 | |
| 360 | // mark rest of blocks as used tail |
| 361 | // TODO for a run of many blocks can make this more efficient |
| 362 | for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) { |
| 363 | ATB_FREE_TO_TAIL(bl); |
| 364 | } |
| 365 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 366 | // get pointer to first block |
| 367 | void *ret_ptr = (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK); |
mux | cc849f7 | 2014-04-05 15:49:03 +0200 | [diff] [blame] | 368 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 369 | #if MICROPY_ENABLE_FINALISER |
| 370 | if (has_finaliser) { |
| 371 | // clear type pointer in case it is never set |
| 372 | ((mp_obj_base_t*)ret_ptr)->type = MP_OBJ_NULL; |
| 373 | // set mp_obj flag only if it has a finaliser |
| 374 | FTB_SET(start_block); |
| 375 | } |
| 376 | #endif |
| 377 | |
| 378 | return ret_ptr; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 379 | } |
| 380 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 381 | /* |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 382 | void *gc_alloc(machine_uint_t n_bytes) { |
| 383 | return _gc_alloc(n_bytes, false); |
| 384 | } |
| 385 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 386 | void *gc_alloc_with_finaliser(machine_uint_t n_bytes) { |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 387 | return _gc_alloc(n_bytes, true); |
| 388 | } |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 389 | */ |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 390 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 391 | // force the freeing of a piece of memory |
| 392 | void gc_free(void *ptr_in) { |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 393 | if (gc_lock_depth > 0) { |
| 394 | // TODO how to deal with this error? |
| 395 | return; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 398 | machine_uint_t ptr = (machine_uint_t)ptr_in; |
| 399 | |
| 400 | if (VERIFY_PTR(ptr)) { |
| 401 | machine_uint_t block = BLOCK_FROM_PTR(ptr); |
| 402 | if (ATB_GET_KIND(block) == AT_HEAD) { |
| 403 | // free head and all of its tail blocks |
| 404 | do { |
| 405 | ATB_ANY_TO_FREE(block); |
| 406 | block += 1; |
| 407 | } while (ATB_GET_KIND(block) == AT_TAIL); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 412 | machine_uint_t gc_nbytes(void *ptr_in) { |
| 413 | machine_uint_t ptr = (machine_uint_t)ptr_in; |
| 414 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 415 | if (VERIFY_PTR(ptr)) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 416 | machine_uint_t block = BLOCK_FROM_PTR(ptr); |
| 417 | if (ATB_GET_KIND(block) == AT_HEAD) { |
| 418 | // work out number of consecutive blocks in the chain starting with this on |
| 419 | machine_uint_t n_blocks = 0; |
| 420 | do { |
| 421 | n_blocks += 1; |
| 422 | } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL); |
| 423 | return n_blocks * BYTES_PER_BLOCK; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // invalid pointer |
| 428 | return 0; |
| 429 | } |
| 430 | |
mux | 8782676 | 2014-03-12 21:00:23 +0200 | [diff] [blame] | 431 | #if 0 |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 432 | // old, simple realloc that didn't expand memory in place |
Damien George | 6fc765c | 2014-03-07 00:21:51 +0000 | [diff] [blame] | 433 | void *gc_realloc(void *ptr, machine_uint_t n_bytes) { |
| 434 | machine_uint_t n_existing = gc_nbytes(ptr); |
| 435 | if (n_bytes <= n_existing) { |
| 436 | return ptr; |
| 437 | } else { |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 438 | // TODO false is incorrect! Should get value from current block! |
| 439 | void *ptr2 = gc_alloc(n_bytes, |
| 440 | #if MICROPY_ENABLE_FINALISER |
| 441 | FTB_GET(BLOCK_FROM_PTR((machine_uint_t)ptr)) |
| 442 | #else |
| 443 | false |
| 444 | #endif |
| 445 | ); |
Damien George | 6fc765c | 2014-03-07 00:21:51 +0000 | [diff] [blame] | 446 | if (ptr2 == NULL) { |
| 447 | return ptr2; |
| 448 | } |
| 449 | memcpy(ptr2, ptr, n_existing); |
| 450 | gc_free(ptr); |
| 451 | return ptr2; |
| 452 | } |
| 453 | } |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 454 | |
| 455 | #else // Alternative gc_realloc impl |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 456 | |
mux | fbaa147 | 2014-03-05 23:23:04 +0200 | [diff] [blame] | 457 | void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) { |
Damien George | 443e018 | 2014-04-08 11:31:21 +0000 | [diff] [blame] | 458 | if (gc_lock_depth > 0) { |
| 459 | return NULL; |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 460 | } |
| 461 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame^] | 462 | // check for pure allocation |
mux | fbaa147 | 2014-03-05 23:23:04 +0200 | [diff] [blame] | 463 | if (ptr_in == NULL) { |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 464 | return gc_alloc(n_bytes, false); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 465 | } |
mux | fbaa147 | 2014-03-05 23:23:04 +0200 | [diff] [blame] | 466 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame^] | 467 | machine_uint_t ptr = (machine_uint_t)ptr_in; |
| 468 | |
| 469 | // sanity check the ptr |
| 470 | if (!VERIFY_PTR(ptr)) { |
| 471 | return NULL; |
| 472 | } |
| 473 | |
Paul Sokolovsky | c86889d | 2014-04-20 11:45:16 +0300 | [diff] [blame] | 474 | // get first block |
| 475 | machine_uint_t block = BLOCK_FROM_PTR(ptr); |
| 476 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame^] | 477 | // sanity check the ptr is pointing to the head of a block |
| 478 | if (ATB_GET_KIND(block) != AT_HEAD) { |
| 479 | return NULL; |
mux | fbaa147 | 2014-03-05 23:23:04 +0200 | [diff] [blame] | 480 | } |
| 481 | |
Damien George | dde739d | 2014-04-20 18:16:25 +0100 | [diff] [blame^] | 482 | // compute number of new blocks that are requested |
| 483 | machine_uint_t new_blocks = (n_bytes + BYTES_PER_BLOCK) / BYTES_PER_BLOCK; |
| 484 | |
| 485 | // get the number of consecutive tail blocks and |
| 486 | // the number of free blocks after last tail block |
| 487 | // stop if we reach (or are at) end of heap |
| 488 | machine_uint_t n_free = 0; |
| 489 | machine_uint_t n_blocks = 1; // counting HEAD block |
| 490 | machine_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB; |
| 491 | while (block + n_blocks + n_free < max_block) { |
| 492 | if (n_blocks + n_free >= new_blocks) { |
| 493 | // stop as soon as we find enough blocks for n_bytes |
| 494 | break; |
| 495 | } |
| 496 | byte block_type = ATB_GET_KIND(block + n_blocks + n_free); |
| 497 | switch (block_type) { |
| 498 | case AT_FREE: n_free++; continue; |
| 499 | case AT_TAIL: n_blocks++; continue; |
| 500 | case AT_MARK: assert(0); |
| 501 | } |
| 502 | break; |
| 503 | } |
| 504 | |
| 505 | // return original ptr if it already has the requested number of blocks |
| 506 | if (new_blocks == n_blocks) { |
| 507 | return ptr_in; |
| 508 | } |
| 509 | |
| 510 | // check if we can shrink the allocated area |
| 511 | if (new_blocks < n_blocks) { |
| 512 | // free unneeded tail blocks |
| 513 | for (machine_uint_t bl = block + new_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) { |
| 514 | ATB_ANY_TO_FREE(bl); |
| 515 | } |
| 516 | return ptr_in; |
| 517 | } |
| 518 | |
| 519 | // check if we can expand in place |
| 520 | if (new_blocks <= n_blocks + n_free) { |
| 521 | // mark few more blocks as used tail |
| 522 | for (machine_uint_t bl = block + n_blocks; bl < block + new_blocks; bl++) { |
| 523 | assert(ATB_GET_KIND(bl) == AT_FREE); |
| 524 | ATB_FREE_TO_TAIL(bl); |
| 525 | } |
| 526 | return ptr_in; |
| 527 | } |
| 528 | |
| 529 | // can't resize inplace; try to find a new contiguous chain |
| 530 | void *ptr_out = gc_alloc(n_bytes, |
| 531 | #if MICROPY_ENABLE_FINALISER |
| 532 | FTB_GET(block) |
| 533 | #else |
| 534 | false |
| 535 | #endif |
| 536 | ); |
| 537 | |
| 538 | // check that the alloc succeeded |
| 539 | if (ptr_out == NULL) { |
| 540 | return NULL; |
| 541 | } |
| 542 | |
| 543 | DEBUG_printf("gc_realloc: allocating new block\n"); |
| 544 | memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK); |
| 545 | gc_free(ptr_in); |
| 546 | return ptr_out; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 547 | } |
Paul Sokolovsky | ed162b5 | 2014-04-20 11:43:38 +0300 | [diff] [blame] | 548 | #endif // Alternative gc_realloc impl |
mux | 8782676 | 2014-03-12 21:00:23 +0200 | [diff] [blame] | 549 | |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame] | 550 | void gc_dump_info() { |
| 551 | gc_info_t info; |
| 552 | gc_info(&info); |
| 553 | printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free); |
| 554 | printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n", |
| 555 | info.num_1block, info.num_2block, info.max_block); |
| 556 | } |
| 557 | |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 558 | void gc_dump_alloc_table(void) { |
| 559 | printf("GC memory layout:"); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 560 | for (machine_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] | 561 | if (bl % 64 == 0) { |
| 562 | printf("\n%04x: ", (uint)bl); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 563 | } |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 564 | int c = ' '; |
| 565 | switch (ATB_GET_KIND(bl)) { |
| 566 | case AT_FREE: c = '.'; break; |
| 567 | case AT_HEAD: c = 'h'; break; |
| 568 | case AT_TAIL: c = 't'; break; |
| 569 | case AT_MARK: c = 'm'; break; |
| 570 | } |
| 571 | printf("%c", c); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 572 | } |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 573 | printf("\n"); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 574 | } |
| 575 | |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 576 | #if DEBUG_PRINT |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 577 | void gc_test(void) { |
| 578 | machine_uint_t len = 500; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 579 | machine_uint_t *heap = malloc(len); |
| 580 | gc_init(heap, heap + len / sizeof(machine_uint_t)); |
| 581 | void *ptrs[100]; |
| 582 | { |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 583 | machine_uint_t **p = gc_alloc(16, false); |
| 584 | p[0] = gc_alloc(64, false); |
| 585 | p[1] = gc_alloc(1, false); |
| 586 | p[2] = gc_alloc(1, false); |
| 587 | p[3] = gc_alloc(1, false); |
| 588 | machine_uint_t ***p2 = gc_alloc(16, false); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 589 | p2[0] = p; |
| 590 | p2[1] = p; |
| 591 | ptrs[0] = p2; |
| 592 | } |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 593 | for (int i = 0; i < 25; i+=2) { |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 594 | machine_uint_t *p = gc_alloc(i, false); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 595 | printf("p=%p\n", p); |
| 596 | if (i & 3) { |
| 597 | //ptrs[i] = p; |
| 598 | } |
| 599 | } |
| 600 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 601 | printf("Before GC:\n"); |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 602 | gc_dump_alloc_table(); |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 603 | printf("Starting GC...\n"); |
| 604 | gc_collect_start(); |
| 605 | gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*)); |
| 606 | gc_collect_end(); |
| 607 | printf("After GC:\n"); |
Damien George | ce1162a | 2014-02-26 22:55:59 +0000 | [diff] [blame] | 608 | gc_dump_alloc_table(); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 609 | } |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 610 | #endif |
Damien George | d3ebe48 | 2014-01-07 15:20:33 +0000 | [diff] [blame] | 611 | |
| 612 | #endif // MICROPY_ENABLE_GC |