4#ifndef WPI_MEMORY_MEMORY_ARENA_HPP_INCLUDED
5#define WPI_MEMORY_MEMORY_ARENA_HPP_INCLUDED
40 :
memory_block(begin,
static_cast<std::size_t
>(
static_cast<char*
>(end)
41 -
static_cast<char*
>(begin)))
46 bool contains(
const void* address)
const noexcept
48 auto mem =
static_cast<const char*
>(
memory);
49 auto addr =
static_cast<const char*
>(address);
50 return addr >= mem && addr < mem +
size;
56 template <
class BlockAllocator>
60 std::declval<BlockAllocator&>().allocate_block()),
62 std::declval<BlockAllocator&>().next_block_size()),
77 template <
class BlockAllocator,
bool Cached = true>
105 other.head_ =
nullptr;
147 auto mem =
static_cast<void*
>(head_);
153 return head_ ==
nullptr;
159 std::size_t
size() const noexcept;
165 std::size_t usable_size;
167 node(node* p, std::size_t size) noexcept : prev(p), usable_size(size) {}
173 template <
bool Cached>
182 return cached_.empty();
187 return cached_.size();
192 return cached_.top().size;
199 used.steal_top(cached_);
203 template <
class BlockAllocator>
206 cached_.steal_top(used);
209 template <
class BlockAllocator>
215 while (!cached_.empty())
218 while (!to_dealloc.
empty())
219 alloc.deallocate_block(to_dealloc.
pop());
250 template <
class BlockAllocator>
254 alloc.deallocate_block(used.pop());
257 template <
class BlockAllocator>
275 template <
class BlockAllocator,
bool Cached >
277 WPI_EBO(detail::memory_arena_cache<Cached>)
280 "BlockAllocator is not a BlockAllocator!");
299 template <
typename... Args>
312 while (!used_.
empty())
313 allocator_type::deallocate_block(used_.
pop());
352 if (!this->take_from_cache(used_))
353 used_.
push(allocator_type::allocate_block());
355 auto block = used_.
top();
373 auto block = used_.
top();
401 return cache::cache_size();
406 std::size_t
size() const noexcept
417 return this->cache_empty() ?
418 allocator_type::next_block_size()
420 this->cached_block_size();
434#if WPI_MEMORY_EXTERN_TEMPLATE
435 extern template class memory_arena<static_block_allocator, true>;
436 extern template class memory_arena<static_block_allocator, false>;
437 extern template class memory_arena<virtual_block_allocator, true>;
438 extern template class memory_arena<virtual_block_allocator, false>;
447 template <
class RawAllocator = default_allocator,
unsigned Num = 2,
unsigned Den = 1>
451 static_assert(float(Num) / Den >= 1.0,
"invalid growth factor");
475 block_size_ = grow_block_size(block_size_);
484 traits::deallocate_array(get_allocator(), block.memory, block.size, 1,
503 static constexpr auto factor = float(Num) / Den;
509 return block_size * Num / Den;
513 std::size_t block_size_;
516#if WPI_MEMORY_EXTERN_TEMPLATE
517 extern template class growing_block_allocator<>;
518 extern template class memory_arena<growing_block_allocator<>,
true>;
519 extern template class memory_arena<growing_block_allocator<>,
false>;
526 template <
class RawAllocator = default_allocator>
566 block_size_ = block.size;
587 std::size_t block_size_;
590#if WPI_MEMORY_EXTERN_TEMPLATE
591 extern template class fixed_block_allocator<>;
592 extern template class memory_arena<fixed_block_allocator<>,
true>;
593 extern template class memory_arena<fixed_block_allocator<>,
false>;
598 template <
class RawAlloc>
601 template <
template <
class...>
class Wrapper,
class BlockAllocator,
typename... Args>
608 template <
template <
class...>
class Wrapper,
class RawAlloc>
610 RawAlloc alloc = RawAlloc()) -> Wrapper<RawAlloc>
612 return Wrapper<RawAlloc>(block_size,
detail::move(alloc));
620 template <
class BlockOrRawAllocator,
625 BlockAllocator<BlockOrRawAllocator>>::type);
632 template <
class BlockOrRawAllocator,
typename... Args>
642 template <
template <
class...>
class BlockAllocator,
class BlockOrRawAllocator,
645 std::size_t block_size, Args&&... args)
659 constexpr std::size_t
operator"" _KiB(
unsigned long long value)
noexcept
661 return std::size_t(value * 1024);
664 constexpr std::size_t
operator"" _KB(
unsigned long long value)
noexcept
666 return std::size_t(value * 1000);
669 constexpr std::size_t
operator"" _MiB(
unsigned long long value)
noexcept
671 return std::size_t(value * 1024 * 1024);
674 constexpr std::size_t
operator"" _MB(
unsigned long long value)
noexcept
676 return std::size_t(value * 1000 * 1000);
679 constexpr std::size_t
operator"" _GiB(
unsigned long long value)
noexcept
681 return std::size_t(value * 1024 * 1024 * 1024);
684 constexpr std::size_t
operator"" _GB(
unsigned long long value)
noexcept
686 return std::size_t(value * 1000 * 1000 * 1000);
The default specialization of the wpi::memory::allocator_traits.
The default specialization of the allocator_traits for a RawAllocator.
Definition allocator_traits.hpp:292
static void deallocate_array(allocator_type &state, void *array, std::size_t count, std::size_t size, std::size_t alignment) noexcept
Definition allocator_traits.hpp:328
traits_detail::allocator_type< Allocator > allocator_type
Definition allocator_traits.hpp:294
static void * allocate_array(allocator_type &state, std::size_t count, std::size_t size, std::size_t alignment)
Definition allocator_traits.hpp:308
void do_shrink_to_fit(BlockAllocator &alloc) noexcept
Definition memory_arena.hpp:210
std::size_t cache_size() const noexcept
Definition memory_arena.hpp:185
void do_deallocate_block(BlockAllocator &, detail::memory_block_stack &used) noexcept
Definition memory_arena.hpp:204
std::size_t cached_block_size() const noexcept
Definition memory_arena.hpp:190
bool take_from_cache(detail::memory_block_stack &used) noexcept
Definition memory_arena.hpp:195
bool cache_empty() const noexcept
Definition memory_arena.hpp:180
std::size_t cached_block_size() const noexcept
Definition memory_arena.hpp:240
void do_deallocate_block(BlockAllocator &alloc, detail::memory_block_stack &used) noexcept
Definition memory_arena.hpp:251
bool cache_empty() const noexcept
Definition memory_arena.hpp:230
void do_shrink_to_fit(BlockAllocator &) noexcept
Definition memory_arena.hpp:258
bool take_from_cache(detail::memory_block_stack &) noexcept
Definition memory_arena.hpp:245
std::size_t cache_size() const noexcept
Definition memory_arena.hpp:235
Definition memory_arena.hpp:174
Definition memory_arena.hpp:97
memory_block_stack() noexcept
Definition memory_arena.hpp:99
bool empty() const noexcept
Definition memory_arena.hpp:151
friend void swap(memory_block_stack &a, memory_block_stack &b) noexcept
Definition memory_arena.hpp:115
memory_block_stack(memory_block_stack &&other) noexcept
Definition memory_arena.hpp:103
static constexpr std::size_t implementation_offset() noexcept
Definition memory_arena.hpp:127
memory_block_stack & operator=(memory_block_stack &&other) noexcept
Definition memory_arena.hpp:108
void push(allocated_mb block) noexcept
std::size_t size() const noexcept
inserted_mb top() const noexcept
Definition memory_arena.hpp:144
allocated_mb pop() noexcept
void steal_top(memory_block_stack &other) noexcept
~memory_block_stack() noexcept
Definition memory_arena.hpp:101
bool owns(const void *ptr) const noexcept
A BlockAllocator that allows only one block allocation.
Definition memory_arena.hpp:528
std::size_t next_block_size() const noexcept
Definition memory_arena.hpp:570
allocator_type & get_allocator() noexcept
Definition memory_arena.hpp:576
fixed_block_allocator(std::size_t block_size, allocator_type alloc=allocator_type()) noexcept
Definition memory_arena.hpp:536
memory_block allocate_block()
Definition memory_arena.hpp:545
void deallocate_block(memory_block block) noexcept
Definition memory_arena.hpp:560
typename traits::allocator_type allocator_type
Definition memory_arena.hpp:532
A BlockAllocator that uses a given RawAllocator for allocating the blocks.
Definition memory_arena.hpp:450
static float growth_factor() noexcept
Definition memory_arena.hpp:501
memory_block allocate_block()
Definition memory_arena.hpp:470
std::size_t next_block_size() const noexcept
Definition memory_arena.hpp:489
static std::size_t grow_block_size(std::size_t block_size) noexcept
Definition memory_arena.hpp:507
growing_block_allocator(std::size_t block_size, allocator_type alloc=allocator_type()) noexcept
Definition memory_arena.hpp:461
void deallocate_block(memory_block block) noexcept
Definition memory_arena.hpp:482
typename traits::allocator_type allocator_type
Definition memory_arena.hpp:456
allocator_type & get_allocator() noexcept
Definition memory_arena.hpp:495
A memory arena that manages huge memory blocks for a higher-level allocator.
Definition memory_arena.hpp:278
BlockAllocator allocator_type
Definition memory_arena.hpp:284
memory_arena(memory_arena &&other) noexcept
Definition memory_arena.hpp:321
void shrink_to_fit() noexcept
Definition memory_arena.hpp:387
friend void swap(memory_arena &a, memory_arena &b) noexcept
Definition memory_arena.hpp:338
memory_block allocate_block()
Definition memory_arena.hpp:350
std::integral_constant< bool, Cached > is_cached
Definition memory_arena.hpp:285
std::size_t cache_size() const noexcept
Definition memory_arena.hpp:399
memory_block current_block() const noexcept
Definition memory_arena.hpp:362
std::size_t next_block_size() const noexcept
Definition memory_arena.hpp:415
allocator_type & get_allocator() noexcept
Definition memory_arena.hpp:425
std::size_t capacity() const noexcept
Definition memory_arena.hpp:393
void deallocate_block() noexcept
Definition memory_arena.hpp:371
static constexpr std::size_t min_block_size(std::size_t byte_size) noexcept
Definition memory_arena.hpp:290
~memory_arena() noexcept
Definition memory_arena.hpp:307
bool owns(const void *ptr) const noexcept
Definition memory_arena.hpp:379
memory_arena & operator=(memory_arena &&other) noexcept
Definition memory_arena.hpp:328
std::size_t size() const noexcept
Definition memory_arena.hpp:406
memory_arena(std::size_t block_size, Args &&... args)
Definition memory_arena.hpp:300
A special case of out_of_memory errors thrown when a low-level allocator with a fixed size runs out o...
Definition error.hpp:121
#define WPI_MEMORY_LOG_PREFIX
Definition config.hpp:46
#define WPI_THROW(Ex)
Definition config.hpp:33
The typedef wpi::memory::default_allocator.
implementation_defined make_block_allocator_t
Takes either a BlockAllocator or a RawAllocator.
Definition memory_arena.hpp:622
constexpr bool cached_arena
Definition memory_arena.hpp:89
make_block_allocator_t< BlockOrRawAllocator > make_block_allocator(std::size_t block_size, Args &&... args)
Definition memory_arena.hpp:633
constexpr bool uncached_arena
Definition memory_arena.hpp:90
detail namespace with internal helper functions
Definition input_adapters.h:32
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
BlockAllocator make_block_allocator(std::true_type, std::size_t block_size, Args &&... args)
Definition memory_arena.hpp:602
void adl_swap(T &a, T &b) noexcept
Definition utility.hpp:60
growing_block_allocator< RawAlloc > default_block_wrapper
Definition memory_arena.hpp:599
T && forward(typename std::remove_reference< T >::type &t) noexcept
Definition utility.hpp:31
constexpr std::size_t max_alignment
Definition align.hpp:42
std::remove_reference< T >::type && move(T &&arg) noexcept
Definition utility.hpp:25
std::true_type is_block_allocator_impl(int,,,)
void debug_check_pointer(Functor condition, const allocator_info &info, void *ptr)
Definition debug_helpers.hpp:71
void debug_fill_internal(void *, std::size_t, bool) noexcept
Definition debug_helpers.hpp:62
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
Contains information about an allocator.
Definition error.hpp:23
Traits that check whether a type models concept BlockAllocator.
Definition memory_arena.hpp:73
A memory block.
Definition memory_arena.hpp:28
memory_block(void *mem, std::size_t s) noexcept
Definition memory_arena.hpp:36
memory_block(void *begin, void *end) noexcept
Definition memory_arena.hpp:39
std::size_t size
The size of the memory block (might be 0).
Definition memory_arena.hpp:30
memory_block() noexcept
Definition memory_arena.hpp:33
void * memory
The address of the memory block (might be nullptr).
Definition memory_arena.hpp:29
bool contains(const void *address) const noexcept
Definition memory_arena.hpp:46
#define WPI_SFINAE(Expr)
Definition utility.hpp:85
#define WPI_MEMORY_ASSERT(Expr)
Definition assert.hpp:46