/*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: Heap.hpp Date: 2021-6-9 Author: Reece ***/ #pragma once namespace Aurora::Memory { struct ProxyHeap; struct Heap { virtual AuSPtr AllocateDivision(AuUInt32 heap, AuUInt32 alignment = 32) = 0; virtual Types::size_t GetChunkSize(const void *pHead) = 0; virtual HeapStats &GetStats() = 0; template T ZAlloc(Types::size_t length) { return reinterpret_cast(_ZAlloc(length)); } template T ZAlloc(Types::size_t length, Types::size_t align) { return reinterpret_cast(_ZAlloc(length, align)); } template T *ZAlloc() { return reinterpret_cast(_ZAlloc(sizeof(T))); } template T *NewArray(Types::size_t count) { return ZAlloc(count * sizeof(T)); } template T *NewArray(Types::size_t count, Types::size_t align) { return ZAlloc(count * sizeof(T), align); } /// Fast, unsafe alloc template T FAlloc(Types::size_t length) { return reinterpret_cast(_FAlloc(length)); } template T FAlloc(Types::size_t length, Types::size_t align) { return reinterpret_cast(_FAlloc(length, align)); } template T ZRealloc(T pHead, Types::size_t length) { return reinterpret_cast(_ZRealloc(reinterpret_cast(pHead), length)); } template T ZRealloc(T pHead, Types::size_t length, Types::size_t alloc) { return reinterpret_cast(_ZRealloc(reinterpret_cast(pHead), length), alloc); } template T FRealloc(T pHead, Types::size_t length) { return reinterpret_cast(_FRealloc(reinterpret_cast(pHead), length)); } template T FRealloc(T pHead, Types::size_t length, Types::size_t alloc) { return reinterpret_cast(_FRealloc(reinterpret_cast(pHead), length), alloc); } template void Free(T pHead) { _Free(reinterpret_cast(pHead)); } protected: template static void DeleteThat(T *pThat) { static const auto kAlignment = AuMax(alignof(T), sizeof(void *)); if constexpr (AuIsClass_v #if !defined(AURT_HEAP_NO_STL) && !std::is_trivially_destructible_v #endif ) { pThat->~T(); } auto &pHeap = *(Heap **)(((char *)pThat) - kAlignment); pHeap->_Free(&pHeap); } 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 uCount = (AuUInt)pVoids[1]; if constexpr (AuIsClass_v #if !defined(AURT_HEAP_NO_STL) && !std::is_trivially_destructible_v #endif ) { for (AU_ITERATE_N(i, uCount)) { auto &refElement = pThat[i]; refElement.~T(); } } pHeap->_Free(pVoids); } template static void DeleteThatCastedOnce(T *pThat) { static const auto kAlignment = AuMax(alignof(T), sizeof(void *)); auto pBaseClass = AuStaticCast(pThat); if constexpr (AuIsClass_v #if !defined(AURT_HEAP_NO_STL) && !std::is_trivially_destructible_v #endif ) { pBaseClass->~Z(); } auto &pHeap = *(Heap **)(((char *)pBaseClass) - kAlignment); pHeap->_Free(&pHeap); } template 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 #if !defined(AURT_HEAP_NO_STL) && !std::is_trivially_constructible_v #endif ) { 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; return AuSPtr((T *)(pPtr + kAlignment), &Heap::DeleteThat); } // 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 #if !defined(AURT_HEAP_NO_STL) && !std::is_trivially_constructible_v #endif ) { 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; return AuUPtr)>((T *)(pPtr + kAlignment), &Heap::DeleteThat); } template AuSPtr NewClassArray(AuUInt uElements, Args &&... fillCtr) { static const auto kAlignment = AuMax(alignof(T), sizeof(void *) * 2); AuUInt8 *pPtr; if (!uElements) { return {}; } auto pThat = this->GetSelfReferenceRaw(); if (!pThat) { pThat = this; } if constexpr (AuIsClass_v #if !defined(AURT_HEAP_NO_STL) && !std::is_trivially_constructible_v #endif ) { 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; return AuSPtr((T *)(pPtr + kAlignment), &Heap::DeleteThatArray); } // 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; if (!uElements) { return AuUPtr)>(nullptr, &Heap::RetardedSpecWrittenByRetards); } auto pThat = this->GetSelfReferenceRaw(); if (!pThat) { pThat = this; } if constexpr (AuIsClass_v #if !defined(AURT_HEAP_NO_STL) && !std::is_trivially_constructible_v #endif ) { 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 cstatic AuUPtr)> NullUniquePointer() { return AuUPtr)>(nullptr, &Heap::RetardedSpecWrittenByRetards); } 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(); } } template static AuSPtr ToSmartPointer(AuSPtr heap, T *pHead, bool bPinHeap = true) { auto handle = bPinHeap ? heap : AuSPtr {}; auto pHeap = heap.get(); return AuSPtr(pHead, [handle, pHeap](T *pDeleteMe) { if constexpr (AuIsClass_v #if !defined(AURT_HEAP_NO_STL) && !std::is_trivially_destructible_v #endif ) { pDeleteMe->~T(); } pHeap->Free(pDeleteMe); }); } template using HUPOf_t = AuUPtr)>; protected: friend struct ProxyHeap; friend struct HeapAccessor; virtual AuSPtr GetSelfReference() = 0; // may return empty/default. not all heaps are sharable. virtual Heap *GetSelfReferenceRaw() = 0; virtual AU_ALLOC void *_ZAlloc(Types::size_t uLength) = 0; virtual AU_ALLOC void *_ZAlloc(Types::size_t uLength, Types::size_t align) = 0; virtual AU_ALLOC void *_FAlloc(Types::size_t uLength) = 0; virtual AU_ALLOC void *_FAlloc(Types::size_t uLength, Types::size_t align) = 0; virtual AU_ALLOC void *_ZRealloc(void *pBase, Types::size_t uLength, Types::size_t uAlign) = 0; virtual AU_ALLOC void *_ZRealloc(void *pBase, Types::size_t uLength) = 0; virtual AU_ALLOC void *_FRealloc(void *pBase, Types::size_t uLength, Types::size_t uAlign) = 0; virtual AU_ALLOC void *_FRealloc(void *pBase, Types::size_t uLength) = 0; virtual void _Free(void* pBase) = 0; }; struct HeapAccessor { cstatic AuSPtr GetSelfReference(Heap *pHeap) { return pHeap->GetSelfReference(); } cstatic Heap *GetSelfReferenceRaw(Heap *pHeap) { return pHeap->GetSelfReferenceRaw(); } }; /** Returns a heap interface backed by the default allocator */ AUKN_SHARED_API(DefaultDiscontiguousHeap, Heap); inline Heap *GetDefaultDiscontiguousHeap() { return DefaultDiscontiguousHeapNew(); } inline AuSPtr GetDefaultDiscontiguousHeapShared() { // Might not allocate the control block under some STLs, unlike DefaultDiscontiguousHeapSharedShared() which will generally always allocate a control block under most STLs return AuUnsafeRaiiToShared(GetDefaultDiscontiguousHeap()); } /** Allocates uLength amount of contiguous virtual memory @warning Heaps are guaranteed to outlive their allocations; heaps are the one object that effectively own a single reference count on themselves. Requesting termination before all of its' memory has been free will result, pHead at worst, a warning. Expect to leak unless all allocs have been paired by a free. I do not expect to implement force frees simply because all our primary use cases keep track of dtors to forcefully release leaked objects. Use RequestHeapOfRegion to be backed by caller owned memory. @return a heap backed by uLength bytes of virtual memory */ AUKN_SHARED_API(AllocHeap, Heap, AuUInt uLength); AUKN_SHARED_API(RequestHeapOfRegion, Heap, const MemoryViewWrite &memory); // AllocHeap but use mimalloc (or the default allocator) instead AUKN_SHARED_API(AllocHeapMimalloc, Heap, AuUInt uLength); // Proxies an existing heap with encapsulated statistics AUKN_SHARED_API(HeapProxy, Heap, const AuSPtr &pHead); // Proxies an existing heap with encapsulated statistics and leak detector AUKN_SHARED_API(HeapProxyEx, Heap, const AuSPtr &pHead, LeakFinderAlloc_f pfAlloc, LeakFinderFree_f pfFree); }