feat: migrate all large MCP server allocations and MP3 buffers to PSRAM (SPIRAM)

This commit is contained in:
Adolfo Reyna
2026-06-27 10:04:30 -04:00
parent 0ae9b43f87
commit e0a92ea50e
2 changed files with 26 additions and 9 deletions
@@ -9,6 +9,7 @@
#include <cJSON.h>
#include <esp_log.h>
#include <esp_http_server.h>
#include <esp_heap_caps.h>
#include <mbedtls/base64.h>
#include <string>
@@ -18,6 +19,14 @@
namespace tt::service::webserver {
static void* psram_malloc(size_t size) {
void* ptr = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (ptr != nullptr) {
return ptr;
}
return malloc(size);
}
static const auto LOGGER = Logger("McpHandler");
#define MCP_JSON_BODY_LIMIT (256 * 1024)
@@ -164,7 +173,7 @@ static uint8_t* base64_decode(const char* input, size_t* output_size) {
size_t input_size = strlen(payload);
size_t approx_output_len = (input_size * 3) / 4;
uint8_t* output = (uint8_t*)malloc(approx_output_len + 4);
uint8_t* output = (uint8_t*)psram_malloc(approx_output_len + 4);
if (output == NULL) {
return NULL;
}
@@ -622,7 +631,7 @@ esp_err_t WebServerService::handleApiMcp(httpd_req_t* request) {
return ESP_FAIL;
}
char* body = (char*)malloc(content_len + 1);
char* body = (char*)psram_malloc(content_len + 1);
if (body == NULL) {
httpd_resp_send_err(request, HTTPD_500_INTERNAL_SERVER_ERROR, "Out of memory");
return ESP_FAIL;
@@ -699,7 +708,7 @@ esp_err_t WebServerService::handleApiScreenRaw(httpd_req_t* request) {
return ESP_FAIL;
}
uint8_t* body = (uint8_t*)malloc(content_len);
uint8_t* body = (uint8_t*)psram_malloc(content_len);
if (body == NULL) {
httpd_resp_send_err(request, HTTPD_500_INTERNAL_SERVER_ERROR, "Out of memory");
return ESP_FAIL;