Skip to content

Commit 09b754f

Browse files
committed
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.
1 parent afa8b2e commit 09b754f

File tree

1 file changed

+4
-18
lines changed
  • ports/espressif/supervisor

1 file changed

+4
-18
lines changed

ports/espressif/supervisor/port.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,6 @@ static size_t spiram_size_usable(void) {
102102
}
103103
#endif
104104

105-
// Heap sizes for when there is no external RAM for CircuitPython to use
106-
// exclusively.
107-
#ifdef CONFIG_IDF_TARGET_ESP32
108-
// TODO: Determine better: 520kB of internal RAM; similar to 512kB for ESP32-S3.
109-
#define HEAP_SIZE (88 * 1024)
110-
#endif
111-
#ifdef CONFIG_IDF_TARGET_ESP32S2
112-
#define HEAP_SIZE (48 * 1024)
113-
#endif
114-
#ifdef CONFIG_IDF_TARGET_ESP32S3
115-
#define HEAP_SIZE (176 * 1024)
116-
#endif
117-
#ifdef CONFIG_IDF_TARGET_ESP32C3
118-
#define HEAP_SIZE (88 * 1024)
119-
#endif
120-
121105
uint32_t *heap;
122106
uint32_t heap_size;
123107

@@ -231,8 +215,10 @@ safe_mode_t port_init(void) {
231215
#endif
232216

233217
if (heap == NULL) {
234-
heap = malloc(HEAP_SIZE);
235-
heap_size = HEAP_SIZE / sizeof(uint32_t);
218+
size_t heap_total = heap_caps_get_total_size(MALLOC_CAP_8BIT);
219+
heap_size = MIN(heap_caps_get_largest_free_block(MALLOC_CAP_8BIT), heap_total / 2);
220+
heap = malloc(heap_size);
221+
heap_size = heap_size / sizeof(uint32_t);
236222
}
237223
if (heap == NULL) {
238224
heap_size = 0;

0 commit comments

Comments
 (0)