#include <memorypool.h>
Classes | |
struct | Page |
Public Member Functions | |
MemoryPool (size_t chunkSize) | |
void * | allocChunk () |
void | freeChunk (void *ptr) |
void | freeUnusedPages () |
size_t | getChunkSize () const |
Protected Member Functions | |
MemoryPool (const MemoryPool &) | |
MemoryPool & | operator= (const MemoryPool &) |
void | allocPage () |
void | addPageToPool (const Page &page) |
bool | isPointerInPage (void *ptr, const Page &page) |
Protected Attributes | |
const size_t | _chunkSize |
Array< Page > | _pages |
void * | _next |
size_t | _chunksPerPage |
This class provides a pool of memory 'chunks' of identical size. The size of a chunk is determined when creating the memory pool.
Using a memory pool may yield better performance and memory usage when allocating and deallocating many memory blocks of equal size. E.g. the Common::String class uses a memory pool for the refCount variables (each the size of an int) it allocates for each string instance.
|
explicit |
Constructor for a memory pool with the given chunk size.
chunkSize | the chunk size of this memory pool |
void* Common::MemoryPool::allocChunk | ( | ) |
Allocate a new chunk from the memory pool.
void Common::MemoryPool::freeChunk | ( | void * | ptr | ) |
Return a chunk to the memory pool. The given pointer must have been obtained from calling the allocChunk() method of the very same MemoryPool instance. Passing any other pointer (e.g. to a chunk from another MemoryPool, or a malloc'ed memory block) will lead to undefined behavior and may result in a crash (if you are lucky) or in silent data corruption.
void Common::MemoryPool::freeUnusedPages | ( | ) |
Perform garbage collection. The memory pool stores all the chunks it manages in memory 'pages' obtained via the classic memory allocation APIs (i.e. malloc/free). Ordinarily, once a page has been allocated, it won't be released again during the life time of the memory pool. The exception is when this method is called.
|
inline |
Return the chunk size used by this memory pool.