From 4321deee1e7bee3f769d9a9a3e0f1f88eb790801 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Fri, 12 Feb 2016 13:52:22 -0800 Subject: [PATCH] ChibiOS memory helper functions. --- firmware/common/chibios_cpp.cpp | 20 ++++++++++++++++++++ firmware/common/chibios_cpp.hpp | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/firmware/common/chibios_cpp.cpp b/firmware/common/chibios_cpp.cpp index 74f4c5d6..eab2fcb0 100644 --- a/firmware/common/chibios_cpp.cpp +++ b/firmware/common/chibios_cpp.cpp @@ -21,6 +21,8 @@ #include "chibios_cpp.hpp" +#include + #include void* operator new(size_t size) { @@ -38,3 +40,21 @@ void operator delete(void* p) noexcept { void operator delete[](void* p) noexcept { chHeapFree(p); } + +extern uint8_t __heap_base__[]; +extern uint8_t __heap_end__[]; + +namespace chibios { + +size_t heap_size() { + return __heap_end__ - __heap_base__; +} + +size_t heap_used() { + const auto core_free = chCoreStatus(); + size_t heap_free = 0; + chHeapStatus(NULL, &heap_free); + return heap_size() - (core_free + heap_free); +} + +} /* namespace chibios */ diff --git a/firmware/common/chibios_cpp.hpp b/firmware/common/chibios_cpp.hpp index dd423777..5cb4af2e 100644 --- a/firmware/common/chibios_cpp.hpp +++ b/firmware/common/chibios_cpp.hpp @@ -31,4 +31,11 @@ void* operator new[](size_t size); void operator delete(void* p); void operator delete[](void* p); +namespace chibios { + +size_t heap_size(); +size_t heap_used(); + +} /* namespace chibios */ + #endif/*__CHIBIOS_CPP_H__*/