Dynamic memory pools are a pool of memory blocks that can be dynamically allocated and freed at run-time. Their memory blocks are allocated on a given Memory Segment. They have the particularity that if there is no block available when attempting to get one, it will be allocated from free space on the memory segment.
Note that the dynamic memory pools DO NOT guarantee the data integrity between a block free and a block get operation. |
The dynamic memory pools can be used to allocate general-purpose memory blocks and hardware memory blocks. Hence, two different functions can be used to create a dynamic memory pool:
Function | Description | Use case |
---|---|---|
Mem_DynPoolCreate() | Creates a standard memory pool and allows to specify the memory alignment of each memory block. | General-purpose memory block. |
Mem_DynPoolCreateHW() | Creates a hardware memory pool. The memory blocks will be aligned as specified and padded as per memory segment properties. | Memory blocks that can be read/written via a DMA engine. |
#define CACHE_LINE_LEN 32u static CPU_INT08U MemSegData[4096u]; static MEM_SEG MemorySegment; static MEM_DYN_POOL DynamicMemPool; static MEM_DYN_POOL DynamicMemPoolHW; static void Mem_DynPoolExample (void) { CPU_INT08U *p_blk; CPU_INT08U *p_blk_hw; LIB_ERR err_lib; /* ------------ CREATION OF MEMORY SEGMENT (1)----------- */ Mem_SegCreate("Name", /* Name of mem seg (for debugging purposes). */ &MemorySegment, /* Pointer to memory segment structure. */ &MemSegData, /* Base address of memory segment data. */ 4096u, /* Length, in byte, of the memory segment. */ CACHE_LINE_LEN, /* Padding alignment value. */ &err_lib); if (err_lib != LIB_MEM_ERR_NONE) { /* Validate memory segment creation is successful. */ /* Handle error case. */ return; } /* - CREATION OF GENERAL-PURPOSE DYNAMIC MEMORY POOL -- */ Mem_DynPoolCreate("General-purpose dynamic memory pool", /* Name of dynamic pool (for debugging purposes). */ &DynamicMemPool, /* Pointer to dynamic memory pool data. */ &MemorySegment, /* Pointer to segment from which to allocate blocks. */ 20u, /* Block size, in bytes. */ sizeof(CPU_ALIGN), /* Block alignment, in bytes. */ 10u, /* Initial number of blocks. */ 10u, /* Maximum number of blocks. */ &err_lib); if (err_lib != LIB_MEM_ERR_NONE) { /* Validate dynamic memory pools creation is successful.*/ /* Handle error case. */ return; } /* ----- CREATION OF HARDWARE DYNAMIC MEMORY POOL ----- */ Mem_DynPoolCreateHW("Hardware dynamic memory pool", /* Name of dynamic pool (for debugging purposes). */ &DynamicMemPoolHW, /* Pointer to dynamic memory pool data. */ &MemorySegment, /* Pointer to segment from which to allocate blocks. */ 20u, /* Block size, in bytes. */ 8u, /* Block alignment, in bytes. */ 10u, /* Initial number of blocks. */ LIB_MEM_BLK_QTY_UNLIMITED, /* Maximum number of blocks. */ &err_lib); if (err_lib != LIB_MEM_ERR_NONE) { /* Validate dynamic memory pools creation is successful.*/ /* Handle error case. */ return; } /* --------------- BLOCK GET OPERATION ---------------- */ p_blk = (CPU_INT08U *)Mem_DynPoolBlkGet(&DynamicMemPool, &err_lib); if (err_lib != LIB_MEM_ERR_NONE) { /* Validate block get operation is successful. */ /* Handle error case. */ return; } /* ----------- HARDWARE BLOCK GET OPERATION ----------- */ p_blk_hw = (CPU_INT08U *)Mem_DynPoolBlkGet(&DynamicMemPoolHW, &err_lib); if (err_lib != LIB_MEM_ERR_NONE) { /* Validate block get operation is successful. */ /* Handle error case. */ return; } /* ... */ /* --------------- BLOCK FREE OPERATION --------------- */ Mem_DynPoolBlkFree( &DynamicMemPool, (void *)p_blk, &err_lib); if (err_lib != LIB_MEM_ERR_NONE) { /* Validate block free operation is successful. */ /* Handle error case. */ return; } /* ---------- HARDWARE BLOCK FREE OPERATION ----------- */ Mem_DynPoolBlkFree( &DynamicMemPoolHW, (void *)p_blk_hw, &err_lib); if (err_lib != LIB_MEM_ERR_NONE) { /* Validate block free operation is successful. */ /* Handle error case. */ return; } } |
|