![]() |
Public API Reference |
![]() |
A quick-allocation pool for storage of arbitrary data. More...
#include <csutil/mempool.h>
Inherited by TEventMemPool.
Public Member Functions | |
void * | Alloc (size_t) |
Allocate the specified number of bytes. | |
csMemoryPool (size_t gran=4096) | |
Construct a new memory pool. | |
void | Empty () |
Release all memory allocated by the pool. | |
void const * | Store (void const *, size_t) |
Store a copy of a block of memory of the indicated size. | |
char const * | Store (char const *) |
Store a null-terminated C-string. | |
~csMemoryPool () | |
Destroy the memory pool, freeing all allocated storage. |
A quick-allocation pool for storage of arbitrary data.
Pointers to allocations made from the pool are guaranteed to remain valid as long as the pool is alive; the pool contents are never relocated. All individually allocated memory chunks are freed when the pool itself is destroyed. This memory management scheme is suitable for algorithms which need to allocate and manipulate many chunks of memory in a non-linear fashion where the life-time of each memory chunk is not predictable. Rather than complicating the algorithm by having it carefully track each memory chunk to determine when it would be safe to dispose of the memory, it can instead create a csMemoryPool at the start, and destroy the pool at the end. During processing, it can allocate memory chunks from the pool as needed, and simply forget about them when no longer needed, knowing that they will be freed en-masse when the pool itself is disposed. This is often a cheaper, simpler, and faster alternative to reference-counting or automatic garbage collection.
csMemoryPool::csMemoryPool | ( | size_t | gran = 4096 | ) | [inline] |
Construct a new memory pool.
If a size is provided, it is taken as a recommendation in bytes of the granularity of the internal allocations made by the pool, but is not a hard limit. Client allocations from the pool can be both smaller and larger than this number. A larger number will result in fewer interactions with the system heap (which translates to better performance), but at the cost of potential unused but allocated space. A smaller number translates to a greater number of interactions with the system heap (which is slow), but means less potential wasted memory.
csMemoryPool::~csMemoryPool | ( | ) | [inline] |
void* csMemoryPool::Alloc | ( | size_t | ) |
Allocate the specified number of bytes.
void csMemoryPool::Empty | ( | ) |
void const* csMemoryPool::Store | ( | void const * | , |
size_t | |||
) |
Store a copy of a block of memory of the indicated size.
char const* csMemoryPool::Store | ( | char const * | ) |
Store a null-terminated C-string.