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