From 00c0d78199fcfbbb20828be5e06fd2d271fa4c1e Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sun, 24 Mar 2024 23:49:26 +0100 Subject: Initial commit --- deimos/core/allocator.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 deimos/core/allocator.cpp (limited to 'deimos/core/allocator.cpp') diff --git a/deimos/core/allocator.cpp b/deimos/core/allocator.cpp new file mode 100644 index 0000000..a525bfd --- /dev/null +++ b/deimos/core/allocator.cpp @@ -0,0 +1,55 @@ +#include "deimos/core/allocator.h" + +#include + +namespace deimos +{ + +class SystemAllocatorImpl : public IAllocator +{ +public: + SystemAllocatorImpl() : IAllocator(MemoryScope{0}) {} + + void* Reallocate( + void* old_ptr, int64 /* old_size */, int64 new_size, + const char* /* file */, int32 /* line */) override + { + if (old_ptr == nullptr) + { + return new_size > 0 ? ::malloc((uint64)new_size) : nullptr; + } + + if (new_size == 0) + { + if (old_ptr != nullptr) + { + ::free(old_ptr); + } + } + else + { + return ::realloc(old_ptr, (uint64)new_size); + } + return nullptr; + } +}; + +class AllocatorApiImpl : public AllocatorApi +{ + SystemAllocatorImpl m_system_impl; + +public: + AllocatorApiImpl() + { + system = &m_system_impl; + } +}; + +AllocatorApi* BootstrapAllocatorApi() +{ + static AllocatorApiImpl g_instance{}; + return &g_instance; +} + +} // namespace deimos + -- cgit