CppCommon  1.0.4.1
C++ Common Library
Classes | Public Member Functions | List of all members
CppCommon::PoolMemoryManager< TAuxMemoryManager > Class Template Reference

Memory pool manager class. More...

#include <allocator_pool.h>

Public Member Functions

 PoolMemoryManager (TAuxMemoryManager &auxiliary)
 Initialize memory pool manager with an auxiliary memory manager. More...
 
 PoolMemoryManager (TAuxMemoryManager &auxiliary, size_t page, size_t pages=0)
 Initialize memory pool manager with an auxiliary memory manager, single page size and max pages count. More...
 
 PoolMemoryManager (TAuxMemoryManager &auxiliary, void *buffer, size_t capacity)
 Initialize memory pool manager with an auxiliary memory manager and a given buffer. More...
 
 PoolMemoryManager (const PoolMemoryManager &)=delete
 
 PoolMemoryManager (PoolMemoryManager &&)=delete
 
 ~PoolMemoryManager ()
 
PoolMemoryManageroperator= (const PoolMemoryManager &)=delete
 
PoolMemoryManageroperator= (PoolMemoryManager &&)=delete
 
size_t allocated () const noexcept
 Allocated memory in bytes. More...
 
size_t allocations () const noexcept
 Count of active memory allocations. More...
 
size_t page () const noexcept
 Page size in bytes. More...
 
size_t pages () const noexcept
 Max pages size. More...
 
size_t max_size () const noexcept
 Maximum memory block size, that could be allocated by the memory manager. More...
 
TAuxMemoryManager & auxiliary () noexcept
 Auxiliary memory manager. More...
 
void * malloc (size_t size, size_t alignment=alignof(std::max_align_t))
 Allocate a new memory block of the given size. More...
 
void free (void *ptr, size_t size)
 Free the previously allocated memory block. More...
 
void reset ()
 Reset the memory manager. More...
 
void reset (size_t page, size_t pages=0)
 Reset the memory manager with a given signle page size and max pages count. More...
 
void reset (void *buffer, size_t capacity)
 Reset the memory manager with a given buffer. More...
 
void clear ()
 Clear memory pool. More...
 

Detailed Description

template<class TAuxMemoryManager = DefaultMemoryManager>
class CppCommon::PoolMemoryManager< TAuxMemoryManager >

Memory pool manager class.

Memory pool manager uses a pre-allocated memory buffer or several memory pages in order to create an effective free-list data structure, that allows to allocate and free memory.

During the allocation memory pool manager will return a first-fit memory block in the free list with concatenating joint blocks to avoid memory defragmentation.

If the allocated block is huge and does not fit into the memory pool page then it will be allocated directly from auxiliary memory manager.

Not thread-safe.

Examples
memory_pool.cpp.

Definition at line 32 of file allocator_pool.h.

Constructor & Destructor Documentation

◆ PoolMemoryManager() [1/5]

template<class TAuxMemoryManager = DefaultMemoryManager>
CppCommon::PoolMemoryManager< TAuxMemoryManager >::PoolMemoryManager ( TAuxMemoryManager &  auxiliary)
inlineexplicit

Initialize memory pool manager with an auxiliary memory manager.

Memory pool will have unlimited pages of size 65536.

Parameters
auxiliary- Auxiliary memory manager

Definition at line 41 of file allocator_pool.h.

◆ PoolMemoryManager() [2/5]

template<class TAuxMemoryManager >
CppCommon::PoolMemoryManager< TAuxMemoryManager >::PoolMemoryManager ( TAuxMemoryManager &  auxiliary,
size_t  page,
size_t  pages = 0 
)
inlineexplicit

Initialize memory pool manager with an auxiliary memory manager, single page size and max pages count.

Parameters
auxiliary- Auxiliary memory manager
page- Page size in bytes
pages- Max pages count. Zero value means unlimited count (default is 0)

Definition at line 12 of file allocator_pool.inl.

◆ PoolMemoryManager() [3/5]

template<class TAuxMemoryManager >
CppCommon::PoolMemoryManager< TAuxMemoryManager >::PoolMemoryManager ( TAuxMemoryManager &  auxiliary,
void *  buffer,
size_t  capacity 
)
inlineexplicit

Initialize memory pool manager with an auxiliary memory manager and a given buffer.

Parameters
auxiliary- Auxiliary memory manager
buffer- Pool buffer
capacity- Pool buffer capacity

Definition at line 27 of file allocator_pool.inl.

◆ PoolMemoryManager() [4/5]

template<class TAuxMemoryManager = DefaultMemoryManager>
CppCommon::PoolMemoryManager< TAuxMemoryManager >::PoolMemoryManager ( const PoolMemoryManager< TAuxMemoryManager > &  )
delete

◆ PoolMemoryManager() [5/5]

