A managed private heap for small allocations. More...
#include <memory.h>
Public Member Functions | |
void * | alloc (size_t size) |
Allocate memory from the pager heap. | |
virtual void | dealloc (void *memory) |
Return memory back to pager heap. | |
void * | dup (void *memory, size_t size) |
Duplicate existing memory block into allocated memory. | |
char * | dup (const char *string) |
Duplicate NULL terminated string into allocated memory. | |
void | lock (void) |
Lock the memory pager mutex. | |
mempager (size_t page=0) | |
Construct a memory pager. | |
void | purge (void) |
Purge all allocated memory and heap pages immediately. | |
void | unlock (void) |
Unlock the memory pager mutex. | |
unsigned | utilization (void) |
Determine fragmentation level of acquired heap pages. | |
void * | zalloc (size_t size) |
Allocate memory from the pager heap. | |
virtual | ~mempager () |
Destroy a memory pager. |
A managed private heap for small allocations.
This is used to allocate a large number of small objects from a paged heap as needed and to then release them together all at once. This pattern has significiently less overhead than using malloc and offers less locking contention since the memory pager can also have it's own mutex. Pager pool allocated memory is always aligned to the optimal data size for the cpu bus and pages are themselves created from memory aligned allocations. A page size for a memory pager should be some multiple of the OS paging size.
The mempager uses a strategy of allocating fixed size pages as needed from the real heap and allocating objects from these pages as needed. A new page is allocated from the real heap when there is insufficient space in the existing page to complete a request. The largest single memory allocation one can make is restricted by the page size used, and it is best to allocate objects a significent fraction smaller than the page size, as fragmentation occurs at the end of pages when there is insufficent space in the current page to complete a request.
Definition at line 183 of file memory.h.
ucc::mempager::mempager | ( | size_t | page = 0 |
) |
Construct a memory pager.
page | size to use or 0 for OS allocation size. |
virtual ucc::mempager::~mempager | ( | ) | [virtual] |
Destroy a memory pager.
Release all pages back to the heap at once.
void* ucc::mempager::alloc | ( | size_t | size | ) | [virtual] |
Allocate memory from the pager heap.
The size of the request must be less than the size of the memory page used. The memory pager mutex is locked during this operation and then released.
size | of memory request. |
Reimplemented from ucc::memalloc.
virtual void ucc::mempager::dealloc | ( | void * | memory | ) | [virtual] |
Return memory back to pager heap.
This actually does nothing, but might be used in a derived class to create a memory heap that can also receive (free) memory allocated from our heap and reuse it, for example in a full private malloc implimentation in a derived class.
memory | to free back to private heap. |
char* ucc::mempager::dup | ( | const char * | string | ) |
Duplicate NULL terminated string into allocated memory.
The mutex lock is acquired to perform this operation and then released.
string | to copy into memory. |
Reimplemented from ucc::memalloc.
void* ucc::mempager::dup | ( | void * | memory, | |
size_t | size | |||
) |
Duplicate existing memory block into allocated memory.
The mutex lock is acquired to perform this operation and then released.
memory | to data copy from. | |
size | of memory to allocate. |
Reimplemented from ucc::memalloc.
void ucc::mempager::lock | ( | void | ) | [inline] |
unsigned ucc::mempager::utilization | ( | void | ) |
Determine fragmentation level of acquired heap pages.
This is represented as an average % utilization (0-100) and represents the used portion of each allocated heap page vs the page size. Since requests that cannot fit on an already allocated page are moved into a new page, there is some unusable space left over at the end of the page. When utilization approaches 100, this is good. A low utilization may suggest a larger page size should be used.
Reimplemented from ucc::memalloc.
Reimplemented in ucc::assoc_pointer< T, I, M, P >.
void* ucc::mempager::zalloc | ( | size_t | size | ) |
Allocate memory from the pager heap.
The size of the request must be less than the size of the memory page used. The memory pager mutex is locked during this operation and then released. This version zeros memory after the mutex lock has been released.
size | of memory request. |
Reimplemented from ucc::memalloc.