[+] CppHeapWrapper.hpp
This commit is contained in:
parent
24fec2301c
commit
17d113b74d
83
Include/Aurora/Memory/CppHeapWrapper.hpp
Normal file
83
Include/Aurora/Memory/CppHeapWrapper.hpp
Normal file
@ -0,0 +1,83 @@
|
||||
/***
|
||||
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||||
|
||||
File: CppHeapWrapper.hpp
|
||||
Date: 2023-12-22
|
||||
Author: Reece
|
||||
***/
|
||||
#pragma once
|
||||
|
||||
namespace Aurora::Memory
|
||||
{
|
||||
template <class T>
|
||||
struct CppHeapWrapper
|
||||
{
|
||||
using value_type = T;
|
||||
|
||||
using size_type = size_t;
|
||||
using difference_type = ptrdiff_t;
|
||||
|
||||
using propagate_on_container_move_assignment = AuTrueType;
|
||||
using is_always_equal = AuTrueType;
|
||||
|
||||
#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 CppHeapWrapper(std::shared_ptr<Heap> pHeap) :
|
||||
pHeap(pHeap)
|
||||
{ }
|
||||
|
||||
AU_COPY_MOVE_DEF(CppHeapWrapper)
|
||||
|
||||
std::shared_ptr<Heap> pHeap;
|
||||
|
||||
constexpr void deallocate(const T *pType,
|
||||
const size_t count)
|
||||
{
|
||||
this->pHeap->Free((T *)pType);
|
||||
}
|
||||
|
||||
constexpr AU_ALLOC T *allocate(const size_t count)
|
||||
{
|
||||
if (!count)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto pData = this->pHeap->FAlloc(count, alignof(T));
|
||||
if (!pData)
|
||||
{
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
return (T *)pData;
|
||||
}
|
||||
|
||||
#if defined(AU_LANG_CPP_23)
|
||||
constexpr std::allocation_result<T *> allocate_at_least(const size_t count)
|
||||
{
|
||||
return { this->allocate(count), count };
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
@ -17,6 +17,7 @@
|
||||
#include "Cache.hpp"
|
||||
#include "SwapLock.hpp"
|
||||
#include "Transition.hpp"
|
||||
#include "CppHeapWrapper.hpp"
|
||||
|
||||
namespace Aurora::Memory
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user