diff --git a/Include/auROXTL/AU_Z.hpp b/Include/auROXTL/AU_Z.hpp index 944fe72..431f3b6 100644 --- a/Include/auROXTL/AU_Z.hpp +++ b/Include/auROXTL/AU_Z.hpp @@ -88,3 +88,7 @@ //#define _AURORA_AVOID_DUMB_STL_TYPES #define _AURORA_AVOID_EXTREMLY_DUMB_STL_TYPES +// TODO: +#if !defined(_AURORA_NULLEXPT_BRANCH) +#define _AURORA_NULLEXPT_BRANCH +#endif \ No newline at end of file diff --git a/Include/auROXTL/STLShims/ExtendStlLikeSharedPtr.hpp b/Include/auROXTL/STLShims/ExtendStlLikeSharedPtr.hpp index 9bba1c6..7d2e97e 100644 --- a/Include/auROXTL/STLShims/ExtendStlLikeSharedPtr.hpp +++ b/Include/auROXTL/STLShims/ExtendStlLikeSharedPtr.hpp @@ -369,20 +369,6 @@ namespace Aurora::Memory } } - auline void throwif() - { - if (!cached) [[unlikely]] - { - #if defined(_AURORA_NULLEXPT_BRANCH_BUG_CHECK) - cached = Base_t::operator bool(); - if (!cached) [[likely]] - #endif - { - AU_THROW_STRING("ExSharedPointer Null Access Violation"); - } - } - } - auline void _cache() { cached = Base_t::operator bool(); diff --git a/Include/auROXTL/auMemoryModel.hpp b/Include/auROXTL/auMemoryModel.hpp index 80d28a4..e64189a 100644 --- a/Include/auROXTL/auMemoryModel.hpp +++ b/Include/auROXTL/auMemoryModel.hpp @@ -27,13 +27,6 @@ using AuWPtr = AURORA_RUNTIME_AU_WEAK_PTR; #define AURORA_RUNTIME_AU_UNIQUE_PTR std::unique_ptr #endif -#if !defined(AURORA_RUNTIME_AU_DEFAULT_DELETER) - #define AURORA_RUNTIME_AU_DEFAULT_DELETER std::default_delete -#endif - -template -using AuDefaultDeleter = AURORA_RUNTIME_AU_DEFAULT_DELETER; - #include "STLShims/ExtendStlLikeSharedPtr.hpp" template @@ -66,111 +59,6 @@ static auline void AuMemoryPanic(const char *msg) #endif } -template -static auline AuSPtr AuMakeShared(Args&&... args) -{ - try - { - return AURORA_RUNTIME_MAKE_SHARED(AuForward(args)...); - } - catch (...) - { - return {}; - } -} - -template -static auline AuSPtr AuMakeSharedPanic(Args&&... args) -{ - try - { - auto pShared = AURORA_RUNTIME_MAKE_SHARED(AuForward(args)...); - if (!pShared) - { - AuMemoryPanic("Failed to allocate "); // TODO: non-rtti typename - } - - return pShared; - } - catch (...) - { - AuMemoryPanic("Failed to allocate "); // TODO: non-rtti typename - return {}; - } -} - -template -static auline AuSPtr AuMakeSharedThrow(Args&&... args) -{ - try - { - auto pShared = AURORA_RUNTIME_MAKE_SHARED(AuForward(args)...); - if (!pShared) - { - // TODO: Agnostic SysPushErrorMemory - AU_THROW_STRING("Failed to allocate "); // TODO: non-rtti typename - } - - return pShared; - } - catch (...) - { - // TODO: Agnostic SysPushErrorCatch - AU_THROW_STRING("Failed to allocate "); // TODO: non-rtti typename - return {}; - } -} - -namespace __audetail -{ - struct Dummy - { - char a; - }; - - inline AuSPtr GetDummyDeleter() - { - static AuSPtr d; - if (!d) - { - #if defined(AURORA_COMPILER_MSVC) - return d; - #endif - d = AuMakeShared(); - } - return d; - } -} - -template -static auline AuSPtr AuMakeSharedArray(AuUInt count) -{ - try - { - #if defined(AU_LANG_CPP_20) && 0 - return AURORA_RUNTIME_AU_SHARED_PTR(count); - #else - return AURORA_RUNTIME_AU_SHARED_PTR(new T[count], AuDefaultDeleter()); - #endif - } - catch (...) - { - return {}; - } -} - -template -static AuSPtr AuUnsafeRaiiToShared(T *in) -{ - return AuSPtr(__audetail::GetDummyDeleter(), in); -} - -template -static AuSPtr AuUnsafeRaiiToShared(const AuUPtr &in) -{ - return AuUnsafeRaiiToShared(in.get()); -} - template static constexpr AuUInt AuArraySize(const T(&array)[Z]) { @@ -240,6 +128,9 @@ static void auline AuSafeDelete(T *in) delete static_cast(in); } +#if !defined(AURORA_RUNTIME_AU_DEFAULT_DELETER) + #define AURORA_RUNTIME_AU_DEFAULT_DELETER std::default_delete +#endif // TODO: Move me #include "auOptional.hpp" @@ -274,13 +165,12 @@ namespace Aurora::Memory AU_COPY_MOVE(BaseAuroraRuntimeAllocator) - constexpr BaseAuroraRuntimeAllocator() - {} + constexpr BaseAuroraRuntimeAllocator() noexcept + { } template constexpr BaseAuroraRuntimeAllocator(const BaseAuroraRuntimeAllocator &) noexcept - { - } + { } void *allocate_bytes(std::size_t nbytes, std::size_t alignment = alignof(std::max_align_t)) @@ -386,17 +276,208 @@ namespace Aurora::Memory }; template - using PrimitiveArrayAllocator = BaseAuroraRuntimeAllocator; + using PrimitiveArrayAllocator = BaseAuroraRuntimeAllocator; template - using ClassArrayAllocator = BaseAuroraRuntimeAllocator; - + using ClassArrayAllocator = BaseAuroraRuntimeAllocator; + template - using StringAllocator = BaseAuroraRuntimeAllocator; - + using StringAllocator = BaseAuroraRuntimeAllocator; + + template + using SharedControlBlockAllocator = BaseAuroraRuntimeAllocator; + + template + struct DefaultRuntimeDeleter + { + constexpr DefaultRuntimeDeleter() noexcept = default; + + template + DefaultRuntimeDeleter(const DefaultRuntimeDeleter &) noexcept + { } + + void operator()(T *pThat) const + { + if constexpr (AuIsClass_v && + !AuIsTriviallyDestructible_v) + { + pThat->~T(); + } + + Aurora::Memory::__Free(pThat); + } + }; + + template + struct DefaultRuntimeDeleter + { + constexpr DefaultRuntimeDeleter() noexcept = default; + + template + DefaultRuntimeDeleter(const DefaultRuntimeDeleter &) noexcept + { } + + template + void operator()(Z *pThat) const + { + AURORA_RUNTIME_AU_DEFAULT_DELETER()(pThat); + } + }; + #endif } +#if defined(AURORA_ROXTL_ALLOCATORS_USE_STD) + + template + using AuDefaultDeleter = AURORA_RUNTIME_AU_DEFAULT_DELETER; + +#else + + template + using AuDefaultDeleter = Aurora::Memory::DefaultRuntimeDeleter; + +#endif + +template +static auline AuSPtr AuMakeShared(Args&&... args) +{ + using Z = AuRemoveConst_t; + + try + { + #if defined(AURORA_ROXTL_ALLOCATORS_USE_STD) + return AURORA_RUNTIME_MAKE_SHARED(AuForward(args)...); + #else + auto pNext = Aurora::Memory::__FAlloc(sizeof(T), alignof(T)); + if (!pNext) + { + return nullptr; + } + + auto pNextClass = (Z *)pNext; + new (pNextClass) Z (AuForward(args)...); + return std::shared_ptr(pNextClass, Aurora::Memory::DefaultRuntimeDeleter {}, Aurora::Memory::SharedControlBlockAllocator {}); + #endif + } + catch (...) + { + return {}; + } +} + +template +static auline AuSPtr AuMakeSharedPanic(Args&&... args) +{ +#if defined(AURORA_ROXTL_ALLOCATORS_USE_STD) + try + { + auto pShared = AURORA_RUNTIME_MAKE_SHARED(AuForward(args)...); + if (!pShared) + { + AuMemoryPanic("Failed to allocate "); // TODO: non-rtti typename + } + + return pShared; + } + catch (...) + { + AuMemoryPanic("Failed to allocate "); // TODO: non-rtti typename + return {}; + } +#else + auto pShared = AuMakeShared(AuForward(args)...); + if (!pShared) + { + AuMemoryPanic("Failed to allocate "); // TODO: non-rtti typename + } + + return pShared; +#endif +} + +template +static auline AuSPtr AuMakeSharedThrow(Args&&... args) +{ +#if defined(AURORA_ROXTL_ALLOCATORS_USE_STD) + try + { + auto pShared = AURORA_RUNTIME_MAKE_SHARED(AuForward(args)...); + if (!pShared) + { + // TODO: Agnostic SysPushErrorMemory + AU_THROW_STRING("Failed to allocate "); // TODO: non-rtti typename + } + + return pShared; + } + catch (...) + { + // TODO: Agnostic SysPushErrorCatch + AU_THROW_STRING("Failed to allocate "); // TODO: non-rtti typename + return {}; + } +#else + auto pShared = AuMakeShared(AuForward(args)...); + if (!pShared) + { + AU_THROW_STRING("Failed to allocate "); // TODO: non-rtti typename + } + + return pShared; +#endif +} + +template +static auline AuSPtr AuMakeSharedArray(AuUInt count) +{ + try + { + #if defined(AU_LANG_CPP_20) && 0 + return AURORA_RUNTIME_AU_SHARED_PTR(count); + #else + return AURORA_RUNTIME_AU_SHARED_PTR(new T[count], AURORA_RUNTIME_AU_DEFAULT_DELETER()); + #endif + } + catch (...) + { + return {}; + } +} + +namespace __audetail +{ + struct Dummy + { + char a; + }; + + inline AuSPtr GetDummyDeleter() + { + static AuSPtr d; + if (!d) + { + #if defined(AURORA_COMPILER_MSVC) + return d; + #endif + d = AuMakeShared(); + } + return d; + } +} + +template +static AuSPtr AuUnsafeRaiiToShared(T *in) +{ + return AuSPtr(__audetail::GetDummyDeleter(), in); +} + +template +static AuSPtr AuUnsafeRaiiToShared(const AuUPtr &in) +{ + return AuUnsafeRaiiToShared(in.get()); +} + template inline constexpr bool operator==(const Aurora::Memory::BaseAuroraRuntimeAllocator &lhs, const Aurora::Memory::BaseAuroraRuntimeAllocator &rhs) noexcept