Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
LinearAllocator.hpp
1#pragma once
2#include <utility>
3#include <type_traits>
4
14 public:
16
17 void Initialize(void* ownedMemory, size_t size);
18 bool Initialize(size_t size);
19 void* AllocateRaw(size_t size, size_t alignment);
20 void Clear();
21 void ClearAndZero();
22 void Destroy();
23
24 template<typename T, typename... Args>
25 T* Allocate(Args&&... params) {
26 static_assert(std::is_constructible_v<T, Args...>, "Type T must be constructible with given arguments.");
27
28 T* ptr = static_cast<T*>(AllocateRaw(sizeof(T), alignof(T)));
29 if (ptr != nullptr) {
30 // Call the constructor on the newly allocated memory
31 new (ptr) T(std::forward<Args>(params)...);
32 }
33
34 return ptr;
35 }
36
37 template<typename T>
38 T* AllocateWithoutConstructor() {
39 return static_cast<T*>(Allocate(sizeof(T), alignof(T)));
40 }
41
42 size_t GetUsedSize() const;
43
44 private:
45 size_t totalMemorySize;
46 size_t usedSize;
47
48 void* memory;
49 bool hasAllocatedOwnMemory;
50 };
51}
A linear allocator where memory can only be deallocated when the entire allocator is cleared.
Definition LinearAllocator.hpp:13
Definition AllocatorConcept.hpp:11