AuroraRuntime/Source/Memory/AuDefaultHeap.cpp

144 lines
3.8 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuDefaultHeap.cpp
Date: 2021-6-13
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "AuDefaultHeap.hpp"
#include "AuHeap.hpp"
#include <Source/Debug/MemoryCrunch.hpp>
#include <mimalloc.h>
namespace Aurora::Memory
{
AuSPtr<Heap> DefaultHeap::AllocateDivision(AuUInt32 heap, AuUInt32 alignment)
{
return AllocateDivisionGlobal(this, heap, alignment);
}
void *DefaultHeap::_ZAlloc(Types::size_t length)
{
return AuMemory::_ZAlloc(length);
}
void *DefaultHeap::_ZAlloc(Types::size_t length, Types::size_t align)
{
return AuMemory::_ZAlloc(length, align);
}
Types::size_t DefaultHeap::GetChunkSize(const void *head)
{
return AuMemory::GetChunkSize(head);
}
void *DefaultHeap::_FAlloc(Types::size_t length)
{
return AuMemory::_FAlloc(length);
}
void *DefaultHeap::_FAlloc(Types::size_t length, Types::size_t align)
{
return AuMemory::_FAlloc(length, align);
}
void *DefaultHeap::_ZRealloc(void *buffer, Types::size_t length, Types::size_t align)
{
return AuMemory::_ZRealloc(buffer, length, align);
}
void *DefaultHeap::_ZRealloc(void *buffer, Types::size_t length)
{
return AuMemory::_ZRealloc(buffer, length);
}
void *DefaultHeap::_FRealloc(void *buffer, Types::size_t length, Types::size_t align)
{
return AuMemory::_FRealloc(buffer, length, align);
}
void *DefaultHeap::_FRealloc(void *buffer, Types::size_t length)
{
return AuMemory::_FRealloc(buffer, length);
}
void DefaultHeap::_Free(void *buffer)
{
return AuMemory::_Free(buffer);
}
AuSPtr<Heap> DefaultHeap::GetSelfReference()
{
return {};
}
Heap *DefaultHeap::GetSelfReferenceRaw()
{
return this;
}
struct WalkInstance
{
bool(*fCallback)(void *, void *);
void *pSecondArg;
};
static bool mi_block_visit_funHandler(const mi_heap_t *heap, const mi_heap_area_t *area, void *block, size_t block_size, void *arg)
{
auto pWalkInstance = (WalkInstance *)arg;
if (!block)
{
return true;
}
if (!area->used)
{
return true;
}
#if 0
for (AU_ITERATE_N(i, area->used))
{
if (!pWalkInstance->fCallback(((AuUInt *)block) + (i * area->block_size), pWalkInstance->pSecondArg))
{
return false;
}
}
#else
return pWalkInstance->fCallback(block, pWalkInstance->pSecondArg);
#endif
return true;
}
void DefaultHeap::WalkHeap(bool(*fCallback)(void *, void*), void *pSecondArg)
{
WalkInstance inst;
inst.fCallback = fCallback;
inst.pSecondArg = pSecondArg;
mi_heap_visit_blocks(mi_heap_get_default(), true , &mi_block_visit_funHandler, &inst);
}
void DefaultHeap::UpdateStats()
{
auto other = AuDebug::gReserveHeap->GetStats();
this->stats.bIsSharedWithOtherHeaps = true;
this->stats.uBytesLiveCounter = gBytesCounterAllocated + other.uBytesLiveCounter;
this->stats.uBytesPeakCounter = AuMax(gBytesCounterPeak, other.uBytesPeakCounter);
if (!this->stats.uBytesCapacity)
{
this->stats.uBytesCapacity = Aurora::HWInfo::GetMemStatSystem /*should be process, but process is this with extra steps.*/().value_or(AuHwInfo::RamStat { }).qwAvailable;
}
}
static DefaultHeap gDefaultAllocation;
AUKN_SYM Heap *DefaultDiscontiguousHeapNew()
{
return &gDefaultAllocation;
}
AUKN_SYM void DefaultDiscontiguousHeapRelease(Heap * heap) {}
}