A fast, linear stack-based memory allocator for temporary allocations.
More...
#include <StackAllocator.hpp>
|
using | marker_type = std::uintptr_t |
|
using | checkpoint_t = marker_type |
|
using | size_type = std::size_t |
|
|
| StackAllocator (size_type size=1000) |
| Constructs a StackAllocator with the specified capacity.
|
|
| StackAllocator (void *memory, size_type size) |
| Constructs a StackAllocator with a given memory block and size.
|
|
| ~StackAllocator () |
| Destructor for the StackAllocator class.
|
|
| StackAllocator (const StackAllocator &)=delete |
|
StackAllocator & | operator= (const StackAllocator &)=delete |
|
| StackAllocator (StackAllocator &&)=delete |
|
StackAllocator & | operator= (StackAllocator &&)=delete |
|
template<typename T > |
T * | alloc_t () noexcept |
| Allocates memory for a single object of type T.
|
|
void * | alloc (size_type size, size_type alignment=alignof(std::max_align_t)) noexcept |
| Allocates a block of memory from the stack allocator with the specified size and alignment.
|
|
checkpoint_t | save_checkpoint () const noexcept |
| Saves the current state of the stack allocator.
|
|
void | restore_checkpoint (checkpoint_t checkpoint) |
| Restores the stack allocator to a previously saved checkpoint.
|
|
size_type | bytes_used () const noexcept |
| Returns the number of bytes currently used by the stack allocator.
|
|
size_type | bytes_remaining () const noexcept |
| Returns the number of bytes remaining in the stack allocator.
|
|
size_type | capacity () const noexcept |
| Returns the total capacity of the stack allocator.
|
|
void | reset () noexcept |
| Resets the stack allocator to its initial state.
|
|
A fast, linear stack-based memory allocator for temporary allocations.
Allocations are linear, with memory reclaimed on reset or restoring to a checkpoint. Ideal for short-lived, bulk-freed allocations.
Key Features:
- Allocates aligned memory blocks for single objects of any type.
- Supports checkpoints to save and restore allocation state.
- Disallows copying and moving for safety.
- Monitors memory usage and capacity.
- Manages internal memory only
Usage Notes:
- Allocations valid only until allocator reset or checkpoint restore.
- Doesn't invoke constructors/destructors; use placement new and manual cleanup.
- Not thread-safe; designed for single-threaded use.
◆ StackAllocator() [1/2]
intns::memory::StackAllocator::StackAllocator |
( |
size_type |
size = 1000 | ) |
|
Constructs a StackAllocator with the specified capacity.
- Parameters
-
capacity | The size in bytes of the memory block to allocate. |
- Exceptions
-
std::runtime_error | If capacity is zero or memory allocation fails. |
◆ StackAllocator() [2/2]
intns::memory::StackAllocator::StackAllocator |
( |
void * |
memory, |
|
|
size_type |
size |
|
) |
| |
Constructs a StackAllocator with a given memory block and size.
- Warning
- Does NOT take ownership of the memory, must be free'd externally.
- Parameters
-
memory | Pointer to the memory buffer to be managed. |
size | Size of the memory buffer in bytes. |
- Exceptions
-
std::runtime_error | If memory is null, size is zero, or buffer is too small after alignment. |
◆ ~StackAllocator()
intns::memory::StackAllocator::~StackAllocator |
( |
| ) |
|
Destructor for the StackAllocator class.
Releases the allocation by freeing the memory at start_marker_.
◆ alloc()
void * intns::memory::StackAllocator::alloc |
( |
size_type |
size, |
|
|
size_type |
alignment = alignof( std::max_align_t) |
|
) |
| |
|
inlinenoexcept |
Allocates a block of memory from the stack allocator with the specified size and alignment.
Allocates a contiguous size
-byte block aligned to alignment
from the stack buffer. Returns nullptr if size is zero, exceeds capacity, or if alignment is invalid (not a power of two or too large).
The allocation advances the internal marker; subsequent allocations occur after the returned block. The function is [[nodiscard]] to encourage checking the return value.
- Parameters
-
size | Number of bytes to allocate. |
alignment | Alignment in bytes (default: alignof(std::max_align_t)). |
- Returns
- Pointer to allocated memory or nullptr if allocation fails.
◆ alloc_t()
template<typename T >
T * intns::memory::StackAllocator::alloc_t |
( |
| ) |
|
|
inlinenoexcept |
Allocates memory for a single object of type T.
This function performs several compile-time checks to ensure that T is a valid type for allocation:
- T must not be a reference type.
- T must not be void.
- T must be destructible.
- T must have a non-zero size.
Aligns the allocation to T's requirements; if capacity is insufficient or out of bounds, returns nullptr. Otherwise, returns the allocated pointer and advances the marker.
- Template Parameters
-
T | The type of object to allocate. |
- Returns
- The allocated memory for T, or nullptr on failure.
◆ bytes_remaining()
size_type intns::memory::StackAllocator::bytes_remaining |
( |
| ) |
const |
|
inlinenoexcept |
Returns the number of bytes remaining in the stack allocator.
- Returns
- The number of bytes remaining that can be allocated.
- Note
- This function is noexcept and guarantees not to throw exceptions.
◆ bytes_used()
size_type intns::memory::StackAllocator::bytes_used |
( |
| ) |
const |
|
inlinenoexcept |
Returns the number of bytes currently used by the stack allocator.
- Returns
- The number of bytes used.
- Note
- This function is noexcept and guarantees not to throw exceptions.
◆ capacity()
size_type intns::memory::StackAllocator::capacity |
( |
| ) |
const |
|
inlinenoexcept |
Returns the total capacity of the stack allocator.
- Returns
- The maximum number of bytes that can be stored.
- Note
- This function is noexcept and guarantees not to throw exceptions.
◆ reset()
void intns::memory::StackAllocator::reset |
( |
| ) |
|
|
inlinenoexcept |
Resets the stack allocator to its initial state.
- Note
- This function is noexcept and guarantees not to throw exceptions.
◆ restore_checkpoint()
void intns::memory::StackAllocator::restore_checkpoint |
( |
checkpoint_t |
checkpoint | ) |
|
|
inline |
Restores the stack allocator to a previously saved checkpoint.
- Parameters
-
checkpoint | The previous allocation state to restore. |
- Exceptions
-
std::runtime_error | if checkpoint is out of valid memory range. |
◆ save_checkpoint()
checkpoint_t intns::memory::StackAllocator::save_checkpoint |
( |
| ) |
const |
|
inlinenoexcept |
Saves the current state of the stack allocator.
- Returns
- checkpoint_t The current checkpoint marker.
The documentation for this class was generated from the following files: