2022-10-09 05:13:17 +00:00
|
|
|
#ifndef _CGAMEALLOCATOR
|
|
|
|
#define _CGAMEALLOCATOR
|
2022-09-20 04:35:24 +00:00
|
|
|
|
|
|
|
#include <Kyoto/Alloc/CMediumAllocPool.hpp>
|
2022-10-01 06:19:09 +00:00
|
|
|
#include <Kyoto/Alloc/IAllocator.hpp>
|
|
|
|
#include <types.h>
|
2022-09-20 04:35:24 +00:00
|
|
|
|
|
|
|
class CSmallAllocPool;
|
|
|
|
class CMediumAllocPool;
|
|
|
|
class COsContext;
|
|
|
|
class CGameAllocator : public IAllocator {
|
|
|
|
public:
|
|
|
|
class SGameMemInfo {
|
2022-09-22 06:03:38 +00:00
|
|
|
friend class CGameAllocator;
|
2022-10-01 06:19:09 +00:00
|
|
|
|
2022-09-20 04:35:24 +00:00
|
|
|
public:
|
2022-10-01 06:19:09 +00:00
|
|
|
SGameMemInfo(SGameMemInfo* prev, SGameMemInfo* next, SGameMemInfo* nextFree, uint len,
|
|
|
|
const char* fileAndLine, const char* type)
|
2022-09-20 04:35:24 +00:00
|
|
|
: x0_priorGuard(0xefefefef)
|
|
|
|
, x4_len(len)
|
|
|
|
, x8_fileAndLine(fileAndLine)
|
|
|
|
, xc_type(type)
|
|
|
|
, x10_prev(prev)
|
|
|
|
, x14_next(next)
|
|
|
|
, x18_nextFree(nextFree)
|
|
|
|
, x1c_postGuard(0xeaeaeaea) {}
|
|
|
|
|
|
|
|
SGameMemInfo* GetPrev() { return (SGameMemInfo*)((size_t)x10_prev & ~31); }
|
|
|
|
void SetPrev(SGameMemInfo* prev) {
|
2022-10-01 06:19:09 +00:00
|
|
|
void* ptr = x10_prev;
|
|
|
|
x10_prev = prev;
|
|
|
|
x10_prev = (SGameMemInfo*)(((size_t)ptr & 31) | ((size_t)x10_prev & ~31));
|
2022-09-20 04:35:24 +00:00
|
|
|
}
|
|
|
|
SGameMemInfo* GetNext() const { return (SGameMemInfo*)((size_t)x14_next & ~31); }
|
|
|
|
void SetNext(SGameMemInfo* next) {
|
2022-10-01 06:19:09 +00:00
|
|
|
void* ptr = x14_next;
|
|
|
|
x14_next = next;
|
|
|
|
x14_next = (SGameMemInfo*)(((size_t)ptr & 31) | ((size_t)x14_next & ~31));
|
2022-09-20 04:35:24 +00:00
|
|
|
}
|
2022-10-09 05:37:23 +00:00
|
|
|
uint GetPrevMaskedFlags();
|
|
|
|
uint GetNextMaskedFlags();
|
2022-09-20 04:35:24 +00:00
|
|
|
void SetTopOfHeapAllocated(bool topOfHeap);
|
|
|
|
size_t GetLength() const { return x4_len; }
|
|
|
|
SGameMemInfo* GetNextFree() const { return (SGameMemInfo*)((size_t)x18_nextFree & ~31); }
|
|
|
|
void SetNextFree(SGameMemInfo* info) {
|
2022-10-01 06:19:09 +00:00
|
|
|
void* ptr = x18_nextFree;
|
|
|
|
x18_nextFree = info;
|
|
|
|
x18_nextFree = (SGameMemInfo*)(((size_t)ptr & 31) | ((size_t)x18_nextFree & ~31));
|
2022-09-20 04:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAllocated() const { return ((size_t)x10_prev) & 1; }
|
2022-09-22 06:03:38 +00:00
|
|
|
|
|
|
|
bool IsPostGuardIntact() const { return x1c_postGuard == 0xeaeaeaea; }
|
|
|
|
bool IsPriorGuardIntact() const { return x0_priorGuard == 0xefefefef; }
|
|
|
|
|
2022-09-20 04:35:24 +00:00
|
|
|
private:
|
|
|
|
int x0_priorGuard;
|
|
|
|
size_t x4_len;
|
|
|
|
const char* x8_fileAndLine;
|
|
|
|
const char* xc_type;
|
|
|
|
SGameMemInfo* x10_prev;
|
|
|
|
SGameMemInfo* x14_next;
|
|
|
|
SGameMemInfo* x18_nextFree;
|
|
|
|
int x1c_postGuard;
|
|
|
|
};
|
|
|
|
|
|
|
|
SGameMemInfo* GetMemInfoFromBlockPtr(const void* ptr) const;
|
|
|
|
CGameAllocator();
|
|
|
|
~CGameAllocator();
|
|
|
|
|
|
|
|
bool Initialize(COsContext& ctx);
|
|
|
|
void Shutdown();
|
2023-02-06 07:21:28 +00:00
|
|
|
void* Alloc(size_t size, EHint hint, EScope scope, EType type, const CCallStack& cs);
|
2022-09-20 04:35:24 +00:00
|
|
|
SGameMemInfo* FindFreeBlock(uint);
|
|
|
|
SGameMemInfo* FindFreeBlockFromTopOfHeap(uint);
|
|
|
|
uint FixupAllocPtrs(SGameMemInfo*, uint, uint, EHint, const CCallStack&);
|
|
|
|
void UpdateAllocDebugStats(unsigned int, unsigned int, unsigned int);
|
2023-02-06 07:21:28 +00:00
|
|
|
bool Free(const void* ptr);
|
2022-09-20 04:35:24 +00:00
|
|
|
bool FreeNormalAllocation(const void* ptr);
|
2023-02-06 07:21:28 +00:00
|
|
|
void ReleaseAll();
|
2022-10-01 06:19:09 +00:00
|
|
|
void* AllocSecondary(size_t size, EHint hint, EScope scope, EType type,
|
2023-02-06 07:21:28 +00:00
|
|
|
const CCallStack& cs);
|
|
|
|
bool FreeSecondary(const void* ptr);
|
|
|
|
void ReleaseAllSecondary();
|
|
|
|
void SetOutOfMemoryCallback(FOutOfMemoryCb cb, const void* target);
|
|
|
|
int EnumAllocations(FEnumAllocationsCb func, const void* ptr, bool b) const;
|
|
|
|
SAllocInfo GetAllocInfo(const void* ptr) const;
|
|
|
|
SMetrics GetMetrics() const;
|
|
|
|
void OffsetFakeStatics(int offset);
|
2022-09-20 04:35:24 +00:00
|
|
|
|
|
|
|
static uint GetFreeBinEntryForSize(uint);
|
|
|
|
void AddFreeEntryToFreeList(SGameMemInfo*);
|
|
|
|
void RemoveFreeEntryFromFreeList(SGameMemInfo*);
|
|
|
|
void DumpAllocations() const;
|
|
|
|
size_t GetLargestFreeChunk() const;
|
|
|
|
|
|
|
|
private:
|
2022-10-09 05:37:23 +00:00
|
|
|
SGameMemInfo** GetBinPtr(uint bin) { return &x14_bins[bin]; }
|
|
|
|
uchar x4_;
|
|
|
|
uchar x5_;
|
|
|
|
uchar x6_;
|
|
|
|
uchar x7_;
|
|
|
|
uint x8_heapSize;
|
2022-09-20 04:35:24 +00:00
|
|
|
SGameMemInfo* xc_first;
|
|
|
|
SGameMemInfo* x10_last;
|
|
|
|
SGameMemInfo* x14_bins[16];
|
2022-10-09 05:37:23 +00:00
|
|
|
uint x54_;
|
2022-09-20 04:35:24 +00:00
|
|
|
FOutOfMemoryCb x58_oomCallback;
|
|
|
|
const void* x5c_oomTarget;
|
|
|
|
CSmallAllocPool* x60_smallAllocPool;
|
|
|
|
void* x64_smallAllocMainData;
|
|
|
|
void* x68_smallAllocBookKeeping;
|
|
|
|
bool x6c_;
|
|
|
|
int x70_;
|
|
|
|
CMediumAllocPool* x74_mediumPool;
|
|
|
|
void* x78_;
|
|
|
|
bool x7c_;
|
|
|
|
uint x80_;
|
|
|
|
uint x84_;
|
|
|
|
uint x88_;
|
|
|
|
uint x8c_;
|
|
|
|
uint x90_heapSize2;
|
|
|
|
uint x94_;
|
|
|
|
uint x98_;
|
|
|
|
uint x9c_;
|
|
|
|
uint xa0_;
|
|
|
|
uint xa4_;
|
|
|
|
uint xa8_;
|
|
|
|
uint xac_;
|
|
|
|
mutable uint xb0_;
|
|
|
|
void* xb4_physicalAddr;
|
|
|
|
uint xb8_fakeStatics;
|
|
|
|
uint xbc_;
|
|
|
|
};
|
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
#endif // _CGAMEALLOCATOR
|