mcrl2::utilities::memory_pool

Include file:

#include "mcrl2/utilities/memory_pool.h
class mcrl2::utilities::memory_pool

The memory pool allocates elements of size T from blocks.

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

Private types

type Block

typedef for std::array< Slot, ElementsPerBlock >

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

type Freelist

typedef for typename detail::free_list< T >

type FreelistIt

typedef for typename Freelist::iterator

type SizeType

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

type Slot

typedef for typename Freelist::slot

Private attributes

mcrl2::utilities::spinlock m_block_mutex

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

std::forward_list<Block> m_blocks

The list of blocks allocated by this pool.

SizeType m_current_index
Freelist m_freelist

Indicates the head of the freelist.

SizeType m_number_of_blocks

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

Public member functions

T *allocate()

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.

std::size_t capacity() const noexcept

Returns: The total number of elements that could be stored in this memory pool.

std::size_t consolidate()

Frees blocks that are no longer storing elements of T.

Returns: The number of blocks that have been removed.

void deallocate(T *pointer)

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

bool has_free_slots() const noexcept

Returns: True when thi memory pool has space for at least one more element without allocating new memory.

memory_pool() = default
memory_pool(memory_pool &&other) = default
memory_pool &operator=(memory_pool &&other) = default
~memory_pool()

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

Private member functions

bool contains(T *p)

Returns: Check whether the pointer is contained in this memory pool.