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