Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions ports/unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions ports/unix/variants/coverage/mpconfigvariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stddef.h>
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)
Expand Down
10 changes: 8 additions & 2 deletions py/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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++) {
Expand All @@ -900,13 +902,15 @@ static void gc_sweep_free_blocks(void) {
#endif
} else {
last_used_block = block;
any_used = true;
}
break;

case AT_MARK:
ATB_MARK_TO_HEAD(area, block);
free_tail = 0;
last_used_block = block;
any_used = true;
break;
}
}
Expand All @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cmdline/cmd_parsetree.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -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\+
2 changes: 1 addition & 1 deletion tests/cmdline/cmd_showbc.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -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\+
2 changes: 1 addition & 1 deletion tests/cmdline/cmd_showbc_const.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -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\+
2 changes: 1 addition & 1 deletion tests/cmdline/cmd_showbc_opt.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -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\+
2 changes: 1 addition & 1 deletion tests/cmdline/cmd_verbose.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -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\+
13 changes: 13 additions & 0 deletions tests/cmdline/gc_split_heap_auto.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions tests/cmdline/gc_split_heap_auto.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
no corruption
4 changes: 2 additions & 2 deletions tests/micropython/meminfo.py.exp
Original file line number Diff line number Diff line change
@@ -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\]\+:
########
Expand Down
Loading