Monte Carlo Benchmarking Engine
High-performance SIMD Monte Carlo engine (AVX2/NEON) with custom memory allocators and perf logging.
 
Loading...
Searching...
No Matches
PoolAllocator Struct Reference

Fast aligned bump allocator for multithreaded simulations. More...

#include <pool.hpp>

Public Member Functions

 PoolAllocator (size_t bytes)
 Construct a new PoolAllocator with a given size.
 
 ~PoolAllocator ()
 Destroy the PoolAllocator and free the buffer.
 
template<typename T>
T * allocate (std::size_t align=alignof(T))
 Allocates memory for type T with specified alignment (default = alignof(T)).
 
void reset ()
 Reset the allocator to reuse buffer (memory).
 

Public Attributes

char * memory
 Raw memory block.
 
std::size_t capacity
 Total capacity in bytes.
 
std::size_t offset
 Offset for bump allocation.
 

Detailed Description

Fast aligned bump allocator for multithreaded simulations.

Allocates memory from a preallocated buffer using pointer arithmetic. All memory is aligned to 64 bytes to maximize cache and SIMD performance.

Definition at line 137 of file pool.hpp.

Constructor & Destructor Documentation

◆ PoolAllocator()

PoolAllocator::PoolAllocator ( size_t bytes)
explicit

Construct a new PoolAllocator with a given size.

Parameters
bytesNumber of bytes to preallocate (must be multiple of 64)

Definition at line 147 of file pool.hpp.

147 {
148 memory = static_cast<char*>(std::aligned_alloc(64, bytes));
149 capacity = bytes;
150 offset = 0;
151 assert(memory && "Failed to allocate aligned memory");
152 }
std::size_t capacity
Total capacity in bytes.
Definition pool.hpp:139
char * memory
Raw memory block.
Definition pool.hpp:138
std::size_t offset
Offset for bump allocation.
Definition pool.hpp:140

◆ ~PoolAllocator()

PoolAllocator::~PoolAllocator ( )

Destroy the PoolAllocator and free the buffer.

Definition at line 157 of file pool.hpp.

157 {
158 std::free(memory);
159 }

Member Function Documentation

◆ allocate()

template<typename T>
T * PoolAllocator::allocate ( std::size_t align = alignof(T))

Allocates memory for type T with specified alignment (default = alignof(T)).

Template Parameters
TType of data.
Parameters
alignAlignment in bytes (default: alignof(T)).
Returns
Pointer to aligned memory or nullptr on overflow.

Definition at line 168 of file pool.hpp.

168 {
169 std::size_t current = offset;
170 offset += sizeof(T); // 64-byte spacing to avoid false sharing
171
172 if (current + sizeof(T) > capacity) return nullptr;
173
174 char* ptr = memory + current;
175 std::uintptr_t aligned = (reinterpret_cast<std::uintptr_t>(ptr) + (align - 1)) & ~(align - 1);
176
177 return reinterpret_cast<T*>(aligned);
178 }

◆ reset()

void PoolAllocator::reset ( )

Reset the allocator to reuse buffer (memory).

Definition at line 183 of file pool.hpp.

183 {
184 offset = 0;
185 }

Member Data Documentation

◆ capacity

std::size_t PoolAllocator::capacity

Total capacity in bytes.

Definition at line 139 of file pool.hpp.

◆ memory

char* PoolAllocator::memory

Raw memory block.

Definition at line 138 of file pool.hpp.

◆ offset

std::size_t PoolAllocator::offset

Offset for bump allocation.

Definition at line 140 of file pool.hpp.


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