libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
MemPool Class Reference

Thread-safe memory pool allocator with external metadata. More...

#include <mempool.h>

Classes

class  Block
 Represents a contiguous region of memory in the pool. More...
 
struct  Stats
 Statistics about the current state of the memory pool. More...
 

Public Types

using BlockSet = Set< Block >
 Sorted set of blocks ordered by address.
 
using BlockMap = Map< uintptr_t, Block >
 Map from aligned address to allocated block.
 

Public Member Functions

 MemPool ()
 Constructs an empty memory pool with a default hex name.
 
void addRegion (uintptr_t startingAddress, size_t size)
 Adds a contiguous memory region to the pool.
 
void addRegion (void *startingAddress, size_t size)
 Adds a contiguous memory region to the pool.
 
const Stringname () const
 Returns the name of this memory pool.
 
void setName (const String &val)
 Sets the name of this memory pool.
 
Stats stats () const
 Returns current pool statistics.
 
BlockSet memoryMap () const
 Returns a combined set of all free and allocated blocks.
 
void dump () const
 Dumps the memory map to the logger for debugging.
 
voidallocate (size_t size, size_t alignment=1)
 Allocates a block from the pool.
 
void free (void *ptr)
 Frees a previously allocated block back to the pool.
 

Static Public Member Functions

static bool isValidAlignment (size_t val)
 Returns true if the given value is a valid alignment (power of two).
 

Detailed Description

Thread-safe memory pool allocator with external metadata.

Manages a pool of memory that can be allocated from with alignment support. It does not use the managed memory buffer for any metadata, so it can manage memory that this class cannot directly access — for example, memory on a remote device, hardware device, or video memory. Free blocks are automatically coalesced on deallocation.

Member Function Documentation

◆ addRegion() [1/2]

void MemPool::addRegion ( uintptr_t  startingAddress,
size_t  size 
)

Adds a contiguous memory region to the pool.

Parameters
startingAddressThe starting address of the region.
sizeThe size of the region in bytes.

◆ addRegion() [2/2]

void MemPool::addRegion ( void startingAddress,
size_t  size 
)
inline

Adds a contiguous memory region to the pool.

Parameters
startingAddressPointer to the start of the region.
sizeThe size of the region in bytes.

◆ allocate()

void * MemPool::allocate ( size_t  size,
size_t  alignment = 1 
)

Allocates a block from the pool.

Parameters
sizeThe number of bytes to allocate.
alignmentThe alignment requirement (must be a power of two, default 1).
Returns
A pointer to the aligned allocation, or nullptr on failure.

◆ free()

void MemPool::free ( void ptr)

Frees a previously allocated block back to the pool.

Adjacent free blocks are automatically coalesced.

Parameters
ptrThe pointer returned by allocate(). Passing nullptr is a no-op.

◆ isValidAlignment()

static bool MemPool::isValidAlignment ( size_t  val)
inlinestatic

Returns true if the given value is a valid alignment (power of two).

Parameters
valThe alignment value to test.
Returns
true if val is a non-zero power of two.

◆ memoryMap()

BlockSet MemPool::memoryMap ( ) const

Returns a combined set of all free and allocated blocks.

Returns
A BlockSet containing every block in the pool.

◆ setName()

void MemPool::setName ( const String val)
inline

Sets the name of this memory pool.

Parameters
valThe new name.

◆ stats()

Stats MemPool::stats ( ) const

Returns current pool statistics.

Returns
A Stats struct with usage information.

The documentation for this class was generated from the following file: