AuroraRuntime/Include/auROXTL/auMemoryModel.hpp
Reece f717511a10 [+] NT OutputDebugStringW logger sink
[*] Split objects
[*] Consider making ABI object api boilerplate
[*] Refactor STLShims (roxtl)
2022-03-31 01:31:40 +01:00

221 lines
5.0 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
#if !defined(AURORA_RUNTIME_AU_DEFAULT_DELETER)
#define AURORA_RUNTIME_AU_DEFAULT_DELETER std::default_delete
#endif
template <class T>
using AuDefaultDeleter = AURORA_RUNTIME_AU_DEFAULT_DELETER<T>;
#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
template <class T, typename... Args>
static auline AuSPtr<T> AuMakeShared(Args&&... args)
{
try
{
return AURORA_RUNTIME_MAKE_SHARED<T>(AuForward<Args>(args)...);
}
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 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], AuDefaultDeleter<T[]>());
#endif
}
catch (...)
{
return {};
}
}
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, AuUInt Z>
static constexpr int AuArraySize(const T(&array)[Z])
{
return Z;
}
#if defined(DEBUG) || defined(STAGING)
template <class ... T>
static auline void AU_NORETURN SysPanic(T... args);
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)
{
Z re;
SysPanic("Tried to free: 0x{:x}, type \"{}\" / \"{}\"", AuUInt(in), typeid(in).name(), typeid(re).name());
}
delete static_cast<Z>(in);
}
#else
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");
delete static_cast<Z>(in);
}
#endif
namespace Aurora::Memory
{
#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);
template <class T>
struct BaseAuroraRuntimeAllocator
{
typedef T value_type;
AU_COPY_MOVE(BaseAuroraRuntimeAllocator)
constexpr BaseAuroraRuntimeAllocator()
{}
template <class U> constexpr BaseAuroraRuntimeAllocator(const BaseAuroraRuntimeAllocator <U> &) noexcept
{
}
inline [[nodiscard]] constexpr T* allocate(Types::size_t n)
{
if (auto p = (__FAlloc(n * sizeof(T), alignof(T))))
{
return AuReinterpretCast<T*>(p);
}
throw std::bad_alloc();
}
inline constexpr void deallocate(T *p, std::size_t n) noexcept
{
__Free(p);
}
inline bool operator==(const BaseAuroraRuntimeAllocator &op)
{
return true;
}
};
template<typename T>
using PrimitiveArrayAllocator = BaseAuroraRuntimeAllocator<T>;
template<typename T>
using ClassArrayAllocator = BaseAuroraRuntimeAllocator<T>;
template<typename T>
using StringAllocator = BaseAuroraRuntimeAllocator<T>;
#endif
}