10#ifndef MCRL2_UTILITIES_MEMORY_POOL_H_
11#define MCRL2_UTILITIES_MEMORY_POOL_H_
19#include <forward_list>
31 std::size_t ElementsPerBlock = 1024,
32 bool ThreadSafe =
false>
45 bool first_block =
true;
53 auto& slot = block[index];
54 if (!slot.is_marked())
56 reinterpret_cast<T*
>(&slot)->~T();
63 for (
auto& slot : block)
65 if (!slot.is_marked())
67 reinterpret_cast<T*
>(&slot)->~T();
129 auto block_before_it =
m_blocks.before_begin();
138 bool block_only_freelist =
true;
139 for (
Slot& slot : block)
141 if (slot.is_marked())
148 block_only_freelist =
false;
153 if (block_only_freelist)
159 it =
m_blocks.erase_after(block_before_it);
164 block_before_it = it;
194 using Slot =
typename Freelist::slot;
197 using Block = std::array<Slot, ElementsPerBlock>;
200 using SizeType =
typename std::conditional<ThreadSafe, std::atomic<std::size_t>, std::size_t>::type;
218 assert(p !=
nullptr);
220 std::uintptr_t
pointer =
reinterpret_cast<std::uintptr_t
>(p);
223 std::uintptr_t firstSlot =
reinterpret_cast<std::uintptr_t
>(block.data());
This essentially implements the std::forward_list, with the difference that it does not own the nodes...
The memory pool allocates elements of size T from blocks.
typename Freelist::slot Slot
~memory_pool()
Triggers the (possibly non-trivial) destructor of all elements stored in the pool.
std::size_t capacity() const noexcept
memory_pool & operator=(memory_pool &&other)=default
memory_pool(memory_pool &&other)=default
T * allocate()
Reuses memory from block and allocates a new block when no slots are free.
std::array< Slot, ElementsPerBlock > Block
An array that stores ElementsPerBlock number of objects of type T.
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.
mcrl2::utilities::mutex m_block_mutex
Ensures that the block list is only modified by a single thread.
typename Freelist::iterator FreelistIt
typename std::conditional< ThreadSafe, std::atomic< std::size_t >, std::size_t >::type SizeType
The last slot in the first block that has never been returned by allocate.
typename detail::free_list< T > Freelist
bool has_free_slots() const noexcept
Freelist m_freelist
Indicates the head of the freelist.
std::forward_list< Block > m_blocks
The list of blocks allocated by this pool.
SizeType m_number_of_blocks
Equal to the size of the blocks array to prevent iterating over the block list.
This is simply an exclusive lock based on the standard library with the ability to perform no locks w...
Inherit from this class to prevent it from being copyable.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...