8
8
//
9
9
10
10
#include " TlsfAllocator.h"
11
- #include " Logging.h"
12
11
13
12
#include < memory>
14
13
14
+ #include " Logging.h"
15
+
15
16
#define TO_BYTES (val ) reinterpret_cast <uint8_t *>(val)
16
17
#define TO_HEADER (val ) reinterpret_cast <BlockHeader*>(val)
17
18
#define TO_FOOTER (val ) reinterpret_cast <BlockFooter*>(val)
@@ -39,7 +40,8 @@ TlsfAllocator::TlsfAllocator() {}
39
40
TlsfAllocator::TlsfAllocator (const uint64_t size)
40
41
{
41
42
uint64_t paddedSize = PAD_SIZE (size);
42
- if (size == 0 || size < MIN_ALLOCATION || size > (UINT64_MAX - METADATA_OVERHEAD) || paddedSize < size)
43
+ if (size == 0 || size < MIN_ALLOCATION || size > (UINT64_MAX - METADATA_OVERHEAD) ||
44
+ paddedSize < size)
43
45
return ;
44
46
45
47
uint64_t maxBuckets = (FL (size) - FL_MIN_INDEX) + 1 ;
@@ -57,18 +59,19 @@ TlsfAllocator::TlsfAllocator(const uint64_t size)
57
59
58
60
data = TO_BYTES (calloc (1 , allocSize));
59
61
60
- CC_ASSERT (data, " Allocation returned null. This should not happen and implies an implementation failure." )
62
+ CC_ASSERT (
63
+ data,
64
+ " Allocation returned null. This should not happen and implies an implementation failure." )
61
65
62
- freeList = (FreeBlockNode**)(data + paddedSize);
63
- slBitmasks = (uint16_t *)(data + paddedSize + freeListSize);
66
+ freeList = (FreeBlockNode**) (data + paddedSize);
67
+ slBitmasks = (uint16_t *) (data + paddedSize + freeListSize);
64
68
65
69
CreateHeader (data, paddedSize, FREE);
66
70
67
71
AddNewBlock (paddedSize, TO_HEADER (data));
68
72
}
69
73
70
- void TlsfAllocator::CreateHeader (uint8_t * ptr, const uint64_t size,
71
- HeaderFlags flags)
74
+ void TlsfAllocator::CreateHeader (uint8_t * ptr, const uint64_t size, HeaderFlags flags)
72
75
{
73
76
if (!ptr) return ;
74
77
BlockHeader* header = TO_HEADER (ptr);
@@ -78,8 +81,9 @@ void TlsfAllocator::CreateHeader(uint8_t* ptr, const uint64_t size,
78
81
void * TlsfAllocator::Allocate (const uint64_t & size)
79
82
{
80
83
uint64_t requiredSize = sizeof (BlockHeader) + size + sizeof (BlockFooter);
81
- if (!data || capacity == 0 ||size == 0 || requiredSize < size || requiredSize > totalBytesRemaining
82
- || size < MIN_ALLOCATION) return nullptr ;
84
+ if (!data || capacity == 0 || size == 0 || requiredSize < size ||
85
+ requiredSize > totalBytesRemaining || size < MIN_ALLOCATION)
86
+ return nullptr ;
83
87
84
88
FreeBlockNode* block = FindFreeBlock (requiredSize);
85
89
@@ -96,7 +100,8 @@ void* TlsfAllocator::Allocate(const uint64_t& size)
96
100
return ptr;
97
101
}
98
102
99
- TlsfAllocator::BlockHeader* TlsfAllocator::TrySplitBlock (TlsfAllocator::FreeBlockNode* node, uint64_t & allocatedSize)
103
+ TlsfAllocator::BlockHeader* TlsfAllocator::TrySplitBlock (TlsfAllocator::FreeBlockNode* node,
104
+ uint64_t & allocatedSize)
100
105
{
101
106
if (!node) return nullptr ;
102
107
@@ -106,8 +111,8 @@ TlsfAllocator::BlockHeader* TlsfAllocator::TrySplitBlock(TlsfAllocator::FreeBloc
106
111
107
112
if (!RemoveFreeBlock (node)) return nullptr ;
108
113
109
- if (oldSize <= allocatedSize || ((oldSize - allocatedSize) <
110
- (HEADER_SIZE + FREE_BLOCK_SIZE + FOOTER_SIZE)))
114
+ if (oldSize <= allocatedSize ||
115
+ ((oldSize - allocatedSize) < (HEADER_SIZE + FREE_BLOCK_SIZE + FOOTER_SIZE)))
111
116
{
112
117
allocatedSize = oldSize;
113
118
return header;
@@ -122,7 +127,7 @@ TlsfAllocator::BlockHeader* TlsfAllocator::TrySplitBlock(TlsfAllocator::FreeBloc
122
127
123
128
void TlsfAllocator::AddNewBlock (const uint64_t size, BlockHeader* header)
124
129
{
125
- uint64_t fl = 0 ,sl = 0 , index;
130
+ uint64_t fl = 0 , sl = 0 , index;
126
131
index = CalculateFreeBlockIndices (size, fl, sl);
127
132
128
133
CreateHeader (TO_BYTES (header), size, FREE);
@@ -209,14 +214,15 @@ const uint64_t TlsfAllocator::GetNextFreeSlotIndex(uint64_t& fl, uint64_t& sl)
209
214
210
215
fl = __builtin_ctzll (fl);
211
216
CC_ASSERT (slBitmasks[fl] > 0 ,
212
- " SlBitmasks is returning 0. This should not be happening and indicates an implementation error." )
217
+ " SlBitmasks is returning 0. This should not be happening and indicates an "
218
+ " implementation error." )
213
219
214
220
sl = __builtin_ctz (slBitmasks[fl]);
215
221
216
222
return fl * MAX_SL_BUCKETS + sl;
217
223
}
218
224
219
- uint64_t TlsfAllocator::CalculateFreeBlockIndices (uint64_t size, OUT uint64_t & fl, OUT uint64_t & sl)
225
+ uint64_t TlsfAllocator::CalculateFreeBlockIndices (uint64_t size, OUT uint64_t & fl, OUT uint64_t & sl)
220
226
{
221
227
uint64_t rawFl = FL (size);
222
228
fl = rawFl - FL_MIN_INDEX;
@@ -253,7 +259,7 @@ TlsfAllocator::BlockHeader* TlsfAllocator::GetPrevHeader(TlsfAllocator::BlockHea
253
259
uint8_t * rawPrevFooter = TO_BYTES (GetPrevFooter (header));
254
260
if (!IsValid (rawPrevFooter)) return nullptr ;
255
261
uint8_t * prevHeader = (rawPrevFooter - TO_FOOTER (rawPrevFooter)->totalBlockSize ) + FOOTER_SIZE;
256
- if (!IsValid (prevHeader)) return nullptr ;
262
+ if (!IsValid (prevHeader)) return nullptr ;
257
263
return TO_HEADER (prevHeader);
258
264
}
259
265
@@ -347,8 +353,14 @@ bool TlsfAllocator::IsValid(uint8_t* ptr)
347
353
return ptr && ptr >= data && ptr < (data + (capacity + HEADER_SIZE + FOOTER_SIZE));
348
354
}
349
355
350
- const uint64_t TlsfAllocator::Capacity () { return capacity; }
356
+ const uint64_t TlsfAllocator::Capacity ()
357
+ {
358
+ return capacity;
359
+ }
351
360
352
- const uint64_t TlsfAllocator::BytesRemaining () { return bytesRemaining; }
361
+ const uint64_t TlsfAllocator::BytesRemaining ()
362
+ {
363
+ return bytesRemaining;
364
+ }
353
365
354
366
} // namespace Siege
0 commit comments