mempager Class Reference

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

#include <memory.h>

Inheritance diagram for mempager:
Inheritance graph
[legend]
Collaboration diagram for mempager:
Collaboration graph
[legend]

Public Member Functions

 mempager (size_t page=0)
 Construct a memory pager.
virtual ~mempager ()
 Destroy a memory pager.
void lock (void)
 Lock the memory pager mutex.
void unlock (void)
 Unlock the memory pager mutex.
unsigned utilization (void)
 Determine fragmentation level of acquired heap pages.
void purge (void)
 Purge all allocated memory and heap pages immediately.
virtual void dealloc (void *memory)
 Return memory back to pager heap.
void * alloc (size_t size)
 Allocate memory from the pager heap.
void * zalloc (size_t size)
 Allocate memory from the pager heap.
char * dup (const char *string)
 Duplicate NULL terminated string into allocated memory.
void * dup (void *memory, size_t size)
 Duplicate existing memory block into allocated memory.

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 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.

Author:
David Sugar <dyfet@gnutelephony.org>

Definition at line 179 of file memory.h.


Constructor & Destructor Documentation

mempager::mempager ( size_t  page = 0  ) 

Construct a memory pager.

Parameters:
page size to use or 0 for OS allocation size.
virtual mempager::~mempager (  )  [virtual]

Destroy a memory pager.

Release all pages back to the heap at once.


Member Function Documentation

void* 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.

Parameters:
size of memory request.
Returns:
allocated memory or NULL if not possible.

Reimplemented from memalloc.

virtual void 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.

Parameters:
memory to free back to private heap.
void* 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.

Parameters:
memory to data copy from.
size of memory to allocate.
Returns:
allocated memory with copy or NULL if cannot allocate.

Reimplemented from memalloc.

char* mempager::dup ( const char *  string  ) 

Duplicate NULL terminated string into allocated memory.

The mutex lock is acquired to perform this operation and then released.

Parameters:
string to copy into memory.
Returns:
allocated memory with copy of string or NULL if cannot allocate.

Reimplemented from memalloc.

void mempager::lock ( void   )  [inline]

Lock the memory pager mutex.

It will be more efficient to lock the pager and then call the locked allocator than using alloc which seperately locks and unlocks for each request when a large number of allocation requests are being batched together.

Definition at line 202 of file memory.h.

unsigned 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.

Returns:
pager utilization.

Reimplemented from memalloc.

Reimplemented in assoc_pointer< T, I, M, P >.

void* 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.

Parameters:
size of memory request.
Returns:
allocated memory or NULL if not possible.

Reimplemented from memalloc.


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

Generated on 28 Dec 2009 for UCommon by  doxygen 1.6.1