Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
DynamicAllocator.hpp
1#pragma once
2
3#include <string>
4#include <functional>
5#include <stdint.h>
6#include <map>
7#include <utility>
8
9#include "../SmartPointers.hpp"
10
11namespace Grindstone::Memory::Allocators {
20 public:
21 struct FreeHeader {
22 size_t blockSize;
23 FreeHeader* nextFreeBlock = nullptr;
24 };
25
27 size_t blockSize;
28 uint8_t padding;
29 };
30
31 enum class SearchPolicy {
32 FirstSearch = 0,
33 BestSearch
34 };
35
37
38 bool Initialize(size_t size);
39 void Initialize(void* ownedMemory, size_t size);
40
41 void* AllocateRaw(size_t size, size_t alignment, const char* debugName);
42 bool Free(void* memPtr);
43
44 bool IsEmpty() const;
45
46 size_t GetTotalMemorySize() const;
47 size_t GetPeakSize() const;
48 size_t GetUsedSize() const;
49 void* GetMemory() const;
50
51#ifdef _DEBUG
52 const std::map<void*, const char*>& GetNameMap() const {
53 return nameMap;
54 }
55#endif
56
57 template<typename T>
58 Grindstone::SharedPtr<T> MakeShared(T* ptr) {
59 return Grindstone::SharedPtr<T>(ptr, deleterFn);
60 }
61
62 template<typename T>
63 Grindstone::UniquePtr<T> MakeUnique(T* ptr) {
64 return Grindstone::UniquePtr<T>(ptr, deleterFn);
65 }
66
67 template<typename T, typename... Args>
68 Grindstone::SharedPtr<T> AllocateShared(Args&&... params) {
69 static_assert(std::is_constructible_v<T, Args...>, "Type T must be constructible with given arguments.");
70
71 T* ptr = static_cast<T*>(AllocateRaw(sizeof(T), alignof(T), typeid(T).name()));
72 if (ptr != nullptr) {
73 // Call the constructor on the newly allocated memory
74 new (ptr) T(std::forward<Args>(params)...);
75 }
76
77 return Grindstone::SharedPtr<T>(ptr, deleterFn);
78 }
79
80 template<typename T, typename... Args>
81 Grindstone::UniquePtr<T> AllocateUnique(Args&&... params) {
82 static_assert(std::is_constructible_v<T, Args...>, "Type T must be constructible with given arguments.");
83
84 T* ptr = static_cast<T*>(AllocateRaw(sizeof(T), alignof(T), typeid(T).name()));
85 if (ptr != nullptr) {
86 // Call the constructor on the newly allocated memory
87 new (ptr) T(std::forward<Args>(params)...);
88 }
89
90 return Grindstone::UniquePtr<T>(ptr, deleterFn);
91 }
92
93 template<typename T, typename... Args>
94 T* AllocateRaw(Args&&... params) {
95 static_assert(std::is_constructible_v<T, Args...>, "Type T must be constructible with given arguments.");
96
97 T* ptr = static_cast<T*>(AllocateRaw(sizeof(T), alignof(T), typeid(T).name()));
98 if (ptr != nullptr) {
99 // Call the constructor on the newly allocated memory
100 new (ptr) T(std::forward<Args>(params)...);
101 }
102
103 return ptr;
104 }
105
106 template<typename T>
107 bool Free(void* memPtr) {
108 if (memPtr != nullptr) {
109 return false;
110 }
111
112 reinterpret_cast<T*>(memPtr)->~T();
113 Free(memPtr);
114
115 return true;
116 }
117
118 private:
119 void InitializeImpl(void* ownedMemory, size_t size);
120
121 void* startMemory = nullptr;
122 void* endMemory = nullptr;
123 FreeHeader* firstFreeHeader = nullptr;
124
125 std::function<void(void*)> deleterFn;
126 size_t totalMemorySize = 0;
127 size_t usedSize = 0;
128 size_t peakSize = 0;
129 SearchPolicy searchPolicy = SearchPolicy::BestSearch;
130 bool shouldClear = false;
131 bool hasAllocatedOwnMemory = false;
132
133#ifdef _DEBUG
134 std::map<void*, const char*> nameMap;
135#endif
136 };
137}
A dynamic allocator represented by a linked list.
Definition DynamicAllocator.hpp:19
Definition SharedPtr.hpp:5
Definition UniquePtr.hpp:7