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 char 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 template<typename T, typename... Args>
52 Grindstone::Memory::SmartPointers::SharedPtr<T> AllocateShared(Args&&... params) {
53 T* ptr = static_cast<T*>(AllocateRaw(sizeof(T), alignof(T), typeid(T).name()));
54 if (ptr != nullptr) {
55 // Call the constructor on the newly allocated memory
56 new (ptr) T(std::forward<Args>(params)...);
57 }
58
59 return SharedPtr<T>(ptr, deleterFn);
60 }
61
62 template<typename T, typename... Args>
63 Grindstone::Memory::SmartPointers::UniquePtr<T> AllocateUnique(Args&&... params) {
64 T* ptr = static_cast<T*>(AllocateRaw(sizeof(T), alignof(T), typeid(T).name()));
65 if (ptr != nullptr) {
66 // Call the constructor on the newly allocated memory
67 new (ptr) T(std::forward<Args>(params)...);
68 }
69
70 return UniquePtr<T>(ptr, deleterFn);
71 }
72
73 template<typename T, typename... Args>
74 T* AllocateRaw(Args&&... params) {
75 T* ptr = static_cast<T*>(AllocateRaw(sizeof(T), alignof(T), typeid(T).name()));
76 if (ptr != nullptr) {
77 // Call the constructor on the newly allocated memory
78 new (ptr) T(std::forward<Args>(params)...);
79 }
80
81 return ptr;
82 }
83
84 template<typename T>
85 bool Free(void* memPtr) {
86 if (memPtr != nullptr) {
87 return false;
88 }
89
90 reinterpret_cast<T*>(memPtr)->~T();
91 Free(memPtr);
92
93 return true;
94 }
95
96 private:
97 void FindAvailable(size_t size, size_t alignment, size_t& padding, FreeHeader*& previousFreeHeader, FreeHeader*& freeHeader) const;
98 void FindAvailableFirst(size_t size, size_t alignment, size_t& padding, FreeHeader*& previousFreeHeader, FreeHeader*& freeHeader) const;
99 void FindAvailableBest(size_t size, size_t alignment, size_t& padding, FreeHeader*& previousFreeHeader, FreeHeader*& freeHeader) const;
100 void Coalesce(FreeHeader* previousNode, FreeHeader* freeNode);
101 void InitializeImpl(void* ownedMemory, size_t size);
102
103 void* startMemory = nullptr;
104 void* endMemory = nullptr;
105 FreeHeader* firstFreeHeader = nullptr;
106
107 std::function<void(void*)> deleterFn;
108 size_t totalMemorySize = 0;
109 size_t usedSize = 0;
110 size_t peakSize = 0;
111 SearchPolicy searchPolicy = SearchPolicy::BestSearch;
112 bool shouldClear = false;
113 bool hasAllocatedOwnMemory = false;
114
115#ifdef _DEBUG
116 std::map<void*, const char*> nameMap;
117#endif
118 };
119}
A dynamic allocator represented by a linked list.
Definition DynamicAllocator.hpp:19