2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/CGameAllocator.hpp"
|
2020-09-08 17:58:55 +00:00
|
|
|
#include <new>
|
2017-09-12 15:27:48 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
|
|
|
logvisor::Module AllocLog("metaforce::CGameAllocator");
|
2017-09-12 15:27:48 +00:00
|
|
|
|
2019-06-12 02:05:17 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wclass-memaccess"
|
|
|
|
|
2020-09-08 17:58:55 +00:00
|
|
|
u32 CGameAllocator::GetFreeBinEntryForSize(size_t len) {
|
|
|
|
size_t bin = 0;
|
|
|
|
size_t binSize = 32;
|
|
|
|
while (true) {
|
|
|
|
if (binSize > 0x200000) {
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
if (len < binSize) {
|
|
|
|
break;
|
2017-09-12 15:27:48 +00:00
|
|
|
}
|
2020-09-08 17:58:55 +00:00
|
|
|
binSize <<= 1;
|
|
|
|
++bin;
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2020-09-08 17:58:55 +00:00
|
|
|
return bin;
|
2017-09-12 15:27:48 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 17:58:55 +00:00
|
|
|
bool CGameAllocator::Initialize() { return true; }
|
2020-12-03 03:01:57 +00:00
|
|
|
void* CGameAllocator::Alloc() { return nullptr; }
|
2022-01-16 06:59:00 +00:00
|
|
|
bool CGameAllocator::Free(void* ptr) { return 0; }
|
2020-12-03 03:01:57 +00:00
|
|
|
void CGameAllocator::ReleaseAll() {}
|
|
|
|
void CGameAllocator::AllocSecondary() {}
|
|
|
|
void CGameAllocator::FreeSecondary() {}
|
|
|
|
void CGameAllocator::ReleaseAllSecondary() {}
|
2022-01-16 06:59:00 +00:00
|
|
|
|
|
|
|
void CGameAllocator::SetOutOfMemoryCallback(IAllocator::FOutOfMemoryCb cb, void* target) {
|
|
|
|
x58_oomCallBack = cb;
|
|
|
|
x5c_oomTarget = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 CGameAllocator::EnumAllocations(IAllocator::FEnumAllocationsCb cb, const void* ptr, bool b) {
|
|
|
|
s32 ret = 0;
|
|
|
|
SAllocInfo info;
|
|
|
|
info.x0_infoPtr = xc_first;
|
|
|
|
while (true) {
|
|
|
|
if (info.x0_infoPtr == nullptr) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (static_cast<SGameMemInfo*>(info.x0_infoPtr)->x1c_postGuard != 0xeaeaeaea) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (static_cast<SGameMemInfo*>(info.x0_infoPtr)->x0_priorGuard != 0xefefefef) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* next = static_cast<SGameMemInfo*>(info.x0_infoPtr)->GetNext();
|
|
|
|
info.x10_type = static_cast<SGameMemInfo*>(info.x0_infoPtr)->xc_type;
|
|
|
|
info.x8_hasPrevious = static_cast<SGameMemInfo*>(info.x0_infoPtr)->x10_prev != nullptr;
|
|
|
|
info.xc_fileAndLne = static_cast<SGameMemInfo*>(info.x0_infoPtr)->x8_fileAndLine;
|
|
|
|
info.x4_len = static_cast<SGameMemInfo*>(info.x0_infoPtr)->x4_len;
|
|
|
|
info.x9_ = false;
|
|
|
|
(*cb)(info, ptr);
|
|
|
|
|
|
|
|
++ret;
|
|
|
|
info.x0_infoPtr = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(next) & ~sizeof(SGameMemInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-06-05 05:25:46 +00:00
|
|
|
IAllocator::SAllocInfo CGameAllocator::GetAllocInfo(void* ptr) {
|
|
|
|
SGameMemInfo* info = GetMemInfoFromBlockPtr(ptr);
|
|
|
|
return {.x0_infoPtr = info,
|
|
|
|
.x4_len = info->x4_len,
|
|
|
|
.x8_hasPrevious = info->x10_prev != nullptr,
|
|
|
|
.x9_ = false,
|
2022-01-16 06:59:00 +00:00
|
|
|
.xc_fileAndLne = info->x8_fileAndLine,
|
2021-06-05 05:25:46 +00:00
|
|
|
.x10_type = info->xc_type};
|
|
|
|
}
|
2022-01-16 06:59:00 +00:00
|
|
|
|
2021-06-05 05:25:46 +00:00
|
|
|
void CGameAllocator::OffsetFakeStatics(s32 offset) { xb8_fakeStatics += offset; }
|
2022-01-16 06:59:00 +00:00
|
|
|
IAllocator::SMetrics CGameAllocator::GetMetrics() {
|
|
|
|
u32 mediumAllocTotalAllocated = x74_mediumPool == nullptr ? 0 : x74_mediumPool->GetTotalEntries();
|
|
|
|
u32 mediumAllocBlocksAvailable = x74_mediumPool == nullptr ? 0 : x74_mediumPool->GetNumBlocksAvailable();
|
|
|
|
u32 mediumAllocAllocatedSize =
|
|
|
|
x74_mediumPool == nullptr ? 0 : x74_mediumPool->GetTotalEntries() - x74_mediumPool->GetNumBlocksAvailable();
|
|
|
|
u32 mediumAllocNumAllocs = x74_mediumPool == nullptr ? 0 : x74_mediumPool->GetNumAllocs();
|
|
|
|
|
|
|
|
u32 smallAllocRemainingSize = 0;
|
|
|
|
u32 smallAllocAllocatedSize = 0;
|
|
|
|
u32 smallAllocNumAllocs = 0;
|
|
|
|
if (x60_smallPool == nullptr) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
smallAllocRemainingSize = x60_smallPool->GetNumBlocksAvailable();
|
|
|
|
smallAllocAllocatedSize = x60_smallPool->GetTotalEntries();
|
|
|
|
smallAllocNumAllocs = x60_smallPool->GetNumAllocs();
|
|
|
|
}
|
|
|
|
|
|
|
|
return SMetrics(x8_heapSize, x80_, x84_, x88_, x8c_, x90_heapSize2, x94_, x98_, x9c_, xa0_, xa4_, smallAllocNumAllocs,
|
|
|
|
smallAllocAllocatedSize, smallAllocRemainingSize, mediumAllocNumAllocs, mediumAllocAllocatedSize,
|
|
|
|
mediumAllocBlocksAvailable, x80_ - xb0_, xb4_, xbc_, mediumAllocTotalAllocated, xb8_fakeStatics);
|
|
|
|
}
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|