diff --git a/Libraries/Cordio/wsf/sources/targets/baremetal/wsf_heap.c b/Libraries/Cordio/wsf/sources/targets/baremetal/wsf_heap.c index 3c84251274a..992a98fabe9 100644 --- a/Libraries/Cordio/wsf/sources/targets/baremetal/wsf_heap.c +++ b/Libraries/Cordio/wsf/sources/targets/baremetal/wsf_heap.c @@ -24,6 +24,7 @@ #if defined ( __GNUC__ ) #include +#include #endif /* __GNUC__ */ #include "wsf_types.h" @@ -38,37 +39,16 @@ Macros **************************************************************************************************/ -#ifndef WSF_HEAP_SIZE -#if(PAL_CFG_LL_MAX == 1) -/* Larger link layer configurations will require more heap space. */ -#define WSF_HEAP_SIZE 0x18000 -#else -/* This is the minimum heap size. */ -#define WSF_HEAP_SIZE 0x8000 -#endif -#endif - /************************************************************************************************** Global Variables **************************************************************************************************/ -static void* freeStartAddr = 0; -static uint32_t freeLen = 0; - -/*************************************************************************************************/ -/*! - * \brief Initialize the heap memory. - */ -/*************************************************************************************************/ -static void wsfHeapInit(void) -{ - freeStartAddr = sbrk(WSF_HEAP_SIZE); - freeLen = WSF_HEAP_SIZE; -} +static void* freeStartAddr = NULL; +static uint32_t heapUsed = 0; /*************************************************************************************************/ /*! - * \brief Reserve heap memory. + * \brief Allocate heap memory. * * \param size Number of bytes of heap memory used. */ @@ -78,16 +58,8 @@ void WsfHeapAlloc(uint32_t size) /* Round up to nearest multiple of 4 for word alignment */ size = (size + 3) & ~3; - if(freeStartAddr == 0) { - wsfHeapInit(); - } - - if(freeLen < size) { - WSF_ASSERT(FALSE); - } - - freeStartAddr += size; - freeLen -= size; + freeStartAddr = sbrk(size); + heapUsed += size; } /*************************************************************************************************/ @@ -99,8 +71,9 @@ void WsfHeapAlloc(uint32_t size) /*************************************************************************************************/ void *WsfHeapGetFreeStartAddress(void) { - if(freeStartAddr == 0) { - wsfHeapInit(); + if(freeStartAddr == (caddr_t)-1) { + WSF_ASSERT(0); + return NULL; } return freeStartAddr; @@ -115,11 +88,9 @@ void *WsfHeapGetFreeStartAddress(void) /*************************************************************************************************/ uint32_t WsfHeapCountAvailable(void) { - if(freeStartAddr == 0) { - wsfHeapInit(); - } + struct mallinfo temp_mallinfo = mallinfo(); - return freeLen; + return temp_mallinfo.fordblks; } /*************************************************************************************************/ @@ -131,9 +102,5 @@ uint32_t WsfHeapCountAvailable(void) /*************************************************************************************************/ uint32_t WsfHeapCountUsed(void) { - if(freeStartAddr == 0) { - wsfHeapInit(); - } - - return (WSF_HEAP_SIZE - freeLen); + return heapUsed; }