Embedded Artistry libmemory
Memory library for embedded systems (malloc and friends)
malloc_freertos.c File Reference
#include <assert.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
Include dependency graph for malloc_freertos.c:

Go to the source code of this file.

Macros

#define FREERTOS_HEAP_REGION_CNT   2
 

Functions

static int cmp_heap (const void *a, const void *b)
 
void malloc_addblock (void *addr, size_t size)
 Assign blocks of memory for use by malloc(). More...
 
 __attribute__ ((weak))
 
void * malloc (size_t size)
 
void free (void *ptr)
 

Variables

static const uint8_t heap_region_max = FREERTOS_HEAP_REGION_CNT
 Maximum number of heap regions that can be specified. More...
 
static volatile uint8_t heap_region_cnt = 0
 Current number of allocated heap regions. More...
 
static HeapRegion_t heap_regions [FREERTOS_HEAP_REGION_CNT+1]
 
static volatile bool initialized_ = false
 

Macro Definition Documentation

◆ FREERTOS_HEAP_REGION_CNT

#define FREERTOS_HEAP_REGION_CNT   2

NOTE: This FreeRTOS malloc implementation requires heap_5.c

Please define the correct heap_region for your project.Your application can define this macro to increase the number of heap regions

Definition at line 26 of file malloc_freertos.c.

Function Documentation

◆ __attribute__()

__attribute__ ( (weak)  )

Definition at line 90 of file malloc_freertos.c.

91 {
92  assert((heap_region_cnt > 0) && !initialized_);
93 
94  if(heap_region_cnt > 0 && !initialized_)
95  {
96  // Sort the heap regions so addresses are in the correct order
97  qsort(heap_regions, heap_region_cnt, sizeof(HeapRegion_t), cmp_heap);
98 
99  // Pass the array into vPortDefineHeapRegions() to enable malloc()
100  vPortDefineHeapRegions(heap_regions);
101 
102  // Signal to any waiting threads that we are done initializing
103  initialized_ = true;
104  }
105 }
static HeapRegion_t heap_regions[FREERTOS_HEAP_REGION_CNT+1]
static volatile bool initialized_
static volatile uint8_t heap_region_cnt
Current number of allocated heap regions.
static int cmp_heap(const void *a, const void *b)

References cmp_heap(), heap_region_cnt, heap_regions, and initialized_.

◆ cmp_heap()

static int cmp_heap ( const void *  a,
const void *  b 
)
static

Definition at line 54 of file malloc_freertos.c.

55 {
56  const HeapRegion_t* ua = a;
57  const HeapRegion_t* ub = b;
58 
59  return ((ua->pucStartAddress < ub->pucStartAddress)
60  ? -1
61  : ((ua->pucStartAddress != ub->pucStartAddress)));
62 }

Referenced by __attribute__().

◆ free()

void free ( void *  ptr)

free should NEVER be called before malloc is init'd

Definition at line 126 of file malloc_freertos.c.

127 {
129  assert(initialized_);
130 
131  if(ptr)
132  {
133  // We simply wrap the FreeRTOS call into a standard form
134  vPortFree(ptr);
135  }
136 }
static volatile bool initialized_

References initialized_.

◆ malloc()

void* malloc ( size_t  size)

Definition at line 107 of file malloc_freertos.c.

108 {
109  void* ptr = NULL;
110 
111  while(!initialized_)
112  {
113  // Thread blocks until application malloc has been correctly initialized
114  vTaskDelay(1);
115  }
116 
117  if(size > 0)
118  {
119  // We simply wrap the FreeRTOS call into a standard form
120  ptr = pvPortMalloc(size);
121  } // else NULL if there was an error
122 
123  return ptr;
124 }
static volatile bool initialized_

References initialized_.

◆ malloc_addblock()

void malloc_addblock ( void *  addr,
size_t  size 
)

Assign blocks of memory for use by malloc().

malloc_addblock must be called before memory allocation calls are made. In this FreeRTOS implementation, malloc() calls will block until memory has been allocated

Definition at line 69 of file malloc_freertos.c.

70 {
71  assert(addr && (size > 0));
72  assert((heap_region_cnt < heap_region_max) && "Too many heap regions!");
73 
74  // Increment the count early to claim a spot in case of multi-threads
75  uint8_t cnt = heap_region_cnt++;
76 
77  if(cnt < heap_region_max)
78  {
79  // We have space - add it to the table
80  heap_regions[cnt].pucStartAddress = (uint8_t*)addr;
81  heap_regions[cnt].xSizeInBytes = size;
82  }
83  else
84  {
85  // Decrement the count if we don't have space
87  }
88 }
static HeapRegion_t heap_regions[FREERTOS_HEAP_REGION_CNT+1]
static const uint8_t heap_region_max
Maximum number of heap regions that can be specified.
static volatile uint8_t heap_region_cnt
Current number of allocated heap regions.

References heap_region_cnt, heap_region_max, and heap_regions.

Variable Documentation

◆ heap_region_cnt

volatile uint8_t heap_region_cnt = 0
static

Current number of allocated heap regions.

Definition at line 35 of file malloc_freertos.c.

Referenced by __attribute__(), and malloc_addblock().

◆ heap_region_max

const uint8_t heap_region_max = FREERTOS_HEAP_REGION_CNT
static

Maximum number of heap regions that can be specified.

Definition at line 32 of file malloc_freertos.c.

Referenced by malloc_addblock().

◆ heap_regions

HeapRegion_t heap_regions[FREERTOS_HEAP_REGION_CNT+1]
static

FreeRTOS internal memory pool stucture when using heap_5.c

The block with the lowest starting address should appear first in the array

An additional block is allocated to serve as a NULL terminator

Definition at line 44 of file malloc_freertos.c.

Referenced by __attribute__(), and malloc_addblock().

◆ initialized_

volatile bool initialized_ = false
static

Flag that is used in malloc() to cause competing threads to wait until initialization is completed before allocating memory.

Definition at line 50 of file malloc_freertos.c.

Referenced by __attribute__(), free(), and malloc().