4#ifndef GREM_CORE_DATA_ARENA_HPP
5#define GREM_CORE_DATA_ARENA_HPP
17#include <memory_resource>
29 : remainingMemoryBegin(initialMemory.data())
30 , remainingMemorySize(initialMemory.size())
31 , nextExtraMemoryChunkSize(nextExtraMemoryChunkSize)
32 , memoryResource(memoryResource) {
37 for (
const ExtraMemoryAllocation& chunk : extraMemory) {
38 memoryResource->deallocate(chunk.memory, chunk.size, chunk.alignment);
46 : remainingMemoryBegin(std::exchange(other.remainingMemoryBegin,
nullptr))
47 , remainingMemorySize(std::exchange(other.remainingMemorySize,
size_t{0}))
48 , nextExtraMemoryChunkSize(std::exchange(other.nextExtraMemoryChunkSize,
size_t{0}))
49 , memoryResource(std::exchange(other.memoryResource,
nullptr))
50 , extraMemory(std::exchange(other.extraMemory, {}))
51 , nextAvailableExtraMemoryIndex(std::exchange(other.nextAvailableExtraMemoryIndex,
size_t{0})) {}
57 remainingMemoryBegin = std::exchange(other.remainingMemoryBegin,
nullptr);
58 remainingMemorySize = std::exchange(other.remainingMemorySize,
size_t{0});
59 nextExtraMemoryChunkSize = std::exchange(other.nextExtraMemoryChunkSize,
size_t{0});
60 memoryResource = std::exchange(other.memoryResource,
nullptr);
61 extraMemory = std::exchange(other.extraMemory, {});
62 nextAvailableExtraMemoryIndex = std::exchange(other.nextAvailableExtraMemoryIndex,
size_t{0});
67 remainingMemoryBegin = initialMemory.data();
68 remainingMemorySize = initialMemory.size();
69 nextAvailableExtraMemoryIndex = 0;
73 void*
do_allocate(std::size_t size, std::size_t alignment)
final {
79 void* result = std::align(alignment, size, remainingMemoryBegin, remainingMemorySize);
82 result = allocateExtraMemory(size, alignment);
84 remainingMemoryBegin =
static_cast<byte*
>(remainingMemoryBegin) + size;
85 remainingMemorySize -= size;
91 [[nodiscard]]
bool do_is_equal(
const std::pmr::memory_resource& other)
const noexcept final {
92 return this == &other;
99 struct ExtraMemoryAllocation {
105 [[nodiscard]]
GREM_NOINLINE void* allocateExtraMemory(
size_t size,
size_t alignment) {
106 ExtraMemoryAllocation* foundChunk =
nullptr;
107 size_t chunkIndex = nextAvailableExtraMemoryIndex;
108 while (chunkIndex < extraMemory.size()) {
109 ExtraMemoryAllocation& chunk = extraMemory[chunkIndex++];
110 if (chunk.size >= size && chunk.alignment >= alignment) {
112 nextAvailableExtraMemoryIndex = chunkIndex;
118 const size_t newChunkSize =
max(size, nextExtraMemoryChunkSize);
119 const size_t newChunkAlignment =
max(alignment,
alignof(max_align_t));
120 if (extraMemory.capacity() < 4) {
121 extraMemory.reserve(4);
123 void*
const newChunkMemory = memoryResource->allocate(newChunkSize, newChunkAlignment);
125 foundChunk = &extraMemory.emplace_back(ExtraMemoryAllocation{
126 .memory = newChunkMemory,
127 .size = newChunkSize,
128 .alignment = newChunkAlignment,
131 memoryResource->deallocate(newChunkMemory, newChunkSize, newChunkAlignment);
134 nextExtraMemoryChunkSize += nextExtraMemoryChunkSize / 2;
135 nextAvailableExtraMemoryIndex = chunkIndex + 1;
138 remainingMemoryBegin = foundChunk->memory;
139 remainingMemorySize = foundChunk->size;
140 return remainingMemoryBegin;
143 void* remainingMemoryBegin;
144 size_t remainingMemorySize;
145 size_t nextExtraMemoryChunkSize;
146 std::pmr::memory_resource* memoryResource;
148 size_t nextAvailableExtraMemoryIndex = 0;
151template <
size_t InplaceSize = 1024,
size_t InplaceAlignment = alignof(max_align_t)>
158 :
Arena(std::
pmr::get_default_resource()) {}
161 :
Arena(
max(
size_t{1024}, InplaceSize + InplaceSize / 2), memoryResource) {}
164 :
Arena(nextExtraMemoryChunkSize, std::pmr::get_default_resource()) {}
167 :
ArenaResource(initialMemory, nextExtraMemoryChunkSize, memoryResource) {}
177 reset(initialMemory);
181 alignas(InplaceAlignment)
byte initialMemory[InplaceSize];
184template <
size_t InplaceAlignment>
191 :
Arena(std::
pmr::get_default_resource()) {}
194 :
Arena(
size_t{1024}, memoryResource) {}
197 :
Arena(nextExtraMemoryChunkSize, std::pmr::get_default_resource()) {}
200 :
ArenaResource({}, nextExtraMemoryChunkSize, memoryResource) {}
224 template <
typename U>
226 : arena(other.arena) {}
237 throw std::bad_array_new_length{};
239 return static_cast<T*
>(arena->ArenaResource::do_allocate(n *
sizeof(T),
alignof(T)));
244 template <
typename U>
246 return arena == other.arena;
250 template <
typename U>
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
#define GREM_NOINLINE
Definition attributes.hpp:25
ArenaAllocator(const ArenaAllocator &other) noexcept=default
GREM_ALWAYS_INLINE ArenaAllocator(ArenaResource *arena) noexcept
Definition Arena.hpp:219
GREM_ALWAYS_INLINE ArenaAllocator(const ArenaAllocator< U > &other) noexcept
Definition Arena.hpp:225
GREM_ALWAYS_INLINE T * allocate(std::size_t n)
Definition Arena.hpp:235
GREM_ALWAYS_INLINE bool operator==(const ArenaAllocator< U > &other) const noexcept
Definition Arena.hpp:245
ArenaAllocator & operator=(ArenaAllocator &&other) noexcept=default
GREM_ALWAYS_INLINE void deallocate(T *, std::size_t) noexcept
Definition Arena.hpp:242
T value_type
Definition Arena.hpp:217
ArenaAllocator(ArenaAllocator &&other) noexcept=default
~ArenaAllocator()=default
friend class ArenaAllocator
Definition Arena.hpp:251
ArenaAllocator & operator=(const ArenaAllocator &other) noexcept=default
~ArenaResource() override
Definition Arena.hpp:36
ArenaResource & operator=(const ArenaResource &)=delete
GREM_ALWAYS_INLINE void reset(Span< byte > initialMemory) noexcept
Definition Arena.hpp:66
ArenaResource(const ArenaResource &)=delete
GREM_ALWAYS_INLINE ArenaResource(Span< byte > initialMemory, size_t nextExtraMemoryChunkSize, std::pmr::memory_resource *memoryResource) noexcept
Definition Arena.hpp:28
bool do_is_equal(const std::pmr::memory_resource &other) const noexcept final
Definition Arena.hpp:91
ArenaResource & operator=(ArenaResource &&other) noexcept
Definition Arena.hpp:53
void * do_allocate(std::size_t size, std::size_t alignment) final
Definition Arena.hpp:73
void do_deallocate(void *, std::size_t, std::size_t) final
Definition Arena.hpp:89
ArenaResource(ArenaResource &&other) noexcept
Definition Arena.hpp:45
friend class ArenaAllocator
Definition Arena.hpp:97
GREM_ALWAYS_INLINE Arena() noexcept
Definition Arena.hpp:190
static constexpr size_t INPLACE_SIZE
Definition Arena.hpp:187
GREM_ALWAYS_INLINE Arena(std::pmr::memory_resource *memoryResource) noexcept
Definition Arena.hpp:193
GREM_ALWAYS_INLINE Arena(size_t nextExtraMemoryChunkSize, std::pmr::memory_resource *memoryResource) noexcept
Definition Arena.hpp:199
Arena(Arena &&) noexcept=default
~Arena() override=default
GREM_ALWAYS_INLINE Arena(size_t nextExtraMemoryChunkSize) noexcept
Definition Arena.hpp:196
GREM_ALWAYS_INLINE void release() noexcept
Definition Arena.hpp:209
Arena(const Arena &)=delete
static constexpr size_t INPLACE_ALIGNMENT
Definition Arena.hpp:188
Arena & operator=(Arena &&)=delete
GREM_ALWAYS_INLINE Arena() noexcept
Definition Arena.hpp:157
Arena & operator=(const Arena &)=delete
static constexpr size_t INPLACE_SIZE
Definition Arena.hpp:154
GREM_ALWAYS_INLINE Arena(size_t nextExtraMemoryChunkSize, std::pmr::memory_resource *memoryResource) noexcept
Definition Arena.hpp:166
Arena(const Arena &)=delete
GREM_ALWAYS_INLINE void release() noexcept
Definition Arena.hpp:176
GREM_ALWAYS_INLINE Arena(std::pmr::memory_resource *memoryResource) noexcept
Definition Arena.hpp:160
GREM_ALWAYS_INLINE Arena(size_t nextExtraMemoryChunkSize) noexcept
Definition Arena.hpp:163
~Arena() override=default
static constexpr size_t INPLACE_ALIGNMENT
Definition Arena.hpp:155
Definition Allocation.hpp:554
grem::ArrayList< T, std::pmr::polymorphic_allocator< T > > ArrayList
Definition ArrayList.hpp:691
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
static constexpr T MAX
Definition fundamentals.hpp:29