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