Initial CGameAllocator RE

This commit is contained in:
Phillip Stephens 2020-09-08 10:58:55 -07:00
parent 91996ab561
commit f660266750
Signed by: Antidote
GPG Key ID: F8BEE4C83DACA60D
2 changed files with 37 additions and 63 deletions

View File

@ -1,57 +1,26 @@
#include "Runtime/CGameAllocator.hpp"
#include <new>
namespace metaforce {
logvisor::Module AllocLog("metaforce::CGameAllocator");
#pragma GCC diagnostic ignored "-Wclass-memaccess"
std::vector<CGameAllocator::SAllocationDescription> CGameAllocator::m_allocations;
u8* CGameAllocator::Alloc(size_t len) {
size_t roundedLen = ROUND_UP_64(len + sizeof(SChunkDescription));
for (SAllocationDescription& alloc : m_allocations) {
/* We need to supply enough room for allocation information */
if (alloc.freeOffset + roundedLen < alloc.allocSize) {
u8* ptr = alloc.memptr.get() + alloc.freeOffset;
SChunkDescription* chunkInfo = reinterpret_cast<SChunkDescription*>(ptr);
*chunkInfo = SChunkDescription();
chunkInfo->parent = &alloc;
chunkInfo->len = len;
alloc.freeOffset += roundedLen;
return ptr + sizeof(SChunkDescription);
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;
}
binSize <<= 1;
++bin;
}
/* 1MiB minimum allocation to prevent constantly allocating small amounts of memory */
size_t allocSz = len;
if (allocSz < (1 * 1024 * 1024 * 1024))
allocSz = 1 * 1024 * 1024 * 1024;
/* Pad size to allow for allocation information */
allocSz = ROUND_UP_64(allocSz + sizeof(SChunkDescription));
auto& alloc = m_allocations.emplace_back();
alloc.memptr.reset(new u8[allocSz]);
u8* ptr = alloc.memptr.get();
alloc.allocSize = allocSz;
alloc.freeOffset += roundedLen;
SChunkDescription* chunkInfo = reinterpret_cast<SChunkDescription*>(ptr);
*chunkInfo = SChunkDescription();
chunkInfo->parent = &alloc;
chunkInfo->len = len;
return ptr + sizeof(SChunkDescription);
return bin;
}
void CGameAllocator::Free(u8* ptr) {
SChunkDescription* info = reinterpret_cast<SChunkDescription*>(ptr - sizeof(SChunkDescription));
if (info->magic != 0xE8E8E8E8 || info->sentinal != 0xEFEFEFEF) {
AllocLog.report(logvisor::Fatal, FMT_STRING("Invalid chunk description, memory corruption!"));
return;
}
SAllocationDescription& alloc = *info->parent;
size_t roundedLen = ROUND_UP_32(info->len + sizeof(SChunkDescription));
alloc.freeOffset -= roundedLen;
/* Invalidate chunk allocation descriptor */
memset(info, 0, ROUND_UP_64(info->len + sizeof(SChunkDescription)));
}
bool CGameAllocator::Initialize() { return true; }
} // namespace metaforce

View File

@ -7,24 +7,29 @@
#include "Runtime/RetroTypes.hpp"
namespace metaforce {
class CGameAllocator {
struct SAllocationDescription {
std::unique_ptr<u8[]> memptr;
size_t allocSize = 0;
ptrdiff_t freeOffset = 0;
};
struct SChunkDescription {
u32 magic = 0xE8E8E8E8;
SAllocationDescription* parent;
size_t len = 0;
u32 sentinal = 0xEFEFEFEF;
};
static std::vector<SAllocationDescription> m_allocations;
class CCallStack {
const char* x0_line;
const char* x4_type;
public:
static u8* Alloc(size_t len);
static void Free(u8* ptr);
CCallStack(int lineNum, const char* lineStr, const char* type) : x0_line(lineStr), x4_type(type) {}
};
class CGameAllocator {
struct SGameMemInfo {
u32 x0_sentinel = 0xefefefef;
size_t x4_len = 0;
const char* x8_line;
const char* xc_type;
SGameMemInfo* x10_prev = nullptr;
void* x14_ = nullptr;
void* x18_ = nullptr;
u32 x1c_canary = 0xeaeaeaea;
};
static u32 GetFreeBinEntryForSize(size_t len);
std::array<SGameMemInfo, 16> x14_bins;
public:
bool Initialize();//const COsContext& ctx);
};
} // namespace metaforce