diff options
Diffstat (limited to 'deimos/core/allocator.h')
-rw-r--r-- | deimos/core/allocator.h | 103 |
1 files changed, 83 insertions, 20 deletions
diff --git a/deimos/core/allocator.h b/deimos/core/allocator.h index 4acdb6d..b156620 100644 --- a/deimos/core/allocator.h +++ b/deimos/core/allocator.h @@ -8,13 +8,9 @@ namespace deimos struct MemoryScope { uint32 id; };
-class IAllocator
+struct IAllocator
{
-protected:
- const MemoryScope m_scope;
-
-public:
- constexpr explicit IAllocator(MemoryScope scope) : m_scope{scope} {}
+ IAllocator() = default;
deimos_NO_COPY_MOVE(IAllocator);
@@ -23,42 +19,109 @@ public: [[nodiscard]]
virtual void* Reallocate(
void* old_ptr, int64 old_size, int64 new_size,
- const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) = 0;
+ MemoryScope scope, const SourceLocation& source_location = {}) = 0;
+};
+
+
+class Allocator
+{
+ IAllocator* m_allocator;
+ const MemoryScope m_scope;
+
+public:
+ constexpr explicit Allocator(IAllocator* allocator, MemoryScope scope) :
+ m_allocator{allocator}, m_scope{scope}
+ {}
+
+ deimos_NO_COPY_MOVE(Allocator);
+
+ ~Allocator() = default;
[[nodiscard]]
constexpr void* Allocate(
int64 new_size,
- const char* file = __builtin_FILE(), int32 line = __builtin_LINE())
+ const SourceLocation& source_location = {})
+ {
+ return m_allocator->Reallocate(nullptr, 0, new_size, m_scope, source_location);
+ }
+
+ [[nodiscard]]
+ void* Reallocate(
+ void* old_ptr, int64 old_size, int64 new_size,
+ const SourceLocation& source_location = {})
{
- return Reallocate(nullptr, 0, new_size, file, line);
+ return m_allocator->Reallocate(old_ptr, old_size, new_size, m_scope, source_location);
}
constexpr void Free(
void* old_ptr, int64 old_size,
- const char* file = __builtin_FILE(), int32 line = __builtin_LINE())
+ const SourceLocation& source_location = {})
{
- (void)Reallocate(old_ptr, old_size, 0, file, line);
+ (void)m_allocator->Reallocate(old_ptr, old_size, 0, m_scope, source_location);
}
template<typename T, typename... Args>
- T* New(
- Args&&... args,
- const char* file = __builtin_FILE(), int32 line = __builtin_LINE())
+ T* NewInner(
+ const SourceLocation& source_location,
+ Args&&... args)
{
- void* ptr = Allocate(sizeof(T), file, line);
+ void* ptr = Allocate(sizeof(T), source_location);
return new(ptr) T(std::forward<Args>(args)...);
}
template<typename T>
- void Delete(
- T* t,
- const char* file = __builtin_FILE(), int32 line = __builtin_LINE())
+ constexpr T* New(const SourceLocation& source_location = {})
+ {
+ return NewInner<T>(source_location);
+ }
+
+ template<typename T, typename A0>
+ constexpr T* New(A0&& arg0, const SourceLocation& source_location = {})
+ {
+ return NewInner<T>(source_location, std::forward<A0>(arg0));
+ }
+
+ template<typename T, typename A0, typename A1>
+ constexpr T* New(
+ A0&& arg0, A1&& arg1,
+ const SourceLocation& source_location = {})
+ {
+ return NewInner<T>(source_location,
+ std::forward<A0>(arg0),
+ std::forward<A1>(arg1));
+ }
+
+ template<typename T, typename A0, typename A1, typename A2>
+ constexpr T* New(
+ A0&& arg0, A1&& arg1, A2&& arg2,
+ const SourceLocation& source_location = {})
+ {
+ return NewInner<T>(source_location,
+ std::forward<A0>(arg0),
+ std::forward<A1>(arg1),
+ std::forward<A2>(arg2));
+ }
+
+ template<typename T, typename A0, typename A1, typename A2, typename A3>
+ constexpr T* New(
+ A0&& arg0, A1&& arg1, A2&& arg2, A3&& arg3,
+ const SourceLocation& source_location = {})
+ {
+ return NewInner<T>(source_location,
+ std::forward<A0>(arg0),
+ std::forward<A1>(arg1),
+ std::forward<A2>(arg2),
+ std::forward<A3>(arg3));
+ }
+
+ template<typename T>
+ void Delete(T* t, const SourceLocation& source_location = {})
{
if constexpr (!kIsTriviallyDestructible<T>)
{
t->~T();
}
- Free(t, sizeof(T), file, line);
+ Free(t, sizeof(T), source_location);
}
};
@@ -67,7 +130,7 @@ class AllocatorApi public:
static constexpr IdName kApiName{"deimos::AllocatorApi"};
- IAllocator* system{};
+ Allocator* system{};
};
} // namespace deimos
|