Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions sim/FreeRTOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,60 @@ void APP_ERROR_HANDLER(int err) {
}

namespace {
std::unordered_map<void*, size_t> allocatedMemory;
size_t currentFreeHeap = configTOTAL_HEAP_SIZE;
size_t minimumEverFreeHeap = configTOTAL_HEAP_SIZE;
bool heapTrackingAlive = false;

struct HeapTracking {
std::unordered_map<void*, size_t> allocatedMemory;
size_t currentFreeHeap = configTOTAL_HEAP_SIZE;
size_t minimumEverFreeHeap = configTOTAL_HEAP_SIZE;

HeapTracking() {
heapTrackingAlive = true;
}

~HeapTracking() {
heapTrackingAlive = false;
}
};

HeapTracking heapTracking;
}

void* pvPortMalloc(size_t xWantedSize) {
void* ptr = malloc(xWantedSize);
allocatedMemory[ptr] = xWantedSize;
if (!heapTrackingAlive) {
return ptr;
}
heapTracking.allocatedMemory[ptr] = xWantedSize;

const size_t currentSize =
std::accumulate(allocatedMemory.begin(), allocatedMemory.end(), 0, [](const size_t lhs, const std::pair<void*, size_t>& item) {
return lhs + item.second;
});
const size_t currentSize = std::accumulate(heapTracking.allocatedMemory.begin(),
heapTracking.allocatedMemory.end(),
0,
[](const size_t lhs, const std::pair<void*, size_t>& item) {
return lhs + item.second;
});

currentFreeHeap = configTOTAL_HEAP_SIZE - currentSize;
minimumEverFreeHeap = std::min(currentFreeHeap, minimumEverFreeHeap);
heapTracking.currentFreeHeap = configTOTAL_HEAP_SIZE - currentSize;
heapTracking.minimumEverFreeHeap = std::min(heapTracking.currentFreeHeap, heapTracking.minimumEverFreeHeap);

return ptr;
}

void vPortFree(void* pv) {
allocatedMemory.erase(pv);
return free(pv);
if (heapTrackingAlive) {
heapTracking.allocatedMemory.erase(pv);
}
free(pv);
}

size_t xPortGetHeapSize(void) {
return configTOTAL_HEAP_SIZE;
}

size_t xPortGetFreeHeapSize(void) {
return currentFreeHeap;
return heapTracking.currentFreeHeap;
}

size_t xPortGetMinimumEverFreeHeapSize(void) {
return minimumEverFreeHeap;
return heapTracking.minimumEverFreeHeap;
}
Loading