8000 esp32/main: Allocate at most 1/2 of available IDF heap for MP heap. · micropython/micropython@23b1a4e · GitHub
[go: up one dir, main page]

Skip to content

Commit 23b1a4e

Browse files
committed
esp32/main: Allocate at most 1/2 of available IDF heap for MP heap.
So that there is some memory left for the OS, eg for ssl buffers. See issue #7214. Signed-off-by: Damien George <damien@micropython.org>
1 parent 648656d commit 23b1a4e

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

ports/esp32/main.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ void mp_task(void *pvParameter) {
9797
#endif
9898
machine_init();
9999

100-
// TODO: CONFIG_SPIRAM_SUPPORT is for 3.3 compatibility, remove after move to 4.0.
101-
#if CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_SPIRAM_SUPPORT
102-
// Try to use the entire external SPIRAM directly for the heap
103100
size_t mp_task_heap_size;
104-
void *mp_task_heap = (void *)SOC_EXTRAM_DATA_LOW;
101+
void *mp_task_heap = NULL;
102+
103+
#if CONFIG_ESP32_SPIRAM_SUPPORT
104+
// Try to use the entire external SPIRAM directly for the heap
105+
mp_task_heap = (void *)SOC_EXTRAM_DATA_LOW;
105106
switch (esp_spiram_get_chip_size()) {
106107
case ESP_SPIRAM_SIZE_16MBITS:
107108
mp_task_heap_size = 2 * 1024 * 1024;
@@ -112,28 +113,28 @@ void mp_task(void *pvParameter) {
112113
break;
113114
default:
114115
// No SPIRAM, fallback to normal allocation
115-
mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
116-
mp_task_heap = malloc(mp_task_heap_size);
116+
mp_task_heap = NULL;
117117
break;
118118
}
119119
#elif CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT
120120
// Try to use the entire external SPIRAM directly for the heap
121-
size_t mp_task_heap_size;
122121
size_t esp_spiram_size = esp_spiram_get_size();
123-
void *mp_task_heap = (void *)SOC_EXTRAM_DATA_HIGH - esp_spiram_size;
124122
if (esp_spiram_size > 0) {
123+
mp_task_heap = (void *)SOC_EXTRAM_DATA_HIGH - esp_spiram_size;
125124
mp_task_heap_size = esp_spiram_size;
126-
} else {
127-
// No SPIRAM, fallback to normal allocation
128-
mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
129-
mp_task_heap = malloc(mp_task_heap_size);
130125
}
131-
#else
132-
// Allocate the uPy heap using malloc and get the largest available region
133-
size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
134-
void *mp_task_heap = malloc(mp_task_heap_size);
135126
#endif
136127

128+
if (mp_task_heap == NULL) {
129+
// Allocate the uPy heap using malloc and get the largest available region,
130+
// limiting to 1/2 total available memory to leave memory for the OS.
131+
mp_task_heap_size = MIN(
132+
heap_caps_get_largest_free_block(MALLOC_CAP_8BIT),
133+
heap_caps_get_total_size(MALLOC_CAP_8BIT) / 2
134+
);
135+
mp_task_heap = malloc(mp_task_heap_size);
136+
}
137+
137138
soft_reset:
138139
// initialise the stack pointer for the main thread
139140
mp_stack_set_top((void *)sp);

0 commit comments

Comments
 (0)
0