1
0
Fork 0
InfiniSim/sim/FreeRTOS.cpp
Jean-François Milants 8ae5ba7c0a Integrate the new heap implementation from InfiniTime (https://github.com/InfiniTimeOrg/InfiniTime/pull/1709).
Since FreeRTOS.h, portmacro_cmsis.h and task.h are now built in C (by lv_mem.c), I had to change some includes and declarations to make them compatible with a C compiler.

Integrating the new memory management from InfiniTime in InfiniSim is not easy because InfiniSim does not include the whole FreeRTOS. Which means that, for example, pvPortMalloc() and vPortFree() are not accessible from InfiniSim.
As a first step, I provided custom implementations for pvPortMalloc(), vPortFree() which are based on ... malloc(). These function keep track of the memory that is currently allocated so that xPortGetFreeHeapSize(), xPortGetMinimumEverFreeHeapSize() return something.

Not that this implementation do not keep track of all the memory allocations done in InfiniTime. It can only "see" those done via pvPortMalloc(). It means that the available memory displayed by InfiniSim will probably be very optimistic.
2023-05-03 22:39:40 +02:00

44 lines
1 KiB
C++

#include "FreeRTOS.h"
#include <algorithm>
#include <map>
#include <stdio.h>
#include <stdlib.h>
void NVIC_SystemReset(void) {}
void APP_ERROR_HANDLER(int err) {
fprintf(stderr, "APP_ERROR_HANDLER: %d", err);
}
namespace {
std::map<void *, size_t> allocatedMemory;
size_t currentFreeHeap = configTOTAL_HEAP_SIZE;
size_t minimumEverFreeHeap = configTOTAL_HEAP_SIZE;
}
void *pvPortMalloc( size_t xWantedSize ) {
void* ptr = malloc(xWantedSize);
allocatedMemory[ptr] = xWantedSize;
size_t currentSize = 0;
std::for_each(allocatedMemory.begin(), allocatedMemory.end(), [&currentSize](const std::pair<void*, size_t>& item){
currentSize += item.second;
});
currentFreeHeap = configTOTAL_HEAP_SIZE - currentSize;
minimumEverFreeHeap = std::min(currentFreeHeap, minimumEverFreeHeap);
return ptr;
}
void vPortFree( void *pv ) {
allocatedMemory.erase(pv);
return free(pv);
}
size_t xPortGetFreeHeapSize(void) {
return currentFreeHeap;
}
size_t xPortGetMinimumEverFreeHeapSize(void) {
return minimumEverFreeHeap;
}