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 String & | name () 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. | |
| void * | allocate (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). | |
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.
Adds a contiguous memory region to the pool.
| startingAddress | The starting address of the region. |
| size | The size of the region in bytes. |
Adds a contiguous memory region to the pool.
| startingAddress | Pointer to the start of the region. |
| size | The size of the region in bytes. |
Allocates a block from the pool.
| size | The number of bytes to allocate. |
| alignment | The alignment requirement (must be a power of two, default 1). |
Frees a previously allocated block back to the pool.
Adjacent free blocks are automatically coalesced.
| ptr | The pointer returned by allocate(). Passing nullptr is a no-op. |
Returns true if the given value is a valid alignment (power of two).
| val | The alignment value to test. |
| BlockSet MemPool::memoryMap | ( | ) | const |
Returns a combined set of all free and allocated blocks.
Sets the name of this memory pool.
| val | The new name. |
| Stats MemPool::stats | ( | ) | const |
Returns current pool statistics.