From ced9e0be174ba35948f68b6804b66a75c290f7d9 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Mon, 15 Jul 2024 00:14:58 +0100 Subject: [PATCH] [*] Optimize global process heap aliases [*] Split AuMemory::Heap impl into .inl file --- Include/Aurora/Memory/Heap.hpp | 406 ++++------------------------ Include/Aurora/Memory/Heap.inl | 432 ++++++++++++++++++++++++++++++ Include/Aurora/Memory/Memory.hpp | 3 +- Include/Aurora/RuntimeAliases.hpp | 15 +- 4 files changed, 498 insertions(+), 358 deletions(-) create mode 100644 Include/Aurora/Memory/Heap.inl diff --git a/Include/Aurora/Memory/Heap.hpp b/Include/Aurora/Memory/Heap.hpp index 5f709baf..449fb33f 100644 --- a/Include/Aurora/Memory/Heap.hpp +++ b/Include/Aurora/Memory/Heap.hpp @@ -16,6 +16,16 @@ namespace Aurora::Memory template struct CppHeapWrapper; + /** + * Note: The following public aliases for heap or global process heap based allocations exist: + * + * AuHUPOf_t AuNewClassArrayUnique([pHeap, ]uElements, ...) + * AuSPtr AuNewClassArray([pHeap, ]uElements, ...) + * AuHUPOf_t AuNewClassUnique([pHeap, ] ...) + * AuSPtr AuNewClass([pHeap, ] ...) + * AuHUPOf_t AuNullHeapPointer() + */ + struct Heap { virtual AuSPtr AllocateDivision(AuUInt32 heap, AuUInt32 alignment = 32) = 0; @@ -23,415 +33,103 @@ namespace Aurora::Memory virtual HeapStats &GetStats() = 0; virtual void WalkHeap(bool(*fCallback)(void *, void *), void *pSecondArg) = 0; - // Potentially slower, zero allocate + /// Potentially slower, zero allocate template - T ZAlloc(Types::size_t uLength) - { - if constexpr (AuIsVoid_v>) - { - return reinterpret_cast(_ZAlloc(uLength)); - } - else - { - return reinterpret_cast(_ZAlloc(uLength, alignof(AuRemovePointer_t))); - } - } + T ZAlloc(Types::size_t uLength); + /// POD zero allocation template - T ZAlloc(Types::size_t uLength, Types::size_t uAlignment) - { - return reinterpret_cast(_ZAlloc(uLength, uAlignment)); - } + T ZAlloc(Types::size_t uLength, Types::size_t uAlignment); + /// POD zero allocation template - T *ZAlloc() - { - return reinterpret_cast(_ZAlloc(sizeof(T), alignof(T))); - } + T *ZAlloc(); + /// POD array, zero allocation template - T *NewArray(Types::size_t uLength) - { - return ZAlloc(uLength * sizeof(T), alignof(T)); - } + T *NewArray(Types::size_t uLength); + /// POD array, zero allocation template - T *NewArray(Types::size_t uLength, Types::size_t uAlignment) - { - return ZAlloc(uLength * sizeof(T), uAlignment); - } + T *NewArray(Types::size_t uLength, Types::size_t uAlignment); - /// Fast, unsafe alloc + /// Fast, POD, non-zeroing allocation template - T FAlloc(Types::size_t uLength) - { - if constexpr (AuIsVoid_v>) - { - return reinterpret_cast(_FAlloc(uLength)); - } - else - { - return reinterpret_cast(_FAlloc(uLength, alignof(AuRemovePointer_t))); - } - } + T FAlloc(Types::size_t uLength); + /// Fast, POD, non-zeroing allocation template - T FAlloc(Types::size_t uLength, Types::size_t uAlignment) - { - return reinterpret_cast(_FAlloc(uLength, uAlignment)); - } + T FAlloc(Types::size_t uLength, Types::size_t uAlignment); + /// Fast, POD, non-zeroing allocation template - T *FAlloc() - { - return reinterpret_cast(_FAlloc(sizeof(T), alignof(T))); - } + T *FAlloc(); // Reallocs - template - T ZRealloc(T pHead, Types::size_t uLength) - { - if constexpr (AuIsVoid_v>) - { - return reinterpret_cast(_ZRealloc(reinterpret_cast(pHead), uLength)); - } - else - { - return reinterpret_cast(_ZRealloc(reinterpret_cast(pHead), uLength, alignof(AuRemovePointer_t))); - } - } + /// POD, zero-based expansion or reallocation template - T ZRealloc(T pHead, Types::size_t uLength, Types::size_t uAlignment) - { - return reinterpret_cast(_ZRealloc(reinterpret_cast(pHead), uLength, uAlignment)); - } + T ZRealloc(T pHead, Types::size_t uLength); + /// POD, zero-based expansion or reallocation template - T FRealloc(T pHead, Types::size_t uLength) - { - if constexpr (AuIsVoid_v>) - { - return reinterpret_cast(_FRealloc(reinterpret_cast(pHead), uLength)); - } - else - { - return reinterpret_cast(_FRealloc(reinterpret_cast(pHead), uLength, alignof(AuRemovePointer_t))); - } - } + T ZRealloc(T pHead, Types::size_t uLength, Types::size_t uAlignment); + /// POD, expansion or reallocation template - T FRealloc(T pHead, Types::size_t uLength, Types::size_t uAlignment) - { - return reinterpret_cast(_FRealloc(reinterpret_cast(pHead), uLength, uAlignment)); - } + T FRealloc(T pHead, Types::size_t uLength); - // Free + /// POD, expansion or reallocation template - void Free(T pHead) - { - _Free(reinterpret_cast(pHead)); - } + T FRealloc(T pHead, Types::size_t uLength, Types::size_t uAlignment); + + /// Free + template + void Free(T pHead); protected: template - static void DeleteThat(T *pThat) - { - static const auto kAlignment = AuMax(alignof(T), sizeof(void *)); - - if constexpr (AuIsClass_v && - !AuIsTriviallyDestructible_v) - { - pThat->~T(); - } - - auto &pHeap = *(Heap **)(((char *)pThat) - kAlignment); - pHeap->_Free(&pHeap); - } + static void DeleteThat(T *pThat); template - static void DeleteThatArray(T *pThat) - { - static const auto kAlignment = AuMax(alignof(T), sizeof(void *) * 2); - - auto pVoids = (void **)(((char *)pThat) - kAlignment); - auto pHeap = (Heap *)pVoids[0]; - auto uLength = (AuUInt)pVoids[1]; - - if constexpr (AuIsClass_v && - !AuIsTriviallyDestructible_v) - { - for (AU_ITERATE_N(i, uLength)) - { - auto &refElement = pThat[i]; - refElement.~T(); - } - } - - pHeap->_Free(pVoids); - } + static void DeleteThatArray(T *pThat); template - static void DeleteThatCastedOnce(T *pThat) - { - static const auto kAlignment = AuMax(alignof(Z), sizeof(void *)); - - auto pBaseClass = AuStaticCast(pThat); - - if constexpr (AuIsClass_v && - !AuIsTriviallyDestructible_v) - { - pBaseClass->~Z(); - } - - auto &pHeap = *(Heap **)(((char *)pBaseClass) - kAlignment); - pHeap->_Free(&pHeap); - } + static void DeleteThatCastedOnce(T *pThat); template - static void RetardedSpecWrittenByRetards(T *pThat) - { - - } + static void RetardedSpecWrittenByRetards(T *pThat); public: template - AuSPtr NewClass(Args &&...args) - { - static const auto kAlignment = AuMax(alignof(T), sizeof(void *)); - AuUInt8 *pPtr; - - auto pThat = this->GetSelfReferenceRaw(); - if (!pThat) - { - pThat = this; - } - - if constexpr (AuIsClass_v && - !AuIsTriviallyConstructible_v) - { - pPtr = pThat->FAlloc(sizeof(T) + kAlignment, kAlignment); - if (pPtr) - { - new (pPtr + kAlignment) T(AuForward(args)...); - } - } - else - { - pPtr = pThat->ZAlloc(sizeof(T) + kAlignment, kAlignment); - } - - if (!pPtr) - { - return {}; - } - - *(void **)pPtr = pThat; - - auto pTThat = (T *)(pPtr + kAlignment); - AUROXTL_COMMODITY_TRY - { - return AuSPtr(pTThat, &Heap::DeleteThat, CppHeapWrapper { this }); - } - AUROXTL_COMMODITY_CATCH - { - Heap::DeleteThat(pTThat); - return {}; - } - } + AuSPtr NewClass(Args &&...args); // note: callers can use AuHUPOf_t pUniquePointer = AuNullHeapPointer() template - AuUPtr)> NewClassUnique(Args &&...args) - { - static const auto kAlignment = AuMax(alignof(T), sizeof(void *)); - AuUInt8 *pPtr; - - auto pThat = this->GetSelfReferenceRaw(); - if (!pThat) - { - pThat = this; - } - - if constexpr (AuIsClass_v && - !AuIsTriviallyConstructible_v) - { - pPtr = pThat->FAlloc(sizeof(T) + kAlignment, kAlignment); - if (pPtr) - { - new (pPtr + kAlignment) T(AuForward(args)...); - } - } - else - { - pPtr = pThat->ZAlloc(sizeof(T) + kAlignment, kAlignment); - } - - if (!pPtr) - { - return AuUPtr)>(nullptr, &Heap::RetardedSpecWrittenByRetards); - } - - *(void **)pPtr = pThat; - - if constexpr (AuIsSame_v) - { - return AuUPtr)>((T *)(pPtr + kAlignment), &Heap::DeleteThat); - } - else - { - return Heap::CastPointer(AuMove(AuUPtr)>((T *)(pPtr + kAlignment), &Heap::DeleteThat))); - } - } + AuUPtr)> NewClassUnique(Args &&...args); template - AuSPtr NewClassArray(AuUInt uElements, Args &&... fillCtr) - { - static const auto kAlignment = AuMax(alignof(T), sizeof(void *) * 2); - AuUInt8 *pPtr; + AuSPtr NewClassArray(AuUInt uElements, Args &&... fillCtr); - if (!uElements) - { - return {}; - } - - auto pThat = this->GetSelfReferenceRaw(); - if (!pThat) - { - pThat = this; - } - - if constexpr (AuIsClass_v && - !AuIsTriviallyConstructible_v) - { - if (bool(pPtr = pThat->FAlloc((sizeof(T) * uElements) + kAlignment, kAlignment))) - { - for (AU_ITERATE_N(i, uElements)) - { - new (pPtr + kAlignment + (sizeof(T) * i)) T(AuForward(fillCtr)...); - } - } - } - else - { - if (bool(pPtr = pThat->ZAlloc((sizeof(T) * uElements) + kAlignment, kAlignment))) - { - if constexpr (sizeof...(Args) != 0) - { - #if defined(AURT_HEAP_NO_STL) - static_assert(false); - #else - auto pElements = (T *)(pPtr + kAlignment); - std::fill(pElements, pElements + uElements, AuForward(fillCtr)...); - #endif - } - } - } - - if (!pPtr) - { - return {}; - } - - auto pVoids = (void **)pPtr; - pVoids[0] = pThat; - pVoids[1] = (void *)uElements; - - auto pTThat = (T *)(pPtr + kAlignment); - AUROXTL_COMMODITY_TRY - { - return AuSPtr(pTThat, &Heap::DeleteThatArray, CppHeapWrapper { this }); - } - AUROXTL_COMMODITY_CATCH - { - Heap::DeleteThatArray(pTThat); - return {}; - } - } + template + AuSPtr NewClassArray2(AuUInt uElements, AuUInt uAlignment, Args &&... fillCtr); // note: callers can use AuHUPOf_t pUniquePointer = AuNullHeapPointer() template - AuUPtr)> NewClassArrayUnique(AuUInt uElements, Args &&... fillCtr) - { - static const auto kAlignment = AuMax(alignof(T), sizeof(void *) * 2); - AuUInt8 *pPtr; + AuUPtr)> NewClassArrayUnique(AuUInt uElements, Args &&... fillCtr); - if (!uElements) - { - return AuUPtr)>(nullptr, &Heap::RetardedSpecWrittenByRetards); - } - - auto pThat = this->GetSelfReferenceRaw(); - if (!pThat) - { - pThat = this; - } - - if constexpr (AuIsClass_v && - !AuIsTriviallyConstructible_v) - { - if (bool(pPtr = pThat->FAlloc((sizeof(T) * uElements) + kAlignment, kAlignment))) - { - for (AU_ITERATE_N(i, uElements)) - { - new (pPtr + kAlignment + (sizeof(T) * i)) T(AuForward(fillCtr)...); - } - } - } - else - { - if (bool(pPtr = pThat->ZAlloc((sizeof(T) * uElements) + kAlignment, kAlignment))) - { - if constexpr (sizeof...(Args) != 0) - { - #if defined(AURT_HEAP_NO_STL) - static_assert(false); - #else - auto pElements = (T *)(pPtr + kAlignment); - std::fill(pElements, pElements + uElements, AuForward(fillCtr)...); - #endif - } - } - } - - if (!pPtr) - { - return AuUPtr)>(nullptr, &Heap::RetardedSpecWrittenByRetards); - } - - auto pVoids = (void **)pPtr; - pVoids[0] = pThat; - pVoids[1] = (void *)uElements; - - return AuUPtr)>((T *)(pPtr + kAlignment), &Heap::DeleteThatArray); - } + template + AuUPtr)> NewClassArray2Unique(AuUInt uElements, AuUInt uAlignment, Args &&... fillCtr); template - cstatic AuUPtr)> NullUniquePointer() - { - return AuUPtr)>(nullptr, &Heap::RetardedSpecWrittenByRetards); - } + cstatic AuUPtr)> NullUniquePointer(); template - cstatic AuUPtr)> CastPointer(AuUPtr)> &&pInPointer) - { - if (!pInPointer) - { - return NullUniquePointer(); - } - else if (pInPointer.get_deleter() == &Heap::DeleteThat) - { - return AuUPtr)>(AuStaticCast(pInPointer.release()), &Heap::DeleteThatCastedOnce); - } - else - { - return NullUniquePointer(); - } - } + cstatic AuUPtr)> CastPointer(AuUPtr)> &&pInPointer); template using HUPOf_t = AuUPtr)>; diff --git a/Include/Aurora/Memory/Heap.inl b/Include/Aurora/Memory/Heap.inl new file mode 100644 index 00000000..fa933a38 --- /dev/null +++ b/Include/Aurora/Memory/Heap.inl @@ -0,0 +1,432 @@ +/*** + Copyright (C) 2021-2024 Jamie Reece Wilson (a/k/a "Reece"). All rights reserved. + + File: Heap.inl + Date: 2024-7-14 + Date: 2021-6-9 + Author: Reece +***/ +#pragma once + +namespace Aurora::Memory +{ + template + T Heap::ZAlloc(Types::size_t uLength) + { + if constexpr (AuIsVoid_v>) + { + return reinterpret_cast(_ZAlloc(uLength)); + } + else + { + return reinterpret_cast(_ZAlloc(uLength, alignof(AuRemovePointer_t))); + } + } + + template + T Heap::ZAlloc(Types::size_t uLength, Types::size_t uAlignment) + { + return reinterpret_cast(_ZAlloc(uLength, uAlignment)); + } + + template + T *Heap::ZAlloc() + { + return reinterpret_cast(_ZAlloc(sizeof(T), alignof(T))); + } + + template + T *Heap::NewArray(Types::size_t uLength) + { + return ZAlloc(uLength * sizeof(T), alignof(T)); + } + + template + T *Heap::NewArray(Types::size_t uLength, Types::size_t uAlignment) + { + return ZAlloc(uLength * sizeof(T), uAlignment); + } + + template + T Heap::FAlloc(Types::size_t uLength) + { + if constexpr (AuIsVoid_v>) + { + return reinterpret_cast(_FAlloc(uLength)); + } + else + { + return reinterpret_cast(_FAlloc(uLength, alignof(AuRemovePointer_t))); + } + } + + template + T Heap::FAlloc(Types::size_t uLength, Types::size_t uAlignment) + { + return reinterpret_cast(_FAlloc(uLength, uAlignment)); + } + + template + T *Heap::FAlloc() + { + return reinterpret_cast(_FAlloc(sizeof(T), alignof(T))); + } + + // Reallocs + template + T Heap::ZRealloc(T pHead, Types::size_t uLength) + { + if constexpr (AuIsVoid_v>) + { + return reinterpret_cast(_ZRealloc(reinterpret_cast(pHead), uLength)); + } + else + { + return reinterpret_cast(_ZRealloc(reinterpret_cast(pHead), uLength, alignof(AuRemovePointer_t))); + } + } + + template + T Heap::ZRealloc(T pHead, Types::size_t uLength, Types::size_t uAlignment) + { + return reinterpret_cast(_ZRealloc(reinterpret_cast(pHead), uLength, uAlignment)); + } + + template + T Heap::FRealloc(T pHead, Types::size_t uLength) + { + if constexpr (AuIsVoid_v>) + { + return reinterpret_cast(_FRealloc(reinterpret_cast(pHead), uLength)); + } + else + { + return reinterpret_cast(_FRealloc(reinterpret_cast(pHead), uLength, alignof(AuRemovePointer_t))); + } + } + + template + T Heap::FRealloc(T pHead, Types::size_t uLength, Types::size_t uAlignment) + { + return reinterpret_cast(_FRealloc(reinterpret_cast(pHead), uLength, uAlignment)); + } + + // Free + template + void Heap::Free(T pHead) + { + _Free(reinterpret_cast(pHead)); + } + + template + void Heap::DeleteThat(T *pThat) + { + static const auto kAlignment = AuMax(alignof(T), sizeof(void *)); + + if constexpr (AuIsClass_v && + !AuIsTriviallyDestructible_v) + { + pThat->~T(); + } + + auto &pHeap = *(Heap **)(((char *)pThat) - kAlignment); + pHeap->_Free(&pHeap); + } + + template + void Heap::DeleteThatArray(T *pThat) + { + static const auto kAlignment = AuMax(alignof(T), sizeof(void *) * 2); + + auto pVoids = (void **)(((char *)pThat) - kAlignment); + auto pHeap = (Heap *)pVoids[0]; + auto uLength = (AuUInt)pVoids[1]; + + if constexpr (AuIsClass_v && + !AuIsTriviallyDestructible_v) + { + for (AU_ITERATE_N(i, uLength)) + { + auto &refElement = pThat[i]; + refElement.~T(); + } + } + + pHeap->_Free(pVoids); + } + + template + void Heap::DeleteThatCastedOnce(T *pThat) + { + static const auto kAlignment = AuMax(alignof(Z), sizeof(void *)); + + auto pBaseClass = AuStaticCast(pThat); + + if constexpr (AuIsClass_v && + !AuIsTriviallyDestructible_v) + { + pBaseClass->~Z(); + } + + auto &pHeap = *(Heap **)(((char *)pBaseClass) - kAlignment); + pHeap->_Free(&pHeap); + } + + template + static void Heap::RetardedSpecWrittenByRetards(T *pThat) + { + + } + + template + AuSPtr Heap::NewClass(Args &&...args) + { + static const auto kAlignment = AuMax(alignof(T), sizeof(void *)); + AuUInt8 *pPtr; + + auto pThat = this->GetSelfReferenceRaw(); + if (!pThat) + { + pThat = this; + } + + if constexpr (AuIsClass_v && + !AuIsTriviallyConstructible_v) + { + pPtr = pThat->FAlloc(sizeof(T) + kAlignment, kAlignment); + if (pPtr) + { + new (pPtr + kAlignment) T(AuForward(args)...); + } + } + else + { + pPtr = pThat->ZAlloc(sizeof(T) + kAlignment, kAlignment); + } + + if (!pPtr) + { + return {}; + } + + *(void **)pPtr = pThat; + + auto pTThat = (T *)(pPtr + kAlignment); + AUROXTL_COMMODITY_TRY + { + return AuSPtr(pTThat, &Heap::DeleteThat, CppHeapWrapper { this }); + } + AUROXTL_COMMODITY_CATCH + { + Heap::DeleteThat(pTThat); + return {}; + } + } + + template + AuUPtr)> Heap::NewClassUnique(Args &&...args) + { + static const auto kAlignment = AuMax(alignof(T), sizeof(void *)); + AuUInt8 *pPtr; + + auto pThat = this->GetSelfReferenceRaw(); + if (!pThat) + { + pThat = this; + } + + if constexpr (AuIsClass_v && + !AuIsTriviallyConstructible_v) + { + pPtr = pThat->FAlloc(sizeof(T) + kAlignment, kAlignment); + if (pPtr) + { + new (pPtr + kAlignment) T(AuForward(args)...); + } + } + else + { + pPtr = pThat->ZAlloc(sizeof(T) + kAlignment, kAlignment); + } + + if (!pPtr) + { + return AuUPtr)>(nullptr, &Heap::RetardedSpecWrittenByRetards); + } + + *(void **)pPtr = pThat; + + if constexpr (AuIsSame_v) + { + return AuUPtr)>((T *)(pPtr + kAlignment), &Heap::DeleteThat); + } + else + { + return Heap::CastPointer(AuMove(AuUPtr)>((T *)(pPtr + kAlignment), &Heap::DeleteThat))); + } + } + + template + AuSPtr Heap::NewClassArray(AuUInt uElements, Args &&... fillCtr) + { + return NewClassArray2(uElements, alignof(T), AuForward(fillCtr)...); + } + + template + AuSPtr Heap::NewClassArray2(AuUInt uElements, AuUInt uAlignment, Args &&... fillCtr) + { + const auto kAlignment = AuMax(uAlignment, sizeof(void *) * 2); + AuUInt8 *pPtr; + + if (!uElements) + { + return {}; + } + + auto pThat = this->GetSelfReferenceRaw(); + if (!pThat) + { + pThat = this; + } + + if constexpr (AuIsClass_v && + !AuIsTriviallyConstructible_v) + { + if (bool(pPtr = pThat->FAlloc((sizeof(T) * uElements) + kAlignment, kAlignment))) + { + for (AU_ITERATE_N(i, uElements)) + { + new (pPtr + kAlignment + (sizeof(T) * i)) T(AuForward(fillCtr)...); + } + } + } + else + { + if (bool(pPtr = pThat->ZAlloc((sizeof(T) * uElements) + kAlignment, kAlignment))) + { + if constexpr (sizeof...(Args) != 0) + { + #if defined(AURT_HEAP_NO_STL) + static_assert(false); + #else + auto pElements = (T *)(pPtr + kAlignment); + std::fill(pElements, pElements + uElements, AuForward(fillCtr)...); + #endif + } + } + } + + if (!pPtr) + { + return {}; + } + + auto pVoids = (void **)pPtr; + pVoids[0] = pThat; + pVoids[1] = (void *)uElements; + + auto pTThat = (T *)(pPtr + kAlignment); + AUROXTL_COMMODITY_TRY + { + return AuSPtr(pTThat, &Heap::DeleteThatArray, CppHeapWrapper { this }); + } + AUROXTL_COMMODITY_CATCH + { + Heap::DeleteThatArray(pTThat); + return {}; + } + } + + template + AuUPtr)> Heap::NewClassArrayUnique(AuUInt uElements, Args &&... fillCtr) + { + return NewClassArray2Unique(uElements, alignof(T), AuForward(fillCtr)...); + } + + template + AuUPtr)> Heap::NewClassArray2Unique(AuUInt uElements, AuUInt uAlignment, Args &&... fillCtr) + { + const auto kAlignment = AuMax(uAlignment, sizeof(void *) * 2); + AuUInt8 *pPtr; + + if (!uElements) + { + return AuUPtr)>(nullptr, &Heap::RetardedSpecWrittenByRetards); + } + + auto pThat = this->GetSelfReferenceRaw(); + if (!pThat) + { + pThat = this; + } + + if constexpr (AuIsClass_v && + !AuIsTriviallyConstructible_v) + { + if (bool(pPtr = pThat->FAlloc((sizeof(T) * uElements) + kAlignment, kAlignment))) + { + for (AU_ITERATE_N(i, uElements)) + { + new (pPtr + kAlignment + (sizeof(T) * i)) T(AuForward(fillCtr)...); + } + } + } + else + { + if (bool(pPtr = pThat->ZAlloc((sizeof(T) * uElements) + kAlignment, kAlignment))) + { + if constexpr (sizeof...(Args) != 0) + { + #if defined(AURT_HEAP_NO_STL) + static_assert(false); + #else + auto pElements = (T *)(pPtr + kAlignment); + std::fill(pElements, pElements + uElements, AuForward(fillCtr)...); + #endif + } + } + } + + if (!pPtr) + { + return AuUPtr)>(nullptr, &Heap::RetardedSpecWrittenByRetards); + } + + auto pVoids = (void **)pPtr; + pVoids[0] = pThat; + pVoids[1] = (void *)uElements; + + return AuUPtr)>((T *)(pPtr + kAlignment), &Heap::DeleteThatArray); + } + + template + AuUPtr)> Heap::NullUniquePointer() + { + return AuUPtr)>(nullptr, &Heap::RetardedSpecWrittenByRetards); + } + + template + AuUPtr)> Heap::CastPointer(AuUPtr)> &&pInPointer) + { + if (!pInPointer) + { + return NullUniquePointer(); + } + else if (pInPointer.get_deleter() == &Heap::DeleteThat) + { + return AuUPtr)>(AuStaticCast(pInPointer.release()), &Heap::DeleteThatCastedOnce); + } + else + { + return NullUniquePointer(); + } + } + + namespace detail + { + inline AuSPtr AllocateArray(Heap *pHeap, AuUInt uLength, AuUInt32 uAlignment) + { + return pHeap->NewClassArray2(uLength, uAlignment); + } + } +} \ No newline at end of file diff --git a/Include/Aurora/Memory/Memory.hpp b/Include/Aurora/Memory/Memory.hpp index 13dd8097..39e32e11 100644 --- a/Include/Aurora/Memory/Memory.hpp +++ b/Include/Aurora/Memory/Memory.hpp @@ -196,4 +196,5 @@ namespace Aurora::Memory #include "ByteBuffer.hpp" #include "ByteBufferPushReadState.hpp" -#include "ByteBufferPushWriteState.hpp" \ No newline at end of file +#include "ByteBufferPushWriteState.hpp" +#include "Heap.inl" \ No newline at end of file diff --git a/Include/Aurora/RuntimeAliases.hpp b/Include/Aurora/RuntimeAliases.hpp index 471df854..32387c99 100644 --- a/Include/Aurora/RuntimeAliases.hpp +++ b/Include/Aurora/RuntimeAliases.hpp @@ -142,28 +142,37 @@ auto AuNullPointer() return Aurora::Memory::Heap::NullUniquePointer(); } +namespace __audetail +{ + inline AuMemory::Heap *gDefaultDiscontiguousHeap = AuMemory::GetDefaultDiscontiguousHeap(); +} + template AuHUPOf_t AuNewClassArrayUnique(AuUInt uElements, Args &&... fillCtr) { - return Aurora::Memory::GetDefaultDiscontiguousHeap()->NewClassArrayUnique(uElements, AuForward(fillCtr)...); + return __audetail::gDefaultDiscontiguousHeap->NewClassArrayUnique(uElements, AuForward(fillCtr)...); } template AuSPtr AuNewClassArray(AuUInt uElements, Args &&... fillCtr) { - return Aurora::Memory::GetDefaultDiscontiguousHeap()->NewClassArray(uElements, AuForward(fillCtr)...); + return __audetail::gDefaultDiscontiguousHeap->NewClassArray(uElements, AuForward(fillCtr)...); } template AuHUPOf_t AuNewClassUnique(Args &&...args) { - return Aurora::Memory::GetDefaultDiscontiguousHeap()->NewClassUnique(AuForward(args)...); + return __audetail::gDefaultDiscontiguousHeap->NewClassUnique(AuForward(args)...); } template AuSPtr AuNewClass(Args &&...args) { +#if !defined(AURORA_RUNTIME_HEADERS_ALWAYS_LOOKUP_HEAP) + return AuMakeShared(AuForward(args)...); +#else return Aurora::Memory::GetDefaultDiscontiguousHeap()->NewClass(AuForward(args)...); +#endif } template