AuroraRuntime/Include/Aurora/Memory/Heap.hpp
2021-06-27 22:25:29 +01:00

139 lines
4.0 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Heap.hpp
Date: 2021-6-9
Author: Reece
***/
#pragma once
namespace Aurora::Memory
{
class Heap
{
public:
virtual Types::size_t GetChunkSize(const void *head) = 0;
template<typename T = void *>
T ZAlloc(Types::size_t length)
{
return reinterpret_cast<T>(_ZAlloc(length));
}
template<typename T = void *>
T ZAlloc(Types::size_t length, Types::size_t align)
{
return reinterpret_cast<T>(_ZAlloc(length, align));
}
template<typename T>
T *ZAlloc()
{
return reinterpret_cast<T *>(_ZAlloc(sizeof(T)));
}
template<typename T>
T *NewArray(Types::size_t count)
{
return ZAlloc<T *>(count * sizeof(T));
}
template<typename T>
T *NewArray(Types::size_t count, Types::size_t align)
{
return ZAlloc<T *>(count * sizeof(T), align);
}
/// Fast, unsafe alloc
template<typename T = void *>
T FAlloc(Types::size_t length)
{
return reinterpret_cast<T>(_FAlloc(length));
}
template<typename T = void *>
T FAlloc(Types::size_t length, Types::size_t align)
{
return reinterpret_cast<T>(_FAlloc(length, align));
}
template<typename T>
T ZRealloc(T in, Types::size_t length)
{
return reinterpret_cast<T>(_ZRealloc(reinterpret_cast<void *>(in), length));
}
template<typename T>
T ZRealloc(T in, Types::size_t length, Types::size_t alloc)
{
return reinterpret_cast<T>(_ZRealloc(reinterpret_cast<void *>(in), length), alloc);
}
template<typename T>
T FRealloc(T in, Types::size_t length)
{
return reinterpret_cast<T>(_SRealloc(reinterpret_cast<void *>(in), length));
}
template<typename T>
T FRealloc(T in, Types::size_t length, Types::size_t alloc)
{
return reinterpret_cast<T>(_SRealloc(reinterpret_cast<void *>(in), length), alloc);
}
template<typename T>
void Free(T in)
{
_Free(reinterpret_cast<void *>(in));
}
template<typename T>
AuSPtr<T> ToSmartPointer(T *in)
{
if (in == nullptr) return {};
return
std::shared_ptr<T>(in,
[=](T *delt)
{
if constexpr (std::is_class_v<T>)
{
delt->~T();
}
this->Free(delt);
});
}
template<typename T>
static AuSPtr<T> ToSmartPointer(AuSPtr<Heap> heap, T *in)
{
return
std::shared_ptr<T>(in,
[=](T *delt)
{
heap->Free(delt);
});
}
private:
virtual AU_ALLOC void *_ZAlloc(Types::size_t length) = 0;
virtual AU_ALLOC void *_ZAlloc(Types::size_t length, Types::size_t align) = 0;
virtual AU_ALLOC void *_FAlloc(Types::size_t length) = 0;
virtual AU_ALLOC void *_FAlloc(Types::size_t length, Types::size_t align) = 0;
virtual AU_ALLOC void *_ZRealloc(void *buffer, Types::size_t length, Types::size_t align) = 0;
virtual AU_ALLOC void *_ZRealloc(void *buffer, Types::size_t length) = 0;
virtual AU_ALLOC void *_FRealloc(void *buffer, Types::size_t length, Types::size_t align) = 0;
virtual AU_ALLOC void *_FRealloc(void *buffer, Types::size_t length) = 0;
virtual void _Free(void* buffer) = 0;
};
/**
Returns a heap interface backed by the default allocator
*/
AUKN_SHARED_API(GetDefaultDiscontiguousHeap, Heap);
/**
Allocates Fize amount of memory
@return a heap backed by allocated memory
*/
AUKN_SHARED_API(AllocHeap, Heap, AuUInt size);
}