Manage thread-safe fixed-size blocks of dynamic memory.
More...
Memory pools are fixed-size blocks of memory that are thread-safe. They operate much faster than the dynamically allocated heap and do not suffer from fragmentation. Being thread-safe, they can be accessed from threads and ISRs alike.
Shared memory is one of the basic models to exchange information between threads. Using memory pools for exchanging data, you can share more complex objects between threads if compared to a Message Queue. Memory pool management functions are used to define and manage such fixed-sized memory pools.
- Note
- Refer to Memory Pool Configuration for RTX5 configuration options.
Working with Memory Pools
Follow these steps to create and use a memory pool:
- Declare a data structure that combines a number of elements:
typedef struct {
uint32_t length;
uint32_t width;
uint32_t height;
uint32_t weight;
} properties_t;
- Declare a memory pool of these objects as a block of memory:
- Then, create the memory pool in a thread:
- Allocate the pool within a thread and fill it with data:
properties_t *object_data;
object_data = (properties_t *) osPoolAlloc(object_pool_id);
object_data->length = 100;
object_data->width = 10;
object_data->height = 23;
object_data->weight = 1000;
struct osMemoryPoolAttr_t |
Data Fields |
const char * |
name |
name of the memory pool |
uint32_t |
attr_bits |
attribute bits |
void * |
cb_mem |
memory for control block |
uint32_t |
cb_size |
size of provided memory for control block |
void * |
mp_mem |
memory for data storage |
uint32_t |
mp_size |
size of provided memory for data storage |
Memory Pool ID identifies the memory pool.
- Parameters
-
[in] | block_count | maximum number of memory blocks in memory pool. |
[in] | block_size | memory block size in bytes. |
[in] | attr | memory pool attributes; NULL: default values. |
- Returns
- memory pool ID for reference by other functions or NULL in case of error.
- Parameters
-
- Returns
- name as NULL terminated string.
- Parameters
-
- Returns
- address of the allocated memory block or NULL in case of no memory is available.
- Parameters
-
[in] | mp_id | memory pool ID obtained by osMemoryPoolNew. |
[in] | block | address of the allocated memory block to be returned to the memory pool. |
- Returns
- status code that indicates the execution status of the function.
- Parameters
-
- Returns
- maximum number of memory blocks.
- Parameters
-
- Returns
- memory block size in bytes.
- Parameters
-
- Returns
- number of memory blocks used.
- Parameters
-
- Returns
- number of memory blocks available.
- Parameters
-
- Returns
- status code that indicates the execution status of the function.