espressif: Set heap size automatically, like micropython

rather than setting the heap size statically, micropython allocates
the biggest contiguous chunk possible, but in no event more than half the
total internal sram. On esp32 this gives 123728 bytes of `gc.mem_free`
in the repl.
This commit is contained in:
Jeff Epler 2022-07-13 14:00:59 -05:00
parent afa8b2ea72
commit 09b754ffa0
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE

View File

@ -102,22 +102,6 @@ static size_t spiram_size_usable(void) {
} }
#endif #endif
// Heap sizes for when there is no external RAM for CircuitPython to use
// exclusively.
#ifdef CONFIG_IDF_TARGET_ESP32
// TODO: Determine better: 520kB of internal RAM; similar to 512kB for ESP32-S3.
#define HEAP_SIZE (88 * 1024)
#endif
#ifdef CONFIG_IDF_TARGET_ESP32S2
#define HEAP_SIZE (48 * 1024)
#endif
#ifdef CONFIG_IDF_TARGET_ESP32S3
#define HEAP_SIZE (176 * 1024)
#endif
#ifdef CONFIG_IDF_TARGET_ESP32C3
#define HEAP_SIZE (88 * 1024)
#endif
uint32_t *heap; uint32_t *heap;
uint32_t heap_size; uint32_t heap_size;
@ -231,8 +215,10 @@ safe_mode_t port_init(void) {
#endif #endif
if (heap == NULL) { if (heap == NULL) {
heap = malloc(HEAP_SIZE); size_t heap_total = heap_caps_get_total_size(MALLOC_CAP_8BIT);
heap_size = HEAP_SIZE / sizeof(uint32_t); heap_size = MIN(heap_caps_get_largest_free_block(MALLOC_CAP_8BIT), heap_total / 2);
heap = malloc(heap_size);
heap_size = heap_size / sizeof(uint32_t);
} }
if (heap == NULL) { if (heap == NULL) {
heap_size = 0; heap_size = 0;