|
|
@@ -23,16 +23,25 @@ struct alignas(16) StbiSharedBlockHeader {
|
|
|
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 getSharedRegionSize(size_t* size) {
|
|
|
+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 (arenaSize == 0 || modelSize == 0 || arenaSize > SIZE_MAX - modelSize) return false;
|
|
|
+ if (modelSize == 0 || arenaSize > SIZE_MAX - modelSize) return false;
|
|
|
*size = arenaSize + modelSize;
|
|
|
return true;
|
|
|
}
|
|
|
@@ -46,8 +55,11 @@ static const char* sharedMemoryUserName(SharedMemoryUser user) {
|
|
|
}
|
|
|
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) {
|
|
|
@@ -67,16 +79,37 @@ bool reserve_psram_shared_region(void) {
|
|
|
const bool alreadyReserved = (sharedRegion != nullptr);
|
|
|
portEXIT_CRITICAL(&sharedMemoryMux);
|
|
|
if (alreadyReserved) return true;
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating shared PSRAM region (" + std::to_string(regionSize) + " bytes)...");
|
|
|
- uint8_t* candidate = static_cast<uint8_t*>(malloc_psram_heap("Shared PSRAM region", regionSize, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
|
- if (candidate == nullptr) {
|
|
|
+ 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; candidate = nullptr; }
|
|
|
+ if (sharedRegion == nullptr) {
|
|
|
+ sharedRegion = candidate;
|
|
|
+ sharedRegionAllocation = candidateAllocation;
|
|
|
+ candidateAllocation = nullptr;
|
|
|
+ }
|
|
|
portEXIT_CRITICAL(&sharedMemoryMux);
|
|
|
- if (candidate != nullptr) free_psram_heap("Unused duplicate shared PSRAM region", candidate);
|
|
|
+ if (candidateAllocation != nullptr) {
|
|
|
+ free_psram_heap("Unused duplicate shared PSRAM region", candidateAllocation);
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
@@ -166,9 +199,16 @@ void *psram_reallocate_shared_stbi_memory(void *ptr, size_t newsize) {
|
|
|
if (sharedPointer) {
|
|
|
const uintptr_t begin = reinterpret_cast<uintptr_t>(sharedRegion);
|
|
|
const uintptr_t address = reinterpret_cast<uintptr_t>(ptr);
|
|
|
- if ((address - begin) >= sizeof(StbiSharedBlockHeader)) {
|
|
|
- const StbiSharedBlockHeader* header = reinterpret_cast<const StbiSharedBlockHeader*>(static_cast<const uint8_t*>(ptr) - sizeof(StbiSharedBlockHeader));
|
|
|
- if (header->magic == STBI_SHARED_BLOCK_MAGIC) { oldSize = header->size; validSharedHeader = true; }
|
|
|
+ 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);
|
|
|
@@ -254,12 +294,18 @@ void *psram_get_shared_tensor_arena_memory(void) {
|
|
|
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 || (sharedMemoryUser != SharedMemoryUser::None && sharedMemoryUser != SharedMemoryUser::Digitization)) unavailable = true;
|
|
|
+ if (sharedRegion == nullptr || !tensorArenaClaimed ||
|
|
|
+ sharedMemoryUser != SharedMemoryUser::Digitization) unavailable = true;
|
|
|
else if (modelMemoryClaimed) duplicateClaim = true;
|
|
|
- else { sharedMemoryUser = SharedMemoryUser::Digitization; modelMemoryClaimed = true; result = sharedRegion + static_cast<size_t>(TENSOR_ARENA_SIZE); }
|
|
|
+ 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" :
|
|
|
@@ -324,3 +370,57 @@ void free_psram_heap(std::string name, void *ptr) {
|
|
|
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);
|
|
|
+}
|