Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-arena.H File Reference

Memory arena for fast bulk allocations. More...

#include <cstdlib>
#include <cstddef>
#include <utility>
#include <new>
#include <aleph.H>
#include <ah-errors.H>
Include dependency graph for ah-arena.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Aleph::AhArenaAllocator
 Arena allocator for fast bump-pointer allocation. More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 

Functions

template<class T , typename... Args>
TAleph::allocate (AhArenaAllocator &arena, Args &&... args)
 Allocate and construct an object in an arena.
 
template<class T >
void Aleph::deallocate (AhArenaAllocator &arena, T *ptr) noexcept
 Destruct and deallocate an object from an arena.
 
template<class T >
void Aleph::dealloc (AhArenaAllocator &arena, T *ptr) noexcept
 Alias for deallocate (backward compatibility).
 

Detailed Description

Memory arena for fast bulk allocations.

Provides an arena allocator that allocates memory using bump-pointer allocation, enabling O(1) allocations with minimal fragmentation. Memory is freed all at once when the arena is destroyed or reset.

Overview

Arena allocators (also known as region-based allocators) are useful when:

  • Many small objects have the same lifetime
  • Allocation speed is critical
  • Memory fragmentation must be avoided
  • Individual deallocation is not needed

Usage Example

// Create arena with 1MB of memory
AhArenaAllocator arena(1024 * 1024);
// Allocate raw memory
void* buf = arena.alloc(100);
// Allocate and construct objects
MyClass* obj = allocate<MyClass>(arena, arg1, arg2);
// Reset arena to reuse memory (no destructors called!)
arena.reset();
// Or use external buffer
char buffer[4096];
AhArenaAllocator stack_arena(buffer, sizeof(buffer));

Thread Safety

This allocator is not thread-safe. For multi-threaded applications, use one arena per thread or add external synchronization.

Author
Leandro Rabindranath León

Definition in file ah-arena.H.