Foundation
Loading...
Searching...
No Matches
AllocatorStack.hpp
Go to the documentation of this file.
1#pragma once
2#include "Allocator.hpp"
3#include "Atomic.hpp"
4namespace Foundation::Core
5{
12 {
13 public:
14 AllocatorStack() = default;
15 AllocatorStack(Arena arena) { Reset(arena); }
19 void Reset(Arena arena)
20 {
21 mMemory = arena.memory;
22 mCurrent = reinterpret_cast<size_type>(arena.memory);
23 mEnd = reinterpret_cast<size_type>(arena.memory) + arena.size;
24 }
28 void Reset() { Reset({nullptr, 0}); }
37 void Deallocate(pointer ptr) override { /* nop */ }
42 {
43 throw std::runtime_error("Allocator does not support reallocation");
44 }
45 constexpr operator bool() const noexcept { return mMemory != nullptr; }
46
47 void QueryBudget(size_t& used, size_t& budget) const override
48 {
49 used = mCurrent - reinterpret_cast<size_type>(mMemory);
50 budget = mEnd - reinterpret_cast<size_type>(mMemory);
51 }
52 private:
53 pointer mMemory{nullptr};
56 };
57} // namespace Foundation::Core
Implements a lock-free stack-based bump allocator.
Definition AllocatorStack.hpp:12
void Reset(Arena arena)
Resets the stack allocator to the initial state, allowing for reuse of the memory block (Arena)
Definition AllocatorStack.hpp:19
pointer Reallocate(pointer ptr, size_type new_size, size_t alignment) override
Definition AllocatorStack.hpp:41
void Deallocate(pointer ptr) override
Definition AllocatorStack.hpp:37
Atomic< size_type > mCurrent
Definition AllocatorStack.hpp:54
pointer mMemory
Definition AllocatorStack.hpp:53
void Reset()
Resets the stack allocator to a non-allocated state.
Definition AllocatorStack.hpp:28
void QueryBudget(size_t &used, size_t &budget) const override
Definition AllocatorStack.hpp:47
pointer Allocate(size_type size, size_type alignment) override
Allocates a block of memory of the specified size with alignment. If the requested size exceeds the a...
Definition AllocatorStack.cpp:2
AllocatorStack(Arena arena)
Definition AllocatorStack.hpp:15
size_type mEnd
Definition AllocatorStack.hpp:55
General Purpose Allocator (GPA) interface.
Definition Allocator.hpp:24
Lock-free atomic primitives and implementations of data structures.
Definition Allocator.hpp:5
std::size_t size_type
Definition Allocator.hpp:6
std::atomic< T > Atomic
Alias of std::atomic<T>.
Definition Atomic.hpp:26
void * pointer
Definition Allocator.hpp:7
T * Construct(Allocator *resource, Args &&...args)
Convenience placement new with object of type T using a Foundation::Core::Allocator.
Definition Allocator.hpp:149
A memory arena allocated from an Allocator.
Definition Allocator.hpp:17
size_type size
Definition Allocator.hpp:19
pointer memory
Definition Allocator.hpp:18