32
32
namespace Siege
33
33
{
34
34
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 ;
36
36
37
37
template <typename T>
38
38
TlsfAllocator<T>::TlsfAllocator() {}
39
39
40
40
template <typename T>
41
- TlsfAllocator<T>::TlsfAllocator(const uint64_t size)
41
+ TlsfAllocator<T>::TlsfAllocator(const T size)
42
42
{
43
- uint64_t paddedSize = PAD_SIZE (size);
43
+ T paddedSize = PAD_SIZE (size);
44
44
if (size == 0 || size < MIN_ALLOCATION_SIZE || size > (INVALID_INDEX (T) - METADATA_OVERHEAD) ||
45
45
paddedSize < size)
46
46
return ;
47
47
48
- uint64_t maxBuckets = (FL (size) - MIN_SIZE_INDEX) + 1 ;
48
+ T maxBuckets = (FL (size) - MIN_SIZE_INDEX) + 1 ;
49
49
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*);
52
52
53
- uint64_t allocSize = paddedSize + slBucketSize + freeListSize;
53
+ T allocSize = paddedSize + slBucketSize + freeListSize;
54
54
55
55
totalSize = paddedSize;
56
56
totalBytesRemaining = paddedSize;
@@ -73,17 +73,17 @@ TlsfAllocator<T>::TlsfAllocator(const uint64_t size)
73
73
}
74
74
75
75
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)
77
77
{
78
78
if (!ptr) return ;
79
79
BlockHeader* header = TO_HEADER (ptr);
80
80
header->sizeAndFlags = (size << FLAG_OFFSET) | flags;
81
81
}
82
82
83
83
template <typename T>
84
- void * TlsfAllocator<T>::Allocate(const uint64_t & size)
84
+ void * TlsfAllocator<T>::Allocate(const T & size)
85
85
{
86
- uint64_t requiredSize = sizeof (BlockHeader) + size + sizeof (BlockFooter);
86
+ T requiredSize = sizeof (BlockHeader) + size + sizeof (BlockFooter);
87
87
if (!data || capacity == 0 || size == 0 || requiredSize < size ||
88
88
requiredSize > totalBytesRemaining || size < MIN_ALLOCATION_SIZE)
89
89
return nullptr ;
@@ -114,9 +114,9 @@ void TlsfAllocator<T>::Deallocate(void** ptr)
114
114
115
115
if (IsFree (header)) return ;
116
116
117
- uint64_t blockSize = GetHeaderSize (header);
117
+ T blockSize = GetHeaderSize (header);
118
118
119
- uint64_t totalBlockSize = blockSize;
119
+ T totalBlockSize = blockSize;
120
120
header = TryCoalesce (header, totalBlockSize);
121
121
BlockFooter* footer = GetFooter (header);
122
122
@@ -140,11 +140,11 @@ void TlsfAllocator<T>::Deallocate(void** ptr)
140
140
141
141
template <typename T>
142
142
typename TlsfAllocator<T>::BlockHeader* TlsfAllocator<T>::TrySplitBlock(TlsfAllocator<T>::FreeBlockNode* node,
143
- uint64_t & allocatedSize)
143
+ T & allocatedSize)
144
144
{
145
145
if (!node) return nullptr ;
146
146
147
- uint64_t oldSize = GetHeaderSize (GetHeader (node));
147
+ T oldSize = GetHeaderSize (GetHeader (node));
148
148
149
149
BlockHeader* header = GetHeader (node);
150
150
@@ -165,9 +165,9 @@ typename TlsfAllocator<T>::BlockHeader* TlsfAllocator<T>::TrySplitBlock(TlsfAllo
165
165
}
166
166
167
167
template <typename T>
168
- void TlsfAllocator<T>::AddNewBlock(const uint64_t size, BlockHeader* header)
168
+ void TlsfAllocator<T>::AddNewBlock(const T size, BlockHeader* header)
169
169
{
170
- uint64_t fl = 0 , sl = 0 , index;
170
+ T fl = 0 , sl = 0 , index;
171
171
index = CalculateFreeBlockIndices (size, fl, sl);
172
172
173
173
CreateHeader (TO_BYTES (header), size, FREE);
@@ -182,7 +182,7 @@ void TlsfAllocator<T>::AddNewBlock(const uint64_t size, BlockHeader* header)
182
182
}
183
183
184
184
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)
186
186
{
187
187
BlockHeader* start = header;
188
188
BlockHeader* prev = GetPrevHeader (start);
@@ -211,9 +211,9 @@ bool TlsfAllocator<T>::RemoveFreeBlock(TlsfAllocator::FreeBlockNode* node)
211
211
212
212
if (!header) return false ;
213
213
214
- uint64_t oldSize = GetHeaderSize (header);
214
+ T oldSize = GetHeaderSize (header);
215
215
216
- uint64_t fl, sl, index;
216
+ T fl, sl, index;
217
217
218
218
index = CalculateFreeBlockIndices (oldSize, fl, sl);
219
219
@@ -232,9 +232,9 @@ bool TlsfAllocator<T>::RemoveFreeBlock(TlsfAllocator::FreeBlockNode* node)
232
232
}
233
233
234
234
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)
236
236
{
237
- uint64_t fl, sl, index;
237
+ T fl, sl, index;
238
238
// 1000 0000 0000 1000
239
239
index = CalculateFreeBlockIndices (size, fl, sl);
240
240
@@ -245,7 +245,7 @@ typename TlsfAllocator<T>::FreeBlockNode* TlsfAllocator<T>::FindFreeBlock(const
245
245
}
246
246
247
247
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)
249
249
{
250
250
sl = __builtin_ctz (slBitmasks[fl] & ~(((1 << (sl + 1 )) - 1 )));
251
251
@@ -267,16 +267,16 @@ const uint64_t TlsfAllocator<T>::GetNextFreeSlotIndex(uint64_t& fl, uint64_t& sl
267
267
}
268
268
269
269
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)
271
271
{
272
- uint64_t rawFl = FL (size);
272
+ T rawFl = FL (size);
273
273
fl = rawFl - MIN_SIZE_INDEX;
274
274
sl = SL (size, fl);
275
275
return fl * MAX_SL_BUCKETS + sl;
276
276
}
277
277
278
278
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)
280
280
{
281
281
if (!ptr) return ;
282
282
BlockFooter* footer = TO_FOOTER (ptr);
@@ -345,7 +345,7 @@ typename TlsfAllocator<T>::FreeBlockNode* TlsfAllocator<T>::GetFreeBlock(BlockHe
345
345
}
346
346
347
347
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)
349
349
{
350
350
return freeList[fl * MAX_SL_BUCKETS + sl];
351
351
}
@@ -354,7 +354,7 @@ template<typename T>
354
354
typename TlsfAllocator<T>::BlockFooter* TlsfAllocator<T>::GetFooter(TlsfAllocator::BlockHeader* header)
355
355
{
356
356
if (!header) return nullptr ;
357
- uint64_t size = GetHeaderSize (header);
357
+ T size = GetHeaderSize (header);
358
358
uint8_t * rawFooter = TO_BYTES (header) + (size - FOOTER_SIZE);
359
359
if (!rawFooter || !IsValid (rawFooter)) return nullptr ;
360
360
return TO_FOOTER (rawFooter);
@@ -376,7 +376,7 @@ uint8_t* TlsfAllocator<T>::GetBlockData(TlsfAllocator::BlockHeader* header)
376
376
}
377
377
378
378
template <typename T>
379
- const uint64_t TlsfAllocator<T>::GetHeaderSize(BlockHeader* header)
379
+ const T TlsfAllocator<T>::GetHeaderSize(BlockHeader* header)
380
380
{
381
381
return header->sizeAndFlags >> FLAG_OFFSET;
382
382
}
@@ -388,7 +388,7 @@ bool TlsfAllocator<T>::IsFree(BlockHeader* header)
388
388
}
389
389
390
390
template <typename T>
391
- bool TlsfAllocator<T>::IsFree(uint64_t fl, uint64_t sl)
391
+ bool TlsfAllocator<T>::IsFree(T fl, T sl)
392
392
{
393
393
return slBitmasks[fl] & (1 << sl);
394
394
}
@@ -415,13 +415,13 @@ bool TlsfAllocator<T>::IsValid(uint8_t* ptr)
415
415
}
416
416
417
417
template <typename T>
418
- const uint64_t TlsfAllocator<T>::Capacity()
418
+ const T TlsfAllocator<T>::Capacity()
419
419
{
420
420
return capacity;
421
421
}
422
422
423
423
template <typename T>
424
- const uint64_t TlsfAllocator<T>::BytesRemaining()
424
+ const T TlsfAllocator<T>::BytesRemaining()
425
425
{
426
426
return bytesRemaining;
427
427
}
0 commit comments