summaryrefslogtreecommitdiff
path: root/deimos/core/allocator.h
diff options
context:
space:
mode:
Diffstat (limited to 'deimos/core/allocator.h')
-rw-r--r--deimos/core/allocator.h74
1 files changed, 74 insertions, 0 deletions
diff --git a/deimos/core/allocator.h b/deimos/core/allocator.h
new file mode 100644
index 0000000..4acdb6d
--- /dev/null
+++ b/deimos/core/allocator.h
@@ -0,0 +1,74 @@
+#pragma once
+
+#include "deimos/core/base.h"
+#include "deimos/core/id_name.h"
+
+namespace deimos
+{
+
+struct MemoryScope { uint32 id; };
+
+class IAllocator
+{
+protected:
+ const MemoryScope m_scope;
+
+public:
+ constexpr explicit IAllocator(MemoryScope scope) : m_scope{scope} {}
+
+ deimos_NO_COPY_MOVE(IAllocator);
+
+ virtual ~IAllocator() = default;
+
+ [[nodiscard]]
+ virtual void* Reallocate(
+ void* old_ptr, int64 old_size, int64 new_size,
+ const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) = 0;
+
+ [[nodiscard]]
+ constexpr void* Allocate(
+ int64 new_size,
+ const char* file = __builtin_FILE(), int32 line = __builtin_LINE())
+ {
+ return Reallocate(nullptr, 0, new_size, file, line);
+ }
+
+ constexpr void Free(
+ void* old_ptr, int64 old_size,
+ const char* file = __builtin_FILE(), int32 line = __builtin_LINE())
+ {
+ (void)Reallocate(old_ptr, old_size, 0, file, line);
+ }
+
+ template<typename T, typename... Args>
+ T* New(
+ Args&&... args,
+ const char* file = __builtin_FILE(), int32 line = __builtin_LINE())
+ {
+ void* ptr = Allocate(sizeof(T), file, line);
+ return new(ptr) T(std::forward<Args>(args)...);
+ }
+
+ template<typename T>
+ void Delete(
+ T* t,
+ const char* file = __builtin_FILE(), int32 line = __builtin_LINE())
+ {
+ if constexpr (!kIsTriviallyDestructible<T>)
+ {
+ t->~T();
+ }
+ Free(t, sizeof(T), file, line);
+ }
+};
+
+class AllocatorApi
+{
+public:
+ static constexpr IdName kApiName{"deimos::AllocatorApi"};
+
+ IAllocator* system{};
+};
+
+} // namespace deimos
+