Merge pull request #97 from InfiniTimeOrg/heap-unification

Integrate the new heap implementation from InfiniTime
main
NeroBurner 2023-05-18 20:32:33 +07:00 committed by GitHub
commit e6dcf3f743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 104 additions and 31 deletions

@ -443,8 +443,7 @@ public:
motorController.Init();
settingsController.Init();
lv_mem_monitor(&mem_mon);
printf("initial free_size = %u\n", mem_mon.free_size);
printf("initial free_size = %u\n", xPortGetFreeHeapSize());
// update time to current system time once on startup
dateTimeController.SetCurrentTime(std::chrono::system_clock::now());
@ -919,17 +918,18 @@ public:
}
if (print_memory_usage) {
lv_mem_monitor(&mem_mon);
if (mem_mon.free_size != mem_mon_last_free_size) {
auto currentFreeHeap = xPortGetFreeHeapSize();
if (currentFreeHeap != lastFreeHeapSize) {
auto minimumEverFreeHeap = xPortGetMinimumEverFreeHeapSize();
// 14KiB is the LVGL memory size used in InfiniTime
constexpr uint32_t pinetime_lvgl_memory = 14U*1024U;
uint32_t mem_used = LV_MEM_SIZE - mem_mon.free_size;
constexpr uint32_t pinetime_heap_memory = configTOTAL_HEAP_SIZE;
uint32_t mem_used = pinetime_heap_memory - currentFreeHeap;
// The "budget" value shows how much free lvgl memory the PineTime
// would have free and will go negative when more memory is used
// in the simulator than is available on the real hardware.
int32_t budget = pinetime_lvgl_memory - mem_used;
printf("Mem: %5u used (change: %+5d, peak: %5u) %d budget left\n", mem_used, mem_mon_last_free_size - mem_mon.free_size, mem_mon.max_used, budget);
mem_mon_last_free_size = mem_mon.free_size;
int32_t budget = configTOTAL_HEAP_SIZE - mem_used;
printf("Mem: %5u used (change: %+5d, peak: %5u) %d budget left\n", mem_used, lastFreeHeapSize - currentFreeHeap, minimumEverFreeHeap, budget);
lastFreeHeapSize = currentFreeHeap;
}
}
@ -940,7 +940,6 @@ public:
}
bool print_memory_usage = false;
lv_mem_monitor_t mem_mon;
// variables to create and destroy an lvgl overlay to indicate a turned off screen
bool screen_off_created = false;
@ -985,11 +984,13 @@ private:
bool left_release_sent = true; // make sure to send one mouse button release event
bool right_last_state = false; // varable used to send message only on changing state
uint32_t mem_mon_last_free_size = LV_MEM_SIZE;
size_t lastFreeHeapSize = configTOTAL_HEAP_SIZE;
GifManager gif_manager;
};
int mallocFailedCount = 0;
int stackOverflowCount = 0;
int main(int argc, char **argv)
{
// parse arguments

@ -1,3 +1,45 @@
#include "FreeRTOS.h"
#include <numeric>
#include <unordered_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::unordered_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;
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;
});
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;
}

@ -29,8 +29,13 @@
#ifndef INC_FREERTOS_H
#define INC_FREERTOS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "portmacro_cmsis.h"
//#include "app_error.h"
#include <stddef.h>
// from nrf_error.h
/** @defgroup NRF_ERRORS_BASE Error Codes Base number definitions
@ -59,21 +64,17 @@
#define NRF_ERROR_FORBIDDEN (NRF_ERROR_BASE_NUM + 15) ///< Forbidden Operation
#define NRF_ERROR_INVALID_ADDR (NRF_ERROR_BASE_NUM + 16) ///< Bad Memory Address
#define NRF_ERROR_BUSY (NRF_ERROR_BASE_NUM + 17) ///< Busy
#include <stdexcept>
#include <string> // std::to_string()
template<typename T>
void APP_ERROR_HANDLER(T err) {
throw std::runtime_error("APP_ERROR_HANDLER: " + std::to_string(err));
}
struct SCB_t {
unsigned ICSR = 0;
};
void APP_ERROR_HANDLER(int err);
typedef struct SCB_t {
unsigned ICSR;
} SCB_t;
static SCB_t SCB_member;
static SCB_t *SCB = &SCB_member;
//#define SCB_ICSR_VECTACTIVE_Msk (0x1FFUL /*<< SCB_ICSR_VECTACTIVE_Pos*/) /*!< SCB ICSR: VECTACTIVE Mask */
constexpr unsigned SCB_ICSR_VECTACTIVE_Msk = 0x01;
const unsigned SCB_ICSR_VECTACTIVE_Msk = 0x01;
/**
\brief System Reset
@ -82,4 +83,16 @@ constexpr unsigned SCB_ICSR_VECTACTIVE_Msk = 0x01;
// copied from nRF5_SDK_15.3.0_59ac345/components/toolchain/cmsis/include/core_cm4.h
void NVIC_SystemReset(void);
#define configTOTAL_HEAP_SIZE (1024 * 40)
size_t xPortGetFreeHeapSize(void);
size_t xPortGetMinimumEverFreeHeapSize(void);
void *pvPortMalloc(size_t xWantedSize);
void vPortFree(void *pv);
#ifdef __cplusplus
}
#endif
#endif /* INC_FREERTOS_H */

