461 lines
11 KiB
C++
461 lines
11 KiB
C++
/***
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: auMemoryModel.hpp
|
|
Date: 2022-2-1
|
|
File: AuroraUtils.hpp
|
|
File: auROXTLUtils.hpp
|
|
Date: 2021-6-9
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
|
|
#include "auTypes.hpp"
|
|
|
|
#if !defined(AURORA_RUNTIME_AU_SHARED_PTR)
|
|
#define AURORA_RUNTIME_AU_SHARED_PTR std::shared_ptr
|
|
#endif
|
|
|
|
#if !defined(AURORA_RUNTIME_AU_WEAK_PTR)
|
|
#define AURORA_RUNTIME_AU_WEAK_PTR std::weak_ptr
|
|
#endif
|
|
|
|
template <class T>
|
|
using AuWPtr = AURORA_RUNTIME_AU_WEAK_PTR<T>;
|
|
|
|
#if !defined(AURORA_RUNTIME_AU_UNIQUE_PTR)
|
|
#define AURORA_RUNTIME_AU_UNIQUE_PTR std::unique_ptr
|
|
#endif
|
|
|
|
#include "STLShims/ExtendStlLikeSharedPtr.hpp"
|
|
|
|
template <class T>
|
|
using AuSPtr = typename Aurora::Memory::ExSharedPtr<T, AURORA_RUNTIME_AU_SHARED_PTR<T>>;
|
|
|
|
template <class T, typename Deleter_t>
|
|
using AuUPtr = AURORA_RUNTIME_AU_UNIQUE_PTR<T, Deleter_t>;
|
|
|
|
#if !defined(AU_AuEnableSharedFromThis)
|
|
#define AU_AuEnableSharedFromThis
|
|
|
|
template <class X>
|
|
struct AuEnableSharedFromThis : Aurora::Memory::ExSharedFromThis<X, std::enable_shared_from_this<X>>
|
|
{};
|
|
|
|
#endif
|
|
|
|
#if !defined(AURORA_RUNTIME_MAKE_SHARED)
|
|
#define AURORA_RUNTIME_MAKE_SHARED std::make_shared
|
|
#endif
|
|
|
|
static inline AU_NORETURN void SysPanic(const char *pMsg);
|
|
|
|
static auline void AuMemoryPanic(const char *msg)
|
|
{
|
|
#if defined(AURORA_RUNTIME_MEMORY_PANIC)
|
|
AURORA_RUNTIME_MEMORY_PANIC(msg);
|
|
#else
|
|
SysPanic(msg);
|
|
#endif
|
|
}
|
|
|
|
#include "MemoryModel/auArraySize.hpp"
|
|
#include "MemoryModel/auSizeOf.hpp"
|
|
#include "MemoryModel/auOffsetOf.hpp"
|
|
|
|
|
|
template <typename T>
|
|
static auto AuTryLockMemoryType(T weak) -> decltype(weak.lock())
|
|
{
|
|
if (weak.expired())
|
|
{
|
|
return {};
|
|
}
|
|
|
|
AUROXTL_COMMODITY_TRY
|
|
{
|
|
return weak.lock();
|
|
}
|
|
AUROXTL_COMMODITY_CATCH
|
|
{
|
|
return {};
|
|
}
|
|
}
|
|
|
|
template <class Z, typename T>
|
|
static void auline AuSafeDelete(T *in)
|
|
{
|
|
static_assert(AuIsBaseOf_v<T, AuRemovePointer_t<Z>>, "Couldn't not safe delete from type T because it is not derived from Z");
|
|
|
|
if (in == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
delete static_cast<Z>(in);
|
|
}
|
|
|
|
#if !defined(AURORA_RUNTIME_AU_DEFAULT_DELETER)
|
|
#define AURORA_RUNTIME_AU_DEFAULT_DELETER std::default_delete
|
|
#endif
|
|
|
|
namespace Aurora::Memory
|
|
{
|
|
template <class T>
|
|
struct CppHeapWrapper;
|
|
|
|
template <class T>
|
|
struct PmrCppHeapWrapper;
|
|
|
|
#if defined(AURORA_ROXTL_ALLOCATORS_USE_STD)
|
|
|
|
template<typename T>
|
|
using PrimitiveArrayAllocator = std::allocator<T>;
|
|
|
|
template<typename T>
|
|
using ClassArrayAllocator = std::allocator<T>;
|
|
|
|
template<typename T>
|
|
using StringAllocator = std::allocator<T>;
|
|
|
|
#else
|
|
|
|
static void *__FAlloc(Types::size_t length, Types::size_t align);
|
|
static void __Free(void *buffer);
|
|
static AuUInt __SizeOf(void *pHead);
|
|
|
|
template <class T>
|
|
struct BaseAuroraRuntimeAllocator
|
|
{
|
|
typedef T value_type;
|
|
|
|
AU_COPY_MOVE(BaseAuroraRuntimeAllocator)
|
|
|
|
constexpr BaseAuroraRuntimeAllocator() noexcept
|
|
{ }
|
|
|
|
template <class U>
|
|
constexpr BaseAuroraRuntimeAllocator(const BaseAuroraRuntimeAllocator <U> &) noexcept
|
|
{ }
|
|
|
|
void *allocate_bytes(std::size_t nbytes,
|
|
std::size_t alignment = alignof(std::max_align_t))
|
|
{
|
|
if (auto pRet = __FAlloc(nbytes, alignment))
|
|
{
|
|
return pRet;
|
|
}
|
|
else
|
|
{
|
|
throw std::bad_alloc();
|
|
}
|
|
}
|
|
|
|
void deallocate_bytes(void *p,
|
|
std::size_t nbytes,
|
|
std::size_t alignment = alignof(std::max_align_t))
|
|
{
|
|
return __Free(p);
|
|
}
|
|
|
|
#if defined(AU_LANG_CPP_23)
|
|
constexpr std::allocation_result<T *> allocate_at_least(const size_t count)
|
|
{
|
|
auto pThat = this->allocate(count);
|
|
return { (T *)pThat, __SizeOf(pThat) / sizeof(T) };
|
|
}
|
|
#endif
|
|
|
|
template <class U, class... Args >
|
|
void construct(U *p, Args&&... args)
|
|
{
|
|
new ((void *)p) U(AuForward<Args>(args)...);
|
|
}
|
|
|
|
template <class U, class... Args >
|
|
void construct_at(U *p, Args&&... args)
|
|
{
|
|
new ((void *)p) U(AuForward<Args>(args)...);
|
|
}
|
|
|
|
void deallocate(const T *pType,
|
|
const size_t count)
|
|
{
|
|
this->deallocate_bytes((void *)pType, 0, 0);
|
|
}
|
|
|
|
AU_ALLOC T *allocate(const size_t count)
|
|
{
|
|
if (!count)
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
return (T *)this->allocate_bytes(count * sizeof(T), alignof(T));
|
|
}
|
|
|
|
template <class U>
|
|
U *allocate_object(std::size_t n = 1)
|
|
{
|
|
return (U *)this->allocate_bytes(sizeof(U) * n, alignof(U));
|
|
}
|
|
|
|
template <class U>
|
|
void deallocate_object(U *p, std::size_t n = 1)
|
|
{
|
|
this->deallocate_bytes(p, 0, 0);
|
|
}
|
|
|
|
template <class U, class... CtorArgs>
|
|
U *new_object(CtorArgs&&... args)
|
|
{
|
|
U *p = this->allocate_object<U>();
|
|
try
|
|
{
|
|
this->construct(p, AuForward<CtorArgs>(args)...);
|
|
}
|
|
catch (...)
|
|
{
|
|
this->deallocate_object(p);
|
|
throw;
|
|
}
|
|
return p;
|
|
}
|
|
|
|
template <class U>
|
|
void delete_object(U *p)
|
|
{
|
|
if constexpr (AuIsClass_v<U> &&
|
|
!AuIsTriviallyDestructible_v<U>)
|
|
{
|
|
p->~U();
|
|
}
|
|
|
|
this->deallocate_object(p);
|
|
}
|
|
|
|
template <class Z>
|
|
inline constexpr bool operator==(const Aurora::Memory::BaseAuroraRuntimeAllocator<Z> &rhs) noexcept
|
|
{
|
|
return true;
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
using PrimitiveArrayAllocator = BaseAuroraRuntimeAllocator<T>;
|
|
|
|
template<typename T>
|
|
using ClassArrayAllocator = BaseAuroraRuntimeAllocator<T>;
|
|
|
|
template<typename T>
|
|
using StringAllocator = BaseAuroraRuntimeAllocator<T>;
|
|
|
|
template<typename T>
|
|
using SharedControlBlockAllocator = BaseAuroraRuntimeAllocator<T>;
|
|
|
|
template <class T>
|
|
struct DefaultRuntimeDeleter
|
|
{
|
|
constexpr DefaultRuntimeDeleter() noexcept = default;
|
|
|
|
template <class Z>
|
|
DefaultRuntimeDeleter(const DefaultRuntimeDeleter<Z> &) noexcept
|
|
{ }
|
|
|
|
void operator()(T *pThat) const
|
|
{
|
|
if constexpr (AuIsClass_v<T> &&
|
|
!AuIsTriviallyDestructible_v<T>)
|
|
{
|
|
pThat->~T();
|
|
}
|
|
|
|
Aurora::Memory::__Free(pThat);
|
|
}
|
|
};
|
|
|
|
template <class T>
|
|
struct DefaultRuntimeDeleter<T[]>
|
|
{
|
|
constexpr DefaultRuntimeDeleter() noexcept = default;
|
|
|
|
template <class Z>
|
|
DefaultRuntimeDeleter(const DefaultRuntimeDeleter<Z> &) noexcept
|
|
{ }
|
|
|
|
template <class Z>
|
|
void operator()(Z *pThat) const
|
|
{
|
|
AURORA_RUNTIME_AU_DEFAULT_DELETER<T[]>()(pThat);
|
|
}
|
|
};
|
|
|
|
#endif
|
|
}
|
|
|
|
#if defined(AURORA_ROXTL_ALLOCATORS_USE_STD)
|
|
|
|
template <class T>
|
|
using AuDefaultDeleter = AURORA_RUNTIME_AU_DEFAULT_DELETER<T>;
|
|
|
|
#else
|
|
|
|
template <class T>
|
|
using AuDefaultDeleter = Aurora::Memory::DefaultRuntimeDeleter<T>;
|
|
|
|
#endif
|
|
|
|
template <class T, typename... Args>
|
|
static auline AuSPtr<T> AuMakeShared(Args&&... args)
|
|
{
|
|
using Z = AuRemoveConst_t<T>;
|
|
|
|
try
|
|
{
|
|
#if defined(AURORA_ROXTL_ALLOCATORS_USE_STD)
|
|
return AURORA_RUNTIME_MAKE_SHARED<T>(AuForward<Args>(args)...);
|
|
#else
|
|
auto pNext = Aurora::Memory::__FAlloc(sizeof(T), alignof(T));
|
|
if (!pNext)
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
auto pNextClass = (Z *)pNext;
|
|
new (pNextClass) Z (AuForward<Args>(args)...);
|
|
return std::shared_ptr<T>(pNextClass, Aurora::Memory::DefaultRuntimeDeleter<Z> {}, Aurora::Memory::SharedControlBlockAllocator<T> {});
|
|
#endif
|
|
}
|
|
catch (...)
|
|
{
|
|
return {};
|
|
}
|
|
}
|
|
|
|
template <class T, typename... Args>
|
|
static auline AuSPtr<T> AuMakeSharedPanic(Args&&... args)
|
|
{
|
|
#if defined(AURORA_ROXTL_ALLOCATORS_USE_STD)
|
|
try
|
|
{
|
|
auto pShared = AURORA_RUNTIME_MAKE_SHARED<T>(AuForward<Args>(args)...);
|
|
if (!pShared)
|
|
{
|
|
AuMemoryPanic("Failed to allocate <X>"); // TODO: non-rtti typename
|
|
}
|
|
|
|
return pShared;
|
|
}
|
|
catch (...)
|
|
{
|
|
AuMemoryPanic("Failed to allocate <X>"); // TODO: non-rtti typename
|
|
return {};
|
|
}
|
|
#else
|
|
auto pShared = AuMakeShared<T>(AuForward<Args>(args)...);
|
|
if (!pShared)
|
|
{
|
|
AuMemoryPanic("Failed to allocate <X>"); // TODO: non-rtti typename
|
|
}
|
|
|
|
return pShared;
|
|
#endif
|
|
}
|
|
|
|
template <class T, typename... Args>
|
|
static auline AuSPtr<T> AuMakeSharedThrow(Args&&... args)
|
|
{
|
|
#if defined(AURORA_ROXTL_ALLOCATORS_USE_STD)
|
|
try
|
|
{
|
|
auto pShared = AURORA_RUNTIME_MAKE_SHARED<T>(AuForward<Args>(args)...);
|
|
if (!pShared)
|
|
{
|
|
// TODO: Agnostic SysPushErrorMemory
|
|
AU_THROW_STRING("Failed to allocate <X>"); // TODO: non-rtti typename
|
|
}
|
|
|
|
return pShared;
|
|
}
|
|
catch (...)
|
|
{
|
|
// TODO: Agnostic SysPushErrorCatch
|
|
AU_THROW_STRING("Failed to allocate <X>"); // TODO: non-rtti typename
|
|
return {};
|
|
}
|
|
#else
|
|
auto pShared = AuMakeShared<T>(AuForward<Args>(args)...);
|
|
if (!pShared)
|
|
{
|
|
AU_THROW_STRING("Failed to allocate <X>"); // TODO: non-rtti typename
|
|
}
|
|
|
|
return pShared;
|
|
#endif
|
|
}
|
|
|
|
template <class T>
|
|
static auline AuSPtr<T> AuMakeSharedArray(AuUInt count)
|
|
{
|
|
try
|
|
{
|
|
#if defined(AU_LANG_CPP_20) && 0
|
|
return AURORA_RUNTIME_AU_SHARED_PTR<T[]>(count);
|
|
#else
|
|
return AURORA_RUNTIME_AU_SHARED_PTR<T>(new T[count], AURORA_RUNTIME_AU_DEFAULT_DELETER<T[]>());
|
|
#endif
|
|
}
|
|
catch (...)
|
|
{
|
|
return {};
|
|
}
|
|
}
|
|
|
|
namespace __audetail
|
|
{
|
|
struct Dummy
|
|
{
|
|
char a;
|
|
};
|
|
|
|
inline AuSPtr<Dummy> GetDummyDeleter()
|
|
{
|
|
static AuSPtr<Dummy> d;
|
|
if (!d)
|
|
{
|
|
#if defined(AURORA_COMPILER_MSVC)
|
|
return d;
|
|
#endif
|
|
d = AuMakeShared<Dummy>();
|
|
}
|
|
return d;
|
|
}
|
|
}
|
|
|
|
template <class T>
|
|
static AuSPtr<T> AuUnsafeRaiiToShared(T *in)
|
|
{
|
|
return AuSPtr<T>(__audetail::GetDummyDeleter(), in);
|
|
}
|
|
|
|
template <class T, class Z>
|
|
static AuSPtr<T> AuUnsafeRaiiToShared(const AuUPtr<T, Z> &in)
|
|
{
|
|
return AuUnsafeRaiiToShared(in.get());
|
|
}
|
|
|
|
template <class T, class Z>
|
|
inline constexpr bool operator==(const Aurora::Memory::BaseAuroraRuntimeAllocator<T> &lhs,
|
|
const Aurora::Memory::BaseAuroraRuntimeAllocator<Z> &rhs) noexcept
|
|
{
|
|
return true;
|
|
}
|
|
|
|
template <class T, class Z>
|
|
inline constexpr bool operator!=(const Aurora::Memory::BaseAuroraRuntimeAllocator<T> &lhs,
|
|
const Aurora::Memory::BaseAuroraRuntimeAllocator<Z> &rhs) noexcept
|
|
{
|
|
return false;
|
|
}
|
|
|
|
#include <auROXTL/MemoryModel/auSetAllocator.hpp> |