UCommon
ucommon::mempager Class Reference

A managed private heap for small allocations. More...

#include <memory.h>

Inheritance diagram for ucommon::mempager:
Collaboration diagram for ucommon::mempager:

Public Member Functions

void assign (mempager &source)
 Assign foreign pager to us.
 
virtual void dealloc (void *memory)
 Return memory back to pager heap.
 
 mempager (const mempager &copy)
 
 mempager (size_t page=0)
 Construct a memory pager.
 
void purge (void)
 Purge all allocated memory and heap pages immediately.
 
unsigned utilization (void)
 Determine fragmentation level of acquired heap pages.
 
virtual ~mempager ()
 Destroy a memory pager.
 
- Public Member Functions inherited from ucommon::memalloc
void assign (memalloc &source)
 Assign foreign pager to us.
 
unsigned max (void) const
 Get the maximum number of pages that are permitted.
 
 memalloc (const memalloc &copy)
 
 memalloc (size_t page=0)
 Construct a memory pager.
 
unsigned pages (void) const
 Get the number of pages that have been allocated from the real heap.
 
void purge (void)
 Purge all allocated memory and heap pages immediately.
 
size_t size (void) const
 Get the size of a memory page.
 
unsigned utilization (void) const
 Determine fragmentation level of acquired heap pages.
 
virtual ~memalloc ()
 Destroy a memory pager.
 

Protected Member Functions

virtual void * _alloc (size_t size)
 Allocate memory from the pager heap.
 
virtual void _lock (void)
 Lock the memory pager mutex.
 
virtual void _unlock (void)
 Unlock the memory pager mutex.
 
- Protected Member Functions inherited from ucommon::memalloc
page_t * pager (void)
 Acquire a new page from the heap.
 

Additional Inherited Members

- Protected Attributes inherited from ucommon::memalloc
unsigned limit
 

Detailed Description

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.

Author
David Sugar dyfet.nosp@m.@gnu.nosp@m.telep.nosp@m.hony.nosp@m..org

Definition at line 184 of file memory.h.

Constructor & Destructor Documentation

◆ mempager()

ucommon::mempager::mempager ( size_t page = 0)

Construct a memory pager.

Parameters
pagesize to use or 0 for OS allocation size.

◆ ~mempager()

virtual ucommon::mempager::~mempager ( )
virtual

Destroy a memory pager.

Release all pages back to the heap at once.

Member Function Documentation

◆ _alloc()

virtual void * ucommon::mempager::_alloc ( size_t size)
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.

Parameters
sizeof memory request.
Returns
allocated memory or NULL if not possible.

Reimplemented from ucommon::memalloc.

◆ _lock()

virtual void ucommon::mempager::_lock ( void )
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.

◆ _unlock()

virtual void ucommon::mempager::_unlock ( void )
protectedvirtual

Unlock the memory pager mutex.

Reimplemented from ucommon::LockingProtocol.

◆ assign()

void ucommon::mempager::assign ( mempager & source)

Assign foreign pager to us.

This relocates the heap references to our object, clears the other object.

◆ dealloc()

virtual void ucommon::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 implementation in a derived class.

Parameters
memoryto free back to private heap.

◆ utilization()

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.

Returns
pager utilization.

The documentation for this class was generated from the following file: