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"
4#include "Logging.hpp"
5namespace Foundation::Core
6{
13 {
14 public:
15 AllocatorStack() = default;
16 AllocatorStack(Arena arena) { Reset(arena); }
20 void Reset(Arena arena)
21 {
22 mMemory = arena.memory;
23 mCurrent = reinterpret_cast<size_type>(arena.memory);
24 mEnd = reinterpret_cast<size_type>(arena.memory) + arena.size;
25 }
29 void Reset() { Reset({nullptr, 0}); }
34 pointer Allocate(size_type size, size_type alignment) noexcept override;
38 void Deallocate(pointer ptr) noexcept override { /* nop */ }
40 pointer Reallocate(pointer ptr, size_type new_size, size_t alignment) noexcept override
41 {
42 CHECK_MSG(false, "Allocator does not support reallocation");
43 return nullptr;
44 }
45 constexpr operator bool() const noexcept { return mMemory != nullptr; }
46 private:
47 pointer mMemory{nullptr};
50 };
51} // namespace Foundation::Core
#define CHECK_MSG(expr, format_str,...)
Implements a lock-free stack-based bump allocator.
Definition AllocatorStack.hpp:13
void Reset(Arena arena)
Resets the stack allocator to the initial state, allowing for reuse of the memory block (Arena)
Definition AllocatorStack.hpp:20
Atomic< size_type > mCurrent
Definition AllocatorStack.hpp:48
pointer Reallocate(pointer ptr, size_type new_size, size_t alignment) noexcept override
Definition AllocatorStack.hpp:40
pointer mMemory
Definition AllocatorStack.hpp:47
void Reset()
Resets the stack allocator to a non-allocated state.
Definition AllocatorStack.hpp:29
pointer Allocate(size_type size, size_type alignment) noexcept 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:16
size_type mEnd
Definition AllocatorStack.hpp:49
void Deallocate(pointer ptr) noexcept override
Definition AllocatorStack.hpp:38
Allocator interface (noexcept)
Definition Allocator.hpp:29
Lock-free atomic primitives and implementations of data structures.
Definition Allocator.hpp:6
std::size_t size_type
Definition Allocator.hpp:7
std::atomic< T > Atomic
Alias of std::atomic<T>.
Definition Atomic.hpp:26
void * pointer
Definition Allocator.hpp:8
A memory arena allocated from an Allocator.
Definition Allocator.hpp:21
size_type size
Definition Allocator.hpp:23
pointer memory
Definition Allocator.hpp:22