Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
MemoryAllocator.hpp
1#pragma once
2
3#include <Common/Memory/Allocators/DynamicAllocator.hpp>
4#include <Common/String.hpp>
5
6namespace Grindstone::Memory::AllocatorCore {
8 Allocators::DynamicAllocator dynamicAllocator;
9 };
10
12 void SetAllocatorState(Grindstone::Memory::AllocatorCore::AllocatorState* newAllocatorState);
13
14 bool Initialize(size_t sizeInMegs);
15 void CloseAllocator();
16
17 StringRef AllocateString(size_t size);
18 StringRef AllocateString(Grindstone::StringRef srcString);
19
20 bool FreeWithoutDestructor(void* memPtr);
21
22 size_t GetPeak();
23 size_t GetUsed();
24 size_t GetTotal();
25
26 bool IsEmpty();
27
28 void* AllocateRaw(size_t size, size_t alignment, const char* debugName);
29
30 template<typename T>
31 Grindstone::SharedPtr<T> MakeShared(T* ptr) {
32 return GetAllocatorState()->dynamicAllocator.MakeShared(ptr);
33 }
34
35 template<typename T>
36 Grindstone::UniquePtr<T> MakeUnique(T* ptr) {
37 return GetAllocatorState()->dynamicAllocator.MakeUnique(ptr);
38 }
39
40 template<typename T, typename... Args>
41 Grindstone::UniquePtr<T> AllocateUnique(Args&&... params) {
42 return GetAllocatorState()->dynamicAllocator.AllocateUnique<T>(std::forward<Args>(params)...);
43 }
44
45 template<typename T, typename... Args>
46 Grindstone::SharedPtr<T> AllocateShared(Args&&... params) {
47 return GetAllocatorState()->dynamicAllocator.AllocateShared<T>(std::forward<Args>(params)...);
48 }
49
50 template<typename T, typename... Args>
51 T* Allocate(Args&&... params) {
52 T* ptr = static_cast<T*>(GetAllocatorState()->dynamicAllocator.AllocateRaw(sizeof(T), alignof(T), typeid(T).name()));
53 if (ptr != nullptr) {
54 // Call the constructor on the newly allocated memory
55 new (ptr) T(std::forward<Args>(params)...);
56 }
57
58 return ptr;
59 }
60
61 template<typename T>
62 T* AllocateArray(size_t arraySize) {
63 T* ptr = static_cast<T*>(GetAllocatorState()->dynamicAllocator.AllocateRaw(sizeof(T) * arraySize, alignof(T), typeid(T).name()));
64 return ptr;
65 }
66
67 template<typename T, typename... Args>
68 T* AllocateArray(size_t arraySize, Args&&... params) {
69 T* ptr = static_cast<T*>(GetAllocatorState()->dynamicAllocator.AllocateRaw(sizeof(T) * arraySize, alignof(T), typeid(T).name()));
70 if (ptr != nullptr) {
71 // Call the constructor on the newly allocated memory
72 for (size_t i = 0; i < arraySize; ++i) {
73 new (ptr + i) T(std::forward<Args>(params)...);
74 }
75 }
76
77 return ptr;
78 }
79
80 template<typename T, typename... Args>
81 T* AllocateNamed(const char* debugName, Args&&... params) {
82 T* ptr = static_cast<T*>(GetAllocatorState()->dynamicAllocator.AllocateRaw(sizeof(T), alignof(T), debugName));
83 if (ptr != nullptr) {
84 // Call the constructor on the newly allocated memory
85 new (ptr) T(std::forward<Args>(params)...);
86 }
87
88 return ptr;
89 }
90
91 template<typename T>
92 T* AllocateArrayNamed(const char* debugName, size_t arraySize) {
93 T* ptr = static_cast<T*>(GetAllocatorState()->dynamicAllocator.AllocateRaw(sizeof(T) * arraySize, alignof(T), debugName));
94 return ptr;
95 }
96
97 template<typename T, typename... Args>
98 T* AllocateArrayNamed(const char* debugName, size_t arraySize, Args&&... params) {
99 T* ptr = static_cast<T*>(GetAllocatorState()->dynamicAllocator.AllocateRaw(sizeof(T) * arraySize, alignof(T), debugName));
100 if (ptr != nullptr) {
101 // Call the constructor on the newly allocated memory
102 for (size_t i = 0; i < arraySize; ++i) {
103 new (ptr + i) T(std::forward<Args>(params)...);
104 }
105 }
106
107 return ptr;
108 }
109
110 template<typename T>
111 bool Free(T* memPtr) {
112 if (memPtr == nullptr) {
113 return false;
114 }
115
116 reinterpret_cast<T*>(memPtr)->~T();
117 GetAllocatorState()->dynamicAllocator.Free(memPtr);
118
119 return true;
120 }
121};
A dynamic allocator represented by a linked list.
Definition DynamicAllocator.hpp:19
Definition SharedPtr.hpp:5
Definition UniquePtr.hpp:7