From 79351592900fe1c378cdec7dd10786d1ccdf963a Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Sat, 20 Apr 2024 22:37:07 +0100 Subject: [PATCH] [+] std::pmr requirements --- Include/Aurora/Memory/CppHeapWrapper.hpp | 140 +++++++++++++++++++---- Include/Aurora/Memory/Memory.hpp | 9 +- 2 files changed, 122 insertions(+), 27 deletions(-) diff --git a/Include/Aurora/Memory/CppHeapWrapper.hpp b/Include/Aurora/Memory/CppHeapWrapper.hpp index 3143bc89..3b3a93e8 100644 --- a/Include/Aurora/Memory/CppHeapWrapper.hpp +++ b/Include/Aurora/Memory/CppHeapWrapper.hpp @@ -9,6 +9,18 @@ namespace Aurora::Memory { + namespace detail + { + struct AccessorICantEven + { + template + cstatic void Set(const T &fuckCpp, Z that) + { + fuckCpp.pFuckCppRetardsFixYourWorthlessSpec2 = decltype(fuckCpp.pFuckCppRetardsFixYourWorthlessSpec2)(that); + } + }; + } + template struct CppHeapWrapper { @@ -66,10 +78,17 @@ namespace Aurora::Memory pFuckCppRetardsFixYourWorthlessSpec(pFuckCppRetardsFixYourWorthlessSpec) { } + template + inline CppHeapWrapper(const CppHeapWrapper &fuckCpp) + { + this->pFuckCppRetardsFixYourWorthlessSpec = (CppHeapWrapper *)&fuckCpp; + detail::AccessorICantEven::Set(fuckCpp, this); + } + inline CppHeapWrapper(const CppHeapWrapper &fuckCpp) { this->pFuckCppRetardsFixYourWorthlessSpec = (CppHeapWrapper *)&fuckCpp; - fuckCpp.pFuckCppRetardsFixYourWorthlessSpec2 = this; + detail::AccessorICantEven::Set(fuckCpp, this); } inline CppHeapWrapper(CppHeapWrapper &&fuckCpp) @@ -102,54 +121,113 @@ namespace Aurora::Memory AU_DEF(CppHeapWrapper) AU_OPERATOR_COPY_MOVE(CppHeapWrapper) - constexpr void deallocate(const T *pType, - const size_t count) + void *allocate_bytes(std::size_t nbytes, + std::size_t alignment = alignof(std::max_align_t)) { - this->GetHeapRaw()->Free((T *)pType); - } - - constexpr AU_ALLOC T *allocate(const size_t count) - { - if (!count) + if (auto pRet = this->GetHeapRaw()->FAlloc(nbytes, alignment)) { - return nullptr; + return pRet; } - - auto pData = this->GetHeapRaw()->FAlloc(count * sizeof(T), alignof(T)); - if (!pData) + else { throw std::bad_alloc(); } - return (T *)pData; } + void deallocate_bytes(void *p, + std::size_t nbytes, + std::size_t alignment = alignof(std::max_align_t)) noexcept + { + return this->GetHeapRaw()->Free(p); + } + + #if defined(AU_LANG_CPP_23) + std::allocation_result allocate_at_least(const size_t count) + { + auto pThat = this->allocate(count); + return { (T *)pThat, this->GetHeapRaw()->GetChunkSize(pThat) / sizeof(T) }; + } + #endif + template void construct(U *p, Args&&... args) { - if constexpr (AuIsClass_v) + if constexpr (AuIsClass_v) { - new ((void *)p) T(AuForward(args)...); + new ((void *)p) U(AuForward(args)...); } } template void construct_at(U *p, Args&&... args) { - if constexpr (AuIsClass_v) + if constexpr (AuIsClass_v) { - new ((void *)p) T(AuForward(args)...); + new ((void *)p) U(AuForward(args)...); } } - #if defined(AU_LANG_CPP_23) - constexpr std::allocation_result allocate_at_least(const size_t count) + void deallocate(const T *pType, + const size_t count) noexcept { - auto pThat = this->allocate(count); - return { (T *)pThat, this->GetHeapRaw()->GetChunkSize(pThat) / sizeof(T) }; + this->deallocate_bytes((void *)pType, 0, 0); } - #endif - std::shared_ptr GetHeap() + 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(); + } + + this->deallocate_object(p); + } + + std::shared_ptr GetHeap() const { if (this->pFuckCppRetardsFixYourWorthlessSpec) { @@ -165,7 +243,7 @@ namespace Aurora::Memory } } - Heap *GetHeapRaw() + Heap *GetHeapRaw() const { if (this->pFuckCppRetardsFixYourWorthlessSpec) { @@ -213,6 +291,16 @@ namespace Aurora::Memory } } + inline bool operator==(const CppHeapWrapper &heap) + { + return this->GetHeapRaw() == heap.GetHeapRaw(); + } + + template + inline bool operator==(const CppHeapWrapper &heap) + { + return this->GetHeapRaw() == heap.GetHeapRaw(); + } private: // should be sizeof(void *) * 4 = [pHeap, pControlBlock, pParent, pSingleThreadChild] // nor not. it doesnt matter. @@ -224,5 +312,7 @@ namespace Aurora::Memory #endif mutable CppHeapWrapper *pFuckCppRetardsFixYourWorthlessSpec {}; mutable CppHeapWrapper *pFuckCppRetardsFixYourWorthlessSpec2 {}; + + friend struct detail::AccessorICantEven; }; } \ No newline at end of file diff --git a/Include/Aurora/Memory/Memory.hpp b/Include/Aurora/Memory/Memory.hpp index ffbcea3d..13dd8097 100644 --- a/Include/Aurora/Memory/Memory.hpp +++ b/Include/Aurora/Memory/Memory.hpp @@ -54,9 +54,14 @@ namespace Aurora::Memory return _FAlloc(uLength, uAlignment); } - static void __Free(void *buffer) + static void __Free(void *pHead) { - _Free(buffer); + _Free(pHead); + } + + static AuUInt __SizeOf(void *pHead) + { + return GetChunkSize(pHead); } #if !defined(_CPPSHARP)