Skip to content

Commit

Permalink
Using dynamic memory allocation.
Browse files Browse the repository at this point in the history
This is a significant API change. We need to call WsfHeapAlloc with
the required size before calling WsfHeapGetFreeStartAddress. This
will allow us to do dynamic memory allocation, instead of allocating
a large block of memory on initialization.
  • Loading branch information
kevin-gillespie committed Sep 18, 2024
1 parent 7f1b724 commit 4618758
Showing 1 changed file with 12 additions and 45 deletions.
57 changes: 12 additions & 45 deletions Libraries/Cordio/wsf/sources/targets/baremetal/wsf_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#if defined ( __GNUC__ )
#include <unistd.h>
#include <malloc.h>
#endif /* __GNUC__ */

#include "wsf_types.h"
Expand All @@ -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.
*/
Expand All @@ -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;
}

/*************************************************************************************************/
Expand All @@ -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;
Expand All @@ -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;
}

/*************************************************************************************************/
Expand All @@ -131,9 +102,5 @@ uint32_t WsfHeapCountAvailable(void)
/*************************************************************************************************/
uint32_t WsfHeapCountUsed(void)
{
if(freeStartAddr == 0) {
wsfHeapInit();
}

return (WSF_HEAP_SIZE - freeLen);
return heapUsed;
}

0 comments on commit 4618758

Please sign in to comment.