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