Skip to content

Commit 6259a82

Browse files
committed
Ran format
1 parent aa47b75 commit 6259a82

File tree

4 files changed

+120
-78
lines changed

4 files changed

+120
-78
lines changed

engine/utils/TlsfAllocator.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
//
99

1010
#include "TlsfAllocator.h"
11-
#include "Logging.h"
1211

1312
#include <memory>
1413

14+
#include "Logging.h"
15+
1516
#define TO_BYTES(val) reinterpret_cast<uint8_t*>(val)
1617
#define TO_HEADER(val) reinterpret_cast<BlockHeader*>(val)
1718
#define TO_FOOTER(val) reinterpret_cast<BlockFooter*>(val)
@@ -39,7 +40,8 @@ TlsfAllocator::TlsfAllocator() {}
3940
TlsfAllocator::TlsfAllocator(const uint64_t size)
4041
{
4142
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)
4345
return;
4446

4547
uint64_t maxBuckets = (FL(size) - FL_MIN_INDEX) + 1;
@@ -57,18 +59,19 @@ TlsfAllocator::TlsfAllocator(const uint64_t size)
5759

5860
data = TO_BYTES(calloc(1, allocSize));
5961

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.")
6165

62-
freeList = (FreeBlockNode**)(data + paddedSize);
63-
slBitmasks = (uint16_t*)(data + paddedSize + freeListSize);
66+
freeList = (FreeBlockNode**) (data + paddedSize);
67+
slBitmasks = (uint16_t*) (data + paddedSize + freeListSize);
6468

6569
CreateHeader(data, paddedSize, FREE);
6670

