ucommon
7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
![]() ![]() |
A managed private heap for small allocations. More...
#include <memory.h>
Public Member Functions | |
mempager (size_t page=0) | |
Construct a memory pager. More... | |
mempager (const mempager ©) | |
virtual | ~mempager () |
Destroy a memory pager. More... | |
unsigned | utilization (void) |
Determine fragmentation level of acquired heap pages. More... | |
void | purge (void) |
Purge all allocated memory and heap pages immediately. More... | |
virtual void | dealloc (void *memory) |
Return memory back to pager heap. More... | |
void | assign (mempager &source) |
Assign foreign pager to us. More... | |
![]() | |
memalloc (size_t page=0) | |
Construct a memory pager. More... | |
memalloc (const memalloc ©) | |
virtual | ~memalloc () |
Destroy a memory pager. More... | |
unsigned | pages (void) const |
Get the number of pages that have been allocated from the real heap. More... | |
unsigned | max (void) const |
Get the maximum number of pages that are permitted. More... | |
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... | |
void | purge (void) |
Purge all allocated memory and heap pages immediately. More... | |
void | assign (memalloc &source) |
Assign foreign pager to us. More... | |
Protected Member Functions | |
virtual void | _lock (void) __OVERRIDE |
Lock the memory pager mutex. More... | |
virtual void | _unlock (void) __OVERRIDE |
Unlock the memory pager mutex. More... | |
virtual void * | _alloc (size_t size) __OVERRIDE |
Allocate memory from the pager heap. More... | |
![]() | |
page_t * | pager (void) |
Acquire a new page from the heap. More... | |
virtual void * | _alloc (size_t size) __OVERRIDE |
Allocate memory from the pager heap. More... | |
Private Attributes | |
pthread_mutex_t | mutex |
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. |
Definition at line 230 of file memory.cpp.
References mutex.
ucommon::mempager::mempager | ( | const mempager & | copy | ) |
Definition at line 236 of file memory.cpp.
References mutex.
|
virtual |
Destroy a memory pager.
Release all pages back to the heap at once.
Definition at line 242 of file memory.cpp.
References mutex, and ucommon::memalloc::purge().
|
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.
Definition at line 279 of file memory.cpp.
References ucommon::memalloc::_alloc(), mutex, and ucommon::memalloc::size().
Referenced by ucommon::shell::collapse(), ucommon::shell::getargv(), ucommon::shell::parse(), ucommon::shell::restart(), and ucommon::shell::setsym().
|
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.
Definition at line 248 of file memory.cpp.
References mutex.
|
protectedvirtual |
void ucommon::mempager::assign | ( | mempager & | source | ) |
Assign foreign pager to us.
This relocates the heap references to our object, clears the other object.
Definition at line 290 of file memory.cpp.
References ucommon::memalloc::assign(), and mutex.
|
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. |
Definition at line 275 of file memory.cpp.
void ucommon::mempager::purge | ( | void | ) |
Purge all allocated memory and heap pages immediately.
Definition at line 268 of file memory.cpp.
References mutex, and ucommon::memalloc::purge().
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.
Definition at line 258 of file memory.cpp.
References mutex, and ucommon::memalloc::utilization().
|
mutableprivate |
Definition at line 187 of file memory.h.
Referenced by _alloc(), _lock(), _unlock(), assign(), mempager(), purge(), utilization(), and ~mempager().