Skip to content

Commit e918a04

Browse files
committed
refactor: apply const-correctness to member functions
1 parent b3ecdcb commit e918a04

2 files changed

Lines changed: 15 additions & 23 deletions

File tree

include/tlsf.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ struct TlsfBlock
8888

8989
/*
9090
91+
92+
9193
General user allocatable Block Layout:
9294
+--------------------------------------------------------------------------------------------------------------------+
9395
| Padding(if any) | TlsfBlockHeader | User Area | Padding (if any) | TlsfBlockFooter |
@@ -185,13 +187,13 @@ class TlsfAllocator
185187
//this method assumes that (rawEndAddress - FOOTER_SIZE) is aligned for footer else we will have problem finding footer and header
186188
TlsfBlockHeader* createTlsfBlock(const size_t rawStartAddress, const size_t rawEndAddress) const;
187189

188-
bool checkIfSecondLevelEmpty(size_t firstLevelIndex) const;
190+
bool checkIfSecondLevelEmpty(const size_t firstLevelIndex) const;
189191

190-
LeastSetBitIndexResult getLeastSetBitIndex(size_t bitmap) const;
192+
LeastSetBitIndexResult getLeastSetBitIndex(const size_t bitmap) const;
191193

192-
TwoLevelIndex getTwoLevelIndex(size_t size) const;
194+
TwoLevelIndex getTwoLevelIndex(const size_t size) const;
193195

194-
TwoLevelIndex getTwoLevelIndexWithFreeBlock(size_t size) const;
196+
TwoLevelIndex getTwoLevelIndexWithFreeBlock(const size_t size) const;
195197

196198

197199

@@ -220,6 +222,6 @@ class TlsfAllocator
220222

221223
~TlsfAllocator();
222224

223-
void* allocate(size_t size);
225+
void* allocate(const size_t size);
224226
void deallocate(void* ptr);
225227
};

src/tlsf.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,18 @@
77
#include <cassert>
88

99

10-
bool TlsfAllocator::checkIfSecondLevelEmpty(size_t firstLevelIndex) const
10+
bool TlsfAllocator::checkIfSecondLevelEmpty(const size_t firstLevelIndex) const
1111
{
1212

13-
// To check if Second Level Bitmap of a particular First level is empty
14-
//TODO : Write code just to check if its zero no need for calling getLeastSetBitIndex()
15-
1613

1714

1815
if (firstLevelIndex >= sizeof(m_firstLevelBitmap) * 8) //bits
1916
{
2017
return true;//returning true because index is out of bounds
2118
}
2219

23-
const LeastSetBitIndexResult leastSetBitIndexResult = getLeastSetBitIndex(m_secondLevelBitmap[firstLevelIndex]);
24-
25-
if(!leastSetBitIndexResult.found )
26-
{
27-
return true;
28-
}
29-
30-
return false;
3120

21+
return m_secondLevelBitmap[firstLevelIndex] == 0;
3222

3323

3424
}
@@ -44,7 +34,7 @@ bool TlsfAllocator::checkIfSecondLevelEmpty(size_t firstLevelIndex) const
4434

4535

4636

47-
TlsfAllocator::LeastSetBitIndexResult TlsfAllocator::getLeastSetBitIndex(size_t bitmap) const
37+
TlsfAllocator::LeastSetBitIndexResult TlsfAllocator::getLeastSetBitIndex(const size_t bitmap) const
4838
{
4939

5040

@@ -80,6 +70,7 @@ TlsfAllocator::LeastSetBitIndexResult TlsfAllocator::getLeastSetBitIndex(size_t
8070

8171
#else
8272
//fallback to manual search
73+
//rewrite to use binary search??
8374
setIndex = 0;
8475
for(size_t i = 0; i < sizeof(bitmap) * 8; ++i)
8576
{
@@ -103,7 +94,7 @@ TlsfAllocator::LeastSetBitIndexResult TlsfAllocator::getLeastSetBitIndex(size_t
10394

10495

10596

106-
TwoLevelIndex TlsfAllocator::getTwoLevelIndex(size_t size) const
97+
TwoLevelIndex TlsfAllocator::getTwoLevelIndex(const size_t size) const
10798
{
10899
TwoLevelIndex index;
109100

@@ -190,8 +181,7 @@ if(!intrinsicFound)
190181

191182

192183

193-
TwoLevelIndex TlsfAllocator::getTwoLevelIndexWithFreeBlock(size_t size) const
194-
184+
TwoLevelIndex TlsfAllocator::getTwoLevelIndexWithFreeBlock(const size_t size) const
195185
{
196186
TwoLevelIndex twoLevelIndex = getTwoLevelIndex(size);
197187

@@ -892,10 +882,10 @@ TlsfAllocator::~TlsfAllocator()
892882

893883

894884

895-
void* TlsfAllocator::allocate(size_t size)
885+
void* TlsfAllocator::allocate(const size_t size)
896886
{
897887

898-
888+
assert(size != 0);
899889

900890
TlsfBlockHeader* allocatedBlock = getFreeBlock(size);
901891

0 commit comments

Comments
 (0)