Dynamic Memory Pools

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:

FunctionDescriptionUse 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.

Dynamic memory pool usage example

Listing - Dynamic Memory Pool Usage Example gives an usage example of dynamic memory pools.

Listing - Dynamic Memory Pool Usage Example
#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(         "Segment name",                  /* Name of mem seg (for debugging purposes).            */
                           &MemorySegment,                  /* Pointer to memory segment structure.                 */
                  (CPU_ADDR)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 -- */  (2)
    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.                            */  (3)
                       10u,                                 /* Maximum number of blocks.                            */  (4)
                      &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 ----- */  (5)
    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.                            */  (6)
                        &err_lib);
    if (err_lib != LIB_MEM_ERR_NONE) {                      /* Validate dynamic memory pools creation is successful.*/
        /* Handle error case. */
        return;
    }


                                                            /* --------------- BLOCK GET OPERATION ---------------- */  (7)
    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 ----------- */  (8)
    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 --------------- */  (9)
    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 ----------- */  (10)
    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;
    }
}

(1) Creation of the memory segment from which memory blocks will be allocated.

(2) Creation of the dynamic memory pool. This memory pool will be used to allocate general-purpose memory blocks of 20 bytes and aligned on CPU word boundaries.

(3) At creation 10 blocks will be allocated and available.

(4) A maximum of 10 blocks can be allocated from this dynamic memory pools. Since the initial number of block equals the maximum number of blocks, this will create a static memory pool.

(5) Creation of a hardware dynamic memory pool. The requested alignment is 8 bytes, however, since the padding alignment specified at the time of the memory segment creation is 32 bytes, the memory blocks will be aligned on a 32-byte boundary and will have a length of 32 bytes.

(6) No limit of memory block quantity is specified for this dynamic memory pool. Hence, once more than 10 blocks will be taken from the pool, the dynamic memory pool will start allocating blocks from the free space of the memory segment. Once a memory block will be freed, it will be available for the next allocation. It will be possible to allocate memory blocks until the memory segment overflows.

(7) A general-purpose memory block is taken from the pool. The block is aligned on a CPU word boundary and is at least 20 bytes long.

(8) A hardware memory block is taken from the pool. The block is aligned on a 32 bytes boundary and is padded in order to meet the padding alignment requirement of the memory segment. It is then safe to be read/written via a DMA engine.

(9) (10) Memory blocks are freed back to their respective dynamic memory pools. They are available for the next allocation. Note that the data that was present on these memory blocks WILL BE altered when freed.


Related pages