mCRL2
Loading...
Searching...
No Matches
mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe > Class Template Reference

The memory pool allocates elements of size T from blocks. More...

#include <memory_pool.h>

Inheritance diagram for mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >:
mcrl2::utilities::noncopyable mcrl2::utilities::block_allocator< T, ElementsPerBlock, ThreadSafe >

Public Member Functions

 memory_pool ()=default
 
 ~memory_pool ()
 Triggers the (possibly non-trivial) destructor of all elements stored in the pool.
 
T * allocate ()
 Reuses memory from block and allocates a new block when no slots are free.
 
void deallocate (T *pointer)
 Free the memory used by the given pointer that has been allocated by this pool.
 
std::size_t consolidate ()
 Frees blocks that are no longer storing elements of T.
 
bool has_free_slots () const noexcept
 
std::size_t capacity () const noexcept
 
 memory_pool (memory_pool &&other)=default
 
memory_pooloperator= (memory_pool &&other)=default
 

Private Types

using Freelist = typename detail::free_list< T >
 
using FreelistIt = typename Freelist::iterator
 
using Slot = typename Freelist::slot
 
using Block = std::array< Slot, ElementsPerBlock >
 An array that stores ElementsPerBlock number of objects of type T.
 
using SizeType = typename std::conditional< ThreadSafe, std::atomic< std::size_t >, std::size_t >::type
 The last slot in the first block that has never been returned by allocate.
 

Private Member Functions

bool contains (T *p)
 
- Private Member Functions inherited from mcrl2::utilities::noncopyable
 noncopyable ()=default
 
 noncopyable (const noncopyable &)=delete
 
noncopyableoperator= (const noncopyable &)=delete
 

Private Attributes

SizeType m_current_index = ElementsPerBlock
 
SizeType m_number_of_blocks = 0
 Equal to the size of the blocks array to prevent iterating over the block list.
 
std::forward_list< Blockm_blocks
 The list of blocks allocated by this pool.
 
mcrl2::utilities::mutex m_block_mutex = {}
 Ensures that the block list is only modified by a single thread.
 
Freelist m_freelist
 Indicates the head of the freelist.
 

Detailed Description

template<class T, std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
class mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >

The memory pool allocates elements of size T from blocks.

When ThreadSafe is true then the thread-safe guarantees will be satisfied.

Definition at line 33 of file memory_pool.h.

Member Typedef Documentation

◆ Block

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
using mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::Block = std::array<Slot, ElementsPerBlock>
private

An array that stores ElementsPerBlock number of objects of type T.

Definition at line 197 of file memory_pool.h.

◆ Freelist

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
using mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::Freelist = typename detail::free_list<T>
private

Definition at line 192 of file memory_pool.h.

◆ FreelistIt

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
using mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::FreelistIt = typename Freelist::iterator
private

Definition at line 193 of file memory_pool.h.

◆ SizeType

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
using mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::SizeType = typename std::conditional<ThreadSafe, std::atomic<std::size_t>, std::size_t>::type
private

The last slot in the first block that has never been returned by allocate.

Definition at line 200 of file memory_pool.h.

◆ Slot

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
using mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::Slot = typename Freelist::slot
private

Definition at line 194 of file memory_pool.h.

Constructor & Destructor Documentation

◆ memory_pool() [1/2]

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::memory_pool ( )
default

◆ ~memory_pool()

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::~memory_pool ( )
inline

Triggers the (possibly non-trivial) destructor of all elements stored in the pool.

For all actual elements stored in the pool trigger the destructor.

Definition at line 40 of file memory_pool.h.

◆ memory_pool() [2/2]

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::memory_pool ( memory_pool< T, ElementsPerBlock, ThreadSafe > &&  other)
default

Member Function Documentation

◆ allocate()

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
T * mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::allocate ( )
inline

Reuses memory from block and allocates a new block when no slots are free.

Returns
A pointer to a block of memory that can store an object of type T. \threadsafe

Definition at line 80 of file memory_pool.h.

◆ capacity()

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
std::size_t mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::capacity ( ) const
inlinenoexcept
Returns
The total number of elements that could be stored in this memory pool.

Definition at line 182 of file memory_pool.h.

◆ consolidate()

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
std::size_t mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::consolidate ( )
inline

Frees blocks that are no longer storing elements of T.

Returns
The number of blocks that have been removed.

Definition at line 121 of file memory_pool.h.

◆ contains()

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
bool mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::contains ( T *  p)
inlineprivate
Returns
Check whether the pointer is contained in this memory pool.

Definition at line 216 of file memory_pool.h.

◆ deallocate()

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
void mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::deallocate ( T *  pointer)
inline

Free the memory used by the given pointer that has been allocated by this pool.

Definition at line 109 of file memory_pool.h.

◆ has_free_slots()

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
bool mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::has_free_slots ( ) const
inlinenoexcept
Returns
True when thi memory pool has space for at least one more element without allocating new memory.

Definition at line 175 of file memory_pool.h.

◆ operator=()

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
memory_pool & mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::operator= ( memory_pool< T, ElementsPerBlock, ThreadSafe > &&  other)
default

Member Data Documentation

◆ m_block_mutex

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
mcrl2::utilities::mutex mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::m_block_mutex = {}
private

Ensures that the block list is only modified by a single thread.

Definition at line 210 of file memory_pool.h.

◆ m_blocks

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
std::forward_list<Block> mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::m_blocks
private

The list of blocks allocated by this pool.

Definition at line 207 of file memory_pool.h.

◆ m_current_index

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
SizeType mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::m_current_index = ElementsPerBlock
private

Definition at line 201 of file memory_pool.h.

◆ m_freelist

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
Freelist mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::m_freelist
private

Indicates the head of the freelist.

Definition at line 213 of file memory_pool.h.

◆ m_number_of_blocks

template<class T , std::size_t ElementsPerBlock = 1024, bool ThreadSafe = false>
SizeType mcrl2::utilities::memory_pool< T, ElementsPerBlock, ThreadSafe >::m_number_of_blocks = 0
private

Equal to the size of the blocks array to prevent iterating over the block list.

Definition at line 204 of file memory_pool.h.


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