AuroraRuntime/Source/Memory/DefaultHeap.cpp

127 lines
3.7 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: DefaultHeap.cpp
Date: 2021-6-13
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Memory.hpp"
#include "DefaultHeap.hpp"
#include "Heap.hpp"
#include <Source/Debug/MemoryCrunch.hpp>
#include <mimalloc.h>
namespace Aurora::Memory
{
struct DefaultHeap : BaseHeap
{
AuSPtr<Heap> AllocateDivision(AuUInt32 heap, AuUInt32 alignment) override
{
return AllocateDivisionGlobal(this, heap, alignment);
}
void *_ZAlloc(Types::size_t length) override
{
return Aurora::Memory::_ZAlloc(length);
}
void *_ZAlloc(Types::size_t length, Types::size_t align) override
{
return Aurora::Memory::_ZAlloc(length, align);
}
Types::size_t GetChunkSize(const void *head) override
{
return Aurora::Memory::GetChunkSize(head);
}
void *_FAlloc(Types::size_t length) override
{
return Aurora::Memory::_FAlloc(length);
}
void *_FAlloc(Types::size_t length, Types::size_t align) override
{
return Aurora::Memory::_FAlloc(length, align);
}
void *_ZRealloc(void *buffer, Types::size_t length, Types::size_t align) override
{
return Aurora::Memory::_ZRealloc(buffer, length, align);
}
void *_ZRealloc(void *buffer, Types::size_t length) override
{
return Aurora::Memory::_ZRealloc(buffer, length);
}
void *_FRealloc(void *buffer, Types::size_t length, Types::size_t align) override
{
return Aurora::Memory::_FRealloc(buffer, length, align);
}
void *_FRealloc(void *buffer, Types::size_t length) override
{
return Aurora::Memory::_FRealloc(buffer, length);
}
void _Free(void *buffer) override
{
return Aurora::Memory::_Free(buffer);
}
AuSPtr<Heap> GetSelfReference() override
{
return {};
}
Heap *GetSelfReferenceRaw() override
{
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;
return pWalkInstance->fCallback(block, pWalkInstance->pSecondArg);
}
void WalkHeap(bool(*fCallback)(void *, void*), void *pSecondArg) override
{
WalkInstance inst;
inst.fCallback = fCallback;
inst.pSecondArg = pSecondArg;
mi_heap_visit_blocks(nullptr, true, &mi_block_visit_funHandler, &inst);
}
void UpdateStats() override
{
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) {}
}