|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Arena allocator for fast bump-pointer allocation. More...
#include <ah-arena.H>
Public Types | |
| enum class | TemplateType { TEMPLATE } |
| Tag type for dispatching to templated allocation methods. More... | |
Public Member Functions | |
| AhArenaAllocator (void *buffer, const size_t size) noexcept | |
| Construct arena from existing memory buffer. | |
| AhArenaAllocator (const char *buffer, const size_t size) noexcept | |
| Overload for const buffer (uses const_cast internally). | |
| AhArenaAllocator (const size_t size=DEFAULT_SIZE) | |
| Construct arena with internally allocated memory. | |
| ~AhArenaAllocator () | |
| Destructor. | |
| AhArenaAllocator (AhArenaAllocator &&other) noexcept | |
| Move constructor. | |
| AhArenaAllocator & | operator= (AhArenaAllocator &&other) noexcept |
| Move assignment operator. | |
| AhArenaAllocator (const AhArenaAllocator &)=delete | |
| AhArenaAllocator & | operator= (const AhArenaAllocator &)=delete |
| void | reset () noexcept |
| Reset arena, making all memory available again. | |
| void * | alloc (const size_t size) noexcept |
| Allocate raw memory from the arena. | |
| void * | alloc_aligned (const size_t size, const size_t alignment) noexcept |
| Allocate aligned memory from the arena. | |
| void | dealloc (const void *addr, const size_t size) noexcept |
| Deallocate memory (only effective for LIFO pattern). | |
| template<typename T , typename... Args> | |
| T * | alloc (TemplateType tag, Args &&... args) |
| Allocate and construct an object of type T. | |
| template<typename T > | |
| void | dealloc (TemplateType tag, void *ptr) noexcept |
| Destruct and deallocate an object. | |
| bool | is_valid () const noexcept |
| Returns true if the arena is valid (has memory). | |
| bool | owns_memory () const noexcept |
| Returns true if the arena owns its memory. | |
| size_t | allocated_size () const noexcept |
| Get total bytes currently allocated. | |
| size_t | available_size () const noexcept |
| Get remaining bytes available. | |
| size_t | capacity () const noexcept |
| Get total arena capacity. | |
| bool | empty () const noexcept |
| Returns true if the arena is empty (no allocations). | |
| bool | full () const noexcept |
| Returns true if the arena is full (no space left). | |
| const void * | base_addr () const noexcept |
| Get base address of the arena. | |
| const void * | next_avail_addr () const noexcept |
| Get next available address (current allocation pointer). | |
| const void * | end_addr () const noexcept |
| Get end address (one past last valid byte). | |
| bool | contains (const void *ptr) const noexcept |
| Check if a pointer is within this arena's range. | |
| void * | allocate (size_t size) noexcept |
| void | deallocate (const void *addr, size_t size) noexcept |
Static Public Attributes | |
| static constexpr size_t | DEFAULT_SIZE = 1024 * 1024 |
| Default arena size (1 MB). | |
Private Attributes | |
| char * | base_addr_ = nullptr |
| char * | curr_addr_ = nullptr |
| char * | end_addr_ = nullptr |
| bool | owns_memory_ = false |
Arena allocator for fast bump-pointer allocation.
AhArenaAllocator provides a simple and fast memory allocation strategy where allocations are satisfied by incrementing a pointer ("bump" allocation). Individual deallocations are only effective for LIFO (stack-like) patterns.
| Operation | Complexity |
|---|---|
| alloc() | O(1) |
| dealloc() | O(1) |
| reset() | O(1) |
Definition at line 121 of file ah-arena.H.
Tag type for dispatching to templated allocation methods.
| Enumerator | |
|---|---|
| TEMPLATE | |
Definition at line 130 of file ah-arena.H.
Construct arena from existing memory buffer.
The arena will use the provided buffer but will NOT free it on destruction. The caller retains ownership of the buffer.
| [in] | buffer | Pointer to pre-allocated buffer. |
| [in] | size | Size of the buffer in bytes. |
Definition at line 147 of file ah-arena.H.
Overload for const buffer (uses const_cast internally).
Definition at line 154 of file ah-arena.H.
|
inlineexplicit |
Construct arena with internally allocated memory.
Allocates a contiguous block of memory using malloc(). The memory is automatically freed when the arena is destroyed.
| [in] | size | Size of arena in bytes (default 1MB). |
| std::runtime_error | If memory allocation fails. |
Definition at line 169 of file ah-arena.H.
References ah_runtime_error_if, base_addr_, curr_addr_, end_addr_, owns_memory_, and Aleph::size().
|
inline |
Destructor.
Frees internally allocated memory if any.
Definition at line 187 of file ah-arena.H.
References base_addr_, Aleph::maps(), and owns_memory_.
|
inlinenoexcept |
Move constructor.
Transfers ownership of the arena's memory. The moved-from arena becomes empty and invalid for further allocations.
Definition at line 201 of file ah-arena.H.
References Aleph::maps().
|
delete |
Allocate raw memory from the arena.
Returns a pointer to at least size bytes of memory, or nullptr if insufficient space remains.
| [in] | size | Number of bytes to allocate. |
Definition at line 271 of file ah-arena.H.
References curr_addr_, end_addr_, and Aleph::size().
Referenced by Aleph::allocate(), allocate(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST_F().
|
inline |
Allocate and construct an object of type T.
Allocates sizeof(T) bytes and constructs T in-place using placement new with the provided arguments.
| T | Type to construct. |
| Args | Constructor argument types. |
| [in] | tag | Disambiguation tag (use TemplateType::TEMPLATE) - unused parameter. |
| [in] | args | Constructor arguments (forwarded). |
Definition at line 353 of file ah-arena.H.
References alloc_aligned(), and Aleph::maps().
|
inlinenoexcept |
Allocate aligned memory from the arena.
Returns a pointer to at least size bytes of memory aligned to alignment bytes, or nullptr if insufficient space.
| [in] | size | Number of bytes to allocate. |
| [in] | alignment | Required alignment (must be power of 2). |
Definition at line 294 of file ah-arena.H.
References curr_addr_, end_addr_, Aleph::maps(), and Aleph::size().
Referenced by alloc(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Definition at line 448 of file ah-arena.H.
References alloc(), and Aleph::size().
Referenced by TEST().
|
inlinenoexcept |
Get total bytes currently allocated.
Definition at line 395 of file ah-arena.H.
References base_addr_, and curr_addr_.
Referenced by Aleph::ArenaTreeAllocator< Node >::allocated_size(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlinenoexcept |
Get remaining bytes available.
Definition at line 401 of file ah-arena.H.
References curr_addr_, and end_addr_.
Referenced by Aleph::ArenaTreeAllocator< Node >::available_size(), TEST(), and TEST().
|
inlinenoexcept |
Get total arena capacity.
Definition at line 407 of file ah-arena.H.
References base_addr_, and end_addr_.
Check if a pointer is within this arena's range.
| [in] | ptr | Pointer to check. |
Definition at line 439 of file ah-arena.H.
References base_addr_, end_addr_, and Aleph::maps().
Deallocate memory (only effective for LIFO pattern).
If the address matches the most recently allocated block (and the size is correct), the memory is reclaimed. Otherwise, this is a no-op.
| [in] | addr | Address to deallocate. |
| [in] | size | Size of the allocation. |
Definition at line 326 of file ah-arena.H.
References curr_addr_, Aleph::maps(), and Aleph::size().
Referenced by dealloc(), deallocate(), TEST(), TEST(), TEST(), and TEST().
|
inlinenoexcept |
Destruct and deallocate an object.
Calls the destructor of T and attempts to reclaim the memory (only succeeds for LIFO deallocation pattern).
| T | Type of object. |
| [in] | tag | Disambiguation tag (use TemplateType::TEMPLATE) - unused parameter. |
| [in] | ptr | Pointer to object. |
Definition at line 372 of file ah-arena.H.
References dealloc().
Definition at line 451 of file ah-arena.H.
References dealloc(), Aleph::maps(), and Aleph::size().
Referenced by TEST().
|
inlinenoexcept |
Returns true if the arena is empty (no allocations).
Definition at line 413 of file ah-arena.H.
References base_addr_, and curr_addr_.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), and TEST_F().
Get end address (one past last valid byte).
Definition at line 431 of file ah-arena.H.
References end_addr_.
|
inlinenoexcept |
Returns true if the arena is full (no space left).
Definition at line 419 of file ah-arena.H.
References curr_addr_, and end_addr_.
|
inlinenoexcept |
Returns true if the arena is valid (has memory).
Definition at line 383 of file ah-arena.H.
References base_addr_.
Get next available address (current allocation pointer).
Definition at line 428 of file ah-arena.H.
References curr_addr_.
|
inlinenoexcept |
Move assignment operator.
Frees current memory (if owned) and takes ownership from other.
Definition at line 218 of file ah-arena.H.
References base_addr_, curr_addr_, end_addr_, Aleph::maps(), and owns_memory_.
|
delete |
|
inlinenoexcept |
Returns true if the arena owns its memory.
Definition at line 389 of file ah-arena.H.
References owns_memory_.
|
inlinenoexcept |
Reset arena, making all memory available again.
After reset, the arena can be reused for new allocations. All previously returned pointers become invalid.
Definition at line 256 of file ah-arena.H.
References base_addr_, and curr_addr_.
Definition at line 123 of file ah-arena.H.
Referenced by AhArenaAllocator(), ~AhArenaAllocator(), allocated_size(), base_addr(), capacity(), contains(), empty(), is_valid(), operator=(), and reset().
Definition at line 124 of file ah-arena.H.
Referenced by AhArenaAllocator(), alloc(), alloc_aligned(), allocated_size(), available_size(), dealloc(), empty(), full(), next_avail_addr(), operator=(), and reset().
Definition at line 125 of file ah-arena.H.
Referenced by AhArenaAllocator(), alloc(), alloc_aligned(), available_size(), capacity(), contains(), end_addr(), full(), and operator=().
Definition at line 126 of file ah-arena.H.
Referenced by AhArenaAllocator(), ~AhArenaAllocator(), operator=(), and owns_memory().