Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Creates and initializes a memory pool.

Files

lib_mem.h/lib_mem.c

Prototype

          void  Mem_PoolCreate (MEM_POOL          *pmem_pool,
                                void              *pmem_base_addr,
                                CPU_SIZE_T         mem_size,
                                MEM_POOL_BLK_QTY   blk_nbr,
                                CPU_SIZE_T         blk_size,
                                CPU_SIZE_T         blk_align,
                                CPU_SIZE_T        *poctets_reqd,
                                LIB_ERR           *perr);

Arguments

pmem_pool

Pointer to a memory pool structure to create.

pmem_base_addr

Memory pool base address:

NULL address

Memory pool allocated from general-purpose heap;

Non-NULL address

Memory pool allocated from dedicated memory specified by non-NULL base address.

mem_size

Size of memory pool segment (in octets).

blk_nbr

Number of memory pool blocks to create.

blk_size

Size of memory pool blocks to create (in octets).

blk_align

Alignment of memory pool blocks to create (in octets).

poctets_reqd

Pointer to a variable to …

Return the number of octets required to successfully allocate the memory pool, if any errors;

Return 0, otherwise.

perr

Pointer to variable that will receive the return error code from this function:

LIB_MEM_ERR_NONE
LIB_MEM_ERR_NULL_PTR
LIB_MEM_ERR_HEAP_NOT_FOUND
LIB_MEM_ERR_HEAP_EMPTY
LIB_MEM_ERR_HEAP_OVF
LIB_MEM_ERR_SEG_EMPTY
LIB_MEM_ERR_SEG_OVF
LIB_MEM_ERR_INVALID_SEG_SIZE
LIB_MEM_ERR_INVALID_SEG_OVERLAP
LIB_MEM_ERR_INVALID_BLK_NBR
LIB_MEM_ERR_INVALID_BLK_SIZE
LIB_MEM_ERR_INVALID_BLK_ALIGN

Returned Value

None.

Required Configuration

Available only if LIB_MEM_CFG_ALLOC_EN is DEF_ENABLED in lib_cfg.h (see section 4-4).

Notes / Warnings

pmem_pool must be passed a valid pointer to the address of a declared MEM_POOL variable.

Example Usage

MEM_POOL    AppMemPoolFromHeap;
MEM_POOL    AppMemPoolFromUserMemSeg;
CPU_SIZE_T  octets_reqd;
LIB_ERR     err;


Mem_PoolCreate((MEM_POOL       *)&AppMemPoolFromHeap,
               (void           *)  0,              /* Create pool from heap   ...                    */
               (CPU_SIZE_T      )  0u,
               (MEM_POOL_BLK_QTY) 10u,             /* ... with 10 blocks      ...                    */
               (CPU_SIZE_T      )100u,             /* ... of  100 octets each ...                    */
               (CPU_SIZE_T      )  4u,             /* ... and align each block to a 4-byte boundary. */
               (CPU_SIZE_T     *)&octets_reqd,
               (LIB_ERR        *)&err);

if (err != LIB_ERR_NONE) {
  printf("COULD NOT CREATE MEMORY POOL.");
  if (err == LIB_MEM_ERR_HEAP_EMPTY) {
      printf("Heap empty   ... %u more octets needed.", octets_reqd);
  }
    return;
}


Mem_PoolCreate((MEM_POOL       *)&AppMemPoolFromUserMemSeg,
               (void           *)0x21000000,       /* Create pool from memory at 0x21000000 ...      */
               (CPU_SIZE_T      )10000u,           /* ... from a 10000-octet segment        ...      */
               (MEM_POOL_BLK_QTY)   10u,           /* ... with 10 blocks                    ...      */
               (CPU_SIZE_T      )  100u,           /* ... of 1 00 octets each               ...      */
               (CPU_SIZE_T      )    4u,           /* ... and align each block to a 4-byte boundary. */
               (CPU_SIZE_T     *)&octets_reqd,
               (LIB_ERR        *)&err);

if (err != LIB_ERR_NONE) {
  printf("COULD NOT CREATE MEMORY POOL.");
  if (err == LIB_MEM_ERR_HEAP_EMPTY) {
      printf("Heap    empty ... %u more octets needed.", octets_reqd);
  } else if (err == LIB_MEM_ERR_SEG_EMPTY) {
      printf("Segment empty ... %u more octets needed.", octets_reqd);
  }
    return;
}
  • No labels