diff options
Diffstat (limited to 'deimos/core/temp_allocator.h')
-rw-r--r-- | deimos/core/temp_allocator.h | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/deimos/core/temp_allocator.h b/deimos/core/temp_allocator.h index 4f80192..6d93a77 100644 --- a/deimos/core/temp_allocator.h +++ b/deimos/core/temp_allocator.h @@ -2,9 +2,74 @@ #include "deimos/core/base.h"
#include "deimos/core/id_name.h"
+#include "deimos/core/allocator.h"
namespace deimos
{
+struct TempAllocatorTag { uintptr_t tag; };
+
+class TempAllocatorApi;
+
+class ITempAllocator
+{
+public:
+ ITempAllocator() = default;
+ deimos_NO_COPY_MOVE(ITempAllocator);
+ virtual ~ITempAllocator() = default;
+
+ virtual gsl::owner<void*> Reallocate(TempAllocatorTag tag,
+ gsl::owner<void*> old_ptr, int64_t old_size, int64_t new_size) = 0;
+
+ virtual void Release(TempAllocatorTag tag) = 0;
+};
+
+class TempAllocator : public IAllocator
+{
+ ITempAllocator* m_inner;
+ TempAllocatorTag m_tag;
+
+public:
+ TempAllocator(ITempAllocator* inner, TempAllocatorTag tag) :
+ m_inner{inner}, m_tag{tag}
+ {}
+
+ deimos_NO_COPY_MOVE(TempAllocator);
+
+ ~TempAllocator() override;
+
+ [[nodiscard]]
+ gsl::owner<void*> Reallocate(
+ gsl::owner<void*> old_ptr, int64_t old_size, int64_t new_size,
+ MemoryScope scope, const SourceLocation& source_location = {}) override;
+
+ constexpr Allocator allocator() { return Allocator(this, MemoryScope{0}); }
+};
+
+class TempAllocatorApi
+{
+public:
+ TempAllocatorApi() = default;
+ deimos_NO_COPY_MOVE(TempAllocatorApi);
+ virtual ~TempAllocatorApi() = default;
+
+ static constexpr IdName kApiName{"deimos::TempAllocatorApi"};
+
+ [[nodiscard]] virtual TempAllocator Acquire() = 0;
+};
+
+[[nodiscard]]
+inline gsl::owner<void*> TempAllocator::Reallocate(
+ gsl::owner<void*> old_ptr, int64_t old_size, int64_t new_size,
+ MemoryScope, const SourceLocation&)
+{
+ return m_inner->Reallocate(m_tag, old_ptr, old_size, new_size);
+}
+
+inline TempAllocator::~TempAllocator()
+{
+ m_inner->Release(m_tag);
+}
+
} // namespace deimos
|