21 static const size_t DEBUG_NAME_SIZE = 128;
33 enum class SearchPolicy {
40 bool Initialize(
size_t size);
41 void Initialize(
void* ownedMemory,
size_t size);
43 void* AllocateRaw(
size_t size,
size_t alignment,
const char* debugName);
44 bool Free(
void* memPtr);
48 size_t GetTotalMemorySize()
const;
49 size_t GetPeakSize()
const;
50 size_t GetUsedSize()
const;
51 void* GetMemory()
const;
54 const std::map<void*, char[DEBUG_NAME_SIZE]>& GetNameMap()
const {
69 template<
typename T,
typename... Args>
71 static_assert(std::is_constructible_v<T, Args...>,
"Type T must be constructible with given arguments.");
73 T* ptr =
static_cast<T*
>(AllocateRaw(
sizeof(T),
alignof(T),
typeid(T).name()));
76 new (ptr) T(std::forward<Args>(params)...);
79 return Grindstone::SharedPtr<T>(ptr, deleterFn);
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.");
86 T* ptr =
static_cast<T*
>(AllocateRaw(
sizeof(T),
alignof(T),
typeid(T).name()));
89 new (ptr) T(std::forward<Args>(params)...);
92 return Grindstone::UniquePtr<T>(ptr, deleterFn);
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.");
99 T* ptr =
static_cast<T*
>(AllocateRaw(
sizeof(T),
alignof(T),
typeid(T).name()));
100 if (ptr !=
nullptr) {
102 new (ptr) T(std::forward<Args>(params)...);
109 bool Free(
void* memPtr) {
110 if (memPtr !=
nullptr) {
114 reinterpret_cast<T*
>(memPtr)->~T();
121 void InitializeImpl(
void* ownedMemory,
size_t size);
123 void* startMemory =
nullptr;
124 void* endMemory =
nullptr;
127 std::function<void(
void*)> deleterFn;
128 size_t totalMemorySize = 0;
131 SearchPolicy searchPolicy = SearchPolicy::BestSearch;
132 bool shouldClear =
false;
133 bool hasAllocatedOwnMemory =
false;
136 std::map<void*, char[DEBUG_NAME_SIZE]> nameMap;