diff --git a/ports/unix/main.c b/ports/unix/main.c index e7048dd712..0b27a1f5f5 100644 --- a/ports/unix/main.c +++ b/ports/unix/main.c @@ -477,8 +477,22 @@ MP_NOINLINE int main_(int argc, char **argv) { pre_process_options(argc, argv); #if MICROPY_ENABLE_GC + #if !MICROPY_GC_SPLIT_HEAP char *heap = malloc(heap_size); gc_init(heap, heap + heap_size); + #else + assert(MICROPY_GC_SPLIT_HEAP_N_HEAPS > 0); + 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++) { + heaps[i] = malloc(multi_heap_size); + if (i == 0) { + gc_init(heaps[i], heaps[i] + multi_heap_size); + } else { + gc_add(heaps[i], heaps[i] + multi_heap_size); + } + } + #endif #endif #if MICROPY_ENABLE_PYSTACK @@ -729,7 +743,13 @@ MP_NOINLINE int main_(int argc, char **argv) { #if MICROPY_ENABLE_GC && !defined(NDEBUG) // We don't really need to free memory since we are about to exit the // process, but doing so helps to find memory leaks. + #if !MICROPY_GC_SPLIT_HEAP free(heap); + #else + for (size_t i = 0; i < MICROPY_GC_SPLIT_HEAP_N_HEAPS; i++) { + free(heaps[i]); + } + #endif #endif // printf("total bytes = %d\n", m_get_total_bytes_allocated()); diff --git a/ports/unix/mpconfigport.h b/ports/unix/mpconfigport.h index 312c0cce8f..4252a406ce 100644 --- a/ports/unix/mpconfigport.h +++ b/ports/unix/mpconfigport.h @@ -120,6 +120,10 @@ #define MICROPY_EMIT_ARM (1) #endif #define MICROPY_ENABLE_GC (1) +// Number of heaps to assign if MICROPY_GC_SPLIT_HEAP=1 +#ifndef MICROPY_GC_SPLIT_HEAP_N_HEAPS +#define MICROPY_GC_SPLIT_HEAP_N_HEAPS (1) +#endif #define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1) #define MICROPY_MEM_STATS (1) #define MICROPY_DEBUG_PRINTERS (1) diff --git a/ports/unix/variants/coverage/mpconfigvariant.h b/ports/unix/variants/coverage/mpconfigvariant.h index 30e3a4e5d2..fb70d791ac 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.h +++ b/ports/unix/variants/coverage/mpconfigvariant.h @@ -32,6 +32,8 @@ // Enable additional features. #define MICROPY_DEBUG_PARSE_RULE_NAME (1) +#define MICROPY_GC_SPLIT_HEAP (1) +#define MICROPY_GC_SPLIT_HEAP_N_HEAPS (4) #define MICROPY_TRACKED_ALLOC (1) #define MICROPY_FLOAT_HIGH_QUALITY_HASH (1) #define MICROPY_REPL_EMACS_WORDS_MOVE (1)