MEMPOOL

Description

Manages an already allocated memory region at a specified address by assigning blocks of a requested size. Once assigned, blocks are static and cannot be released or freed. This approach has the following advantages:

  • The managed memory region is divided according to a fixed sequence of block requests. The resulting memory map is deterministic, and the user documentation can precisely describe its arrangement.

  • No dynamic memory allocation implies no memory fragmentation and no need to handle the complexity of sudden out of memory conditions.

  • There might be several memory pools residing on different memory types (a given SRAM bank, external SDRAM, etc.), each one serving a different purpose.

The managed memory region address, each block assigned from it and their sizes align to a 64-bit boundary.

Design and development status

Feature-complete.

Changelog

Version

Date*

Author

Comment

1.0.0

2022.9.7

sgermino

Initial release.

* Date format is Year.Month.Day.

API reference

MEMPOOL_BLOCKSIZE_REMAINS

Request a block with the remaining mempool capacity.

struct MEMPOOL

The user should treat this as an opaque structure. No member should be directly accessed or modified.

void MEMPOOL_Init(struct MEMPOOL *const M, const uintptr_t BaseAddr, const uint32_t Size)

Initializes a MEMPOOL instance.

Parameters
  • BaseAddr – Memory region start address. The address must be aligned to a 64-bit boundary; this condition is asserted.

  • Size – Memory region size, in octets. Must be a multiple of eight; this condition is asserted.

void *MEMPOOL_Block(struct MEMPOOL *const M, const uint32_t ReqSize, const char *const Description)

Reserves and returns a memory block. The user must confirm there is enough space for a block allocation of the requested size; this condition is asserted. See MEMPOOL_Available().

Parameters
  • ReqSize – Requested size or MEMPOOL_BLOCKSIZE_REMAINS to ask for all space left. The returned memory block size is round up to the next multiple of eight.

  • Description – Unique block name, purpose, or NULL. The block will keep a copy of this pointer; it won’t duplicate the original string contents. The user must guarantee that the string pointer passed will not change its memory allocation to avoid a dangling pointer.

Returns

Pointer to the new memory block address. The address is 64-bit aligned.

uint32_t MEMPOOL_BlockSize(void *const Block)

Returns a memory block size.

Parameters
  • Block – A valid memory block pointer as returned by MEMPOOL_Block(); this condition is asserted.

Returns

Memory block size, in octets.

uint32_t MEMPOOL_Available(struct MEMPOOL *const M)

Returns available space for next block allocation.

Returns

Available space for the next block, in octets.