GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Arena.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_ARENA_HPP
5#define GREM_CORE_DATA_ARENA_HPP
6
8
14
15#include <cstddef> // std::size_t
16#include <memory> // std::align
17#include <memory_resource> // std::pmr::memory_resource, std::pmr::get_default_resource
18#include <new> // std::bad_array_new_length
19#include <utility> // std::exchange
20
21namespace grem {
22
23template <typename T>
24class ArenaAllocator; // Forward declaration.
25
26class ArenaResource : public std::pmr::memory_resource {
27public:
28 GREM_ALWAYS_INLINE ArenaResource(Span<byte> initialMemory, size_t nextExtraMemoryChunkSize, std::pmr::memory_resource* memoryResource) noexcept
29 : remainingMemoryBegin(initialMemory.data())
30 , remainingMemorySize(initialMemory.size())
31 , nextExtraMemoryChunkSize(nextExtraMemoryChunkSize)
32 , memoryResource(memoryResource) {
33 GREM_ASSERT(memoryResource);
34 }
35
36 ~ArenaResource() override {
37 for (const ExtraMemoryAllocation& chunk : extraMemory) {
38 memoryResource->deallocate(chunk.memory, chunk.size, chunk.alignment);
39 }
40 }
41
42 ArenaResource(const ArenaResource&) = delete;
44
45 ArenaResource(ArenaResource&& other) noexcept
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})) {}
52
54 if (this == &other) {
55 return *this;
56 }
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});
63 return *this;
64 }
65
66 GREM_ALWAYS_INLINE void reset(Span<byte> initialMemory) noexcept {
67 remainingMemoryBegin = initialMemory.data();
68 remainingMemorySize = initialMemory.size();
69 nextAvailableExtraMemoryIndex = 0;
70 }
71
72protected:
73 void* do_allocate(std::size_t size, std::size_t alignment) final {
74 if (size == 0) {
75 [[unlikely]];
76 size = 1;
77 }
78
79 void* result = std::align(alignment, size, remainingMemoryBegin, remainingMemorySize);
80 if (!result) {
81 [[unlikely]];
82 result = allocateExtraMemory(size, alignment);
83 }
84 remainingMemoryBegin = static_cast<byte*>(remainingMemoryBegin) + size;
85 remainingMemorySize -= size;
86 return result;
87 }
88
89 void do_deallocate(void*, std::size_t, std::size_t) final {}
90
91 [[nodiscard]] bool do_is_equal(const std::pmr::memory_resource& other) const noexcept final {
92 return this == &other;
93 }
94
95private:
96 template <typename T>
97 friend class ArenaAllocator;
98
99 struct ExtraMemoryAllocation {
100 void* memory;
101 size_t size;
102 size_t alignment;
103 };
104
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) {
111 foundChunk = &chunk;
112 nextAvailableExtraMemoryIndex = chunkIndex;
113 break;
114 }
115 }
116
117 if (!foundChunk) {
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);
122 }
123 void* const newChunkMemory = memoryResource->allocate(newChunkSize, newChunkAlignment);
124 try {
125 foundChunk = &extraMemory.emplace_back(ExtraMemoryAllocation{
126 .memory = newChunkMemory,
127 .size = newChunkSize,
128 .alignment = newChunkAlignment,
129 });
130 } catch (...) {
131 memoryResource->deallocate(newChunkMemory, newChunkSize, newChunkAlignment);
132 throw;
133 }
134 nextExtraMemoryChunkSize += nextExtraMemoryChunkSize / 2;
135 nextAvailableExtraMemoryIndex = chunkIndex + 1;
136 }
137
138 remainingMemoryBegin = foundChunk->memory;
139 remainingMemorySize = foundChunk->size;
140 return remainingMemoryBegin;
141 }
142
143 void* remainingMemoryBegin;
144 size_t remainingMemorySize;
145 size_t nextExtraMemoryChunkSize;
146 std::pmr::memory_resource* memoryResource;
147 pmr::ArrayList<ExtraMemoryAllocation> extraMemory{memoryResource};
148 size_t nextAvailableExtraMemoryIndex = 0;
149};
150
151template <size_t InplaceSize = 1024, size_t InplaceAlignment = alignof(max_align_t)>
152class Arena : public ArenaResource {
153public:
154 static constexpr size_t INPLACE_SIZE = InplaceSize;
155 static constexpr size_t INPLACE_ALIGNMENT = InplaceAlignment;
156
158 : Arena(std::pmr::get_default_resource()) {}
159
160 GREM_ALWAYS_INLINE explicit Arena(std::pmr::memory_resource* memoryResource) noexcept
161 : Arena(max(size_t{1024}, InplaceSize + InplaceSize / 2), memoryResource) {}
162
163 GREM_ALWAYS_INLINE explicit Arena(size_t nextExtraMemoryChunkSize) noexcept
164 : Arena(nextExtraMemoryChunkSize, std::pmr::get_default_resource()) {}
165
166 GREM_ALWAYS_INLINE Arena(size_t nextExtraMemoryChunkSize, std::pmr::memory_resource* memoryResource) noexcept
167 : ArenaResource(initialMemory, nextExtraMemoryChunkSize, memoryResource) {}
168
169 ~Arena() override = default;
170
171 Arena(const Arena&) = delete;
172 Arena(Arena&&) = delete;
173 Arena& operator=(const Arena&) = delete;
174 Arena& operator=(Arena&&) = delete;
175
176 GREM_ALWAYS_INLINE void release() noexcept {
177 reset(initialMemory);
178 }
179
180private:
181 alignas(InplaceAlignment) byte initialMemory[InplaceSize];
182};
183
184template <size_t InplaceAlignment>
185class Arena<0, InplaceAlignment> : public ArenaResource {
186public:
187 static constexpr size_t INPLACE_SIZE = 0;
188 static constexpr size_t INPLACE_ALIGNMENT = 0;
189
191 : Arena(std::pmr::get_default_resource()) {}
192
193 GREM_ALWAYS_INLINE explicit Arena(std::pmr::memory_resource* memoryResource) noexcept
194 : Arena(size_t{1024}, memoryResource) {}
195
196 GREM_ALWAYS_INLINE explicit Arena(size_t nextExtraMemoryChunkSize) noexcept
197 : Arena(nextExtraMemoryChunkSize, std::pmr::get_default_resource()) {}
198
199 GREM_ALWAYS_INLINE Arena(size_t nextExtraMemoryChunkSize, std::pmr::memory_resource* memoryResource) noexcept
200 : ArenaResource({}, nextExtraMemoryChunkSize, memoryResource) {}
201
202 ~Arena() override = default;
203
204 Arena(const Arena&) = delete;
205 Arena(Arena&&) noexcept = default;
206 Arena& operator=(const Arena&) = delete;
207 Arena& operator=(Arena&&) noexcept = default;
208
209 GREM_ALWAYS_INLINE void release() noexcept {
210 reset({});
211 }
212};
213
214template <typename T>
216public:
217 using value_type = T;
218
220 : arena(arena) {
221 GREM_ASSERT(arena);
222 }
223
224 template <typename U>
226 : arena(other.arena) {}
227
228 ~ArenaAllocator() = default;
229
230 ArenaAllocator(const ArenaAllocator& other) noexcept = default;
231 ArenaAllocator(ArenaAllocator&& other) noexcept = default;
232 ArenaAllocator& operator=(const ArenaAllocator& other) noexcept = default;
233 ArenaAllocator& operator=(ArenaAllocator&& other) noexcept = default;
234
235 [[nodiscard]] GREM_ALWAYS_INLINE T* allocate(std::size_t n) {
236 if (n > Limits<std::size_t>::MAX / sizeof(T)) {
237 throw std::bad_array_new_length{};
238 }
239 return static_cast<T*>(arena->ArenaResource::do_allocate(n * sizeof(T), alignof(T)));
240 }
241
242 GREM_ALWAYS_INLINE void deallocate(T*, std::size_t) noexcept {}
243
244 template <typename U>
245 [[nodiscard]] GREM_ALWAYS_INLINE bool operator==(const ArenaAllocator<U>& other) const noexcept {
246 return arena == other.arena;
247 }
248
249private:
250 template <typename U>
251 friend class ArenaAllocator;
252
253 ArenaResource* arena;
254};
255
256} // namespace grem
257
258#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
#define GREM_NOINLINE
Definition attributes.hpp:25
Definition Arena.hpp:215
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
Definition Arena.hpp:26
~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
GREM_ALWAYS_INLINE Arena(size_t nextExtraMemoryChunkSize) noexcept
Definition Arena.hpp:196
GREM_ALWAYS_INLINE void release() noexcept
Definition Arena.hpp:209
static constexpr size_t INPLACE_ALIGNMENT
Definition Arena.hpp:188
Arena & operator=(Arena &&)=delete
Arena(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 Span.hpp:146
Definition Allocation.hpp:554
grem::ArrayList< T, std::pmr::polymorphic_allocator< T > > ArrayList
Definition ArrayList.hpp:691
Definition aliases.hpp:9
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