@ -29,7 +29,11 @@
#ifndef PORTMACRO_CMSIS_H
#define PORTMACRO_CMSIS_H
#include <cstdint>
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
typedef uint32_t TickType_t;
#define portMAX_DELAY ( TickType_t ) 0xffffffffUL
@ -41,9 +45,13 @@ typedef unsigned long UBaseType_t;
#define pdPASS pdTRUE
/* RTC register */
using NRF_RTC_Type = uint32_t;
constexpr NRF_RTC_Type portNRF_RTC_REG = 1;
typedef uint32_t NRF_RTC_Type;
const NRF_RTC_Type portNRF_RTC_REG = 1;
void portYIELD_FROM_ISR(BaseType_t);
#ifdef __cplusplus
}
#endif
#endif /* PORTMACRO_CMSIS_H */

@ -30,9 +30,14 @@
#ifndef INC_TASK_H
#define INC_TASK_H
#ifdef __cplusplus
extern "C" {
#endif
#include "portmacro_cmsis.h"
#include <cstdint>
#include <stdint.h>
#include <stddef.h>
// copied from InfiniTime/src/FreeRTOSConfig.h
#define configTICK_RATE_HZ 1024
#define configSTACK_DEPTH_TYPE uint16_t
@ -65,11 +70,11 @@ typedef void (*TaskFunction_t)(void *instance);
* \ingroup Tasks
*/
//typedef void * TaskHandle_t;
struct TaskHandle_t {
void *thread_handle = nullptr;
typedef struct TaskHandle_t {
void *thread_handle;
TaskFunction_t task_fn;
void *instance = nullptr;
};
void *instance;
}TaskHandle_t;
/* Task states returned by eTaskGetState. */
enum eTaskState
@ -101,7 +106,7 @@ is used in assert() statements. */
/* Used with the uxTaskGetSystemState() function to return the state of each task
in the system. */
struct TaskStatus_t {
typedef struct TaskStatus_t {
TaskHandle_t xHandle; /* The handle of the task to which the rest of the information in the structure relates. */
const char *pcTaskName; /* A pointer to the task's name. This value will be invalid if the task was deleted since the structure was populated! */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
UBaseType_t xTaskNumber; /* A number unique to the task. */
@ -111,7 +116,7 @@ struct TaskStatus_t {
uint32_t ulRunTimeCounter; /* The total run time allocated to the task so far, as defined by the run time stats clock. See http://www.freertos.org/rtos-run-time-stats.html. Only valid when configGENERATE_RUN_TIME_STATS is defined as 1 in FreeRTOSConfig.h. */
//StackType_t *pxStackBase; /* Points to the lowest address of the task's stack area. */
uint16_t usStackHighWaterMark; /* The minimum amount of stack space that has remained for the task since the task was created. The closer this value is to zero the closer the task has come to overflowing its stack. */
};
}TaskStatus_t;
/**
* configUSE_TRACE_FACILITY must be defined as 1 in FreeRTOSConfig.h for
@ -323,4 +328,8 @@ TaskHandle_t xTaskGetCurrentTaskHandle();
*/
BaseType_t xTaskGetSchedulerState();
#ifdef __cplusplus
}
#endif
#endif /* INC_TASK_H */