From 8e3a0efa68617e52db4125c17b82d57c7fc7e4a4 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Sat, 20 Apr 2024 22:37:52 +0100 Subject: [PATCH] [+] std::pmr requirements --- Include/auROXTL/auMemoryModel.hpp | 113 +++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 10 deletions(-) diff --git a/Include/auROXTL/auMemoryModel.hpp b/Include/auROXTL/auMemoryModel.hpp index 87ccf33..d3d2048 100644 --- a/Include/auROXTL/auMemoryModel.hpp +++ b/Include/auROXTL/auMemoryModel.hpp @@ -262,6 +262,7 @@ namespace Aurora::Memory static void *__FAlloc(Types::size_t length, Types::size_t align); static void __Free(void *buffer); + static AuUInt __SizeOf(void *pHead); template struct BaseAuroraRuntimeAllocator @@ -273,23 +274,115 @@ namespace Aurora::Memory constexpr BaseAuroraRuntimeAllocator() {} - template constexpr BaseAuroraRuntimeAllocator(const BaseAuroraRuntimeAllocator &) noexcept + template + constexpr BaseAuroraRuntimeAllocator(const BaseAuroraRuntimeAllocator &) noexcept { } - inline constexpr T* allocate(Types::size_t n) + void *allocate_bytes(std::size_t nbytes, + std::size_t alignment = alignof(std::max_align_t)) { - if (auto p = (__FAlloc(n * sizeof(T), alignof(T)))) + if (auto pRet = __FAlloc(nbytes, alignment)) { - return AuReinterpretCast(p); + return pRet; + } + else + { + throw std::bad_alloc(); + } + } + + void deallocate_bytes(void *p, + std::size_t nbytes, + std::size_t alignment = alignof(std::max_align_t)) + { + return __Free(p); + } + + #if defined(AU_LANG_CPP_23) + constexpr std::allocation_result allocate_at_least(const size_t count) + { + auto pThat = this->allocate(count); + return { (T *)pThat, __SizeOf(pThat) / sizeof(T) }; + } + #endif + + template + void construct(U *p, Args&&... args) + { + if constexpr (AuIsClass_v) + { + new ((void *)p) U(AuForward(args)...); + } + } + + template + void construct_at(U *p, Args&&... args) + { + if constexpr (AuIsClass_v) + { + new ((void *)p) U(AuForward(args)...); + } + } + + constexpr void deallocate(const T *pType, + const size_t count) + { + this->deallocate_bytes((void *)pType, 0, 0); + } + + constexpr AU_ALLOC T *allocate(const size_t count) + { + if (!count) + { + return nullptr; + } + + return (T *)this->allocate_bytes(count * sizeof(T), alignof(T)); + } + + template + U *allocate_object(std::size_t n = 1) + { + return (U *)this->allocate_bytes(sizeof(U) * n, alignof(U)); + } + + template + void deallocate_object(U *p, std::size_t n = 1) + { + this->deallocate_bytes(p, 0, 0); + } + + template + U *new_object(CtorArgs&&... args) + { + U *p = this->allocate_object(); + if constexpr (AuIsClass_v && + !AuIsTriviallyConstructible_v) + { + try + { + this->construct(p, AuForward(args)...); + } + catch (...) + { + this->deallocate_object(p); + throw; + } + } + return p; + } + + template + void delete_object(U *p) + { + if constexpr (AuIsClass_v && + !AuIsTriviallyDestructible_v) + { + p->~U(); } - throw std::bad_alloc(); - } - - inline constexpr void deallocate(T *p, std::size_t n) noexcept - { - __Free(p); + this->deallocate_object(p); } inline bool operator==(const BaseAuroraRuntimeAllocator &op)