Embedded Artistry libmemory
Memory library for embedded systems (malloc and friends)
malloc_threadx.c File Reference
#include <assert.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdint.h>
#include <threadx/tx_api.h>
Include dependency graph for malloc_threadx.c:

Go to the source code of this file.

Functions

 __attribute__ ((weak))
 
void malloc_addblock (void *addr, size_t size)
 Assign blocks of memory for use by malloc(). More...
 
void * malloc (size_t size)
 
void free (void *ptr)
 

Variables

static TX_BYTE_POOL malloc_pool_
 ThreadX internal memory pool stucture. More...
 
static volatile bool initialized_ = false
 

Function Documentation

◆ __attribute__()

__attribute__ ( (weak)  )

Definition at line 25 of file malloc_threadx.c.

26 {
27  // Unused here, override to specify your own init functin
28  // Which includes malloc_addblock calls
29 }

◆ free()

void free ( void *  ptr)

free should NEVER be called before malloc is init'd

Definition at line 79 of file malloc_threadx.c.

80 {
82  assert(initialized_);
83 
84  if(ptr)
85  {
86  // We simply wrap the threadX call into a standard form
87  unsigned r = tx_byte_release(ptr);
88  assert(r == TX_SUCCESS);
89  }
90 }
static volatile bool initialized_

References initialized_.

◆ malloc()

void* malloc ( size_t  size)

In the ThreadX implementaiton, we make sure the ThreadX pool has been created before we try to allocate memory, or there will be an error. We sleep our threads until memory has been added.

Definition at line 53 of file malloc_threadx.c.

54 {
55  void* ptr = NULL;
56 
62  while(!initialized_)
63  {
64  tx_thread_sleep(1);
65  }
66 
67  if(size > 0)
68  {
69  // We simply wrap the threadX call into a standard form
70  unsigned r = tx_byte_allocate(&malloc_pool_, &ptr, size, TX_WAIT_FOREVER);
71 
72  // I add the string to provide a more helpful error output. It's value is always true.
73  assert(r == TX_SUCCESS && "malloc failed");
74  } // else NULL if there was an error
75 
76  return ptr;
77 }
static volatile bool initialized_
static TX_BYTE_POOL malloc_pool_
ThreadX internal memory pool stucture.

References initialized_, and malloc_pool_.

◆ 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 ThreadX implementation, malloc() calls will block until memory has been allocated

Definition at line 36 of file malloc_threadx.c.

37 {
38  assert(addr && (size > 0));
39 
40  unsigned r;
41 
42  /*
43  * tx_byte_pool_create is ThreadX's API to create a byte pool using a memory block.
44  * We are essentially just wrapping ThreadX APIs into a simpler form
45  */
46  r = tx_byte_pool_create(&malloc_pool_, "Heap Memory Pool", addr, size);
47  assert(r == TX_SUCCESS);
48 
49  // Signal to any threads waiting on do_malloc that we are done
50  initialized_ = true;
51 }
static volatile bool initialized_
static TX_BYTE_POOL malloc_pool_
ThreadX internal memory pool stucture.

References initialized_, and malloc_pool_.

Variable Documentation

◆ 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 21 of file malloc_threadx.c.

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

◆ malloc_pool_

TX_BYTE_POOL malloc_pool_
static

ThreadX internal memory pool stucture.

Definition at line 15 of file malloc_threadx.c.

Referenced by malloc(), and malloc_addblock().