Monte Carlo Benchmarking Engine
High-performance SIMD Monte Carlo engine (AVX2/NEON) with custom memory allocators and perf logging.
 
Loading...
Searching...
No Matches
pool.hpp
Go to the documentation of this file.
1// =======================================
2// pool.hpp - Fast aligned memory pool
3// =======================================
120
121
122
123#pragma once
124
125#include <cstddef>
126#include <cstdlib>
127#include <new>
128#include <mutex>
129#include <cassert>
130
138 char* memory;
139 std::size_t capacity;
140 std::size_t offset;
141
142public:
147 explicit PoolAllocator(size_t bytes) {
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 }
153
158 std::free(memory);
159 }
160
167 template<typename T>
168 T* allocate(std::size_t align = alignof(T)) {
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 }
179
183 void reset() {
184 offset = 0;
185 }
186};
std::size_t capacity
Total capacity in bytes.
Definition pool.hpp:139
void reset()
Reset the allocator to reuse buffer (memory).
Definition pool.hpp:183
char * memory
Raw memory block.
Definition pool.hpp:138
T * allocate(std::size_t align=alignof(T))
Allocates memory for type T with specified alignment (default = alignof(T)).
Definition pool.hpp:168
~PoolAllocator()
Destroy the PoolAllocator and free the buffer.
Definition pool.hpp:157
std::size_t offset
Offset for bump allocation.
Definition pool.hpp:140
PoolAllocator(size_t bytes)
Construct a new PoolAllocator with a given size.
Definition pool.hpp:147