8000 unix: Option to evenly split GC heap size over multiple heaps. · micropython/micropython@691fca8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 691fca8

Browse files
committed
unix: Option to evenly split GC heap size over multiple heaps.
When multiple GC heaps are enabled for the unix port, the value of the C macro MICROPY_GC_N_HEAPS determines over how many heaps the requested heap size is divided. The coverage variant sets this to 4 to cover the `gc_add()` function and test the multiple heaps feature. The default is 1.
1 parent 7c80102 commit 691fca8

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

ports/unix/main.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,22 @@ MP_NOINLINE int main_(int argc, char **argv) {
464464
pre_process_options(argc, argv);
465465

466466
#if MICROPY_ENABLE_GC
467+
#if !MICROPY_GC_MULTIHEAP
467468
char *heap = malloc(heap_size);
468469
gc_init(heap, heap + heap_size);
470+
#else
471+
assert(MICROPY_GC_N_HEAPS > 0);
472+
char *heaps[MICROPY_GC_N_HEAPS];
473+
long multi_heap_size = heap_size / MICROPY_GC_N_HEAPS;
474+
for (size_t i = 0; i < MICROPY_GC_N_HEAPS; i++) {
475+
heaps[i] = malloc(multi_heap_size);
476+
if (i == 0) {
477+
gc_init(heaps[i], heaps[i] + multi_heap_size);
478+
} else {
479+
gc_add(heaps[i], heaps[i] + multi_heap_size);
480+
}
481+
}
482+
#endif
469483
#endif
470484

471485
#if MICROPY_ENABLE_PYSTACK
@@ -716,7 +730,13 @@ MP_NOINLINE int main_(int argc, char **argv) {
716730
#if MICROPY_ENABLE_GC && !defined(NDEBUG)
717731
// We don't really need to free memory since we are about to exit the
718732
// process, but doing so helps to find memory leaks.
733+
#if !MICROPY_GC_MULTIHEAP
719734
free(heap);
735+
#else
736+
for (size_t i = 0; i < MICROPY_GC_N_HEAPS; i++) {
737+
free(heaps[i]);
738+
}
739+
#endif
720740
#endif
721741

722742
// printf("total bytes = %d\n", m_get_total_bytes_allocated());

ports/unix/mpconfigport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@
120120
#define MICROPY_EMIT_ARM (1)
121121
#endif
122122
#define MICROPY_ENABLE_GC (1)
123+
// Number of heaps to assign if MICROPY_GC_MULTIHEAP=1
124+
#ifndef MICROPY_GC_N_HEAPS
125+
#define MICROPY_GC_N_HEAPS (1)
126+
#endif
123127
#define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1)
124128
#define MICROPY_MEM_STATS (1)
125129
#define MICROPY_DEBUG_PRINTERS (1)

ports/unix/variants/coverage/mpconfigvariant.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@
5656
#define MICROPY_PY_UCRYPTOLIB (1)
5757
#define MICROPY_PY_UCRYPTOLIB_CTR (1)
5858
#define MICROPY_PY_MICROPYTHON_HEAP_LOCKED (1)
59-
#define MICROPY_GC_MULTIHEAP (1)
59+
#define MICROPY_GC_MULTIHEAP (1)
60+
#define MICROPY_GC_N_HEAPS (4)

py/gc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <stddef.h>
3131

3232
void gc_init(void *start, void *end);
33+
34+
// Used to add additional heaps when multiple heaps are enabled.
3335
void gc_add(void *start, void *end);
3436

3537
// These lock/unlock functions can be nested.

0 commit comments

Comments
 (0)
0