diff --git a/ports/unix/main.c b/ports/unix/main.c index dc72d79127c..caaf020ccbb 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -72,6 +72,33 @@ long heap_size = 1024 * 1024 * (sizeof(mp_uint_t) / 4); #define MICROPY_GC_SPLIT_HEAP_N_HEAPS (1) #endif +// CIRCUITPY-CHANGE: let the collector add and free heaps, as it does on the boards +#if MICROPY_GC_SPLIT_HEAP_AUTO +// The heap starts with one of the MICROPY_GC_SPLIT_HEAP_N_HEAPS parts of heap_size and the +// collector adds the others when it needs them, so the total stays the same. +static size_t heaps_added; + +size_t gc_get_max_new_split(void) { + if (heaps_added < MICROPY_GC_SPLIT_HEAP_N_HEAPS - 1) { + return heap_size / MICROPY_GC_SPLIT_HEAP_N_HEAPS; + } + return 0; +} + +void *unix_alloc_heap(size_t size) { + void *heap = malloc(size); + if (heap != NULL) { + heaps_added++; + } + return heap; +} + +void unix_free_heap(void *heap) { + heaps_added--; + free(heap); +} +#endif + #if !MICROPY_PY_SYS_PATH #error "The unix port requires MICROPY_PY_SYS_PATH=1" #endif @@ -488,6 +515,13 @@ MP_NOINLINE int main_(int argc, char **argv) { char *heaps[MICROPY_GC_SPLIT_HEAP_N_HEAPS]; long multi_heap_size = heap_size / MICROPY_GC_SPLIT_HEAP_N_HEAPS; for (size_t i = 0; i < MICROPY_GC_SPLIT_HEAP_N_HEAPS; i++) { + // CIRCUITPY-CHANGE: with MICROPY_GC_SPLIT_HEAP_AUTO the collector adds the other heaps itself + #if MICROPY_GC_SPLIT_HEAP_AUTO + if (i > 0) { + heaps[i] = NULL; + continue; + } + #endif heaps[i] = malloc(multi_heap_size); if (i == 0) { gc_init(heaps[i], heaps[i] + multi_heap_size); diff --git a/ports/unix/variants/coverage/mpconfigvariant.h b/ports/unix/variants/coverage/mpconfigvariant.h index 03f77ec14c6..7695d1b91c5 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.h +++ b/ports/unix/variants/coverage/mpconfigvariant.h @@ -36,6 +36,13 @@ // Enable testing of split heap. #define MICROPY_GC_SPLIT_HEAP (1) #define MICROPY_GC_SPLIT_HEAP_N_HEAPS (4) +// CIRCUITPY-CHANGE: the collector adds and frees heaps, as it does on the boards +#define MICROPY_GC_SPLIT_HEAP_AUTO (1) +#include +void *unix_alloc_heap(size_t size); +void unix_free_heap(void *heap); +#define MP_PLAT_ALLOC_HEAP(size) unix_alloc_heap(size) +#define MP_PLAT_FREE_HEAP(ptr) unix_free_heap(ptr) // Enable additional features. #define MICROPY_DEBUG_PARSE_RULE_NAME (1) diff --git a/py/gc.c b/py/gc.c index 7f741c848b5..deabd6c6e35 100644 --- a/py/gc.c +++ b/py/gc.c @@ -507,7 +507,7 @@ static bool gc_try_add_heap(size_t failed_alloc) { return false; } - gc_add(new_heap, (void *)new_heap + to_alloc); + gc_add(new_heap, (byte *)new_heap + to_alloc); return true; } @@ -878,6 +878,8 @@ static void gc_sweep_free_blocks(void) { for (mp_state_mem_area_t *area = &MP_STATE_MEM(area); area != NULL; area = NEXT_AREA(area)) { size_t last_used_block = 0; + // CIRCUITPY-CHANGE: last_used_block is also 0 when only the first block is in use + bool any_used = false; assert(area->gc_last_used_block <= area->gc_alloc_table_byte_len * BLOCKS_PER_ATB); for (size_t block = 0; block <= area->gc_last_used_block; block++) { @@ -900,6 +902,7 @@ static void gc_sweep_free_blocks(void) { #endif } else { last_used_block = block; + any_used = true; } break; @@ -907,6 +910,7 @@ static void gc_sweep_free_blocks(void) { ATB_MARK_TO_HEAD(area, block); free_tail = 0; last_used_block = block; + any_used = true; break; } } @@ -915,13 +919,15 @@ static void gc_sweep_free_blocks(void) { #if MICROPY_GC_SPLIT_HEAP_AUTO // Free any empty area, aside from the first one - if (last_used_block == 0 && prev_area != NULL) { + if (!any_used && prev_area != NULL) { DEBUG_printf("gc_sweep_free_blocks free empty area %p\n", area); NEXT_AREA(prev_area) = NEXT_AREA(area); MP_PLAT_FREE_HEAP(area); area = prev_area; } prev_area = area; + #else + (void)any_used; #endif } } diff --git a/tests/cmdline/cmd_parsetree.py.exp b/tests/cmdline/cmd_parsetree.py.exp index 672c212a965..0673f217312 100644 --- a/tests/cmdline/cmd_parsetree.py.exp +++ b/tests/cmdline/cmd_parsetree.py.exp @@ -87,5 +87,5 @@ arg names: 48 RETURN_VALUE mem: total=\\d\+, current=\\d\+, peak=\\d\+ stack: \\d\+ out of \\d\+ -GC: total: \\d\+, used: \\d\+, free: \\d\+ +GC: total: \\d\+, used: \\d\+, free: \\d\+, max new split: \\d\+ No. of 1-blocks: \\d\+, 2-blocks: \\d\+, max blk sz: \\d\+, max free sz: \\d\+ diff --git a/tests/cmdline/cmd_showbc.py.exp b/tests/cmdline/cmd_showbc.py.exp index b839de6d1ed..ba9cc0a8e0a 100644 --- a/tests/cmdline/cmd_showbc.py.exp +++ b/tests/cmdline/cmd_showbc.py.exp @@ -643,5 +643,5 @@ arg names: * b 04 RETURN_VALUE mem: total=\\d\+, current=\\d\+, peak=\\d\+ stack: \\d\+ out of \\d\+ -GC: total: \\d\+, used: \\d\+, free: \\d\+ +GC: total: \\d\+, used: \\d\+, free: \\d\+, max new split: \\d\+ No. of 1-blocks: \\d\+, 2-blocks: \\d\+, max blk sz: \\d\+, max free sz: \\d\+ diff --git a/tests/cmdline/cmd_showbc_const.py.exp b/tests/cmdline/cmd_showbc_const.py.exp index 6faeb941d7d..6c5c684ec3f 100644 --- a/tests/cmdline/cmd_showbc_const.py.exp +++ b/tests/cmdline/cmd_showbc_const.py.exp @@ -156,5 +156,5 @@ Kept Kept mem: total=\\d\+, current=\\d\+, peak=\\d\+ stack: \\d\+ out of \\d\+ -GC: total: \\d\+, used: \\d\+, free: \\d\+ +GC: total: \\d\+, used: \\d\+, free: \\d\+, max new split: \\d\+ No. of 1-blocks: \\d\+, 2-blocks: \\d\+, max blk sz: \\d\+, max free sz: \\d\+ diff --git a/tests/cmdline/cmd_showbc_opt.py.exp b/tests/cmdline/cmd_showbc_opt.py.exp index c3047275a9c..9fa0e6f7173 100644 --- a/tests/cmdline/cmd_showbc_opt.py.exp +++ b/tests/cmdline/cmd_showbc_opt.py.exp @@ -118,5 +118,5 @@ arg names: x 14 RETURN_VALUE mem: total=\\d\+, current=\\d\+, peak=\\d\+ stack: \\d\+ out of \\d\+ -GC: total: \\d\+, used: \\d\+, free: \\d\+ +GC: total: \\d\+, used: \\d\+, free: \\d\+, max new split: \\d\+ No. of 1-blocks: \\d\+, 2-blocks: \\d\+, max blk sz: \\d\+, max free sz: \\d\+ diff --git a/tests/cmdline/cmd_verbose.py.exp b/tests/cmdline/cmd_verbose.py.exp index 1982f313cb7..12f93373d30 100644 --- a/tests/cmdline/cmd_verbose.py.exp +++ b/tests/cmdline/cmd_verbose.py.exp @@ -15,5 +15,5 @@ arg names: 1 mem: total=\\d\+, current=\\d\+, peak=\\d\+ stack: \\d\+ out of \\d\+ -GC: total: \\d\+, used: \\d\+, free: \\d\+ +GC: total: \\d\+, used: \\d\+, free: \\d\+, max new split: \\d\+ No. of 1-blocks: \\d\+, 2-blocks: \\d\+, max blk sz: \\d\+, max free sz: \\d\+ diff --git a/tests/cmdline/gc_split_heap_auto.py b/tests/cmdline/gc_split_heap_auto.py new file mode 100644 index 00000000000..df0469fdf8d --- /dev/null +++ b/tests/cmdline/gc_split_heap_auto.py @@ -0,0 +1,13 @@ +# cmdline: -X heapsize=128K +# A heap area added on demand must be kept while its first block is the only one in use. +import gc + +store = [None] * 2000 +for k in range(len(store)): + store[k] = (k,) + gc.collect() + if k and store[k - 1][0] != k - 1: + print("tuple", k - 1, "now reads", store[k - 1]) + break +else: + print("no corruption") diff --git a/tests/cmdline/gc_split_heap_auto.py.exp b/tests/cmdline/gc_split_heap_auto.py.exp new file mode 100644 index 00000000000..c7ae1da706b --- /dev/null +++ b/tests/cmdline/gc_split_heap_auto.py.exp @@ -0,0 +1 @@ +no corruption diff --git a/tests/micropython/meminfo.py.exp b/tests/micropython/meminfo.py.exp index fb9eaa890aa..1b3efcb4aa6 100644 --- a/tests/micropython/meminfo.py.exp +++ b/tests/micropython/meminfo.py.exp @@ -1,10 +1,10 @@ mem: total=\\d\+, current=\\d\+, peak=\\d\+ stack: \\d\+ out of \\d\+ -GC: total: \\d\+, used: \\d\+, free: \\d\+ +GC: total: \\d\+, used: \\d\+, free: \\d\+, max new split: \\d\+ No. of 1-blocks: \\d\+, 2-blocks: \\d\+, max blk sz: \\d\+, max free sz: \\d\+ mem: total=\\d\+, current=\\d\+, peak=\\d\+ stack: \\d\+ out of \\d\+ -GC: total: \\d\+, used: \\d\+, free: \\d\+ +GC: total: \\d\+, used: \\d\+, free: \\d\+, max new split: \\d\+ No. of 1-blocks: \\d\+, 2-blocks: \\d\+, max blk sz: \\d\+, max free sz: \\d\+ GC memory layout; from 0x\[0-9a-f\]\+: ########