psram-aware malloc functions (backport of upstream #4895) (#342)

Introduces new memory allocation functions, based on wled#4895 by @DedeHai

* keep a minimum of 15KB RAM available to UI - improves stability
* S3/S2/C3 automatically use "fast RTC Ram" for small data (reduces fragmentation)
* auto-detects PSRAM (or no PSRAM) when firmware was built with -D BOARD_HAS_PSRAM
* d_malloc() and d_calloc() prefer DRAM if possible (faster), but fall back to PSRAM when free RAM gets low.
This commit is contained in:
Frank Möhle
2026-02-18 17:57:47 +01:00
committed by GitHub
parent 50bb4d2910
commit cce3c0b5d3
19 changed files with 696 additions and 191 deletions

View File

@@ -2562,14 +2562,24 @@ public:
//open programFile
char * programText = nullptr;
uint16_t programFileSize;
size_t programFileSize;
#if ARTI_PLATFORM == ARTI_ARDUINO
programFileSize = programFile.size();
programText = (char *)malloc(programFileSize+1);
programText = (char *)d_malloc(programFileSize+1);
if (programText == nullptr) {
ERROR_ARTI("ARTI-FX: Failed to allocate memory for program file (%u bytes)\n", programFileSize+1);
programFile.close();
return false;
}
programFile.read((byte *)programText, programFileSize);
programText[programFileSize] = '\0';
#else
programText = (char *)malloc(programTextSize);
programText = (char *)malloc(programTextSize+1);
if (programText == nullptr) {
ERROR_ARTI("ARTI-FX: Failed to allocate memory for program file (%u bytes)\n", programTextSize);
programFile.close();
return false;
}
programFile.read(programText, programTextSize);
DEBUG_ARTI("programFile size %lu bytes\n", programFile.gcount());
programText[programFile.gcount()] = '\0';
@@ -2607,7 +2617,13 @@ public:
#endif
if (stages < 1) {
if (nullptr != programText) free(programText); // softhack007 prevent memory leak
// softhack007 prevent memory leak
#if ARTI_PLATFORM == ARTI_ARDUINO
if (nullptr != programText) d_free(programText);
#else
if (nullptr != programText) free(programText);
#endif
programText = nullptr;
close();
return true;
}
@@ -2666,8 +2682,11 @@ public:
#endif
}
#if ARTI_PLATFORM == ARTI_ARDUINO //not on windows as cause crash???
d_free(programText);
#else
free(programText);
#endif
programText = nullptr;
if (stages >= 3)
{