[*] Use PMR allocators instead

This commit is contained in:
Reece Wilson 2024-09-09 06:52:22 +01:00
parent 0571aa8dd4
commit 89686eb022
2 changed files with 243 additions and 3 deletions

View File

@ -21,6 +21,239 @@ namespace Aurora::Memory
};
}
template <class T>
struct PmrCppHeapWrapper
{
using value_type = T;
using size_type = size_t;
using difference_type = ptrdiff_t;
using propagate_on_container_move_assignment = AuTrueType;
using propagate_on_container_copy_assignment = AuTrueType;
using is_always_equal = AuFalseType;
#if defined(AU_LANG_CPP_17)
using pointer = T *;
using const_pointer = const T *;
using reference = T &;
using const_reference = const T &;
template <class Z>
struct rebind
{
using other = CppHeapWrapper<Z>;
};
T *address(T &val) const noexcept
{
return std::addressof(val);
}
const T *address(const T &val) const noexcept
{
return std::addressof(val);
}
#endif
inline PmrCppHeapWrapper(std::shared_ptr<Heap> pHeap) :
#if defined(AU_NO_COMPRESS_CPPHEAP_WRAPPER)
spHeap(pHeap),
pHeap(pHeap.get())
#else
spHeap(pHeap)
#endif
{ }
inline PmrCppHeapWrapper(Heap *pHeap, std::shared_ptr<void> pThat = {}) :
#if defined(AU_NO_COMPRESS_CPPHEAP_WRAPPER)
spHeap(pThat),
pHeap(pHeap)
#else
spHeap(AuSharedPointerFromShared(pHeap, pThat))
#endif
{ }
template <class B>
inline PmrCppHeapWrapper(const PmrCppHeapWrapper<B> &fuckCpp) :
spHeap(fuckCpp.spHeap)
#if defined(AU_NO_COMPRESS_CPPHEAP_WRAPPER)
,pHeap(fuckCpp.pHeap)
#endif
{ }
inline PmrCppHeapWrapper(const PmrCppHeapWrapper &fuckCpp) :
spHeap(fuckCpp.spHeap)
#if defined(AU_NO_COMPRESS_CPPHEAP_WRAPPER)
,pHeap(fuckCpp.pHeap)
#endif
{ }
inline PmrCppHeapWrapper(PmrCppHeapWrapper &&fuckCpp) :
spHeap(fuckCpp.spHeap)
#if defined(AU_NO_COMPRESS_CPPHEAP_WRAPPER)
,pHeap(fuckCpp.pHeap)
#endif
{ }
AU_DEF(PmrCppHeapWrapper)
AU_OPERATOR_COPY_MOVE(PmrCppHeapWrapper)
void *allocate_bytes(std::size_t nbytes,
std::size_t alignment = alignof(std::max_align_t))
{
if (auto pRet = this->GetHeapRaw()->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)) noexcept
{
return this->GetHeapRaw()->Free(p);
}
#if defined(AU_LANG_CPP_23_)
std::allocation_result<T *> allocate_at_least(const size_t count)
{
auto pThat = this->allocate(count);
return { (T *)pThat, this->GetHeapRaw()->GetChunkSize(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) noexcept
{
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);
}
bool IsInitialized() const
{
#if defined(AU_NO_COMPRESS_CPPHEAP_WRAPPER)
return bool(this->pHeap);
#else
return bool(this->spHeap);
#endif
}
std::shared_ptr<Heap> GetHeap() const
{
#if defined(AU_NO_COMPRESS_CPPHEAP_WRAPPER)
return AuSharedPointerFromShared(this->pHeap, this->spHeap);
#else
return this->spHeap;
#endif
}
Heap *GetHeapRaw() const
{
Heap *pRet {};
#if defined(AU_NO_COMPRESS_CPPHEAP_WRAPPER)
pRet = this->pHeap;
#else
pRet = this->spHeap.get();
#endif
if (pRet)
{
return pRet;
}
#if defined(AU_NO_COMPRESS_CPPHEAP_WRAPPER)
return this->pHeap = __audetail::gDefaultDiscontiguousHeap;
#else
return (this->spHeap = __audetail::gDefaultDiscontiguousHeapShared).get();
#endif
}
template <class Z>
bool operator==(const Aurora::Memory::CppHeapWrapper<Z> &rhs) noexcept
{
return this->GetHeapRaw() == rhs.GetHeapRaw();
}
private:
#if defined(AU_NO_COMPRESS_CPPHEAP_WRAPPER)
mutable std::shared_ptr<void> spHeap;
mutable Heap *pHeap {};
#else
mutable std::shared_ptr<Heap> spHeap;
#endif
friend struct detail::AccessorICantEven;
};
template <class T>
struct CppHeapWrapper
{
@ -30,7 +263,8 @@ namespace Aurora::Memory
using difference_type = ptrdiff_t;
using propagate_on_container_move_assignment = AuTrueType;
using is_always_equal = AuTrueType;
using propagate_on_container_copy_assignment = AuTrueType;
using is_always_equal = AuFalseType;
#if defined(AU_LANG_CPP_17)
using pointer = T *;
@ -338,4 +572,11 @@ inline bool operator==(const Aurora::Memory::CppHeapWrapper<T> &lhs,
const Aurora::Memory::CppHeapWrapper<Z> &rhs) noexcept
{
return lhs.GetHeapRaw() == rhs.GetHeapRaw();
}
template <class T, class Z>
inline bool operator==(const Aurora::Memory::PmrCppHeapWrapper<T> &lhs,
const Aurora::Memory::PmrCppHeapWrapper<Z> &rhs) noexcept
{
return lhs.GetHeapRaw() == rhs.GetHeapRaw();
}

View File

@ -1267,10 +1267,9 @@ namespace Aurora::Async
return {};
}
// TODO: ROXTL
if (this->pHeap)
{
pThreadState->pendingWorkItems.get_allocator().SetHeapRaw(this->pHeap.get());
AuResetMember(pThreadState->pendingWorkItems, AuMemory::PmrCppHeapWrapper<WorkEntry_t>());
}
if (!create)