From 298ab886487813519105cbd375b20d8fa3af2ce2 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Wed, 24 Apr 2024 00:52:22 +0100 Subject: [PATCH] [+] AuMemoryView::TryPromoteToSharedView [+] AuMemoryView::TryDemoteFromSharedView [+] AuMemory::RequestHeapOfSharedRegion --- Include/Aurora/Memory/Heap.hpp | 7 +++- Include/Aurora/Memory/MemoryView.hpp | 55 ++++++++++++++++++++++++++++ Source/Memory/AuHeapInternal.cpp | 45 +++++++++++++++++++++++ Source/Memory/AuHeapInternal.hpp | 2 + 4 files changed, 108 insertions(+), 1 deletion(-) diff --git a/Include/Aurora/Memory/Heap.hpp b/Include/Aurora/Memory/Heap.hpp index 08a8b306..3224d4bc 100644 --- a/Include/Aurora/Memory/Heap.hpp +++ b/Include/Aurora/Memory/Heap.hpp @@ -488,10 +488,15 @@ namespace Aurora::Memory AUKN_SHARED_SOO2_NCM(AllocHeap, Heap, kHeapSize, ((AuUInt, uLength)), AuUInt uLength); /** - * @warning the SOO variant cannot guarantee release-on-last-free. + * @warning the SOO variant cannot guarantee release-on-last-free and will panic if an invalid memory handle is provided. */ AUKN_SHARED_SOO2_NCM(RequestHeapOfRegion, Heap, kHeapSize, ((const MemoryViewWrite &, memory)), const MemoryViewWrite &memory); + /** + * @warning the SOO variant cannot guarantee release-on-last-free and will panic if an invalid memory handle is provided. + */ + AUKN_SHARED_SOO2_NCM(RequestHeapOfSharedRegion, Heap, kHeapSize, ((const AuSPtr &, memory)), const AuSPtr &pMemory); + /** * Proxies an existing heap with encapsulated statistics. * This is intended for debugging purposes when accurate heap stats of a heap-subset are desired. diff --git a/Include/Aurora/Memory/MemoryView.hpp b/Include/Aurora/Memory/MemoryView.hpp index 0a16b038..abacbc06 100644 --- a/Include/Aurora/Memory/MemoryView.hpp +++ b/Include/Aurora/Memory/MemoryView.hpp @@ -367,6 +367,61 @@ namespace Aurora::Memory return this->controlBlock.pInUseCounter || this->controlBlock.pPinner; } + + AuSPtr TryPromoteToSharedView(AuSPtr pParent = {}) + { + #if 0 + bool bHasControlBlock = this->HasControlBlock(); + + if (bHasControlBlock) + { + if (pParent) + { + return AuMakeShared(*this, pParent); + } + else + { + return AuMakeShared(*this); + } + } + else + { + if (pParent) + { + return AuMakeShared(*this, pParent); + } + else + { + return {}; + } + } + #else + if (pParent) + { + return AuMakeShared(*this, pParent); + } + + if (this->HasControlBlock()) + { + return AuMakeShared(*this); + } + + return {}; + #endif + } + + bool TryDemoteFromSharedView(const AuSPtr &pCopy) + { + if (pCopy && pCopy->HasControlBlock()) + { + AuResetMember(*this, AuConstReference(*pCopy.get())); + return true; + } + else + { + return false; + } + } }; using MemoryViewRead = MemoryView; diff --git a/Source/Memory/AuHeapInternal.cpp b/Source/Memory/AuHeapInternal.cpp index 13469bcc..fdd09545 100644 --- a/Source/Memory/AuHeapInternal.cpp +++ b/Source/Memory/AuHeapInternal.cpp @@ -31,6 +31,11 @@ namespace Aurora::Memory SysAssert(this->Init(memory)); } + InternalHeap::InternalHeap(const AuSPtr &pMemory) + { + SysAssert(this->Init(pMemory), "Couldn't initialize inline AuHeap! [OOM]"); + } + InternalHeap::InternalHeap(AuUInt uLength) { SysAssert(this->Init(uLength), "Couldn't initialize inline AuHeap! [OOM]"); @@ -70,6 +75,17 @@ namespace Aurora::Memory return this->Init(memory.length, memory.ptr); } + bool InternalHeap::Init(const AuSPtr &pMemory) + { + if (!pMemory || !*pMemory) + { + return false; + } + + this->pBloat_ = pMemory; + return this->Init(pMemory->length, pMemory->ptr); + } + bool InternalHeap::Init(AuUInt uLength, void *ptr) { SysAssert(!this->pBase_, "heap already initialized"); @@ -354,6 +370,35 @@ namespace Aurora::Memory static_cast(pHeap)->RequestTermination(); } + AUKN_SYM Heap *RequestHeapOfSharedRegionNew(const AuSPtr &pMemory) + { + if (!pMemory) + { + SysPushErrorArg(); + return nullptr; + } + + auto pHeap = _new InternalHeap(); + if (!pHeap) + { + return nullptr; + } + + if (!pHeap->Init(pMemory)) + { + delete pHeap; + return nullptr; + } + + return pHeap; + } + + AUKN_SYM void RequestHeapOfSharedRegionRelease(Heap *pHeap) + { + static_cast(pHeap)->RequestTermination(); + } + AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, RequestHeapOfRegion, InternalHeap, (const MemoryViewWrite &, memory)) + AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, RequestHeapOfSharedRegion, InternalHeap, (const AuSPtr &, memory)) AUROXTL_INTERFACE_SOO_SRC_EX(AURORA_SYMBOL_EXPORT, AllocHeap, InternalHeap, (AuUInt, uLength)) } \ No newline at end of file diff --git a/Source/Memory/AuHeapInternal.hpp b/Source/Memory/AuHeapInternal.hpp index 266c3969..4b89d431 100644 --- a/Source/Memory/AuHeapInternal.hpp +++ b/Source/Memory/AuHeapInternal.hpp @@ -23,10 +23,12 @@ namespace Aurora::Memory InternalHeap(); InternalHeap(const MemoryViewWrite &memory); + InternalHeap(const AuSPtr &pMemory); InternalHeap(AuUInt uLength); virtual ~InternalHeap(); bool Init(const MemoryViewWrite &memory); + bool Init(const AuSPtr &pMemory); bool Init(AuUInt uLength, void *ptr = nullptr); static AuUInt GetHeapSize(const void *ptr);