Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdint.h> |
| 4 | #include <string.h> |
| 5 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 6 | #include "mpconfig.h" |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 7 | #include "gc.h" |
| 8 | |
Damien George | d3ebe48 | 2014-01-07 15:20:33 +0000 | [diff] [blame] | 9 | #if MICROPY_ENABLE_GC |
| 10 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 11 | #if 0 // print debugging info |
| 12 | #define DEBUG_PRINT (1) |
| 13 | #define DEBUG_printf(args...) printf(args) |
| 14 | #else // don't print debugging info |
| 15 | #define DEBUG_printf(args...) (void)0 |
| 16 | #endif |
| 17 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 18 | typedef unsigned char byte; |
| 19 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 20 | #define WORDS_PER_BLOCK (4) |
| 21 | #define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD) |
| 22 | #define STACK_SIZE (64) // tunable; minimum is 1 |
| 23 | |
| 24 | static byte *gc_alloc_table_start; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 25 | static machine_uint_t gc_alloc_table_byte_len; |
| 26 | static machine_uint_t *gc_pool_start; |
| 27 | static machine_uint_t *gc_pool_end; |
| 28 | |
| 29 | static int gc_stack_overflow; |
| 30 | static machine_uint_t gc_stack[STACK_SIZE]; |
| 31 | static machine_uint_t *gc_sp; |
| 32 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 33 | // ATB = allocation table byte |
| 34 | // 0b00 = FREE -- free block |
| 35 | // 0b01 = HEAD -- head of a chain of blocks |
| 36 | // 0b10 = TAIL -- in the tail of a chain of blocks |
| 37 | // 0b11 = MARK -- marked head block |
| 38 | |
| 39 | #define AT_FREE (0) |
| 40 | #define AT_HEAD (1) |
| 41 | #define AT_TAIL (2) |
| 42 | #define AT_MARK (3) |
| 43 | |
| 44 | #define BLOCKS_PER_ATB (4) |
| 45 | #define ATB_MASK_0 (0x03) |
| 46 | #define ATB_MASK_1 (0x0c) |
| 47 | #define ATB_MASK_2 (0x30) |
| 48 | #define ATB_MASK_3 (0xc0) |
| 49 | |
| 50 | #define ATB_0_IS_FREE(a) (((a) & ATB_MASK_0) == 0) |
| 51 | #define ATB_1_IS_FREE(a) (((a) & ATB_MASK_1) == 0) |
| 52 | #define ATB_2_IS_FREE(a) (((a) & ATB_MASK_2) == 0) |
| 53 | #define ATB_3_IS_FREE(a) (((a) & ATB_MASK_3) == 0) |
| 54 | |
| 55 | #define BLOCK_SHIFT(block) (2 * ((block) & (BLOCKS_PER_ATB - 1))) |
| 56 | #define ATB_GET_KIND(block) ((gc_alloc_table_start[(block) / BLOCKS_PER_ATB] >> BLOCK_SHIFT(block)) & 3) |
| 57 | #define ATB_ANY_TO_FREE(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_MARK << BLOCK_SHIFT(block))); } while (0) |
| 58 | #define ATB_FREE_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_HEAD << BLOCK_SHIFT(block)); } while (0) |
| 59 | #define ATB_FREE_TO_TAIL(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_TAIL << BLOCK_SHIFT(block)); } while (0) |
| 60 | #define ATB_HEAD_TO_MARK(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] |= (AT_MARK << BLOCK_SHIFT(block)); } while (0) |
| 61 | #define ATB_MARK_TO_HEAD(block) do { gc_alloc_table_start[(block) / BLOCKS_PER_ATB] &= (~(AT_TAIL << BLOCK_SHIFT(block))); } while (0) |
| 62 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 63 | #define BLOCK_FROM_PTR(ptr) (((ptr) - (machine_uint_t)gc_pool_start) / BYTES_PER_BLOCK) |
| 64 | #define PTR_FROM_BLOCK(block) (((block) * BYTES_PER_BLOCK + (machine_uint_t)gc_pool_start)) |
| 65 | #define ATB_FROM_BLOCK(bl) ((bl) / BLOCKS_PER_ATB) |
| 66 | |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 67 | // TODO waste less memory; currently requires that all entries in alloc_table have a corresponding block in pool |
| 68 | void gc_init(void *start, void *end) { |
| 69 | // align end pointer on block boundary |
| 70 | end = (void*)((machine_uint_t)end & (~(BYTES_PER_BLOCK - 1))); |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 71 | DEBUG_printf("Initializing GC heap: %p-%p\n", start, end); |
Damien | bb5316b | 2013-10-22 21:12:29 +0100 | [diff] [blame] | 72 | |
| 73 | // calculate parameters for GC |
| 74 | machine_uint_t total_word_len = (machine_uint_t*)end - (machine_uint_t*)start; |
| 75 | gc_alloc_table_byte_len = total_word_len * BYTES_PER_WORD / (1 + BITS_PER_BYTE / 2 * BYTES_PER_BLOCK); |
| 76 | gc_alloc_table_start = (byte*)start; |
| 77 | machine_uint_t gc_pool_block_len = gc_alloc_table_byte_len * BITS_PER_BYTE / 2; |
| 78 | machine_uint_t gc_pool_word_len = gc_pool_block_len * WORDS_PER_BLOCK; |
| 79 | gc_pool_start = (machine_uint_t*)end - gc_pool_word_len; |
| 80 | gc_pool_end = end; |
| 81 | |
| 82 | // clear ATBs |
| 83 | memset(gc_alloc_table_start, 0, gc_alloc_table_byte_len); |
| 84 | |
| 85 | // allocate first block because gc_pool_start points there and it will never |
| 86 | // be freed, so allocating 1 block with null pointers will minimise memory loss |
| 87 | ATB_FREE_TO_HEAD(0); |
| 88 | for (int i = 0; i < WORDS_PER_BLOCK; i++) { |
| 89 | gc_pool_start[i] = 0; |
| 90 | } |
| 91 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 92 | DEBUG_printf("GC layout:\n"); |
| 93 | DEBUG_printf(" alloc table at %p, length %u bytes\n", gc_alloc_table_start, gc_alloc_table_byte_len); |
| 94 | DEBUG_printf(" pool at %p, length %u blocks = %u words = %u 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] | 95 | } |
| 96 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 97 | #define VERIFY_PTR(ptr) ( \ |
| 98 | (ptr & (BYTES_PER_BLOCK - 1)) == 0 /* must be aligned on a block */ \ |
| 99 | && ptr >= (machine_uint_t)gc_pool_start /* must be above start of pool */ \ |
| 100 | && ptr < (machine_uint_t)gc_pool_end /* must be below end of pool */ \ |
| 101 | ) |
| 102 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 103 | #define VERIFY_MARK_AND_PUSH(ptr) \ |
| 104 | do { \ |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 105 | if (VERIFY_PTR(ptr)) { \ |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 106 | machine_uint_t _block = BLOCK_FROM_PTR(ptr); \ |
| 107 | if (ATB_GET_KIND(_block) == AT_HEAD) { \ |
| 108 | /* an unmarked head, mark it, and push it on gc stack */ \ |
| 109 | ATB_HEAD_TO_MARK(_block); \ |
| 110 | if (gc_sp < &gc_stack[STACK_SIZE]) { \ |
| 111 | *gc_sp++ = _block; \ |
| 112 | } else { \ |
| 113 | gc_stack_overflow = 1; \ |
| 114 | } \ |
| 115 | } \ |
| 116 | } \ |
| 117 | } while (0) |
| 118 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 119 | static void gc_drain_stack(void) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 120 | while (gc_sp > gc_stack) { |
| 121 | // pop the next block off the stack |
| 122 | machine_uint_t block = *--gc_sp; |
| 123 | |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 124 | // work out number of consecutive blocks in the chain starting with this one |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 125 | machine_uint_t n_blocks = 0; |
| 126 | do { |
| 127 | n_blocks += 1; |
| 128 | } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL); |
| 129 | |
| 130 | // check this block's children |
| 131 | machine_uint_t *scan = (machine_uint_t*)PTR_FROM_BLOCK(block); |
| 132 | for (machine_uint_t i = n_blocks * WORDS_PER_BLOCK; i > 0; i--, scan++) { |
| 133 | machine_uint_t ptr2 = *scan; |
| 134 | VERIFY_MARK_AND_PUSH(ptr2); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 139 | static void gc_deal_with_stack_overflow(void) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 140 | while (gc_stack_overflow) { |
| 141 | gc_stack_overflow = 0; |
| 142 | gc_sp = gc_stack; |
| 143 | |
| 144 | // scan entire memory looking for blocks which have been marked but not their children |
| 145 | for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) { |
| 146 | // trace (again) if mark bit set |
| 147 | if (ATB_GET_KIND(block) == AT_MARK) { |
| 148 | *gc_sp++ = block; |
| 149 | gc_drain_stack(); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 155 | static void gc_sweep(void) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 156 | // free unmarked heads and their tails |
| 157 | int free_tail = 0; |
| 158 | for (machine_uint_t block = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) { |
| 159 | switch (ATB_GET_KIND(block)) { |
| 160 | case AT_HEAD: |
| 161 | free_tail = 1; |
| 162 | // fall through to free the head |
| 163 | |
| 164 | case AT_TAIL: |
| 165 | if (free_tail) { |
| 166 | ATB_ANY_TO_FREE(block); |
| 167 | } |
| 168 | break; |
| 169 | |
| 170 | case AT_MARK: |
| 171 | ATB_MARK_TO_HEAD(block); |
| 172 | free_tail = 0; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 178 | void gc_collect_start(void) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 179 | gc_stack_overflow = 0; |
| 180 | gc_sp = gc_stack; |
| 181 | } |
| 182 | |
| 183 | void gc_collect_root(void **ptrs, machine_uint_t len) { |
| 184 | for (machine_uint_t i = 0; i < len; i++) { |
| 185 | machine_uint_t ptr = (machine_uint_t)ptrs[i]; |
| 186 | VERIFY_MARK_AND_PUSH(ptr); |
| 187 | gc_drain_stack(); |
| 188 | } |
| 189 | } |
| 190 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 191 | void gc_collect_end(void) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 192 | gc_deal_with_stack_overflow(); |
| 193 | gc_sweep(); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 194 | } |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 195 | |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 196 | void gc_info(gc_info_t *info) { |
| 197 | info->total = (gc_pool_end - gc_pool_start) * sizeof(machine_uint_t); |
| 198 | info->used = 0; |
| 199 | info->free = 0; |
| 200 | info->num_1block = 0; |
| 201 | info->num_2block = 0; |
| 202 | info->max_block = 0; |
| 203 | for (machine_uint_t block = 0, len = 0; block < gc_alloc_table_byte_len * BLOCKS_PER_ATB; block++) { |
| 204 | machine_uint_t kind = ATB_GET_KIND(block); |
| 205 | if (kind == AT_FREE || kind == AT_HEAD) { |
| 206 | if (len == 1) { |
| 207 | info->num_1block += 1; |
| 208 | } else if (len == 2) { |
| 209 | info->num_2block += 1; |
| 210 | } |
| 211 | if (len > info->max_block) { |
| 212 | info->max_block = len; |
| 213 | } |
| 214 | } |
| 215 | switch (kind) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 216 | case AT_FREE: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 217 | info->free += 1; |
| 218 | len = 0; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 219 | break; |
| 220 | |
| 221 | case AT_HEAD: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 222 | info->used += 1; |
| 223 | len = 1; |
| 224 | break; |
| 225 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 226 | case AT_TAIL: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 227 | info->used += 1; |
| 228 | len += 1; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 229 | break; |
| 230 | |
| 231 | case AT_MARK: |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 232 | // shouldn't happen |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 237 | info->used *= BYTES_PER_BLOCK; |
| 238 | info->free *= BYTES_PER_BLOCK; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void *gc_alloc(machine_uint_t n_bytes) { |
| 242 | machine_uint_t n_blocks = ((n_bytes + BYTES_PER_BLOCK - 1) & (~(BYTES_PER_BLOCK - 1))) / BYTES_PER_BLOCK; |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame^] | 243 | DEBUG_printf("gc_alloc(%u bytes -> %u blocks)\n", n_bytes, n_blocks); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 244 | |
| 245 | // check for 0 allocation |
| 246 | if (n_blocks == 0) { |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | machine_uint_t i; |
| 251 | machine_uint_t end_block; |
| 252 | machine_uint_t start_block; |
| 253 | machine_uint_t n_free = 0; |
| 254 | int collected = 0; |
| 255 | for (;;) { |
| 256 | |
| 257 | // look for a run of n_blocks available blocks |
| 258 | for (i = 0; i < gc_alloc_table_byte_len; i++) { |
| 259 | byte a = gc_alloc_table_start[i]; |
| 260 | if (ATB_0_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 0; goto found; } } else { n_free = 0; } |
| 261 | if (ATB_1_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 1; goto found; } } else { n_free = 0; } |
| 262 | if (ATB_2_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 2; goto found; } } else { n_free = 0; } |
| 263 | if (ATB_3_IS_FREE(a)) { if (++n_free >= n_blocks) { i = i * BLOCKS_PER_ATB + 3; goto found; } } else { n_free = 0; } |
| 264 | } |
| 265 | |
| 266 | // nothing found! |
| 267 | if (collected) { |
| 268 | return NULL; |
| 269 | } |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame^] | 270 | 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] | 271 | gc_collect(); |
| 272 | collected = 1; |
| 273 | } |
| 274 | |
| 275 | // found, ending at block i inclusive |
| 276 | found: |
| 277 | // get starting and end blocks, both inclusive |
| 278 | end_block = i; |
| 279 | start_block = i - n_free + 1; |
| 280 | |
| 281 | // mark first block as used head |
| 282 | ATB_FREE_TO_HEAD(start_block); |
| 283 | |
| 284 | // mark rest of blocks as used tail |
| 285 | // TODO for a run of many blocks can make this more efficient |
| 286 | for (machine_uint_t bl = start_block + 1; bl <= end_block; bl++) { |
| 287 | ATB_FREE_TO_TAIL(bl); |
| 288 | } |
| 289 | |
| 290 | // return pointer to first block |
| 291 | return (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK); |
| 292 | } |
| 293 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 294 | // force the freeing of a piece of memory |
| 295 | void gc_free(void *ptr_in) { |
| 296 | machine_uint_t ptr = (machine_uint_t)ptr_in; |
| 297 | |
| 298 | if (VERIFY_PTR(ptr)) { |
| 299 | machine_uint_t block = BLOCK_FROM_PTR(ptr); |
| 300 | if (ATB_GET_KIND(block) == AT_HEAD) { |
| 301 | // free head and all of its tail blocks |
| 302 | do { |
| 303 | ATB_ANY_TO_FREE(block); |
| 304 | block += 1; |
| 305 | } while (ATB_GET_KIND(block) == AT_TAIL); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 310 | machine_uint_t gc_nbytes(void *ptr_in) { |
| 311 | machine_uint_t ptr = (machine_uint_t)ptr_in; |
| 312 | |
Damien | fd8b6bc | 2013-10-22 20:26:36 +0100 | [diff] [blame] | 313 | if (VERIFY_PTR(ptr)) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 314 | machine_uint_t block = BLOCK_FROM_PTR(ptr); |
| 315 | if (ATB_GET_KIND(block) == AT_HEAD) { |
| 316 | // work out number of consecutive blocks in the chain starting with this on |
| 317 | machine_uint_t n_blocks = 0; |
| 318 | do { |
| 319 | n_blocks += 1; |
| 320 | } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL); |
| 321 | return n_blocks * BYTES_PER_BLOCK; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // invalid pointer |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | void *gc_realloc(void *ptr, machine_uint_t n_bytes) { |
| 330 | machine_uint_t n_existing = gc_nbytes(ptr); |
| 331 | if (n_bytes <= n_existing) { |
| 332 | return ptr; |
| 333 | } else { |
Damien | d2c1a73 | 2013-10-23 21:03:27 +0100 | [diff] [blame] | 334 | // TODO check if we can grow inplace |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 335 | void *ptr2 = gc_alloc(n_bytes); |
Paul Sokolovsky | c0a8374 | 2014-02-11 15:28:37 +0200 | [diff] [blame] | 336 | if (ptr2 == NULL) { |
| 337 | return ptr2; |
| 338 | } |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 339 | memcpy(ptr2, ptr, n_existing); |
Damien | d2c1a73 | 2013-10-23 21:03:27 +0100 | [diff] [blame] | 340 | gc_free(ptr); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 341 | return ptr2; |
| 342 | } |
| 343 | } |
| 344 | |
Paul Sokolovsky | 723a6ed | 2014-02-11 18:01:38 +0200 | [diff] [blame^] | 345 | void gc_dump_info() { |
| 346 | gc_info_t info; |
| 347 | gc_info(&info); |
| 348 | printf("GC: total: " UINT_FMT ", used: " UINT_FMT ", free: " UINT_FMT "\n", info.total, info.used, info.free); |
| 349 | printf(" No. of 1-blocks: " UINT_FMT ", 2-blocks: " UINT_FMT ", max blk sz: " UINT_FMT "\n", |
| 350 | info.num_1block, info.num_2block, info.max_block); |
| 351 | } |
| 352 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 353 | #if DEBUG_PRINT |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 354 | static void gc_dump_at(void) { |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 355 | for (machine_uint_t bl = 0; bl < gc_alloc_table_byte_len * BLOCKS_PER_ATB; bl++) { |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 356 | printf("block %06u ", bl); |
Damien | eefcc79 | 2013-10-22 15:25:25 +0100 | [diff] [blame] | 357 | switch (ATB_GET_KIND(bl)) { |
| 358 | case AT_FREE: printf("FREE"); break; |
| 359 | case AT_HEAD: printf("HEAD"); break; |
| 360 | case AT_TAIL: printf("TAIL"); break; |
| 361 | default: printf("MARK"); break; |
| 362 | } |
| 363 | printf("\n"); |
| 364 | } |
| 365 | } |
| 366 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 367 | void gc_test(void) { |
| 368 | machine_uint_t len = 500; |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 369 | machine_uint_t *heap = malloc(len); |
| 370 | gc_init(heap, heap + len / sizeof(machine_uint_t)); |
| 371 | void *ptrs[100]; |
| 372 | { |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 373 | machine_uint_t **p = gc_alloc(16); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 374 | p[0] = gc_alloc(64); |
| 375 | p[1] = gc_alloc(1); |
| 376 | p[2] = gc_alloc(1); |
| 377 | p[3] = gc_alloc(1); |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 378 | machine_uint_t ***p2 = gc_alloc(16); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 379 | p2[0] = p; |
| 380 | p2[1] = p; |
| 381 | ptrs[0] = p2; |
| 382 | } |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 383 | for (int i = 0; i < 25; i+=2) { |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 384 | machine_uint_t *p = gc_alloc(i); |
| 385 | printf("p=%p\n", p); |
| 386 | if (i & 3) { |
| 387 | //ptrs[i] = p; |
| 388 | } |
| 389 | } |
| 390 | |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 391 | printf("Before GC:\n"); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 392 | gc_dump_at(); |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 393 | printf("Starting GC...\n"); |
| 394 | gc_collect_start(); |
| 395 | gc_collect_root(ptrs, sizeof(ptrs) / sizeof(void*)); |
| 396 | gc_collect_end(); |
| 397 | printf("After GC:\n"); |
Damien | dcced92 | 2013-10-21 23:45:08 +0100 | [diff] [blame] | 398 | gc_dump_at(); |
| 399 | } |
Paul Sokolovsky | af19cbd | 2014-02-10 21:45:54 +0200 | [diff] [blame] | 400 | #endif |
Damien George | d3ebe48 | 2014-01-07 15:20:33 +0000 | [diff] [blame] | 401 | |
| 402 | #endif // MICROPY_ENABLE_GC |