AuroraRuntime/Include/Aurora/Memory/Heap.hpp

111 lines
4.0 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
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
{
struct ProxyHeap;
2024-03-19 15:47:42 +00:00
static const AuUInt8 kHeapSize = 128;
static const AuUInt8 kHeap2Size = 255;
using HeapAccessor = AuHeapAccessor;
using Heap = AuHeap;
2024-01-27 08:04:29 +00:00
struct HeapAdapterHandle
{
void *pReserved[2];
};
struct HeapAdapterInterface
{
HeapAdapterHandle handle;
// Required:
void *(* fAllocate)(HeapAdapterHandle *pHandle,
AuUInt uLength,
AuUInt uAlignment) = nullptr;
// Required:
void (* fFree)(HeapAdapterHandle *pHandle,
void *pPointer) = nullptr;
// Optional:
AuUInt (* fGetBlockSize)(HeapAdapterHandle *pHandle,
void *pPointer) = nullptr;
// Optional:
void (* fHeapDestroy)(HeapAdapterHandle *pHandle) = nullptr;
//
bool bHasAlignmentAwareness {};
};
2021-06-27 21:25:29 +00:00
/**
2024-03-19 15:47:42 +00:00
* Returns a heap interface backed by the default allocator
*/
AUKN_SHARED_API(DefaultDiscontiguousHeap, Heap);
inline Heap *GetDefaultDiscontiguousHeap()
{
return DefaultDiscontiguousHeapNew();
}
inline AuSPtr<Heap> GetDefaultDiscontiguousHeapShared()
{
// Might not allocate the control block under some STLs, unlike DefaultDiscontiguousHeapSharedShared() which will generally always allocate a control block under most STLs
return AuUnsafeRaiiToShared(GetDefaultDiscontiguousHeap());
}
2021-06-27 21:25:29 +00:00
/**
2024-03-19 15:47:42 +00:00
* Allocates uLength amount of contiguous virtual memory
* @return a heap backed by uLength bytes of virtual memory
* @warning the SOO variant cannot guarantee release-on-last-free and will panic if uLength cannot be allocated. Use AllocHeap[Shared/Unique/New](uLength) instead.
*/
AUKN_SHARED_SOO2_NCM(AllocHeap, Heap, kHeapSize, ((AuUInt, uLength)),
AuUInt uLength);
2024-03-19 15:47:42 +00:00
/**
* @warning the SOO variant cannot guarantee release-on-last-free and will panic if an invalid memory handle is provided.
2024-03-19 15:47:42 +00:00
*/
AUKN_SHARED_SOO2_NCM(RequestHeapOfRegion, Heap, kHeapSize, ((const MemoryViewWrite &, memory)),
const MemoryViewWrite &memory);
/**
* @warning the SOO variant cannot guarantee release-on-last-free and will panic if an invalid memory handle is provided.
*/
AUKN_SHARED_SOO2_NCM(RequestHeapOfSharedRegion, Heap, kHeapSize, ((const AuSPtr<MemoryViewWrite> &, memory)),
const AuSPtr<MemoryViewWrite> &pMemory);
2024-03-19 15:47:42 +00:00
/**
* Proxies an existing heap with encapsulated statistics.
* This is intended for debugging purposes when accurate heap stats of a heap-subset are desired.
* @warning this heap cannot guarantee release-on-last-free
*/
AUKN_SHARED_SOO2_NCM(HeapProxy, Heap, kHeap2Size, ((const AuSPtr<Heap> &, pHead)),
const AuSPtr<Heap> &pHead);
2024-03-19 15:47:42 +00:00
/**
* Proxies an existing heap with encapsulated statistics and leak detector
* This is intended for debugging purposes when accurate heap stats of a heap-subset are desired.
* @warning this heap cannot guarantee release-on-last-free
2021-06-27 21:25:29 +00:00
*/
AUKN_SHARED_SOO2_NCM(HeapProxyEx, Heap, kHeap2Size, ((const AuSPtr<Heap> &, pHead), (LeakFinderAlloc_f, pfAlloc), (LeakFinderFree_f, pfFree)),
const AuSPtr<Heap> &pHead,
LeakFinderAlloc_f pfAlloc,
LeakFinderFree_f pfFree);
/**
* Proxies an existing heap allocator library of a malloc and free; bonus points for aligned malloc, get allocation size, and destroy
*/
AUKN_SHARED_SOO2_NCM(HeapAdapter, Heap, kHeap2Size, ((const HeapAdapterInterface &, adapterInterface)),
const HeapAdapterInterface &adapterInterface);
#if defined(AURORA_IS_MODERNNT_DERIVED)
AUKN_SHARED_SOO2_NCM(HeapWin32Adapter, Heap, kHeap2Size, ((void *, hHeap)), void *hHeap);
#endif
2024-01-19 19:40:38 +00:00
}