Foundation
Loading...
Searching...
No Matches
StackAllocator.hpp
Go to the documentation of this file.
1#pragma once
2#include "Allocator.hpp"
3namespace Foundation::Core {
9 class StackAllocator : public Allocator {
10 public:
11 StackAllocator() = default;
13 Reset(arena);
14 }
18 void Reset(Arena arena) {
19 mMemory = arena.memory;
20 mCurrent = reinterpret_cast<size_type>(arena.memory);
21 mEnd = reinterpret_cast<size_type>(arena.memory) + arena.size;
22 }
26 void Reset() {
27 Reset({ nullptr, 0 });
28 }
33 pointer Allocate(size_type size, size_type alignment) override;
37 void Deallocate(pointer ptr) override { /* nop */ }
41 void Deallocate(pointer ptr, size_type size) override { /* nop */ }
45 pointer Reallocate(pointer ptr, size_type new_size, size_t alignment) override {
46 throw std::runtime_error("StackAllocator does not support reallocation");
47 }
48 constexpr operator bool() const noexcept { return mMemory != nullptr; }
49 private:
50 pointer mMemory{ nullptr };
51 std::atomic<size_type> mCurrent{};
53 };
54}
General Purpose Allocator (GPA) interface.
Definition Allocator.hpp:24
Implements an atomic stack-based bump allocator.
Definition StackAllocator.hpp:9
StackAllocator(Arena arena)
Definition StackAllocator.hpp:12
void Reset()
Resets the stack allocator to a non-allocated state.
Definition StackAllocator.hpp:26
void Deallocate(pointer ptr, size_type size) override
Definition StackAllocator.hpp:41
std::atomic< size_type > mCurrent
Definition StackAllocator.hpp:51
size_type mEnd
Definition StackAllocator.hpp:52
pointer Reallocate(pointer ptr, size_type new_size, size_t alignment) override
Definition StackAllocator.hpp:45
pointer mMemory
Definition StackAllocator.hpp:50
void Deallocate(pointer ptr) override
Definition StackAllocator.hpp:37
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 StackAllocator.cpp:5
void Reset(Arena arena)
Resets the stack allocator to the initial state, allowing for reuse of the memory block (Arena)
Definition StackAllocator.hpp:18
Allocators, Data Structures and introspection implementations.
Definition Allocator.hpp:5
std::size_t size_type
Definition Allocator.hpp:6
void * pointer
Definition Allocator.hpp:7
std::unique_ptr< T, StlDeleter< T > > UniquePtr
std::unique_ptr with custom deleter that uses a Foundation::Core::Allocator to deallocate memory.
Definition Allocator.hpp:161
A memory arena allocated from an Allocator.
Definition Allocator.hpp:17
size_type size
Definition Allocator.hpp:19
pointer memory
Definition Allocator.hpp:18