← Documentation
ADS-IDAC Engine 5.0.0
bump_allocator.hpp
Go to the documentation of this file.
1#pragma once
9#include <cstdint>
10#include <stdexcept>
11#include <vector>
12
13namespace bncore {
14
24public:
27 explicit BumpAllocator(std::size_t capacity_doubles)
28 : buffer_(capacity_doubles, 0.0), offset_(0) {}
29
39 [[nodiscard]] double *allocate(std::size_t num_doubles) {
40 if (offset_ + num_doubles > buffer_.size()) {
41 throw std::bad_alloc();
42 }
43 double *ptr = &buffer_[offset_];
44 offset_ += num_doubles;
45 return ptr;
46 }
47
48 // O(1) memory cleanup when moving to the next batch chunk.
51 void reset() { offset_ = 0; }
52
54 [[nodiscard]] std::size_t capacity() const { return buffer_.size(); }
56 [[nodiscard]] std::size_t used() const { return offset_; }
57
58private:
59 std::vector<double> buffer_;
60 std::size_t offset_;
61};
62
63} // namespace bncore
Fixed-capacity scratch buffer of doubles with pointer-bump allocation.
std::size_t capacity() const
Total size of the buffer, in doubles.
double * allocate(std::size_t num_doubles)
Reserves the next num_doubles doubles of the buffer.
std::size_t used() const
Doubles handed out since construction or the last reset().
void reset()
Makes the whole buffer available again. Contents are kept, and every slice handed out before becomes ...
std::size_t offset_
index of the next free double in buffer_
BumpAllocator(std::size_t capacity_doubles)
std::vector< double > buffer_