UCommon
|
A managed private heap for small allocations. More...
#include <memory.h>
Public Member Functions | |
void | assign (mempager &source) |
Assign foreign pager to us. More... | |
virtual void | dealloc (void *memory) |
Return memory back to pager heap. More... | |
mempager (size_t page=0) | |
Construct a memory pager. More... | |
mempager (const mempager ©) | |
void | purge (void) |
Purge all allocated memory and heap pages immediately. | |
unsigned | utilization (void) |
Determine fragmentation level of acquired heap pages. More... | |
virtual | ~mempager () |
Destroy a memory pager. More... | |
![]() | |
void | assign (memalloc &source) |
Assign foreign pager to us. More... | |
unsigned | max (void) const |
Get the maximum number of pages that are permitted. More... | |
memalloc (size_t page=0) | |
Construct a memory pager. More... | |
memalloc (const memalloc ©) | |
unsigned | pages (void) const |
Get the number of pages that have been allocated from the real heap. More... | |
void | purge (void) |
Purge all allocated memory and heap pages immediately. | |
size_t | size (void) const |
Get the size of a memory page. More... | |
unsigned | utilization (void) const |
Determine fragmentation level of acquired heap pages. More... | |
virtual | ~memalloc () |
Destroy a memory pager. More... | |
Protected Member Functions | |
virtual void * | _alloc (size_t size) |
Allocate memory from the pager heap. More... | |
virtual void | _lock (void) |
Lock the memory pager mutex. More... | |
virtual void | _unlock (void) |
Unlock the memory pager mutex. | |
![]() | |
page_t * | pager (void) |
Acquire a new page from the heap. More... | |
Additional Inherited Members | |
![]() | |
unsigned | limit |
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 significantly 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 significant fraction smaller than the page size, as fragmentation occurs at the end of pages when there is insufficient space in the current page to complete a request.
ucommon::mempager::mempager | ( | size_t | page = 0 | ) |
Construct a memory pager.
page | size to use or 0 for OS allocation size. |
|
virtual |
Destroy a memory pager.
Release all pages back to the heap at once.
|
protectedvirtual |
Allocate memory from the pager heap.
The size of the request must be less than the size of the memory page used. This impliments the memory protocol with mutex locking for thread safety. is locked during this operation and then released.
size | of memory request. |
Reimplemented from ucommon::memalloc.
|
protectedvirtual |
Lock the memory pager mutex.
It will be more efficient to lock the pager and then call the locked allocator than using alloc which separately locks and unlocks for each request when a large number of allocation requests are being batched together.
Reimplemented from ucommon::LockingProtocol.
void ucommon::mempager::assign | ( | mempager & | source | ) |
Assign foreign pager to us.
This relocates the heap references to our object, clears the other object.
|
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 implementation in a derived class.
memory | to free back to private heap. |
unsigned ucommon::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 verse 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.