Skip to content

Commit fe50bee

Browse files
committed
De-hardcode Allocator pointer sizes
1 parent 602ec2d commit fe50bee

8 files changed

Lines changed: 110 additions & 61 deletions

File tree

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
#ifndef _ALLOCATORCOMMON
22
#define _ALLOCATORCOMMON
33

4-
static const int kPointerSize = sizeof(void*);
5-
static const int kPointerBits = kPointerSize * 8;
4+
#include <stddef.h> // For size_t
65

6+
static const int kAllocatorPointerSize = sizeof(void*);
7+
static const int kAllocatorPointerBits = kAllocatorPointerSize * 8;
8+
9+
template < size_t PointerSize >
10+
struct PatternExpander;
11+
12+
template <>
13+
struct PatternExpander< 4 > {
14+
typedef unsigned int Type;
15+
typedef size_t MaskType;
16+
static const Type Multiplier = 0x01010101U;
17+
static const MaskType TopNybbleMask = 0xF0000000U;
18+
};
19+
20+
template <>
21+
struct PatternExpander< 8 > {
22+
typedef unsigned long long Type;
23+
static const Type Multiplier = 0x0101010101010101ULL;
24+
enum { TopNybbleShift = 60 };
25+
};
26+
27+
#define EXPAND_PATTERN(byte_val) \
28+
(static_cast< PatternExpander< kAllocatorPointerSize >::Type >(byte_val) * \
29+
PatternExpander< kAllocatorPointerSize >::Multiplier)
30+
31+
static const intptr_t kAllocatorPostGuard = EXPAND_PATTERN(0xEA);
32+
static const intptr_t kAllocatorPriorGuard = EXPAND_PATTERN(0xEF);
33+
static const int kAllocatorPointerTopNybbleMask = PatternExpander< sizeof(void*) >::TopNybbleMask;
734
#endif // _ALLOCATORCOMMON

