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..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
ucommon::mempager Class Reference

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

#include <memory.h>

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

Public Member Functions

 mempager (size_t page=0)
 Construct a memory pager. More...
 
 mempager (const mempager &copy)
 
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...
 
- Public Member Functions inherited from ucommon::memalloc
 memalloc (size_t page=0)
 Construct a memory pager. More...
 
 memalloc (const memalloc &copy)
 
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...
 
- Protected Member Functions inherited from ucommon::memalloc
page_tpager (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

- 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() [1/2]

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

Construct a memory pager.

Parameters
pagesize to use or 0 for OS allocation size.

Definition at line 230 of file memory.cpp.

References mutex.

◆ mempager() [2/2]

ucommon::mempager::mempager ( const mempager copy)

Definition at line 236 of file memory.cpp.

References mutex.

◆ ~mempager()

ucommon::mempager::~mempager ( )
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().

Here is the call graph for this function:

Member Function Documentation

◆ _alloc()

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.

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

Here is the call graph for this function:

◆ _lock()

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.

Definition at line 248 of file memory.cpp.

References mutex.

◆ _unlock()

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

Unlock the memory pager mutex.

Definition at line 253 of file memory.cpp.

References mutex.

◆ assign()

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.

Here is the call graph for this function:

◆ dealloc()

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.

Definition at line 275 of file memory.cpp.

◆ purge()

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

Here is the call graph for this function:

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

Definition at line 258 of file memory.cpp.

References mutex, and ucommon::memalloc::utilization().

Here is the call graph for this function:

Member Data Documentation

◆ mutex

pthread_mutex_t ucommon::mempager::mutex
mutableprivate

Definition at line 187 of file memory.h.

Referenced by _alloc(), _lock(), _unlock(), assign(), mempager(), purge(), utilization(), and ~mempager().


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