Skip to content

Commit c49728a

Browse files
committed
Adjusted allocator input sizes to match size classes
1 parent ee17ad3 commit c49728a

File tree

3 files changed

+199
-202
lines changed

3 files changed

+199
-202
lines changed

engine/utils/allocators/Tlsf.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@
3232
namespace Siege
3333
{
3434
template<typename T>
35-
uint8_t TlsfAllocator<T>::MIN_SIZE_INDEX = sizeof(T) < 4 ? 4 : sizeof(T);
35+
uint8_t TlsfAllocator<T>::MIN_SIZE_INDEX = 4;
3636

3737
template<typename T>
3838
TlsfAllocator<T>::TlsfAllocator() {}
3939

4040
template<typename T>
41-
TlsfAllocator<T>::TlsfAllocator(const uint64_t size)
41+
TlsfAllocator<T>::TlsfAllocator(const T size)
4242
{
43-
uint64_t paddedSize = PAD_SIZE(size);
43+
T paddedSize = PAD_SIZE(size);
4444
if (size == 0 || size < MIN_ALLOCATION_SIZE || size > (INVALID_INDEX(T) - METADATA_OVERHEAD) ||
4545
paddedSize < size)
4646
return;
4747

48-
uint64_t maxBuckets = (FL(size) - MIN_SIZE_INDEX) + 1;
48+
T maxBuckets = (FL(size) - MIN_SIZE_INDEX) + 1;
4949

50-
uint64_t slBucketSize = sizeof(uint16_t) * maxBuckets;
51-
uint64_t freeListSize = maxBuckets * MAX_SL_BUCKETS * sizeof(FreeBlockNode*);
50+
T slBucketSize = sizeof(uint16_t) * maxBuckets;
51+
T freeListSize = maxBuckets * MAX_SL_BUCKETS * sizeof(FreeBlockNode*);
5252

53-
uint64_t allocSize = paddedSize + slBucketSize + freeListSize;
53+
T allocSize = paddedSize + slBucketSize + freeListSize;
5454

5555
totalSize = paddedSize;
5656
totalBytesRemaining = paddedSize;
@@ -73,17 +73,17 @@ TlsfAllocator<T>::TlsfAllocator(const uint64_t size)
7373
}
7474

7575
template<typename T>
76-
void TlsfAllocator<T>::CreateHeader(uint8_t* ptr, const uint64_t size, HeaderFlags flags)
76+
void TlsfAllocator<T>::CreateHeader(uint8_t* ptr, const T size, HeaderFlags flags)
7777
{
7878
if (!ptr) return;
7979
BlockHeader* header = TO_HEADER(ptr);
8080
header->sizeAndFlags = (size << FLAG_OFFSET) | flags;
8181
}
8282

8383
template<typename T>
84-
void* TlsfAllocator<T>::Allocate(const uint64_t& size)
84+
void* TlsfAllocator<T>::Allocate(const T& size)
8585
{
86-
uint64_t requiredSize = sizeof(BlockHeader) + size + sizeof(BlockFooter);
86+
T requiredSize = sizeof(BlockHeader) + size + sizeof(BlockFooter);
8787
if (!data || capacity == 0 || size == 0 || requiredSize < size ||
8888
requiredSize > totalBytesRemaining || size < MIN_ALLOCATION_SIZE)
8989
return nullptr;
@@ -114,9 +114,9 @@ void TlsfAllocator<T>::Deallocate(void** ptr)
114114

115115
if (IsFree(header)) return;
116116

117-
uint64_t blockSize = GetHeaderSize(header);
117+
T blockSize = GetHeaderSize(header);
118118

119-
uint64_t totalBlockSize = blockSize;
119+
T totalBlockSize = blockSize;
120120
header = TryCoalesce(header, totalBlockSize);
121121
BlockFooter* footer = GetFooter(header);
122122

@@ -140,11 +140,11 @@ void TlsfAllocator<T>::Deallocate(void** ptr)
140140

141141
template<typename T>
142142
typename TlsfAllocator<T>::BlockHeader* TlsfAllocator<T>::TrySplitBlock(TlsfAllocator<T>::FreeBlockNode* node,
143-
uint64_t& allocatedSize)
143+
T& allocatedSize)
144144
{
145145
if (!node) return nullptr;
146146

147-
uint64_t oldSize = GetHeaderSize(GetHeader(node));
147+
T oldSize = GetHeaderSize(GetHeader(node));
148148

149149
BlockHeader* header = GetHeader(node);
150150

@@ -165,9 +165,9 @@ typename TlsfAllocator<T>::BlockHeader* TlsfAllocator<T>::TrySplitBlock(TlsfAllo
165165
}
166166

167167
template<typename T>
168-
void TlsfAllocator<T>::AddNewBlock(const uint64_t size, BlockHeader* header)
168+
void TlsfAllocator<T>::AddNewBlock(const T size, BlockHeader* header)
169169
{
170-
uint64_t fl = 0, sl = 0, index;
170+
T fl = 0, sl = 0, index;
171171
index = CalculateFreeBlockIndices(size, fl, sl);
172172

173173
CreateHeader(TO_BYTES(header), size, FREE);
@@ -182,7 +182,7 @@ void TlsfAllocator<T>::AddNewBlock(const uint64_t size, BlockHeader* header)
182182
}
183183

184184
template<typename T>
185-
typename TlsfAllocator<T>::BlockHeader* TlsfAllocator<T>::TryCoalesce(BlockHeader* header, OUT uint64_t& size)
185+
typename TlsfAllocator<T>::BlockHeader* TlsfAllocator<T>::TryCoalesce(BlockHeader* header, OUT T& size)
186186
{
187187
BlockHeader* start = header;
188188
BlockHeader* prev = GetPrevHeader(start);
@@ -211,9 +211,9 @@ bool TlsfAllocator<T>::RemoveFreeBlock(TlsfAllocator::FreeBlockNode* node)
211211

212212
if (!header) return false;
213213

214-
uint64_t oldSize = GetHeaderSize(header);
214+
T oldSize = GetHeaderSize(header);
215215

216-
uint64_t fl, sl, index;
216+
T fl, sl, index;
217217

218218
index = CalculateFreeBlockIndices(oldSize, fl, sl);
219219

@@ -232,9 +232,9 @@ bool TlsfAllocator<T>::RemoveFreeBlock(TlsfAllocator::FreeBlockNode* node)
232232
}
233233

234234
template<typename T>
235-
typename TlsfAllocator<T>::FreeBlockNode* TlsfAllocator<T>::FindFreeBlock(const uint64_t& size)
235+
typename TlsfAllocator<T>::FreeBlockNode* TlsfAllocator<T>::FindFreeBlock(const T& size)
236236
{
237-
uint64_t fl, sl, index;
237+
T fl, sl, index;
238238
// 1000 0000 0000 1000
239239
index = CalculateFreeBlockIndices(size, fl, sl);
240240

@@ -245,7 +245,7 @@ typename TlsfAllocator<T>::FreeBlockNode* TlsfAllocator<T>::FindFreeBlock(const
245245
}
246246

247247
template<typename T>
248-
const uint64_t TlsfAllocator<T>::GetNextFreeSlotIndex(uint64_t& fl, uint64_t& sl)
248+
const T TlsfAllocator<T>::GetNextFreeSlotIndex(T& fl, T& sl)
249249
{
250250
sl = __builtin_ctz(slBitmasks[fl] & ~(((1 << (sl + 1)) - 1)));
251251

@@ -267,16 +267,16 @@ const uint64_t TlsfAllocator<T>::GetNextFreeSlotIndex(uint64_t& fl, uint64_t& sl
267267
}
268268

269269
template<typename T>
270-
uint64_t TlsfAllocator<T>::CalculateFreeBlockIndices(uint64_t size, OUT uint64_t& fl, OUT uint64_t& sl)
270+
T TlsfAllocator<T>::CalculateFreeBlockIndices(T size, OUT T& fl, OUT T& sl)
271271
{
272-
uint64_t rawFl = FL(size);
272+
T rawFl = FL(size);
273273
fl = rawFl - MIN_SIZE_INDEX;
274274
sl = SL(size, fl);
275275
return fl * MAX_SL_BUCKETS + sl;
276276
}
277277

278278
template<typename T>
279-
void TlsfAllocator<T>::CreateFooter(uint8_t* ptr, const uint64_t size)
279+
void TlsfAllocator<T>::CreateFooter(uint8_t* ptr, const T size)
280280
{
281281
if (!ptr) return;
282282
BlockFooter* footer = TO_FOOTER(ptr);
@@ -345,7 +345,7 @@ typename TlsfAllocator<T>::FreeBlockNode* TlsfAllocator<T>::GetFreeBlock(BlockHe
345345
}
346346

347347
template<typename T>
348-
typename TlsfAllocator<T>::FreeBlockNode* TlsfAllocator<T>::GetFreeBlock(const uint64_t fl, const uint64_t sl)
348+
typename TlsfAllocator<T>::FreeBlockNode* TlsfAllocator<T>::GetFreeBlock(const T fl, const T sl)
349349
{
350350
return freeList[fl * MAX_SL_BUCKETS + sl];
351351
}
@@ -354,7 +354,7 @@ template<typename T>
354354
typename TlsfAllocator<T>::BlockFooter* TlsfAllocator<T>::GetFooter(TlsfAllocator::BlockHeader* header)
355355
{
356356
if (!header) return nullptr;
357-
uint64_t size = GetHeaderSize(header);
357+
T size = GetHeaderSize(header);
358358
uint8_t* rawFooter = TO_BYTES(header) + (size - FOOTER_SIZE);
359359
if (!rawFooter || !IsValid(rawFooter)) return nullptr;
360360
return TO_FOOTER(rawFooter);
@@ -376,7 +376,7 @@ uint8_t* TlsfAllocator<T>::GetBlockData(TlsfAllocator::BlockHeader* header)
376376
}
377377

378378
template<typename T>
379-
const uint64_t TlsfAllocator<T>::GetHeaderSize(BlockHeader* header)
379+
const T TlsfAllocator<T>::GetHeaderSize(BlockHeader* header)
380380
{
381381
return header->sizeAndFlags >> FLAG_OFFSET;
382382
}
@@ -388,7 +388,7 @@ bool TlsfAllocator<T>::IsFree(BlockHeader* header)
388388
}
389389

390390
template<typename T>
391-
bool TlsfAllocator<T>::IsFree(uint64_t fl, uint64_t sl)
391+
bool TlsfAllocator<T>::IsFree(T fl, T sl)
392392
{
393393
return slBitmasks[fl] & (1 << sl);
394394
}
@@ -415,13 +415,13 @@ bool TlsfAllocator<T>::IsValid(uint8_t* ptr)
415415
}
416416

417417
template<typename T>
418-
const uint64_t TlsfAllocator<T>::Capacity()
418+
const T TlsfAllocator<T>::Capacity()
419419
{
420420
return capacity;
421421
}
422422

423423
template<typename T>
424-
const uint64_t TlsfAllocator<T>::BytesRemaining()
424+
const T TlsfAllocator<T>::BytesRemaining()
425425
{
426426
return bytesRemaining;
427427
}

engine/utils/allocators/Tlsf.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TlsfAllocator
4848
// S'tructors
4949

5050
TlsfAllocator();
51-
TlsfAllocator(const uint64_t size);
51+
TlsfAllocator(const T size);
5252

5353
// Deleted copy and move constructors
5454

@@ -57,21 +57,21 @@ class TlsfAllocator
5757

5858
// Other functions
5959

60-
void CreateHeader(uint8_t* ptr, const uint64_t size, HeaderFlags flags);
61-
void CreateFooter(uint8_t* ptr, const uint64_t size);
60+
void CreateHeader(uint8_t* ptr, const T size, HeaderFlags flags);
61+
void CreateFooter(uint8_t* ptr, const T size);
6262
FreeBlockNode* CreateFreeBlock(uint8_t* ptr, FreeBlockNode* prev, FreeBlockNode* next);
63-
FreeBlockNode* FindFreeBlock(const uint64_t& size);
63+
FreeBlockNode* FindFreeBlock(const T& size);
6464

65-
const uint64_t GetNextFreeSlotIndex(uint64_t& fl, uint64_t& sl);
65+
const T GetNextFreeSlotIndex(T& fl, T& sl);
6666
bool IsFree(BlockHeader* header);
67-
bool IsFree(uint64_t fl, uint64_t sl);
67+
bool IsFree(T fl, T sl);
6868
bool PrevBlockIsFree(BlockHeader* header);
69-
uint64_t CalculateFreeBlockIndices(uint64_t size, OUT uint64_t& fl, OUT uint64_t& sl);
69+
T CalculateFreeBlockIndices(T size, OUT T& fl, OUT T& sl);
7070

7171
// Buffer manipulation Functions
7272

7373
FreeBlockNode* GetFreeBlock(BlockHeader* header);
74-
FreeBlockNode* GetFreeBlock(const uint64_t fl, const uint64_t sl);
74+
FreeBlockNode* GetFreeBlock(const T fl, const T sl);
7575
uint8_t* GetBlockData(BlockHeader* header);
7676
BlockFooter* GetFooter(BlockHeader* header);
7777
BlockFooter* GetPrevFooter(BlockHeader* header);
@@ -82,37 +82,37 @@ class TlsfAllocator
8282

8383
// Allocate/Deallocate
8484

85-
void* Allocate(const uint64_t& size);
85+
void* Allocate(const T& size);
8686
void Deallocate(void** ptr);
8787

88-
BlockHeader* TrySplitBlock(FreeBlockNode* node, uint64_t& allocatedSize);
88+
BlockHeader* TrySplitBlock(FreeBlockNode* node, T& allocatedSize);
8989
bool RemoveFreeBlock(FreeBlockNode* node);
90-
void AddNewBlock(const uint64_t size, BlockHeader* currentNode);
91-
BlockHeader* TryCoalesce(BlockHeader* header, OUT uint64_t& size);
90+
void AddNewBlock(const T size, BlockHeader* currentNode);
91+
BlockHeader* TryCoalesce(BlockHeader* header, OUT T& size);
9292

9393
// Getters
9494

9595
bool IsValid(uint8_t* ptr);
96-
const uint64_t GetHeaderSize(BlockHeader* header);
97-
const uint64_t Capacity();
98-
const uint64_t BytesRemaining();
99-
const uint64_t TotalBytesRemaining()
96+
const T GetHeaderSize(BlockHeader* header);
97+
const T Capacity();
98+
const T BytesRemaining();
99+
const T TotalBytesRemaining()
100100
{
101101
return totalBytesRemaining;
102102
}
103-
const uint64_t TotalSize()
103+
const T TotalSize()
104104
{
105105
return totalSize;
106106
}
107107
const uint8_t* Data()
108108
{
109109
return data;
110110
}
111-
const uint64_t FlBitmask()
111+
const T FlBitmask()
112112
{
113113
return flBitmask;
114114
}
115-
const uint16_t& SlBitmask(const uint64_t fl)
115+
const uint16_t& SlBitmask(const T fl)
116116
{
117117
return slBitmasks[fl];
118118
}
@@ -121,17 +121,17 @@ class TlsfAllocator
121121

122122
static uint8_t MIN_SIZE_INDEX;
123123

124-
uint64_t totalSize {0};
125-
uint64_t totalBytesRemaining {0};
124+
T totalSize {0};
125+
T totalBytesRemaining {0};
126126

127-
uint64_t capacity {0};
128-
uint64_t bytesRemaining {0};
127+
T capacity {0};
128+
T bytesRemaining {0};
129129

130130
uint8_t* data {nullptr};
131131
FreeBlockNode** freeList {nullptr};
132132

133133
// bitmasks
134-
uint64_t flBitmask {0};
134+
T flBitmask {0};
135135
uint16_t* slBitmasks {nullptr};
136136
};
137137

0 commit comments

Comments
 (0)