‎include/Kyoto/Alloc/CCircularBuffer.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CCircularBuffer {
1313
void* Alloc(int len);
1414
void Free(void* ptr, int len);
1515
int GetAllocatedAmount() const;
16-
void* GetOffsettedMemory(int offset) {
16+
void* GetOffsettedMemory(const int offset) {
1717
return x0_ptr.get() + offset;
1818
}
1919

‎include/Kyoto/Alloc/CGameAllocator.hpp‎

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#ifndef _CGAMEALLOCATOR
22
#define _CGAMEALLOCATOR
33

4-
#include <Kyoto/Alloc/CMediumAllocPool.hpp>
54
#include <Kyoto/Alloc/IAllocator.hpp>
5+
6+
#include <Kyoto/Alloc/CMediumAllocPool.hpp>
67
#include <types.h>
78

89
class CSmallAllocPool;
@@ -25,68 +26,85 @@ class CGameAllocator : public IAllocator {
2526
, x18_nextFree(nextFree)
2627
, x1c_postGuard(0xeaeaeaea) {}
2728

28-
SGameMemInfo* GetPrev() const { return (SGameMemInfo*)((size_t)x10_prev & ~31); }
29+
SGameMemInfo* GetPrev() const {
30+
return reinterpret_cast< SGameMemInfo* >(reinterpret_cast< uintptr_t >(x10_prev) &
31+
~(kAllocatorPointerBits - 1));
32+
}
2933
void SetPrev(SGameMemInfo* prev) {
3034
void* ptr = x10_prev;
3135
x10_prev = prev;
32-
x10_prev = (SGameMemInfo*)(((size_t)ptr & 31) | ((size_t)x10_prev & ~31));
36+
x10_prev = reinterpret_cast< SGameMemInfo* >(
37+
(reinterpret_cast< uintptr_t >(ptr) & (kAllocatorPointerBits - 1)) |
38+
(reinterpret_cast< uintptr_t >(x10_prev) & ~(kAllocatorPointerBits - 1)));
39+
}
40+
SGameMemInfo* GetNext() const {
41+
return reinterpret_cast< SGameMemInfo* >(reinterpret_cast< uintptr_t >(x14_next) &
42+
~(kAllocatorPointerBits - 1));
3343
}
34-
SGameMemInfo* GetNext() const { return (SGameMemInfo*)((size_t)x14_next & ~31); }
3544
void SetNext(SGameMemInfo* next) {
3645
void* ptr = x14_next;
3746
x14_next = next;
38-
x14_next = (SGameMemInfo*)(((size_t)ptr & 31) | ((size_t)x14_next & ~31));
47+
x14_next = reinterpret_cast< SGameMemInfo* >(
48+
(reinterpret_cast< uintptr_t >(ptr) & (kAllocatorPointerBits - 1)) |
49+
(reinterpret_cast< uintptr_t >(x14_next) & ~(kAllocatorPointerBits - 1)));
50+
}
51+
uint GetPrevMaskedFlags() const {
52+
return reinterpret_cast< uintptr_t >(x10_prev) & (kAllocatorPointerBits - 1);
3953
}
40-
uint GetPrevMaskedFlags() const { return reinterpret_cast< size_t >(x10_prev) & 31; }
41-
void SetPrevMaskedFlags(uint flags) {
42-
x10_prev =
43-
reinterpret_cast< SGameMemInfo* >((reinterpret_cast< size_t >(x10_prev) & ~31) | flags);
54+
void SetPrevMaskedFlags(const uint flags) {
55+
x10_prev = reinterpret_cast< SGameMemInfo* >(
56+
(reinterpret_cast< uintptr_t >(x10_prev) & ~(kAllocatorPointerBits - 1)) | flags);
4457
}
45-
void SetAllocated(bool allocated) {
58+
void SetAllocated(const bool allocated) {
4659
if (allocated) {
4760
SetPrevMaskedFlags((GetPrevMaskedFlags() & ~1) | 1);
4861
} else {
49-
x10_prev = reinterpret_cast< SGameMemInfo* >(reinterpret_cast< size_t >(x10_prev) & ~1);
62+
x10_prev = reinterpret_cast< SGameMemInfo* >(reinterpret_cast< uintptr_t >(x10_prev) & ~1);
5063
}
5164
}
5265
uint GetNextMaskedFlags();
53-
void SetTopOfHeapAllocated(bool topOfHeap) {
66+
void SetTopOfHeapAllocated(const bool topOfHeap) {
5467
const uint flags = GetPrevMaskedFlags();
5568
SGameMemInfo* prev = GetPrev();
5669
uint topFlag = 0;
5770
if (topOfHeap) {
5871
topFlag = 2;
5972
}
60-
x10_prev = reinterpret_cast< SGameMemInfo* >(reinterpret_cast< size_t >(prev) |
73+
x10_prev = reinterpret_cast< SGameMemInfo* >(reinterpret_cast< uintptr_t >(prev) |
6174
(topFlag | (flags & ~2)));
6275
}
6376
size_t GetLength() const { return x4_len; }
64-
void SetLength(size_t len) { x4_len = len; }
65-
SGameMemInfo* GetNextFree() const { return (SGameMemInfo*)((size_t)x18_nextFree & ~31); }
77+
void SetLength(const size_t len) { x4_len = len; }
78+
SGameMemInfo* GetNextFree() const {
79+
return reinterpret_cast< SGameMemInfo* >(reinterpret_cast< uintptr_t >(x18_nextFree) &
80+
~(kAllocatorPointerBits - 1));
81+
}
6682
void SetNextFree(SGameMemInfo* info) {
6783
void* ptr = x18_nextFree;
6884
x18_nextFree = info;
69-
x18_nextFree = (SGameMemInfo*)(((size_t)ptr & 31) | ((size_t)x18_nextFree & ~31));
85+
x18_nextFree = reinterpret_cast< SGameMemInfo* >(
86+
(reinterpret_cast< uintptr_t >(ptr) & (kAllocatorPointerBits - 1)) |
87+
(reinterpret_cast< uintptr_t >(x18_nextFree) & ~(kAllocatorPointerBits - 1)));
7088
}
7189

72-
bool IsAllocated() const { return ((size_t)x10_prev) & 1; }
90+
bool IsAllocated() const { return reinterpret_cast< uintptr_t >(x10_prev) & 1; }
7391
void SetNotAllocated() {
7492
void* ptr = x10_prev;
75-
x10_prev = (SGameMemInfo*)((size_t)ptr & ~1);
93+
x10_prev = reinterpret_cast< SGameMemInfo* >(reinterpret_cast< uintptr_t >(ptr) & ~1);
7694
}
7795

78-
bool IsPostGuardIntact() const { return x1c_postGuard == 0xeaeaeaea; }
79-
bool IsPriorGuardIntact() const { return x0_priorGuard == 0xefefefef; }
96+
bool IsPostGuardIntact() const { return x1c_postGuard == kAllocatorPostGuard; }
97+
bool IsPriorGuardIntact() const { return x0_priorGuard == kAllocatorPriorGuard; }
8098

8199
private:
82-
int x0_priorGuard;
100+
size_t x0_priorGuard;
83101
size_t x4_len;
84102
const char* x8_fileAndLine;
85103
const char* xc_type;
86104
SGameMemInfo* x10_prev;
87105
SGameMemInfo* x14_next;
88106
SGameMemInfo* x18_nextFree;
89-
int x1c_postGuard;
107+
size_t x1c_postGuard;
90108
};
91109

92110
SGameMemInfo* GetMemInfoFromBlockPtr(const void* ptr) const;

‎include/Kyoto/Alloc/CSmallAllocPool.hpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ class CSmallAllocPool {
1313
bool Free(const void* ptr);
1414

1515
bool PtrWithinPool(const void* ptr) const {
16-
return uint((reinterpret_cast< const uchar* >(ptr) - reinterpret_cast< uchar* >(x0_mainData)) /
17-
4) < x8_numBlocks;
16+
return static_cast< uint >(
17+
(static_cast< const uchar* >(ptr) - static_cast< uchar* >(x0_mainData)) / 4) <
18+
x8_numBlocks;
1819
}
1920

2021
uint GetIndexFromPtr(const void* ptr) const {
21-
return ((const uchar*)ptr - (const uchar*)x0_mainData) / kPointerSize;
22+
return (static_cast< const uchar* >(ptr) - static_cast< const uchar* >(x0_mainData)) /
23+
kAllocatorPointerSize;
2224
}
23-
long GetEntryValue(uint idx) const { return (long)*((uchar*)x4_bookKeeping + idx); }
25+
intptr_t GetEntryValue(uint idx) const { return *(static_cast< uchar* >(x4_bookKeeping) + idx); }
2426
uchar* GetPtrFromIndex(unsigned int idx) const {
2527
return static_cast< uchar* >(x0_mainData) + (idx << 3);
2628
}

‎include/Kyoto/Alloc/IAllocator.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "types.h"
55
#include <stddef.h>
6+
#include "Kyoto/Alloc/AllocatorCommon.hpp"
67

78
class COsContext;
89
class CCallStack;

‎src/Kyoto/Alloc/CCircularBuffer.cpp‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
#include "Kyoto/Alloc/CCircularBuffer.hpp"
22

3-
CCircularBuffer::CCircularBuffer(void* buf, int len, CCircularBuffer::EOwnership ownership)
4-
: x0_ptr(static_cast<char*>(buf)), x8_bufferLen(len), xc_(0), x10_nextFreeAddr(0), x14_(-1) {
3+
CCircularBuffer::CCircularBuffer(void* buf, const int len, const EOwnership ownership)
4+
: x0_ptr(static_cast< char* >(buf)), x8_bufferLen(len), xc_(0), x10_nextFreeAddr(0), x14_(-1) {
55
if (ownership == kOS_NotOwned) {
6-
x0_ptr.release();
6+
(void)x0_ptr.release();
77
}
88
}
99

10-
bool CCircularBuffer::IsWrappedMemory(int offset, int len) {
10+
bool CCircularBuffer::IsWrappedMemory(const int offset, const int len) {
1111
if (x14_ > -1 && x14_ >= offset && x14_ < (offset + len)) {
1212
return true;
1313
}
1414

1515
return false;
1616
}
1717

18-
void* CCircularBuffer::Alloc(int len) {
19-
uchar* ret;
18+
void* CCircularBuffer::Alloc(const int len) {
2019
if ((x8_bufferLen - x10_nextFreeAddr) >= len && !IsWrappedMemory(x10_nextFreeAddr, len)) {
21-
int offset = x10_nextFreeAddr;
22-
uchar* ptr = (uchar*)x0_ptr.get();
20+
const int offset = x10_nextFreeAddr;
21+
uchar* ptr = reinterpret_cast< uchar* >(x0_ptr.get());
2322
x10_nextFreeAddr = offset + len;
2423
return ptr + offset;
25-
} else if (xc_ >= len && !IsWrappedMemory(0, len)) {
24+
}
25+
if (xc_ >= len && !IsWrappedMemory(0, len)) {
2626
uint r3 = xc_;
2727
xc_ = 0;
2828
x10_nextFreeAddr = len;
@@ -33,7 +33,7 @@ void* CCircularBuffer::Alloc(int len) {
3333
return nullptr;
3434
}
3535

36-
void CCircularBuffer::Free(void* ptr, int len) {
36+
void CCircularBuffer::Free(void* ptr, const int len) {
3737
if (x14_ > -1) {
3838
if (ptr == x0_ptr.get()) {
3939
x14_ = -1;
@@ -52,7 +52,7 @@ void CCircularBuffer::Free(void* ptr, int len) {
5252
}
5353

5454
int CCircularBuffer::GetAllocatedAmount() const {
55-
int tmp = x14_;
55+
const int tmp = x14_;
5656
int res = x10_nextFreeAddr - xc_;
5757
if (tmp != -1) {
5858
res += x8_bufferLen - tmp;

‎src/Kyoto/Alloc/CGameAllocator.cpp‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ static inline U1 T_round_up(U2 val, int align) {
2323
}
2424

2525
CGameAllocator::SGameMemInfo* CGameAllocator::GetMemInfoFromBlockPtr(const void* ptr) const {
26-
return (SGameMemInfo*)((uchar*)(ptr) - sizeof(SGameMemInfo));
26+
return reinterpret_cast< SGameMemInfo* >(reinterpret_cast< uintptr_t >(ptr) -
27+
sizeof(SGameMemInfo));
2728
}
2829

2930
CGameAllocator::CGameAllocator()
@@ -68,9 +69,9 @@ CGameAllocator::~CGameAllocator() {
6869
bool CGameAllocator::Initialize(COsContext& ctx) {
6970
x8_heapSize = ctx.GetBaseFreeRam() - 2 * sizeof(SGameMemInfo);
7071
xc_first = static_cast< SGameMemInfo* >(OSAllocFromArenaLo(x8_heapSize, sizeof(SGameMemInfo)));
71-
xb4_physicalAddr =
72-
reinterpret_cast< void* >(reinterpret_cast< intptr_t >(xc_first) -
73-
(reinterpret_cast< uintptr_t >(xc_first) & 0xf0000000));
72+
xb4_physicalAddr = reinterpret_cast< void* >(
73+
reinterpret_cast< intptr_t >(xc_first) -
74+
(reinterpret_cast< uintptr_t >(xc_first) & kAllocatorPointerTopNybbleMask));
7475
OSGetArenaLo();
7576
x10_last =
7677
reinterpret_cast< SGameMemInfo* >(reinterpret_cast< char* >(xc_first - 1) + x8_heapSize);
@@ -394,7 +395,7 @@ bool CGameAllocator::FreeNormalAllocation(const void* ptr) {
394395
SGameMemInfo* k = info->GetNext();
395396
size_t len = 0;
396397
if (k) {
397-
len = (size_t)k - (size_t)info - sizeof(SGameMemInfo);
398+
len = reinterpret_cast< size_t >(k) - reinterpret_cast< size_t >(info) - sizeof(SGameMemInfo);
398399
}
399400
info->SetLength(len);
400401

@@ -472,10 +473,10 @@ void CGameAllocator::SetOutOfMemoryCallback(FOutOfMemoryCb cb, const void* targe
472473
};
473474

474475
IAllocator::SAllocInfo CGameAllocator::GetAllocInfo(const void* ptr) const {
475-
SGameMemInfo* info = GetMemInfoFromBlockPtr(ptr);
476+
const SGameMemInfo* info = GetMemInfoFromBlockPtr(ptr);
476477

477-
return SAllocInfo((const void*)info, info->GetLength(), info->IsAllocated(), false,
478-
info->x8_fileAndLine, info->xc_type);
478+
return SAllocInfo(info, info->GetLength(), info->IsAllocated(), false, info->x8_fileAndLine,
479+
info->xc_type);
479480
};
480481

481482
IAllocator::SMetrics CGameAllocator::GetMetrics() const {
@@ -487,22 +488,22 @@ IAllocator::SMetrics CGameAllocator::GetMetrics() const {
487488
x74_mediumPool != nullptr
488489
? x74_mediumPool->GetTotalEntries() - x74_mediumPool->GetNumBlocksAvailable()
489490
: 0;
490-
uint mediumAllocNumAllocs = x74_mediumPool != nullptr ? x74_mediumPool->GetNumAllocs() : 0;
491+
const uint mediumAllocNumAllocs = x74_mediumPool != nullptr ? x74_mediumPool->GetNumAllocs() : 0;
491492
SMetrics ret(x8_heapSize, x80_, x84_, x88_, x8c_, x90_heapSize2, x94_, x98_, x9c_, xa0_, xa4_,
492493
x60_smallAllocPool != nullptr ? x60_smallAllocPool->GetNumAllocs() : 0,
493494
x60_smallAllocPool != nullptr ? x60_smallAllocPool->GetAllocatedSize() : 0,
494495
x60_smallAllocPool != nullptr ? x60_smallAllocPool->GetNumBlocksAvailable() : 0,
495496
mediumAllocNumAllocs, mediumAllocAllocatedSize, mediumAllocBlocksAvailable,
496-
x80_ - xb0_, (uintptr_t)xb4_physicalAddr, xbc_, mediumAllocTotalAllocated,
497-
xb8_fakeStatics);
497+
x80_ - xb0_, reinterpret_cast< uintptr_t >(xb4_physicalAddr), xbc_,
498+
mediumAllocTotalAllocated, xb8_fakeStatics);
498499
xb0_ = x80_;
499500
return ret;
500501
};
501502

502503
int CGameAllocator::EnumAllocations(FEnumAllocationsCb func, const void* ptr, bool b) const {
503504

504505
int i = 0;
505-
SGameMemInfo* iter = xc_first;
506+
const SGameMemInfo* iter = xc_first;
506507

507508
while (iter != nullptr) {
508509
if (!iter->IsPostGuardIntact()) {
@@ -513,9 +514,9 @@ int CGameAllocator::EnumAllocations(FEnumAllocationsCb func, const void* ptr, bo
513514
return -1;
514515
}
515516

516-
SGameMemInfo* next = iter->GetNext();
517-
SAllocInfo alloc((const void*)iter, iter->GetLength(), iter->IsAllocated(), false,
518-
iter->x8_fileAndLine, iter->xc_type);
517+
const SGameMemInfo* next = iter->GetNext();
518+
SAllocInfo alloc(iter, iter->GetLength(), iter->IsAllocated(), false, iter->x8_fileAndLine,
519+
iter->xc_type);
519520
func(alloc, ptr);
520521
++i;
521522
iter = next;
@@ -524,7 +525,7 @@ int CGameAllocator::EnumAllocations(FEnumAllocationsCb func, const void* ptr, bo
524525
return i;
525526
};
526527

527-
uint CGameAllocator::GetFreeBinEntryForSize(uint size) {
528+
uint CGameAllocator::GetFreeBinEntryForSize(const uint size) {
528529
uint maxLen = 0x20;
529530
uint bin = 0;
530531

@@ -584,7 +585,7 @@ void CGameAllocator::DumpAllocations() const {
584585
}
585586

586587
size_t CGameAllocator::GetLargestFreeChunk() const {
587-
SGameMemInfo* iter = xc_first;
588+
const SGameMemInfo* iter = xc_first;
588589
size_t ret = 0;
589590
while (iter != nullptr) {
590591
if (!iter->IsAllocated() && iter->GetLength() > ret) {
@@ -595,4 +596,4 @@ size_t CGameAllocator::GetLargestFreeChunk() const {
595596

596597
return ret;
597598
}
598-
void CGameAllocator::OffsetFakeStatics(int offset) { xb8_fakeStatics += offset; }
599+
void CGameAllocator::OffsetFakeStatics(const int offset) { xb8_fakeStatics += offset; }

‎src/Kyoto/Alloc/CSmallAllocPool.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,18 @@ void* CSmallAllocPool::Alloc(uint size) {
9999
bool CSmallAllocPool::Free(const void* arg0) {
100100
int temp_r8 = GetIndexFromPtr(arg0);
101101
int mask = (temp_r8 & 1) ? 0 : 4;
102-
size_t temp_r9 = (size_t)temp_r8 / 2;
102+
size_t temp_r9 = static_cast< size_t >(temp_r8) / 2;
103103
long temp_r4_2 = GetEntryValue(temp_r9);
104104
temp_r4_2 = (temp_r4_2 >> mask) & 0xF;
105105
x18_numBlocksAvailable += temp_r4_2;
106106
int var_r5 = temp_r4_2;
107107
x1c_numAllocs -= 1;
108108
x14_ = temp_r8;
109-
if ((size_t)temp_r8 == (size_t)x10_) {
109+
if (static_cast< size_t >(temp_r8) == static_cast< size_t >(x10_)) {
110110
x10_ = -1;
111111
}
112112

113-
uchar* var_r3 = ((uchar*)x4_bookKeeping) + temp_r9;
113+
uchar* var_r3 = static_cast< uchar* >(x4_bookKeeping) + temp_r9;
114114
while (var_r5 != 0) {
115115
*var_r3 = 0;
116116
var_r5 -= 2;

0 commit comments

Comments
 (0)