Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-arena.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
78# ifndef AH_ARENA_H
79# define AH_ARENA_H
80
81# include <cstdlib>
82# include <cstddef>
83# include <utility>
84# include <new>
85# include <aleph.H>
86# include <ah-errors.H>
87
88namespace Aleph
89{
90
122{
123 char* base_addr_ = nullptr;
124 char* curr_addr_ = nullptr;
125 char* end_addr_ = nullptr;
126 bool owns_memory_ = false;
127
128public:
130 enum class TemplateType { TEMPLATE };
131
133 static constexpr size_t DEFAULT_SIZE = 1024 * 1024;
134
147 AhArenaAllocator(void* buffer, const size_t size) noexcept
148 : base_addr_(static_cast<char*>(buffer)),
151 {}
152
154 AhArenaAllocator(const char* buffer, const size_t size) noexcept
155 : base_addr_(const_cast<char*>(buffer)),
158 {}
159
169 explicit AhArenaAllocator(const size_t size = DEFAULT_SIZE)
170 {
171 base_addr_ = static_cast<char*>(std::malloc(size));
173 << "AhArenaAllocator: cannot allocate " << size << " bytes";
174
177 owns_memory_ = true;
178 }
179
188 {
189 if (owns_memory_ and base_addr_ != nullptr)
190 std::free(base_addr_);
191 }
192
193 // === Move semantics ===
194
202 : base_addr_(other.base_addr_),
203 curr_addr_(other.curr_addr_),
204 end_addr_(other.end_addr_),
205 owns_memory_(other.owns_memory_)
206 {
207 other.base_addr_ = nullptr;
208 other.curr_addr_ = nullptr;
209 other.end_addr_ = nullptr;
210 other.owns_memory_ = false;
211 }
212
219 {
220 if (this != &other)
221 {
222 // Free current memory if we own it
223 if (owns_memory_ && base_addr_ != nullptr)
224 std::free(base_addr_);
225
226 // Take ownership from other
227 base_addr_ = other.base_addr_;
228 curr_addr_ = other.curr_addr_;
229 end_addr_ = other.end_addr_;
230 owns_memory_ = other.owns_memory_;
231
232 // Invalidate other
233 other.base_addr_ = nullptr;
234 other.curr_addr_ = nullptr;
235 other.end_addr_ = nullptr;
236 other.owns_memory_ = false;
237 }
238 return *this;
239 }
240
241 // Non-copyable (owns raw memory)
244
245 // === Allocation interface ===
246
257
271 [[nodiscard]] void* alloc(const size_t size) noexcept
272 {
273 if (size == 0)
274 return nullptr;
275
276 if (const auto avail = static_cast<size_t>(end_addr_ - curr_addr_); size > avail)
277 return nullptr;
278
279 char* result = curr_addr_;
280 curr_addr_ += size;
281 return result;
282 }
283
294 [[nodiscard]] void* alloc_aligned(const size_t size, const size_t alignment) noexcept
295 {
296 if (size == 0 or alignment == 0)
297 return nullptr;
298
299 // Calculate aligned address
300 const auto current = reinterpret_cast<uintptr_t>(curr_addr_);
301 const uintptr_t aligned = (current + alignment - 1) & ~(alignment - 1);
302
303 const auto padding = aligned - current;
304 const auto avail = static_cast<size_t>(end_addr_ - curr_addr_);
305 if (padding > avail)
306 return nullptr;
307 if (size > (avail - padding))
308 return nullptr;
309
310 curr_addr_ = reinterpret_cast<char*>(aligned) + size;
311 return reinterpret_cast<void*>(aligned);
312 }
313
326 void dealloc(const void* addr, const size_t size) noexcept
327 {
328 if (size == 0 or addr == nullptr)
329 return;
330
331 // Only deallocate if this was the last allocation (LIFO)
332 if (static_cast<const char*>(addr) + size == curr_addr_)
333 curr_addr_ -= size;
334 }
335
336 // === Typed allocation ===
337
352 template <typename T, typename... Args>
354 {
355 void* ptr = alloc_aligned(sizeof(T), alignof(T));
356 if (ptr == nullptr)
357 return nullptr;
358 return new (ptr) T(std::forward<Args>(args)...);
359 }
360
371 template <typename T>
372 void dealloc([[maybe_unused]] TemplateType tag, void* ptr) noexcept
373 {
374 if (ptr == nullptr)
375 return;
376 static_cast<T*>(ptr)->~T();
377 dealloc(ptr, sizeof(T));
378 }
379
380 // === Query interface ===
381
384 {
385 return base_addr_ != nullptr;
386 }
387
390 {
391 return owns_memory_;
392 }
393
396 {
397 return static_cast<size_t>(curr_addr_ - base_addr_);
398 }
399
402 {
403 return static_cast<size_t>(end_addr_ - curr_addr_);
404 }
405
408 {
409 return static_cast<size_t>(end_addr_ - base_addr_);
410 }
411
414 {
415 return curr_addr_ == base_addr_;
416 }
417
420 {
421 return curr_addr_ == end_addr_;
422 }
423
425 [[nodiscard]] const void* base_addr() const noexcept { return base_addr_; }
426
428 [[nodiscard]] const void* next_avail_addr() const noexcept { return curr_addr_; }
429
431 [[nodiscard]] const void* end_addr() const noexcept { return end_addr_; }
432
439 [[nodiscard]] bool contains(const void* ptr) const noexcept
440 {
441 const auto p = static_cast<const char*>(ptr);
442 return p >= base_addr_ and p < end_addr_;
443 }
444
445 // === Backward compatibility aliases ===
446
448 [[nodiscard]] void* allocate(size_t size) noexcept { return alloc(size); }
449
451 void deallocate(const void* addr, size_t size) noexcept { dealloc(addr, size); }
452};
453
454// === Free function interface ===
455
477template <class T, typename... Args>
479{
481 std::forward<Args>(args)...);
482}
483
493template <class T>
494void deallocate(AhArenaAllocator& arena, T* ptr) noexcept
495{
496 arena.dealloc<T>(AhArenaAllocator::TemplateType::TEMPLATE, ptr);
497}
498
500template <class T>
501void dealloc(AhArenaAllocator& arena, T* ptr) noexcept
502{
503 deallocate<T>(arena, ptr);
504}
505
506} // end namespace Aleph
507
508# endif // AH_ARENA_H
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:266
Core header for the Aleph-w library.
Arena allocator for fast bump-pointer allocation.
Definition ah-arena.H:122
const void * base_addr() const noexcept
Get base address of the arena.
Definition ah-arena.H:425
AhArenaAllocator(const AhArenaAllocator &)=delete
AhArenaAllocator(AhArenaAllocator &&other) noexcept
Move constructor.
Definition ah-arena.H:201
AhArenaAllocator & operator=(const AhArenaAllocator &)=delete
const void * next_avail_addr() const noexcept
Get next available address (current allocation pointer).
Definition ah-arena.H:428
void deallocate(const void *addr, size_t size) noexcept
Definition ah-arena.H:451
AhArenaAllocator(const size_t size=DEFAULT_SIZE)
Construct arena with internally allocated memory.
Definition ah-arena.H:169
AhArenaAllocator & operator=(AhArenaAllocator &&other) noexcept
Move assignment operator.
Definition ah-arena.H:218
~AhArenaAllocator()
Destructor.
Definition ah-arena.H:187
void dealloc(TemplateType tag, void *ptr) noexcept
Destruct and deallocate an object.
Definition ah-arena.H:372
bool is_valid() const noexcept
Returns true if the arena is valid (has memory).
Definition ah-arena.H:383
bool contains(const void *ptr) const noexcept
Check if a pointer is within this arena's range.
Definition ah-arena.H:439
void * alloc(const size_t size) noexcept
Allocate raw memory from the arena.
Definition ah-arena.H:271
void * alloc_aligned(const size_t size, const size_t alignment) noexcept
Allocate aligned memory from the arena.
Definition ah-arena.H:294
void reset() noexcept
Reset arena, making all memory available again.
Definition ah-arena.H:256
const void * end_addr() const noexcept
Get end address (one past last valid byte).
Definition ah-arena.H:431
AhArenaAllocator(void *buffer, const size_t size) noexcept
Construct arena from existing memory buffer.
Definition ah-arena.H:147
void dealloc(const void *addr, const size_t size) noexcept
Deallocate memory (only effective for LIFO pattern).
Definition ah-arena.H:326
bool owns_memory() const noexcept
Returns true if the arena owns its memory.
Definition ah-arena.H:389
size_t capacity() const noexcept
Get total arena capacity.
Definition ah-arena.H:407
TemplateType
Tag type for dispatching to templated allocation methods.
Definition ah-arena.H:130
size_t allocated_size() const noexcept
Get total bytes currently allocated.
Definition ah-arena.H:395
bool full() const noexcept
Returns true if the arena is full (no space left).
Definition ah-arena.H:419
bool empty() const noexcept
Returns true if the arena is empty (no allocations).
Definition ah-arena.H:413
size_t available_size() const noexcept
Get remaining bytes available.
Definition ah-arena.H:401
T * alloc(TemplateType tag, Args &&... args)
Allocate and construct an object of type T.
Definition ah-arena.H:353
static constexpr size_t DEFAULT_SIZE
Default arena size (1 MB).
Definition ah-arena.H:133
AhArenaAllocator(const char *buffer, const size_t size) noexcept
Overload for const buffer (uses const_cast internally).
Definition ah-arena.H:154
void * allocate(size_t size) noexcept
Definition ah-arena.H:448
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void dealloc(AhArenaAllocator &arena, T *ptr) noexcept
Alias for deallocate (backward compatibility).
Definition ah-arena.H:501
size_t size(Node *root) noexcept
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
T * allocate(AhArenaAllocator &arena, Args &&... args)
Allocate and construct an object in an arena.
Definition ah-arena.H:478
void deallocate(AhArenaAllocator &arena, T *ptr) noexcept
Destruct and deallocate an object from an arena.
Definition ah-arena.H:494
DynList< T > maps(const C &c, Op op)
Classic map operation.