4#ifndef WPI_MEMORY_MEMORY_POOL_HPP_INCLUDED
5#define WPI_MEMORY_MEMORY_POOL_HPP_INCLUDED
8#define WPI_MEMORY_MEMORY_POOL_HAS_MIN_BLOCK_SIZE
46 template <
typename PoolType = node_pool,
class BlockOrRawAllocator = default_allocator>
48 : WPI_EBO(detail::default_leak_checker<detail::memory_pool_leak_handler>)
50 using free_list =
typename PoolType::type;
58 WPI_IMPL_DEFINED(free_list::min_element_size);
66 std::size_t number_of_nodes)
noexcept
69 + free_list::min_block_size(
node_size, number_of_nodes);
79 template <
typename... Args>
81 : arena_(block_size,
detail::forward<Args>(args)...), free_list_(
node_size)
119 if (free_list_.empty())
122 return free_list_.allocate();
130 return free_list_.empty() ? nullptr : free_list_.allocate();
162 free_list_.deallocate(
ptr);
173 free_list_.deallocate(
ptr);
199 return free_list_.node_size();
207 return free_list_.capacity() *
node_size();
231 void allocate_block()
234 free_list_.insert(
static_cast<char*
>(mem.memory), mem.size);
239 auto mem = free_list_.empty() ? nullptr : free_list_.allocate(n *
node_size);
243 mem = free_list_.allocate(n *
node_size);
252 return !pool_type::value || free_list_.empty() ? nullptr :
258 if (!pool_type::value || !arena_.
owns(
ptr))
264 memory_arena<allocator_type, false> arena_;
265 free_list free_list_;
267 friend allocator_traits<memory_pool<PoolType, BlockOrRawAllocator>>;
268 friend composable_allocator_traits<memory_pool<PoolType, BlockOrRawAllocator>>;
271#if WPI_MEMORY_EXTERN_TEMPLATE
272 extern template class memory_pool<node_pool>;
273 extern template class memory_pool<array_pool>;
274 extern template class memory_pool<small_node_pool>;
277 template <
class Type,
class Alloc>
284 template <
typename PoolType,
class ImplRawAllocator>
295 std::size_t alignment)
300 alignment, [&] {
return max_alignment(state); }, state.info());
301 auto mem = state.allocate_node();
302 state.on_allocate(size);
313 std::size_t alignment)
318 alignment, [&] {
return max_alignment(state); }, state.info());
321 auto mem = state.allocate_array(count, size);
322 state.on_allocate(count * size);
328 std::size_t)
noexcept
330 state.deallocate_node(node);
331 state.on_deallocate(size);
336 std::size_t size, std::size_t)
noexcept
338 state.free_list_.deallocate(
array, count * size);
339 state.on_deallocate(count * size);
345 return state.node_size();
351 return state.next_capacity();
358 return state.free_list_.alignment();
364 template <
typename PoolType,
class BlockOrRawAllocator>
375 std::size_t alignment)
noexcept
377 if (size > traits::max_node_size(state) || alignment > traits::max_alignment(state))
379 return state.try_allocate_node();
388 std::size_t size, std::size_t alignment)
noexcept
390 if (size > traits::max_node_size(state)
391 || count * size > traits::max_array_size(state)
392 || alignment > traits::max_alignment(state))
394 return state.try_allocate_array(count, size);
400 std::size_t alignment)
noexcept
402 if (size > traits::max_node_size(state) || alignment > traits::max_alignment(state))
404 return state.try_deallocate_node(node);
410 std::size_t size, std::size_t alignment)
noexcept
412 if (size > traits::max_node_size(state)
413 || count * size > traits::max_array_size(state)
414 || alignment > traits::max_alignment(state))
416 return state.try_deallocate_array(
array, count, size);
420#if WPI_MEMORY_EXTERN_TEMPLATE
421 extern template class allocator_traits<memory_pool<node_pool>>;
422 extern template class allocator_traits<memory_pool<array_pool>>;
423 extern template class allocator_traits<memory_pool<small_node_pool>>;
425 extern template class composable_allocator_traits<memory_pool<node_pool>>;
426 extern template class composable_allocator_traits<memory_pool<array_pool>>;
427 extern template class composable_allocator_traits<memory_pool<small_node_pool>>;
This class is a wrapper around std::array that does compile time size checking.
Definition array.h:26
static void deallocate_node(allocator_type &state, void *node, std::size_t size, std::size_t) noexcept
Definition memory_pool.hpp:327
static void * allocate_array(allocator_type &state, std::size_t count, std::size_t size, std::size_t alignment)
Definition memory_pool.hpp:312
static std::size_t max_alignment(const allocator_type &state) noexcept
Definition memory_pool.hpp:356
static std::size_t max_node_size(const allocator_type &state) noexcept
Definition memory_pool.hpp:343
static std::size_t max_array_size(const allocator_type &state) noexcept
Definition memory_pool.hpp:349
std::true_type is_stateful
Definition memory_pool.hpp:289
static void deallocate_array(allocator_type &state, void *array, std::size_t count, std::size_t size, std::size_t) noexcept
Definition memory_pool.hpp:335
static void * allocate_node(allocator_type &state, std::size_t size, std::size_t alignment)
Definition memory_pool.hpp:294
The default specialization of the allocator_traits for a RawAllocator.
Definition allocator_traits.hpp:292
static bool try_deallocate_node(allocator_type &state, void *node, std::size_t size, std::size_t alignment) noexcept
Definition memory_pool.hpp:399
static void * try_allocate_node(allocator_type &state, std::size_t size, std::size_t alignment) noexcept
Definition memory_pool.hpp:374
static bool try_deallocate_array(allocator_type &state, void *array, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition memory_pool.hpp:409
static void * try_allocate_array(allocator_type &state, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition memory_pool.hpp:387
The default specialization of the composable_allocator_traits for a ComposableAllocator.
Definition allocator_traits.hpp:500
static constexpr std::size_t implementation_offset() noexcept
Definition memory_arena.hpp:127
Definition debug_helpers.hpp:102
no_leak_checker & operator=(no_leak_checker &&) noexcept
Definition debug_helpers.hpp:108
memory_block allocate_block()
Definition memory_arena.hpp:350
std::size_t next_block_size() const noexcept
Definition memory_arena.hpp:415
allocator_type & get_allocator() noexcept
Definition memory_arena.hpp:425
bool owns(const void *ptr) const noexcept
Definition memory_arena.hpp:379
A stateful RawAllocator that manages nodes of fixed size.
Definition memory_pool.hpp:49
std::size_t node_size() const noexcept
Definition memory_pool.hpp:197
std::size_t next_capacity() const noexcept
Definition memory_pool.hpp:213
~memory_pool() noexcept
Definition memory_pool.hpp:88
void * allocate_array(std::size_t n)
Definition memory_pool.hpp:140
void deallocate_node(void *ptr) noexcept
Definition memory_pool.hpp:160
PoolType pool_type
Definition memory_pool.hpp:55
void deallocate_array(void *ptr, std::size_t n) noexcept
Definition memory_pool.hpp:180
memory_pool(memory_pool &&other) noexcept
Definition memory_pool.hpp:95
memory_pool & operator=(memory_pool &&other) noexcept
Definition memory_pool.hpp:102
make_block_allocator_t< BlockOrRawAllocator > allocator_type
Definition memory_pool.hpp:54
static constexpr std::size_t min_block_size(std::size_t node_size, std::size_t number_of_nodes) noexcept
Definition memory_pool.hpp:65
bool try_deallocate_array(void *ptr, std::size_t n) noexcept
Definition memory_pool.hpp:190
void * allocate_node()
Definition memory_pool.hpp:117
allocator_type & get_allocator() noexcept
Definition memory_pool.hpp:220
void * try_allocate_node() noexcept
Definition memory_pool.hpp:128
bool try_deallocate_node(void *ptr) noexcept
Definition memory_pool.hpp:169
std::size_t capacity_left() const noexcept
Definition memory_pool.hpp:205
memory_pool(std::size_t node_size, std::size_t block_size, Args &&... args)
Definition memory_pool.hpp:80
static constexpr std::size_t min_node_size
Definition memory_pool.hpp:57
void * try_allocate_array(std::size_t n) noexcept
Definition memory_pool.hpp:152
#define WPI_MEMORY_LOG_PREFIX
Definition config.hpp:46
#define WPI_THROW(Ex)
Definition config.hpp:33
implementation_defined make_block_allocator_t
Takes either a BlockAllocator or a RawAllocator.
Definition memory_arena.hpp:622
Class wpi::memory::memory_arena and related functionality regarding BlockAllocators.
detail namespace with internal helper functions
Definition input_adapters.h:32
void check_allocation_size(std::size_t passed, Func f, const allocator_info &info)
Definition error.hpp:264
std::remove_reference< T >::type && move(T &&arg) noexcept
Definition utility.hpp:25
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Contains information about an allocator.
Definition error.hpp:23
Definition memory_pool.hpp:30
void operator()(std::ptrdiff_t amount)
#define WPI_MEMORY_ASSERT(Expr)
Definition assert.hpp:46
#define WPI_MEMORY_ASSERT_MSG(Expr, Msg)
Definition assert.hpp:47