|
@@ -2,210 +2,321 @@
|
|
|
#include "../../include/defines.h"
|
|
#include "../../include/defines.h"
|
|
|
#include "psram.h"
|
|
#include "psram.h"
|
|
|
|
|
|
|
|
-static const char* TAG = "PSRAM";
|
|
|
|
|
-
|
|
|
|
|
-using namespace std;
|
|
|
|
|
|
|
+#include <cstring>
|
|
|
|
|
+#include <stdint.h>
|
|
|
|
|
+#include <stdio.h>
|
|
|
|
|
+#include <limits.h>
|
|
|
|
|
+#include "freertos/FreeRTOS.h"
|
|
|
|
|
+#include "freertos/portmacro.h"
|
|
|
|
|
|
|
|
|
|
+static const char* TAG = "PSRAM";
|
|
|
|
|
|
|
|
-void *shared_region = NULL;
|
|
|
|
|
-uint32_t allocatedBytesForSTBI = 0;
|
|
|
|
|
-std::string sharedMemoryInUseFor = "";
|
|
|
|
|
-
|
|
|
|
|
|
|
+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 size_t allocatedBytesForSTBI = 0;
|
|
|
|
|
+static SharedMemoryUser sharedMemoryUser = SharedMemoryUser::None;
|
|
|
|
|
+static bool tensorArenaClaimed = false;
|
|
|
|
|
+static bool modelMemoryClaimed = false;
|
|
|
|
|
+
|
|
|
|
|
+static bool getSharedRegionSize(size_t* size) {
|
|
|
|
|
+ if (size == nullptr) return false;
|
|
|
|
|
+ const size_t arenaSize = static_cast<size_t>(TENSOR_ARENA_SIZE);
|
|
|
|
|
+ const size_t modelSize = static_cast<size_t>(MAX_MODEL_SIZE);
|
|
|
|
|
+ if (arenaSize == 0 || 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;
|
|
|
|
|
+ const uintptr_t begin = reinterpret_cast<uintptr_t>(sharedRegion);
|
|
|
|
|
+ const uintptr_t address = reinterpret_cast<uintptr_t>(ptr);
|
|
|
|
|
+ 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
|
|
|
|
|
|
|
|
-/** Reserve a large block in the PSRAM which will be shared between the different steps.
|
|
|
|
|
- * Each step uses it differently but only wiuthin itself. */
|
|
|
|
|
bool reserve_psram_shared_region(void) {
|
|
bool reserve_psram_shared_region(void) {
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating shared PSRAM region (" +
|
|
|
|
|
- std::to_string(TENSOR_ARENA_SIZE + MAX_MODEL_SIZE) + " bytes)...");
|
|
|
|
|
- shared_region = malloc_psram_heap("Shared PSRAM region", TENSOR_ARENA_SIZE + MAX_MODEL_SIZE,
|
|
|
|
|
- MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
|
|
|
-
|
|
|
|
|
- if (shared_region == NULL) {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocating shared PSRAM region!");
|
|
|
|
|
|
|
+ size_t regionSize = 0;
|
|
|
|
|
+ if (!getSharedRegionSize(®ionSize)) {
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Invalid shared PSRAM region size");
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
- else {
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ portENTER_CRITICAL(&sharedMemoryMux);
|
|
|
|
|
+ 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) {
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocate shared PSRAM region!");
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
|
|
+ portENTER_CRITICAL(&sharedMemoryMux);
|
|
|
|
|
+ if (sharedRegion == nullptr) { sharedRegion = candidate; candidate = nullptr; }
|
|
|
|
|
+ portEXIT_CRITICAL(&sharedMemoryMux);
|
|
|
|
|
+ if (candidate != nullptr) free_psram_heap("Unused duplicate shared PSRAM region", candidate);
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
/*******************************************************************
|
|
/*******************************************************************
|
|
|
* Memory used in Take Image (STBI)
|
|
* Memory used in Take Image (STBI)
|
|
|
*******************************************************************/
|
|
*******************************************************************/
|
|
|
bool psram_init_shared_memory_for_take_image_step(void) {
|
|
bool psram_init_shared_memory_for_take_image_step(void) {
|
|
|
- if (sharedMemoryInUseFor != "") {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Shared memory in PSRAM already in use for " + sharedMemoryInUseFor + "!");
|
|
|
|
|
|
|
+ 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;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init shared memory for step 'Take Image' (STBI buffers)");
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init shared memory for step 'Take Image' (STBI buffers)");
|
|
|
- allocatedBytesForSTBI = 0;
|
|
|
|
|
- sharedMemoryInUseFor = "TakeImage";
|
|
|
|
|
-
|
|
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
void psram_deinit_shared_memory_for_take_image_step(void) {
|
|
void psram_deinit_shared_memory_for_take_image_step(void) {
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Deinit shared memory for step 'Take Image' (STBI buffers)");
|
|
|
|
|
- allocatedBytesForSTBI = 0;
|
|
|
|
|
- sharedMemoryInUseFor = "";
|
|
|
|
|
|
|
+ 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) {
|
|
void *psram_reserve_shared_stbi_memory(size_t size) {
|
|
|
- /* Only large buffers should be placed in the shared PSRAM
|
|
|
|
|
- * If we also place all smaller STBI buffers here, we get artefacts for some reasons. */
|
|
|
|
|
- if (size >= 100000) {
|
|
|
|
|
- if ((allocatedBytesForSTBI + size) > TENSOR_ARENA_SIZE + MAX_MODEL_SIZE) { // Check if it still fits in the shared region
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Shared memory in PSRAM too small (STBI) to fit additional " +
|
|
|
|
|
- std::to_string(size) + " bytes! Available: " + std::to_string(TENSOR_ARENA_SIZE + MAX_MODEL_SIZE - allocatedBytesForSTBI) + " bytes!");
|
|
|
|
|
-
|
|
|
|
|
- return NULL;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating memory (" + std::to_string(size) + " bytes) for STBI (use shared memory in PSRAM)...");
|
|
|
|
|
- allocatedBytesForSTBI += size;
|
|
|
|
|
- return (uint8_t *)shared_region + allocatedBytesForSTBI - size;
|
|
|
|
|
- }
|
|
|
|
|
- else { // Normal PSRAM
|
|
|
|
|
- return malloc_psram_heap("STBI", size, MALLOC_CAP_SPIRAM);
|
|
|
|
|
|
|
+ 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)) return nullptr;
|
|
|
|
|
+ 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) {
|
|
void *psram_reallocate_shared_stbi_memory(void *ptr, size_t newsize) {
|
|
|
- char buf[20];
|
|
|
|
|
- sprintf(buf, "%p", ptr);
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "STBI requested realloc for " + std::string(buf) + " but this is currently unsupported!");
|
|
|
|
|
- return NULL;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-void psram_free_shared_stbi_memory(void *p) {
|
|
|
|
|
- if ((p >= shared_region) && (p <= ((uint8_t *)shared_region + allocatedBytesForSTBI))) { // was allocated inside the shared memory
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Part of shared memory used for STBI (PSRAM, part of shared memory) is free again");
|
|
|
|
|
|
|
+ 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 nullptr;
|
|
|
|
|
+ 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);
|
|
|
|
|
+ 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; }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- else { // Normal PSRAM
|
|
|
|
|
- free_psram_heap("STBI", p);
|
|
|
|
|
|
|
+ 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
|
|
|
|
|
- * During this step we only use the shared part of the PSRAM
|
|
|
|
|
- * for the tmpImage.
|
|
|
|
|
|
|
+ * Memory used in Aligning Step
|
|
|
*******************************************************************/
|
|
*******************************************************************/
|
|
|
void *psram_reserve_shared_tmp_image_memory(void) {
|
|
void *psram_reserve_shared_tmp_image_memory(void) {
|
|
|
- if (sharedMemoryInUseFor != "") {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Shared memory in PSRAM already in use for " + sharedMemoryInUseFor + "!");
|
|
|
|
|
- return NULL;
|
|
|
|
|
|
|
+ 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)...");
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating tmpImage (" + std::to_string(IMAGE_SIZE) + " bytes, use shared memory in PSRAM)...");
|
|
|
- sharedMemoryInUseFor = "Aligning";
|
|
|
|
|
- return shared_region; // Use 1th part of the shared memory for the tmpImage (only user)
|
|
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
void psram_free_shared_temp_image_memory(void) {
|
|
void psram_free_shared_temp_image_memory(void) {
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Shared memory used for tmpImage (PSRAM, part of shared memory) is free again");
|
|
|
|
|
- sharedMemoryInUseFor = "";
|
|
|
|
|
|
|
+ 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
|
|
* Memory used in Digitization Steps
|
|
|
- * During this step we only use the shared part of the PSRAM for the
|
|
|
|
|
- * Tensor Arena and one of the Models.
|
|
|
|
|
- * The shared memory is large enough for the largest model and the
|
|
|
|
|
- * Tensor Arena. Therefore we do not need to monitor the usage.
|
|
|
|
|
*******************************************************************/
|
|
*******************************************************************/
|
|
|
void *psram_get_shared_tensor_arena_memory(void) {
|
|
void *psram_get_shared_tensor_arena_memory(void) {
|
|
|
- if ((sharedMemoryInUseFor == "") || (sharedMemoryInUseFor == "Digitization_Model")) {
|
|
|
|
|
- sharedMemoryInUseFor = "Digitization_Tensor";
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating Tensor Arena (" + std::to_string(TENSOR_ARENA_SIZE) + " bytes, use shared memory in PSRAM)...");
|
|
|
|
|
- return shared_region; // Use 1th part of the shared memory for Tensor
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Shared memory in PSRAM already in use for " + sharedMemoryInUseFor + "!");
|
|
|
|
|
- return NULL;
|
|
|
|
|
|
|
+ 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) {
|
|
void *psram_get_shared_model_memory(void) {
|
|
|
- if ((sharedMemoryInUseFor == "") || (sharedMemoryInUseFor == "Digitization_Tensor")) {
|
|
|
|
|
- sharedMemoryInUseFor = "Digitization_Model";
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating Model memory (" + std::to_string(MAX_MODEL_SIZE) + " bytes, use shared memory in PSRAM)...");
|
|
|
|
|
- return (uint8_t *)shared_region + TENSOR_ARENA_SIZE; // Use 2nd part of the shared memory (after Tensor Arena) for the model
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Shared memory in PSRAM already in use for " + sharedMemoryInUseFor + "!");
|
|
|
|
|
- return NULL;
|
|
|
|
|
|
|
+ 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 (modelMemoryClaimed) duplicateClaim = true;
|
|
|
|
|
+ else { sharedMemoryUser = SharedMemoryUser::Digitization; modelMemoryClaimed = true; result = sharedRegion + static_cast<size_t>(TENSOR_ARENA_SIZE); }
|
|
|
|
|
+ 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) {
|
|
void psram_free_shared_tensor_arena_and_model_memory(void) {
|
|
|
- sharedMemoryInUseFor = "";
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Shared memory used for Tensor Arena and model (PSRAM, part of shared memory) is free again");
|
|
|
|
|
|
|
+ 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
|
|
|
|
|
|
|
+ * General PSRAM heap wrappers
|
|
|
*******************************************************************/
|
|
*******************************************************************/
|
|
|
void *malloc_psram_heap(std::string name, size_t size, uint32_t caps) {
|
|
void *malloc_psram_heap(std::string name, size_t size, uint32_t caps) {
|
|
|
- void *ptr;
|
|
|
|
|
-
|
|
|
|
|
- ptr = heap_caps_malloc(size, caps);
|
|
|
|
|
- if (ptr != NULL) {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocated " + to_string(size) + " bytes in PSRAM for '" + name + "'");
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocate " + to_string(size) + " bytes in PSRAM for '" + name + "'!");
|
|
|
|
|
|
|
+ if (size == 0) {
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Refusing zero-byte PSRAM allocation for '" + name + "'");
|
|
|
|
|
+ return nullptr;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return ptr;
|
|
|
|
|
|
|
+ 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 *realloc_psram_heap(std::string name, void *ptr, size_t size, uint32_t caps) {
|
|
|
- ptr = heap_caps_realloc(ptr, size, caps);
|
|
|
|
|
- if (ptr != NULL) {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Reallocated " + to_string(size) + " bytes in PSRAM for '" + name + "'");
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to reallocate " + to_string(size) + " bytes in PSRAM for '" + name + "'!");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return ptr;
|
|
|
|
|
|
|
+ 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) {
|
|
void *calloc_psram_heap(std::string name, size_t n, size_t size, uint32_t caps) {
|
|
|
- void *ptr;
|
|
|
|
|
-
|
|
|
|
|
- ptr = heap_caps_calloc(n, size, caps);
|
|
|
|
|
- if (ptr != NULL) {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Allocated " + to_string(size) + " bytes in PSRAM for '" + name + "'");
|
|
|
|
|
- }
|
|
|
|
|
- else {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocate " + to_string(size) + " bytes in PSRAM for '" + name + "'!");
|
|
|
|
|
|
|
+ if (size != 0 && n > SIZE_MAX / size) {
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "PSRAM calloc size overflow for '" + name + "'");
|
|
|
|
|
+ return nullptr;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return ptr;
|
|
|
|
|
|
|
+ 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) {
|
|
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 + "'...");
|
|
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Freeing memory in PSRAM used for '" + name + "'...");
|
|
|
heap_caps_free(ptr);
|
|
heap_caps_free(ptr);
|
|
|
}
|
|
}
|