Skip to content

Commit 111fa41

Browse files
amadioSFT
authored andcommitted
Add assertion to ensure that page_size is a power of two
1 parent 4b61a21 commit 111fa41

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib/Utils/PlatformPosix.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ namespace {
5858
}
5959

6060
public:
61-
PointerCheck() {
62-
page_size = ::sysconf(_SC_PAGESIZE);
63-
page_mask = ~(page_size - 1);
61+
PointerCheck() : page_size(::sysconf(_SC_PAGESIZE)), page_mask(~(page_size - 1))
62+
{
63+
assert(IsPowerOfTwo(page_size));
6464
}
6565

6666
bool operator () (const void* P) {
@@ -81,6 +81,13 @@ namespace {
8181
push(P);
8282
return true;
8383
}
84+
private:
85+
bool IsPowerOfTwo(size_t n) {
86+
/* While n is even and larger than 1, divide by 2 */
87+
while (((n & 1) == 0) && n > 1)
88+
n >>= 1;
89+
return n == 1;
90+
}
8491
};
8592
thread_local std::array<const void*, 8> PointerCheck::lines = {};
8693
thread_local unsigned PointerCheck::mostRecent = 0;

0 commit comments

Comments
 (0)