metaforce/Runtime/CGameAllocator.hpp

93 lines
2.2 KiB
C++
Raw Normal View History

2018-10-07 03:42:33 +00:00
#pragma once
#include <cstddef>
#include <memory>
#include <vector>
#include "Runtime/RetroTypes.hpp"
2021-04-10 08:42:06 +00:00
namespace metaforce {
2020-09-08 17:58:55 +00:00
class CCallStack {
const char* x0_line;
const char* x4_type;
2020-10-30 22:56:02 +00:00
2020-09-08 17:58:55 +00:00
public:
CCallStack(int lineNum, const char* lineStr, const char* type) : x0_line(lineStr), x4_type(type) {}
};
2020-10-30 22:56:02 +00:00
enum class EHint {
Unk = (1 << 0),
RoundUpLen = (1 << 1),
};
ENABLE_BITWISE_ENUM(EHint);
enum class EScope {
};
enum class EType {
};
class IAllocator {
public:
2021-06-05 05:25:46 +00:00
struct SAllocInfo {
void* x0_infoPtr;
size_t x4_len;
bool x8_hasPrevious;
bool x9_;
const char* xc_fileAndLne;
const char* x10_type;
};
2020-10-30 22:56:02 +00:00
virtual bool Initialize() = 0; // const COSContext& ctx) = 0;
2021-06-05 05:25:46 +00:00
virtual void* Alloc(size_t size) = 0;
virtual bool Free(void* ptr) = 0;
2020-10-30 22:56:02 +00:00
virtual void ReleaseAll() = 0;
2021-06-05 05:25:46 +00:00
virtual void* AllocSecondary(size_t size) = 0;
virtual bool FreeSecondary(void* ptr) = 0;
2020-10-30 22:56:02 +00:00
virtual void ReleaseAllSecondary() = 0;
virtual void SetOutOfMemoryCallback() = 0;
virtual void EnumAllocations() = 0;
2021-06-05 05:25:46 +00:00
virtual SAllocInfo GetAllocInfo(void* ptr) = 0;
virtual void OffsetFakeStatics(s32 offset) = 0;
2020-10-30 22:56:02 +00:00
virtual void GetMetrics() = 0;
};
class CGameAllocator : public IAllocator {
2020-09-08 17:58:55 +00:00
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;
2018-12-08 05:30:43 +00:00
};
2020-09-08 17:58:55 +00:00
static u32 GetFreeBinEntryForSize(size_t len);
2021-06-05 05:25:46 +00:00
SGameMemInfo* GetMemInfoFromBlockPtr(void* ptr) {
return reinterpret_cast<SGameMemInfo*>(reinterpret_cast<void*>(intptr_t(ptr) - sizeof(SGameMemInfo)));
}
2020-10-30 22:56:02 +00:00
std::array<SGameMemInfo*, 16> x14_bins;
2020-09-08 17:58:55 +00:00
2021-06-05 05:25:46 +00:00
s32 xb8_fakeStatics = 0;
2020-10-30 22:56:02 +00:00
public:
2020-12-03 03:01:57 +00:00
bool Initialize() override; // const COsContext& ctx);
2021-06-05 05:25:46 +00:00
void* Alloc(size_t size) override;
bool Free(void* ptr) override;
2020-12-03 03:01:57 +00:00
void ReleaseAll() override;
2021-06-05 05:25:46 +00:00
void* AllocSecondary(size_t size) override;
bool FreeSecondary(void* ptr) override;
2020-12-03 03:01:57 +00:00
void ReleaseAllSecondary() override;
void SetOutOfMemoryCallback() override;
void EnumAllocations() override;
2021-06-05 05:25:46 +00:00
SAllocInfo GetAllocInfo(void* ptr) override;
void OffsetFakeStatics(s32 offset) override;
2020-12-03 03:01:57 +00:00
void GetMetrics() override;
};
2021-04-10 08:42:06 +00:00
} // namespace metaforce