Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::AhArenaAllocator Class Reference

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.
 
AhArenaAllocatoroperator= (AhArenaAllocator &&other) noexcept
 Move assignment operator.
 
 AhArenaAllocator (const AhArenaAllocator &)=delete
 
AhArenaAllocatoroperator= (const AhArenaAllocator &)=delete
 
void reset () noexcept
 Reset arena, making all memory available again.
 
voidalloc (const size_t size) noexcept
 Allocate raw memory from the arena.
 
voidalloc_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>
Talloc (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 voidbase_addr () const noexcept
 Get base address of the arena.
 
const voidnext_avail_addr () const noexcept
 Get next available address (current allocation pointer).
 
const voidend_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.
 
voidallocate (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

charbase_addr_ = nullptr
 
charcurr_addr_ = nullptr
 
charend_addr_ = nullptr
 
bool owns_memory_ = false
 

Detailed Description

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.

Performance Characteristics

Operation Complexity
alloc() O(1)
dealloc() O(1)
reset() O(1)

Memory Layout

|<------- capacity() ------->|
|<-- allocated -->|<- avail ->|
[=================[ ]
^ ^ ^
const void * base_addr() const noexcept
Get base address of the arena.
Definition ah-arena.H:425
const void * end_addr() const noexcept
Get end address (one past last valid byte).
Definition ah-arena.H:431
size_t capacity() const noexcept
Get total arena capacity.
Definition ah-arena.H:407
DynList< T > maps(const C &c, Op op)
Classic map operation.
Note
Memory is automatically freed when the arena is destroyed.
Not thread-safe. Use separate arenas per thread.

Definition at line 121 of file ah-arena.H.

Member Enumeration Documentation

◆ TemplateType

Tag type for dispatching to templated allocation methods.

Enumerator
TEMPLATE 

Definition at line 130 of file ah-arena.H.

Constructor & Destructor Documentation

◆ AhArenaAllocator() [1/5]

Aleph::AhArenaAllocator::AhArenaAllocator ( void buffer,
const size_t  size 
)
inlinenoexcept

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.

Parameters
[in]bufferPointer to pre-allocated buffer.
[in]sizeSize of the buffer in bytes.
Note
The buffer must remain valid for the lifetime of the arena.
Passing a const buffer is allowed but the arena will write to it.

Definition at line 147 of file ah-arena.H.

◆ AhArenaAllocator() [2/5]

Aleph::AhArenaAllocator::AhArenaAllocator ( const char buffer,
const size_t  size 
)
inlinenoexcept

Overload for const buffer (uses const_cast internally).

Definition at line 154 of file ah-arena.H.

◆ AhArenaAllocator() [3/5]

Aleph::AhArenaAllocator::AhArenaAllocator ( const size_t  size = DEFAULT_SIZE)
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.

Parameters
[in]sizeSize of arena in bytes (default 1MB).
Exceptions
std::runtime_errorIf 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().

◆ ~AhArenaAllocator()

Aleph::AhArenaAllocator::~AhArenaAllocator ( )
inline

Destructor.

Frees internally allocated memory if any.

Warning
Does NOT call destructors on objects allocated in the arena. The caller must ensure all objects are properly destroyed before the arena is destroyed.

Definition at line 187 of file ah-arena.H.

References base_addr_, Aleph::maps(), and owns_memory_.

◆ AhArenaAllocator() [4/5]

Aleph::AhArenaAllocator::AhArenaAllocator ( AhArenaAllocator &&  other)
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().

◆ AhArenaAllocator() [5/5]

Aleph::AhArenaAllocator::AhArenaAllocator ( const AhArenaAllocator )
delete

Member Function Documentation

◆ alloc() [1/2]

void * Aleph::AhArenaAllocator::alloc ( const size_t  size)
inlinenoexcept

Allocate raw memory from the arena.

Returns a pointer to at least size bytes of memory, or nullptr if insufficient space remains.

Parameters
[in]sizeNumber of bytes to allocate.
Returns
Pointer to allocated memory, or nullptr if insufficient space.
Note
Memory is NOT zero-initialized.
Alignment is not guaranteed beyond natural alignment of char. Use alloc_aligned() for specific alignment requirements.

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().

◆ alloc() [2/2]

template<typename T , typename... Args>
T * Aleph::AhArenaAllocator::alloc ( TemplateType  tag,
Args &&...  args 
)
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.

Template Parameters
TType to construct.
ArgsConstructor argument types.
Parameters
[in]tagDisambiguation tag (use TemplateType::TEMPLATE) - unused parameter.
[in]argsConstructor arguments (forwarded).
Returns
Pointer to constructed object, or nullptr if allocation fails.
Note
If allocation fails, no object is constructed and nullptr is returned.

Definition at line 353 of file ah-arena.H.

References alloc_aligned(), and Aleph::maps().

◆ alloc_aligned()

void * Aleph::AhArenaAllocator::alloc_aligned ( const size_t  size,
const size_t  alignment 
)
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.

Parameters
[in]sizeNumber of bytes to allocate.
[in]alignmentRequired alignment (must be power of 2).
Returns
Pointer to aligned memory, or nullptr if insufficient space.

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().

◆ allocate()

void * Aleph::AhArenaAllocator::allocate ( size_t  size)
inlinenoexcept
Deprecated:
Use alloc() instead.

Definition at line 448 of file ah-arena.H.

References alloc(), and Aleph::size().

Referenced by TEST().

◆ allocated_size()

size_t Aleph::AhArenaAllocator::allocated_size ( ) const
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().

◆ available_size()

size_t Aleph::AhArenaAllocator::available_size ( ) const
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().

◆ base_addr()

const void * Aleph::AhArenaAllocator::base_addr ( ) const
inlinenoexcept

Get base address of the arena.

Definition at line 425 of file ah-arena.H.

References base_addr_.

◆ capacity()

size_t Aleph::AhArenaAllocator::capacity ( ) const
inlinenoexcept

Get total arena capacity.

Definition at line 407 of file ah-arena.H.

References base_addr_, and end_addr_.

Referenced by TEST(), TEST(), and TEST().

◆ contains()

bool Aleph::AhArenaAllocator::contains ( const void ptr) const
inlinenoexcept

Check if a pointer is within this arena's range.

Parameters
[in]ptrPointer to check.
Returns
true if ptr is within [base_addr, end_addr).

Definition at line 439 of file ah-arena.H.

References base_addr_, end_addr_, and Aleph::maps().

◆ dealloc() [1/2]

void Aleph::AhArenaAllocator::dealloc ( const void addr,
const size_t  size 
)
inlinenoexcept

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.

Parameters
[in]addrAddress to deallocate.
[in]sizeSize of the allocation.
Note
This enables stack-like (LIFO) allocation patterns where the last allocation can be "undone".

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().

◆ dealloc() [2/2]

template<typename T >
void Aleph::AhArenaAllocator::dealloc ( TemplateType  tag,
void ptr 
)
inlinenoexcept

Destruct and deallocate an object.

Calls the destructor of T and attempts to reclaim the memory (only succeeds for LIFO deallocation pattern).

Template Parameters
TType of object.
Parameters
[in]tagDisambiguation tag (use TemplateType::TEMPLATE) - unused parameter.
[in]ptrPointer to object.

Definition at line 372 of file ah-arena.H.

References dealloc().

◆ deallocate()

void Aleph::AhArenaAllocator::deallocate ( const void addr,
size_t  size 
)
inlinenoexcept
Deprecated:
Use dealloc() instead.

Definition at line 451 of file ah-arena.H.

References dealloc(), Aleph::maps(), and Aleph::size().

Referenced by TEST().

◆ empty()

bool Aleph::AhArenaAllocator::empty ( ) const
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().

◆ end_addr()

const void * Aleph::AhArenaAllocator::end_addr ( ) const
inlinenoexcept

Get end address (one past last valid byte).

Definition at line 431 of file ah-arena.H.

References end_addr_.

◆ full()

bool Aleph::AhArenaAllocator::full ( ) const
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_.

Referenced by TEST(), TEST(), TEST(), TEST(), and TEST().

◆ is_valid()

bool Aleph::AhArenaAllocator::is_valid ( ) const
inlinenoexcept

Returns true if the arena is valid (has memory).

Definition at line 383 of file ah-arena.H.

References base_addr_.

Referenced by TEST(), TEST(), TEST(), TEST(), and TEST().

◆ next_avail_addr()

const void * Aleph::AhArenaAllocator::next_avail_addr ( ) const
inlinenoexcept

Get next available address (current allocation pointer).

Definition at line 428 of file ah-arena.H.

References curr_addr_.

◆ operator=() [1/2]

AhArenaAllocator & Aleph::AhArenaAllocator::operator= ( AhArenaAllocator &&  other)
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_.

◆ operator=() [2/2]

AhArenaAllocator & Aleph::AhArenaAllocator::operator= ( const AhArenaAllocator )
delete

◆ owns_memory()

bool Aleph::AhArenaAllocator::owns_memory ( ) const
inlinenoexcept

Returns true if the arena owns its memory.

Definition at line 389 of file ah-arena.H.

References owns_memory_.

Referenced by TEST(), TEST(), and TEST().

◆ reset()

void Aleph::AhArenaAllocator::reset ( )
inlinenoexcept

Reset arena, making all memory available again.

After reset, the arena can be reused for new allocations. All previously returned pointers become invalid.

Warning
Does NOT call destructors on allocated objects. The caller must destroy objects manually before calling reset().

Definition at line 256 of file ah-arena.H.

References base_addr_, and curr_addr_.

Referenced by TEST(), TEST(), and TEST_F().

Member Data Documentation

◆ base_addr_

char* Aleph::AhArenaAllocator::base_addr_ = nullptr
private

◆ curr_addr_

char* Aleph::AhArenaAllocator::curr_addr_ = nullptr
private

◆ DEFAULT_SIZE

constexpr size_t Aleph::AhArenaAllocator::DEFAULT_SIZE = 1024 * 1024
staticconstexpr

Default arena size (1 MB).

Definition at line 133 of file ah-arena.H.

Referenced by TEST().

◆ end_addr_

char* Aleph::AhArenaAllocator::end_addr_ = nullptr
private

◆ owns_memory_

bool Aleph::AhArenaAllocator::owns_memory_ = false
private

Definition at line 126 of file ah-arena.H.

Referenced by AhArenaAllocator(), ~AhArenaAllocator(), operator=(), and owns_memory().


The documentation for this class was generated from the following file: