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
+14 -6
View File
@@ -28,6 +28,14 @@ namespace tt::mcp {
static const auto LOGGER = Logger("McpSystem");
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);
}
#define AUDIO_SAMPLE_RATE 16000
#define AUDIO_BITS_PER_SAMPLE 16
#define AUDIO_CHUNK_SAMPLES 512
@@ -342,7 +350,7 @@ std::string getScreenshotPbmBase64() {
size_t header_size = 32;
size_t payload_size = row_bytes * state.drawHeight;
size_t pbm_size = header_size + payload_size;
uint8_t* pbm = (uint8_t*)malloc(pbm_size);
uint8_t* pbm = (uint8_t*)psram_malloc(pbm_size);
if (pbm == NULL) {
return "";
}
@@ -921,8 +929,8 @@ bool playMp3Memory(const uint8_t* data, size_t size, int volume, std::string& er
bool success = false;
bool decoded_audio = false;
mp3dec_t* decoder = (mp3dec_t*)malloc(sizeof(mp3dec_t));
mp3d_sample_t* pcm = (mp3d_sample_t*)malloc(
mp3dec_t* decoder = (mp3dec_t*)psram_malloc(sizeof(mp3dec_t));
mp3d_sample_t* pcm = (mp3d_sample_t*)psram_malloc(
MINIMP3_MAX_SAMPLES_PER_FRAME * sizeof(mp3d_sample_t)
);
int configured_rate = 0;
@@ -1000,14 +1008,14 @@ bool playMp3File(const std::string& filename, int volume, std::string& error) {
}
{
uint8_t* input = (uint8_t*)malloc(MP3_INPUT_BUFFER_SIZE);
uint8_t* input = (uint8_t*)psram_malloc(MP3_INPUT_BUFFER_SIZE);
if (input == NULL) {
set_error(error, "Out of memory for MP3 decoding");
goto done;
}
mp3dec_t* decoder = (mp3dec_t*)malloc(sizeof(mp3dec_t));
mp3d_sample_t* pcm = (mp3d_sample_t*)malloc(
mp3dec_t* decoder = (mp3dec_t*)psram_malloc(sizeof(mp3dec_t));
mp3d_sample_t* pcm = (mp3d_sample_t*)psram_malloc(
MINIMP3_MAX_SAMPLES_PER_FRAME * sizeof(mp3d_sample_t)
);
if (decoder == NULL || pcm == NULL) {
@@ -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;