Foundation
Loading...
Searching...
No Matches
Allocator.hpp
Go to the documentation of this file.
1#pragma once
2#include <atomic>
3#include <memory>
4#include <stdexcept>
5namespace Foundation::Core {
6 using size_type = std::size_t;
7 using pointer = void*;
8 inline uintptr_t AlignUp(uintptr_t value, uintptr_t alignment) {
9 return (value + alignment - 1) & ~(alignment - 1);
10 }
11 inline uintptr_t AlignDown(uintptr_t value, uintptr_t alignment) {
12 return (value) & ~(alignment - 1);
13 }
24 class Allocator {
25 public:
26 virtual ~Allocator() = default;
27 virtual pointer Allocate(size_type size, size_t alignment = alignof(std::max_align_t)) = 0;
28 virtual void Deallocate(pointer ptr, size_type size) = 0;
29 virtual void Deallocate(pointer ptr) = 0;
30 virtual pointer Reallocate(pointer ptr, size_type new_size, size_t alignment) = 0;
31
32 Arena AllocateArena(size_type size, size_t alignment) { return { Allocate(size, alignment), size }; }
33 Arena AllocateArena(size_type size) { return { Allocate(size), size }; }
34 void DeallocateArena(Arena arena) {
35 if (arena.memory)
36 Deallocate(arena.memory, arena.size);
37 }
38 Allocator* Ptr() { return this; }
39 };
43 struct ScopedArena {
46 ScopedArena(Allocator* res, size_t size, size_t alignment = alignof(std::max_align_t)) :
47 resource(res), arena(res->AllocateArena(size, alignment)) {};
49 constexpr operator Arena() const { return arena; }
50 constexpr operator bool() const noexcept { return arena.memory != nullptr; }
51 };
52 constexpr size_t kDefaultStackArenaSize = 12 * 1024; // 12 KiB
56 template<size_t Size = kDefaultStackArenaSize> struct StackArena {
57 alignas(std::max_align_t) std::byte data[Size];
58 constexpr operator Arena() { return { reinterpret_cast<void*>(data), Size }; }
59 constexpr operator Arena() const { return { reinterpret_cast<void*>(data), Size }; }
60 };
61
76 template <typename T = void>
79 public:
80 using value_type = T;
81 using size_type = std::size_t;
82 using difference_type = std::ptrdiff_t;
83 using pointer = T*;
84 using const_pointer = const T*;
85 using reference = T&;
86 using const_reference = const T&;
87
88 template<typename U> friend class StlAllocator; // Rebind ctor
89 template<typename U> struct Rebind { using other = StlAllocator<U>; };
90 StlAllocator(Allocator* resource) noexcept : mResource(resource) {}
91 template<typename U>
92 StlAllocator(const StlAllocator<U>& other) noexcept : mResource(other.mResource) {}
93
95 return static_cast<pointer>(mResource->Allocate(n * sizeof(T), alignof(T)));
96 }
97 void deallocate(pointer p, size_type n) noexcept {
98 mResource->Deallocate(p, n * sizeof(T));
99 }
100 void deallocate(pointer p) noexcept {
101 mResource->Deallocate(p, sizeof(T));
102 }
103 // Allocators are deemed equal if they point to the same resource
104 friend bool operator==(const StlAllocator& lhs, const StlAllocator& rhs) noexcept {
105 return lhs.mResource == rhs.mResource;
106 }
107 friend bool operator!=(const StlAllocator& lhs, const StlAllocator& rhs) noexcept {
108 return !(lhs == rhs);
109 }
110 };
111
115 template <typename T>
116 struct StlDeleter {
118 void operator()(T* ptr) noexcept {
119 if (ptr) {
120 std::destroy_at(ptr);
121 mResource->Deallocate(ptr, sizeof(T));
122 }
123 }
124 };
128 template <typename Base, typename Derived, typename ...Args>
130 auto raw = resource->Allocate(sizeof(Derived), alignof(Derived));
131 try {
132 Derived* obj = std::construct_at(static_cast<Derived*>(raw), std::forward<Args>(args)...);
133 return obj;
134 }
135 catch (...) {
136 resource->Deallocate(raw, sizeof(Derived));
137 throw;
138 }
139 }
143 template <typename T, typename ...Args>
144 T* Construct(Allocator* resource, Args&& ...args) {
145 return ConstructBase<T, T>(resource, std::forward<Args>(args)...);
146 }
150 template <typename T>
151 void Destruct(Allocator* resource, T* obj) {
152 StlDeleter<T> deleter(resource);
153 deleter(obj);
154 }
160 template<typename T>
161 using UniquePtr = std::unique_ptr<T, StlDeleter<T>>;
162
169 template <typename Base, typename Derived, typename ...Args>
171 Base* obj = ConstructBase<Base, Derived>(resource, std::forward<Args>(args)...);
172 return UniquePtr<Base>(obj, StlDeleter<Base>{ resource });
173 }
179 template <typename T, typename ...Args>
181 return ConstructUniqueBase<T, T>(resource, std::forward<Args>(args)...);
182 }
183
187 template<typename T>
188 using SharedPtr = std::shared_ptr<T>;
189
196 template<typename Base, typename Derived, typename ...Args>
198 Base* obj = ConstructBase<Base, Derived>(resource, std::forward<Args>(args)...);
199 return { obj, StlDeleter<Base>{ resource } };
200 }
201
207 template <typename T, typename ...Args>
209 return ConstructSharedBase<T, T>(resource, std::forward<Args>(args)...);
210 }
211}
General Purpose Allocator (GPA) interface.
Definition Allocator.hpp:24
Arena AllocateArena(size_type size)
Definition Allocator.hpp:33
virtual pointer Reallocate(pointer ptr, size_type new_size, size_t alignment)=0
virtual ~Allocator()=default
virtual void Deallocate(pointer ptr)=0
virtual void Deallocate(pointer ptr, size_type size)=0
Arena AllocateArena(size_type size, size_t alignment)
Definition Allocator.hpp:32
void DeallocateArena(Arena arena)
Definition Allocator.hpp:34
Allocator * Ptr()
Definition Allocator.hpp:38
virtual pointer Allocate(size_type size, size_t alignment=alignof(std::max_align_t))=0
std::allocator adaptor for Foundation::Core::Allocator
Definition Allocator.hpp:77
StlAllocator(const StlAllocator< U > &other) noexcept
Definition Allocator.hpp:92
StlAllocator(Allocator *resource) noexcept
Definition Allocator.hpp:90
friend bool operator==(const StlAllocator &lhs, const StlAllocator &rhs) noexcept
Definition Allocator.hpp:104
T & reference
Definition Allocator.hpp:85
void deallocate(pointer p, size_type n) noexcept
Definition Allocator.hpp:97
const T * const_pointer
Definition Allocator.hpp:84
friend bool operator!=(const StlAllocator &lhs, const StlAllocator &rhs) noexcept
Definition Allocator.hpp:107
pointer allocate(size_type n)
Definition Allocator.hpp:94
Allocator * mResource
Definition Allocator.hpp:78
std::size_t size_type
Definition Allocator.hpp:81
void deallocate(pointer p) noexcept
Definition Allocator.hpp:100
T * pointer
Definition Allocator.hpp:83
std::ptrdiff_t difference_type
Definition Allocator.hpp:82
const T & const_reference
Definition Allocator.hpp:86
T value_type
Definition Allocator.hpp:80
Allocators, Data Structures and introspection implementations.
Definition Allocator.hpp:5
std::size_t size_type
Definition Allocator.hpp:6
SharedPtr< T > ConstructShared(Allocator *resource, Args &&...args)
Convenience wrapper for calling ConstructSharedBase when Base and Derived are the same type.
Definition Allocator.hpp:208
UniquePtr< T > ConstructUnique(Allocator *resource, Args &&...args)
Convenience wrapper for calling ConstructUniqueBase when Base and Derived are the same type.
Definition Allocator.hpp:180
UniquePtr< Base > ConstructUniqueBase(Allocator *resource, Args &&...args)
Helper function for constructing a pinned resource with a Foundation::Core::Allocator.
Definition Allocator.hpp:170
constexpr size_t kDefaultStackArenaSize
Definition Allocator.hpp:52
uintptr_t AlignUp(uintptr_t value, uintptr_t alignment)
Definition Allocator.hpp:8
void Destruct(Allocator *resource, T *obj)
Convenience destructor for objects allocated with Construct or ConstructBase.
Definition Allocator.hpp:151
uintptr_t AlignDown(uintptr_t value, uintptr_t alignment)
Definition Allocator.hpp:11
std::shared_ptr< T > SharedPtr
std::shared_ptr with custom deleter that uses a Foundation::Core::Allocator to deallocate memory.
Definition Allocator.hpp:188
void * pointer
Definition Allocator.hpp:7
Base * ConstructBase(Allocator *resource, Args &&...args)
Placement new helper for constructing an object of type Derived (which can be a subclass of Base) usi...
Definition Allocator.hpp:129
SharedPtr< Base > ConstructSharedBase(Allocator *resource, Args &&...args)
Helper function for constructing a ref-counted resource with a Foundation::Core::Allocator.
Definition Allocator.hpp:197
T * Construct(Allocator *resource, Args &&...args)
Convenience placement new with object of type T using a Foundation::Core::Allocator.
Definition Allocator.hpp:144
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
RAII wrapper for an arena allocated from an Allocator.
Definition Allocator.hpp:43
Allocator * resource
Definition Allocator.hpp:44
Arena arena
Definition Allocator.hpp:45
~ScopedArena()
Definition Allocator.hpp:48
ScopedArena(Allocator *res, size_t size, size_t alignment=alignof(std::max_align_t))
Definition Allocator.hpp:46
A fixed-size stack memory arena.
Definition Allocator.hpp:56
std::byte data[Size]
Definition Allocator.hpp:57
Definition Allocator.hpp:89
StlAllocator< U > other
Definition Allocator.hpp:89
Custom deleter for Foundation::Core::UniquePtr and Foundation::Core::SharedPtr that uses a Foundation...
Definition Allocator.hpp:116
Allocator * mResource
Definition Allocator.hpp:117
void operator()(T *ptr) noexcept
Definition Allocator.hpp:118