| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- #include "ClassLogFile.h"
- #include "../../include/defines.h"
- #include "psram.h"
- #include <cstring>
- #include <stdint.h>
- #include <stdio.h>
- #include <limits.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/portmacro.h"
- static const char* TAG = "PSRAM";
- namespace {
- enum class SharedMemoryUser : uint8_t { None, TakeImage, Aligning, Digitization };
- static const uint32_t STBI_SHARED_BLOCK_MAGIC = 0x53544249;
- static const size_t STBI_ALIGNMENT = 16;
- struct alignas(16) StbiSharedBlockHeader {
- size_t size;
- uint32_t magic;
- uint8_t reserved[STBI_ALIGNMENT - sizeof(size_t) - sizeof(uint32_t)];
- };
- static_assert(sizeof(StbiSharedBlockHeader) == STBI_ALIGNMENT, "STBI shared allocation header must preserve alignment");
- static portMUX_TYPE sharedMemoryMux = portMUX_INITIALIZER_UNLOCKED;
- static uint8_t* sharedRegion = nullptr;
- static uint8_t* sharedRegionAllocation = nullptr;
- static size_t allocatedBytesForSTBI = 0;
- static SharedMemoryUser sharedMemoryUser = SharedMemoryUser::None;
- static bool tensorArenaClaimed = false;
- static bool modelMemoryClaimed = false;
- static bool getAlignedTensorArenaSize(size_t* size) {
- if (size == nullptr) return false;
- const size_t arenaSize = static_cast<size_t>(TENSOR_ARENA_SIZE);
- if (arenaSize == 0 || arenaSize > SIZE_MAX - (STBI_ALIGNMENT - 1)) return false;
- *size = (arenaSize + STBI_ALIGNMENT - 1) & ~(STBI_ALIGNMENT - 1);
- return true;
- }
- static bool getSharedRegionSize(size_t* size) {
- if (size == nullptr) return false;
- size_t arenaSize = 0;
- if (!getAlignedTensorArenaSize(&arenaSize)) return false;
- const size_t modelSize = static_cast<size_t>(MAX_MODEL_SIZE);
- if (modelSize == 0 || arenaSize > SIZE_MAX - modelSize) return false;
- *size = arenaSize + modelSize;
- return true;
- }
- static const char* sharedMemoryUserName(SharedMemoryUser user) {
- switch (user) {
- case SharedMemoryUser::TakeImage: return "TakeImage";
- case SharedMemoryUser::Aligning: return "Aligning";
- case SharedMemoryUser::Digitization: return "Digitization";
- default: return "";
- }
- }
- static bool isInsideSharedRegionLocked(const void* ptr, size_t regionSize) {
- if (ptr == nullptr || sharedRegion == nullptr) return false;
- if (sharedRegionAllocation == nullptr) return false;
- const uintptr_t allocationBegin = reinterpret_cast<uintptr_t>(sharedRegionAllocation);
- const uintptr_t begin = reinterpret_cast<uintptr_t>(sharedRegion);
- const uintptr_t address = reinterpret_cast<uintptr_t>(ptr);
- if (begin < allocationBegin || begin - allocationBegin >= STBI_ALIGNMENT) return false;
- return address >= begin && (address - begin) < regionSize;
- }
- static bool alignStbiOffset(size_t value, size_t* aligned) {
- if (aligned == nullptr || value > SIZE_MAX - (STBI_ALIGNMENT - 1)) return false;
- *aligned = (value + STBI_ALIGNMENT - 1) & ~(STBI_ALIGNMENT - 1);
- return true;
- }
- } // namespace
- bool reserve_psram_shared_region(void) {
- size_t regionSize = 0;
- if (!getSharedRegionSize(®ionSize)) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Invalid shared PSRAM region size");
- return false;
- }
- portENTER_CRITICAL(&sharedMemoryMux);
- const bool alreadyReserved = (sharedRegion != nullptr);
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (alreadyReserved) return true;
- if (regionSize > SIZE_MAX - (STBI_ALIGNMENT - 1)) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Shared PSRAM region size overflows alignment padding");
- return false;
- }
- const size_t allocationSize = regionSize + STBI_ALIGNMENT - 1;
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating shared PSRAM region (" + std::to_string(regionSize) + " usable bytes)...");
- uint8_t* candidateAllocation = static_cast<uint8_t*>(malloc_psram_heap(
- "Shared PSRAM region", allocationSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
- if (candidateAllocation == nullptr) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocate shared PSRAM region!");
- return false;
- }
- const uintptr_t rawAddress = reinterpret_cast<uintptr_t>(candidateAllocation);
- if (rawAddress > UINTPTR_MAX - (STBI_ALIGNMENT - 1)) {
- free_psram_heap("Invalid shared PSRAM region", candidateAllocation);
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Shared PSRAM region address overflows alignment");
- return false;
- }
- const uintptr_t alignedAddress = (rawAddress + STBI_ALIGNMENT - 1) &
- ~static_cast<uintptr_t>(STBI_ALIGNMENT - 1);
- uint8_t* candidate = reinterpret_cast<uint8_t*>(alignedAddress);
- portENTER_CRITICAL(&sharedMemoryMux);
- if (sharedRegion == nullptr) {
- sharedRegion = candidate;
- sharedRegionAllocation = candidateAllocation;
- candidateAllocation = nullptr;
- }
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (candidateAllocation != nullptr) {
- free_psram_heap("Unused duplicate shared PSRAM region", candidateAllocation);
- }
- return true;
- }
- /*******************************************************************
- * Memory used in Take Image (STBI)
- *******************************************************************/
- bool psram_init_shared_memory_for_take_image_step(void) {
- SharedMemoryUser currentUser;
- bool success = false;
- portENTER_CRITICAL(&sharedMemoryMux);
- currentUser = sharedMemoryUser;
- if (sharedRegion != nullptr && sharedMemoryUser == SharedMemoryUser::None) {
- allocatedBytesForSTBI = 0;
- sharedMemoryUser = SharedMemoryUser::TakeImage;
- success = true;
- }
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (!success) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, sharedRegion == nullptr ?
- "Shared PSRAM region is not initialized" :
- "Shared memory in PSRAM already in use for " + std::string(sharedMemoryUserName(currentUser)) + "!");
- return false;
- }
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init shared memory for step 'Take Image' (STBI buffers)");
- return true;
- }
- void psram_deinit_shared_memory_for_take_image_step(void) {
- bool released = false;
- portENTER_CRITICAL(&sharedMemoryMux);
- if (sharedMemoryUser == SharedMemoryUser::TakeImage) {
- allocatedBytesForSTBI = 0;
- sharedMemoryUser = SharedMemoryUser::None;
- released = true;
- }
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (released) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Deinit shared memory for step 'Take Image' (STBI buffers)");
- else LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Ignoring mismatched STBI shared-memory release");
- }
- void *psram_reserve_shared_stbi_memory(size_t size) {
- if (size == 0) return nullptr;
- if (size < 100000) return malloc_psram_heap("STBI", size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
- size_t regionSize = 0;
- if (!getSharedRegionSize(®ionSize)) {
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Shared PSRAM region is unavailable; allocating STBI buffer from PSRAM heap");
- return malloc_psram_heap("STBI fallback", size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
- }
- size_t userOffset = 0, endOffset = 0, available = 0;
- void* result = nullptr;
- SharedMemoryUser currentUser = SharedMemoryUser::None;
- portENTER_CRITICAL(&sharedMemoryMux);
- currentUser = sharedMemoryUser;
- if (sharedRegion != nullptr && sharedMemoryUser == SharedMemoryUser::TakeImage &&
- allocatedBytesForSTBI <= SIZE_MAX - sizeof(StbiSharedBlockHeader) &&
- alignStbiOffset(allocatedBytesForSTBI + sizeof(StbiSharedBlockHeader), &userOffset) &&
- userOffset <= regionSize && size <= regionSize - userOffset) {
- endOffset = userOffset + size;
- StbiSharedBlockHeader* header = reinterpret_cast<StbiSharedBlockHeader*>(sharedRegion + userOffset - sizeof(StbiSharedBlockHeader));
- header->size = size;
- header->magic = STBI_SHARED_BLOCK_MAGIC;
- std::memset(header->reserved, 0, sizeof(header->reserved));
- result = sharedRegion + userOffset;
- allocatedBytesForSTBI = endOffset;
- } else if (allocatedBytesForSTBI <= regionSize) available = regionSize - allocatedBytesForSTBI;
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (result != nullptr) {
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocated " + std::to_string(size) + " bytes in shared PSRAM for STBI");
- return result;
- }
- LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Shared STBI PSRAM unavailable for " + std::to_string(size) +
- " bytes (owner: " + sharedMemoryUserName(currentUser) + ", available: " + std::to_string(available) +
- " bytes); falling back to PSRAM heap");
- return malloc_psram_heap("STBI fallback", size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
- }
- void *psram_reallocate_shared_stbi_memory(void *ptr, size_t newsize) {
- if (ptr == nullptr) return psram_reserve_shared_stbi_memory(newsize);
- if (newsize == 0) { psram_free_shared_stbi_memory(ptr); return nullptr; }
- size_t regionSize = 0;
- if (!getSharedRegionSize(®ionSize))
- return realloc_psram_heap("STBI", ptr, newsize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
- size_t oldSize = 0;
- bool sharedPointer = false, validSharedHeader = false;
- portENTER_CRITICAL(&sharedMemoryMux);
- sharedPointer = isInsideSharedRegionLocked(ptr, regionSize);
- if (sharedPointer) {
- const uintptr_t begin = reinterpret_cast<uintptr_t>(sharedRegion);
- const uintptr_t address = reinterpret_cast<uintptr_t>(ptr);
- const size_t userOffset = static_cast<size_t>(address - begin);
- if (userOffset >= sizeof(StbiSharedBlockHeader) &&
- (userOffset & (STBI_ALIGNMENT - 1)) == 0) {
- const StbiSharedBlockHeader* header = reinterpret_cast<const StbiSharedBlockHeader*>(
- static_cast<const uint8_t*>(ptr) - sizeof(StbiSharedBlockHeader));
- if (header->magic == STBI_SHARED_BLOCK_MAGIC &&
- header->size > 0 && header->size <= regionSize - userOffset) {
- oldSize = header->size;
- validSharedHeader = true;
- }
- }
- }
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (!sharedPointer) return realloc_psram_heap("STBI", ptr, newsize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
- if (!validSharedHeader) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "STBI realloc received an unknown shared-region pointer");
- return nullptr;
- }
- void* replacement = psram_reserve_shared_stbi_memory(newsize);
- if (replacement == nullptr) return nullptr;
- std::memcpy(replacement, ptr, oldSize < newsize ? oldSize : newsize);
- return replacement;
- }
- void psram_free_shared_stbi_memory(void *ptr) {
- if (ptr == nullptr) return;
- size_t regionSize = 0;
- if (!getSharedRegionSize(®ionSize)) { free_psram_heap("STBI", ptr); return; }
- portENTER_CRITICAL(&sharedMemoryMux);
- const bool isShared = isInsideSharedRegionLocked(ptr, regionSize);
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (!isShared) free_psram_heap("STBI", ptr);
- }
- /*******************************************************************
- * Memory used in Aligning Step
- *******************************************************************/
- void *psram_reserve_shared_tmp_image_memory(void) {
- size_t regionSize = 0;
- if (!getSharedRegionSize(®ionSize) || static_cast<size_t>(IMAGE_SIZE) > regionSize) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "tmpImage does not fit in the reserved shared PSRAM region");
- return nullptr;
- }
- SharedMemoryUser currentUser;
- void* result = nullptr;
- portENTER_CRITICAL(&sharedMemoryMux);
- currentUser = sharedMemoryUser;
- if (sharedRegion != nullptr && sharedMemoryUser == SharedMemoryUser::None) {
- sharedMemoryUser = SharedMemoryUser::Aligning;
- result = sharedRegion;
- }
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (result == nullptr) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Shared memory in PSRAM unavailable for tmpImage (owner: " +
- std::string(sharedMemoryUserName(currentUser)) + ")");
- return nullptr;
- }
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating tmpImage (" + std::to_string(IMAGE_SIZE) + " bytes, use shared memory in PSRAM)...");
- return result;
- }
- void psram_free_shared_temp_image_memory(void) {
- bool released = false;
- portENTER_CRITICAL(&sharedMemoryMux);
- if (sharedMemoryUser == SharedMemoryUser::Aligning) { sharedMemoryUser = SharedMemoryUser::None; released = true; }
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (released) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Shared memory used for tmpImage is free again");
- else LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Ignoring mismatched tmpImage shared-memory release");
- }
- /*******************************************************************
- * Memory used in Digitization Steps
- *******************************************************************/
- void *psram_get_shared_tensor_arena_memory(void) {
- bool unavailable = false, duplicateClaim = false;
- void* result = nullptr;
- SharedMemoryUser currentUser;
- portENTER_CRITICAL(&sharedMemoryMux);
- currentUser = sharedMemoryUser;
- if (sharedRegion == nullptr || (sharedMemoryUser != SharedMemoryUser::None && sharedMemoryUser != SharedMemoryUser::Digitization)) unavailable = true;
- else if (tensorArenaClaimed) duplicateClaim = true;
- else { sharedMemoryUser = SharedMemoryUser::Digitization; tensorArenaClaimed = true; result = sharedRegion; }
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (result == nullptr) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, duplicateClaim ? "Tensor Arena shared memory was claimed more than once" :
- (unavailable ? "Shared PSRAM is unavailable for Tensor Arena (owner: " + std::string(sharedMemoryUserName(currentUser)) + ")" : "Tensor Arena allocation failed"));
- return nullptr;
- }
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating Tensor Arena (" + std::to_string(TENSOR_ARENA_SIZE) + " bytes in shared PSRAM)...");
- return result;
- }
- void *psram_get_shared_model_memory(void) {
- bool unavailable = false, duplicateClaim = false;
- void* result = nullptr;
- size_t modelOffset = 0;
- if (!getAlignedTensorArenaSize(&modelOffset)) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Invalid Tensor Arena size for model placement");
- return nullptr;
- }
- SharedMemoryUser currentUser;
- portENTER_CRITICAL(&sharedMemoryMux);
- currentUser = sharedMemoryUser;
- if (sharedRegion == nullptr || !tensorArenaClaimed ||
- sharedMemoryUser != SharedMemoryUser::Digitization) unavailable = true;
- else if (modelMemoryClaimed) duplicateClaim = true;
- else { sharedMemoryUser = SharedMemoryUser::Digitization; modelMemoryClaimed = true; result = sharedRegion + modelOffset; }
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (result == nullptr) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, duplicateClaim ? "Model shared memory was claimed more than once" :
- (unavailable ? "Shared PSRAM is unavailable for Model (owner: " + std::string(sharedMemoryUserName(currentUser)) + ")" : "Model allocation failed"));
- return nullptr;
- }
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating Model memory (" + std::to_string(MAX_MODEL_SIZE) + " bytes in shared PSRAM)...");
- return result;
- }
- void psram_free_shared_tensor_arena_and_model_memory(void) {
- bool released = false;
- portENTER_CRITICAL(&sharedMemoryMux);
- if (sharedMemoryUser == SharedMemoryUser::Digitization) {
- tensorArenaClaimed = false; modelMemoryClaimed = false;
- sharedMemoryUser = SharedMemoryUser::None; released = true;
- }
- portEXIT_CRITICAL(&sharedMemoryMux);
- if (released) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Shared memory used for Tensor Arena and model is free again");
- else LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Ignoring mismatched Tensor Arena/Model shared-memory release");
- }
- /*******************************************************************
- * General PSRAM heap wrappers
- *******************************************************************/
- void *malloc_psram_heap(std::string name, size_t size, uint32_t caps) {
- if (size == 0) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Refusing zero-byte PSRAM allocation for '" + name + "'");
- return nullptr;
- }
- void* ptr = heap_caps_malloc(size, caps | MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
- if (ptr != nullptr) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocated " + std::to_string(size) + " bytes in PSRAM for '" + name + "'");
- else LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocate " + std::to_string(size) + " bytes in PSRAM for '" + name + "'!");
- return ptr;
- }
- void *realloc_psram_heap(std::string name, void *ptr, size_t size, uint32_t caps) {
- void* resized = heap_caps_realloc(ptr, size, caps | MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
- if (resized != nullptr) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Reallocated " + std::to_string(size) + " bytes in PSRAM for '" + name + "'");
- else if (size != 0) LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to reallocate " + std::to_string(size) + " bytes in PSRAM for '" + name + "'!");
- return resized;
- }
- void *calloc_psram_heap(std::string name, size_t n, size_t size, uint32_t caps) {
- if (size != 0 && n > SIZE_MAX / size) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "PSRAM calloc size overflow for '" + name + "'");
- return nullptr;
- }
- const size_t totalSize = n * size;
- if (totalSize == 0) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Refusing zero-byte PSRAM calloc for '" + name + "'");
- return nullptr;
- }
- void* ptr = heap_caps_calloc(n, size, caps | MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
- if (ptr != nullptr) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocated and cleared " + std::to_string(totalSize) + " bytes in PSRAM for '" + name + "'");
- else LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocate " + std::to_string(totalSize) + " bytes in PSRAM for '" + name + "'!");
- return ptr;
- }
- void free_psram_heap(std::string name, void *ptr) {
- if (ptr == nullptr) return;
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Freeing memory in PSRAM used for '" + name + "'...");
- heap_caps_free(ptr);
- }
- /*******************************************************************
- * Internal SRAM heap wrappers
- *******************************************************************/
- void *malloc_internal_heap(std::string name, size_t size, uint32_t caps) {
- if (size == 0) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Refusing zero-byte internal allocation for '" + name + "'");
- return nullptr;
- }
- const uint32_t internalCaps = (caps | MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) & ~MALLOC_CAP_SPIRAM;
- void* ptr = heap_caps_malloc(size, internalCaps);
- if (ptr != nullptr) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocated " + std::to_string(size) + " bytes in internal SRAM for '" + name + "'");
- else LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocate " + std::to_string(size) + " bytes in internal SRAM for '" + name + "'");
- return ptr;
- }
- void *realloc_internal_heap(std::string name, void *ptr, size_t size, uint32_t caps) {
- if (size == 0) {
- if (ptr != nullptr) {
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Freeing internal SRAM used for '" + name + "'...");
- heap_caps_free(ptr);
- }
- return nullptr;
- }
- if (ptr == nullptr) return malloc_internal_heap(name, size, caps);
- const uint32_t internalCaps = (caps | MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) & ~MALLOC_CAP_SPIRAM;
- void* resized = heap_caps_realloc(ptr, size, internalCaps);
- if (resized != nullptr) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Reallocated " + std::to_string(size) + " bytes in internal SRAM for '" + name + "'");
- else LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to reallocate " + std::to_string(size) + " bytes in internal SRAM for '" + name + "'");
- return resized;
- }
- void *calloc_internal_heap(std::string name, size_t n, size_t size, uint32_t caps) {
- if (size != 0 && n > SIZE_MAX / size) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Internal calloc size overflow for '" + name + "'");
- return nullptr;
- }
- if (n == 0 || size == 0) {
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Refusing zero-byte internal calloc for '" + name + "'");
- return nullptr;
- }
- const uint32_t internalCaps = (caps | MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) & ~MALLOC_CAP_SPIRAM;
- void* ptr = heap_caps_calloc(n, size, internalCaps);
- const size_t totalSize = n * size;
- if (ptr != nullptr) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocated and cleared " + std::to_string(totalSize) + " bytes in internal SRAM for '" + name + "'");
- else LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocate " + std::to_string(totalSize) + " bytes in internal SRAM for '" + name + "'");
- return ptr;
- }
- void free_internal_heap(std::string name, void *ptr) {
- if (ptr == nullptr) return;
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Freeing internal SRAM used for '" + name + "'...");
- heap_caps_free(ptr);
- }
|