From 7fddb6c9dba169442114888a8592123abacd6010 Mon Sep 17 00:00:00 2001 From: Kamil Sroka Date: Fri, 25 Oct 2019 07:28:51 +0200 Subject: [PATCH] [heap] export heap API (#4143) --- include/openthread/heap.h | 8 +++++++ src/core/Makefile.am | 1 + src/core/api/heap_api.cpp | 41 +++++++++++++++++++++++++++++++++++- src/core/common/instance.hpp | 1 + 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/include/openthread/heap.h b/include/openthread/heap.h index 6f2a4a863..101861953 100644 --- a/include/openthread/heap.h +++ b/include/openthread/heap.h @@ -72,6 +72,14 @@ typedef void *(*otHeapCAllocFn)(size_t aCount, size_t aSize); */ typedef void (*otHeapFreeFn)(void *aPointer); +// This is a temporary API and would be removed after moving heap to platform. +// TODO: Remove after moving heap to platform. +void *otHeapCAlloc(size_t aCount, size_t aSize); + +// This is a temporary API and would be removed after moving heap to platform. +// TODO: Remove after moving heap to platform. +void otHeapFree(void *aPointer); + /** * This function sets the external heap CAlloc and Free * functions to be used by the OpenThread stack. diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 0345073d3..b8f7bdec4 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -241,6 +241,7 @@ EXTRA_DIST = \ libopenthread_radio_a_SOURCES = \ api/diags_api.cpp \ + api/heap_api.cpp \ api/instance_api.cpp \ api/link_raw_api.cpp \ api/logging_api.cpp \ diff --git a/src/core/api/heap_api.cpp b/src/core/api/heap_api.cpp index 670e1b452..f3d693105 100644 --- a/src/core/api/heap_api.cpp +++ b/src/core/api/heap_api.cpp @@ -37,7 +37,46 @@ #include "common/instance.hpp" -#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE +#if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE || OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE + +#if OPENTHREAD_RADIO + +void *otHeapCAlloc(size_t aCount, size_t aSize) +{ + OT_UNUSED_VARIABLE(aCount); + OT_UNUSED_VARIABLE(aSize); + + // Should never get called! + assert(false); + + return NULL; +} + +void otHeapFree(void *aPointer) +{ + OT_UNUSED_VARIABLE(aPointer); + + // Should never get called! + assert(false); +} + +#else // OPENTHREAD_RADIO + +void *otHeapCAlloc(size_t aCount, size_t aSize) +{ + return ot::Instance::Get().HeapCAlloc(aCount, aSize); +} + +void otHeapFree(void *aPointer) +{ + ot::Instance::Get().HeapFree(aPointer); +} + +#endif // OPENTHREAD_RADIO + +#endif // !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE || OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE + +#if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE && !OPENTHREAD_RADIO void otHeapSetCAllocFree(otHeapCAllocFn aCAlloc, otHeapFreeFn aFree) { ot::Instance::HeapSetCAllocFree(aCAlloc, aFree); diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp index b173ba622..a9b240373 100644 --- a/src/core/common/instance.hpp +++ b/src/core/common/instance.hpp @@ -272,6 +272,7 @@ public: #elif !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE void HeapFree(void *aPointer) { mHeap.Free(aPointer); } void *HeapCAlloc(size_t aCount, size_t aSize) { return mHeap.CAlloc(aCount, aSize); } + /** * This method returns a reference to the Heap object. *