6771
AddNewBlock(paddedSize, TO_HEADER(data));
6872
}
6973

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)
7275
{
7376
if (!ptr) return;
7477
BlockHeader* header = TO_HEADER(ptr);
@@ -78,8 +81,9 @@ void TlsfAllocator::CreateHeader(uint8_t* ptr, const uint64_t size,
7881
void* TlsfAllocator::Allocate(const uint64_t& size)
7982
{
8083
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;
8387

8488
FreeBlockNode* block = FindFreeBlock(requiredSize);
8589

@@ -96,7 +100,8 @@ void* TlsfAllocator::Allocate(const uint64_t& size)
96100
return ptr;
97101
}
98102

99-
TlsfAllocator::BlockHeader* TlsfAllocator::TrySplitBlock(TlsfAllocator::FreeBlockNode* node, uint64_t& allocatedSize)
103+
TlsfAllocator::BlockHeader* TlsfAllocator::TrySplitBlock(TlsfAllocator::FreeBlockNode* node,
104+
uint64_t& allocatedSize)
100105
{
101106
if (!node) return nullptr;
102107

@@ -106,8 +111,8 @@ TlsfAllocator::BlockHeader* TlsfAllocator::TrySplitBlock(TlsfAllocator::FreeBloc
106111

107112
if (!RemoveFreeBlock(node)) return nullptr;
108113

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)))
111116
{
112117
allocatedSize = oldSize;
113118
return header;
@@ -122,7 +127,7 @@ TlsfAllocator::BlockHeader* TlsfAllocator::TrySplitBlock(TlsfAllocator::FreeBloc
122127

123128
void TlsfAllocator::AddNewBlock(const uint64_t size, BlockHeader* header)
124129
{
125-
uint64_t fl = 0,sl = 0, index;
130+
uint64_t fl = 0, sl = 0, index;
126131
index = CalculateFreeBlockIndices(size, fl, sl);
127132

128133
CreateHeader(TO_BYTES(header), size, FREE);
@@ -209,14 +214,15 @@ const uint64_t TlsfAllocator::GetNextFreeSlotIndex(uint64_t& fl, uint64_t& sl)
209214

210215
fl = __builtin_ctzll(fl);
211216
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.")
213219

214220
sl = __builtin_ctz(slBitmasks[fl]);
215221

216222
return fl * MAX_SL_BUCKETS + sl;
217223
}
218224

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)
220226
{
221227
uint64_t rawFl = FL(size);
222228
fl = rawFl - FL_MIN_INDEX;
@@ -253,7 +259,7 @@ TlsfAllocator::BlockHeader* TlsfAllocator::GetPrevHeader(TlsfAllocator::BlockHea
253259
uint8_t* rawPrevFooter = TO_BYTES(GetPrevFooter(header));
254260
if (!IsValid(rawPrevFooter)) return nullptr;
255261
uint8_t* prevHeader = (rawPrevFooter - TO_FOOTER(rawPrevFooter)->totalBlockSize) + FOOTER_SIZE;
256-
if (!IsValid(prevHeader)) return nullptr;
262+
if (!IsValid(prevHeader)) return nullptr;
257263
return TO_HEADER(prevHeader);
258264
}
259265

@@ -347,8 +353,14 @@ bool TlsfAllocator::IsValid(uint8_t* ptr)
347353
return ptr && ptr >= data && ptr < (data + (capacity + HEADER_SIZE + FOOTER_SIZE));
348354
}
349355

350-
const uint64_t TlsfAllocator::Capacity() { return capacity; }
356+
const uint64_t TlsfAllocator::Capacity()
357+
{
358+
return capacity;
359+
}
351360

352-
const uint64_t TlsfAllocator::BytesRemaining() { return bytesRemaining; }
361+
const uint64_t TlsfAllocator::BytesRemaining()
362+
{
363+
return bytesRemaining;
364+
}
353365

354366
} // namespace Siege

engine/utils/TlsfAllocator.h

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define SIEGE_ENGINE_TLSFALLOCATOR_H
1212

1313
#include <cstdint>
14+
1415
#include "Macros.h"
1516

1617
namespace Siege
@@ -19,6 +20,7 @@ namespace Siege
1920
class TlsfAllocator
2021
{
2122
public:
23+
2224
enum HeaderFlags
2325
{
2426
FULL = 0,
@@ -32,7 +34,8 @@ class TlsfAllocator
3234
FreeBlockNode* prev {nullptr};
3335
};
3436

35-
struct BlockHeader {
37+
struct BlockHeader
38+
{
3639
uint32_t sizeAndFlags {0};
3740
};
3841

@@ -62,7 +65,7 @@ class TlsfAllocator
6265
bool IsFree(BlockHeader* header);
6366
bool IsFree(uint64_t fl, uint64_t sl);
6467
bool PrevBlockIsFree(BlockHeader* header);
65-
uint64_t CalculateFreeBlockIndices(uint64_t size, OUT uint64_t & fl, OUT uint64_t & sl);
68+
uint64_t CalculateFreeBlockIndices(uint64_t size, OUT uint64_t& fl, OUT uint64_t& sl);
6669

6770
// Buffer manipulation Functions
6871

@@ -83,7 +86,7 @@ class TlsfAllocator
8386
template<typename T>
8487
void Deallocate(T*& ptr)
8588
{
86-
uint8_t* raw = (uint8_t*)ptr;
89+
uint8_t* raw = (uint8_t*) ptr;
8790
if (!raw) return;
8891
if (raw < data || raw >= (data + capacity)) return;
8992

@@ -102,7 +105,7 @@ class TlsfAllocator
102105

103106
BlockHeader* nextHeader = GetNextHeader(header);
104107

105-
if (nextHeader && ((uint8_t*)nextHeader < (data + capacity)))
108+
if (nextHeader && ((uint8_t*) nextHeader < (data + capacity)))
106109
{
107110
nextHeader->sizeAndFlags |= PREV_IS_FREE;
108111
}
@@ -125,12 +128,29 @@ class TlsfAllocator
125128
const uint64_t GetHeaderSize(BlockHeader* header);
126129
const uint64_t Capacity();
127130
const uint64_t BytesRemaining();
128-
const uint64_t TotalBytesRemaining() { return totalBytesRemaining; }
129-
const uint64_t TotalSize() { return totalSize; }
130-
const uint8_t* Data() { return data; }
131-
const uint64_t FlBitmask() { return flBitmask; }
132-
const uint16_t& SlBitmask(const uint64_t fl) { return slBitmasks[fl]; }
131+
const uint64_t TotalBytesRemaining()
132+
{
133+
return totalBytesRemaining;
134+
}
135+
const uint64_t TotalSize()
136+
{
137+
return totalSize;
138+
}
139+
const uint8_t* Data()
140+
{
141+
return data;
142+
}
143+
const uint64_t FlBitmask()
144+
{
145+
return flBitmask;
146+
}
147+
const uint16_t& SlBitmask(const uint64_t fl)
148+
{
149+
return slBitmasks[fl];
150+
}
151+
133152
private:
153+
134154
uint64_t totalSize {0};
135155
uint64_t totalBytesRemaining {0};
136156

tests/src/resources/test_ResourceSystem.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <resources/StaticMeshData.h>
1717
#include <resources/Texture2DData.h>
1818
#include <utest.h>
19+
1920
#include <random>
2021

2122
using namespace Siege;

0 commit comments

Comments
 (0)