template<class TAuxMemoryManager = DefaultMemoryManager>
CppCommon::PoolMemoryManager< TAuxMemoryManager >::PoolMemoryManager ( PoolMemoryManager< TAuxMemoryManager > &&  )
delete

◆ ~PoolMemoryManager()

template<class TAuxMemoryManager = DefaultMemoryManager>
CppCommon::PoolMemoryManager< TAuxMemoryManager >::~PoolMemoryManager ( )
inline

Definition at line 58 of file allocator_pool.h.

Member Function Documentation

◆ allocated()

template<class TAuxMemoryManager = DefaultMemoryManager>
size_t CppCommon::PoolMemoryManager< TAuxMemoryManager >::allocated ( ) const
inlinenoexcept

Allocated memory in bytes.

Definition at line 64 of file allocator_pool.h.

◆ allocations()

template<class TAuxMemoryManager = DefaultMemoryManager>
size_t CppCommon::PoolMemoryManager< TAuxMemoryManager >::allocations ( ) const
inlinenoexcept

Count of active memory allocations.

Definition at line 66 of file allocator_pool.h.

◆ auxiliary()

template<class TAuxMemoryManager = DefaultMemoryManager>
TAuxMemoryManager& CppCommon::PoolMemoryManager< TAuxMemoryManager >::auxiliary ( )
inlinenoexcept

Auxiliary memory manager.

Definition at line 77 of file allocator_pool.h.

◆ clear()

template<class TAuxMemoryManager >
void CppCommon::PoolMemoryManager< TAuxMemoryManager >::clear
inline

Clear memory pool.

Definition at line 336 of file allocator_pool.inl.

◆ free()

template<class TAuxMemoryManager >
void CppCommon::PoolMemoryManager< TAuxMemoryManager >::free ( void *  ptr,
size_t  size 
)
inline

Free the previously allocated memory block.

Parameters
ptr- Pointer to the memory block
size- Block size

Definition at line 202 of file allocator_pool.inl.

◆ malloc()

template<class TAuxMemoryManager >
void * CppCommon::PoolMemoryManager< TAuxMemoryManager >::malloc ( size_t  size,
size_t  alignment = alignof(std::max_align_t) 
)
inline

Allocate a new memory block of the given size.

Parameters
size- Block size
alignment- Block alignment (default is alignof(std::max_align_t))
Returns
A pointer to the allocated memory block or nullptr in case of allocation failed

Definition at line 42 of file allocator_pool.inl.

◆ max_size()

template<class TAuxMemoryManager = DefaultMemoryManager>
size_t CppCommon::PoolMemoryManager< TAuxMemoryManager >::max_size ( ) const
inlinenoexcept

Maximum memory block size, that could be allocated by the memory manager.

Definition at line 74 of file allocator_pool.h.

◆ operator=() [1/2]

template<class TAuxMemoryManager = DefaultMemoryManager>
PoolMemoryManager& CppCommon::PoolMemoryManager< TAuxMemoryManager >::operator= ( const PoolMemoryManager< TAuxMemoryManager > &  )
delete

◆ operator=() [2/2]

template<class TAuxMemoryManager = DefaultMemoryManager>
PoolMemoryManager& CppCommon::PoolMemoryManager< TAuxMemoryManager >::operator= ( PoolMemoryManager< TAuxMemoryManager > &&  )
delete

◆ page()

template<class TAuxMemoryManager = DefaultMemoryManager>
size_t CppCommon::PoolMemoryManager< TAuxMemoryManager >::page ( ) const
inlinenoexcept

Page size in bytes.

Definition at line 69 of file allocator_pool.h.

◆ pages()

template<class TAuxMemoryManager = DefaultMemoryManager>
size_t CppCommon::PoolMemoryManager< TAuxMemoryManager >::pages ( ) const
inlinenoexcept

Max pages size.

Definition at line 71 of file allocator_pool.h.

◆ reset() [1/3]

template<class TAuxMemoryManager >
void CppCommon::PoolMemoryManager< TAuxMemoryManager >::reset
inline

Reset the memory manager.

Definition at line 255 of file allocator_pool.inl.

◆ reset() [2/3]

template<class TAuxMemoryManager >
void CppCommon::PoolMemoryManager< TAuxMemoryManager >::reset ( size_t  page,
size_t  pages = 0 
)
inline

Reset the memory manager with a given signle page size and max pages count.

Parameters
page- Page size in bytes
pages- Max pages count. Zero value means unlimited count (default is 0)

Definition at line 274 of file allocator_pool.inl.

◆ reset() [3/3]

template<class TAuxMemoryManager >
void CppCommon::PoolMemoryManager< TAuxMemoryManager >::reset ( void *  buffer,
size_t  capacity 
)
inline

Reset the memory manager with a given buffer.

Parameters
buffer- Pool buffer
capacity- Pool buffer capacity

Definition at line 308 of file allocator_pool.inl.


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