diff --git a/src/backend/CommandAllocator.cpp b/src/backend/CommandAllocator.cpp index 53524fdc23..e40af002b8 100644 --- a/src/backend/CommandAllocator.cpp +++ b/src/backend/CommandAllocator.cpp @@ -88,7 +88,7 @@ namespace backend { blocks[0].size = sizeof(endOfBlock); blocks[0].block = currentPtr; } else { - currentPtr = Align(blocks[0].block, alignof(uint32_t)); + currentPtr = AlignPtr(blocks[0].block, alignof(uint32_t)); } } @@ -101,7 +101,7 @@ namespace backend { } bool CommandIterator::NextCommandId(uint32_t* commandId) { - uint8_t* idPtr = Align(currentPtr, alignof(uint32_t)); + uint8_t* idPtr = AlignPtr(currentPtr, alignof(uint32_t)); ASSERT(idPtr + sizeof(uint32_t) <= blocks[currentBlock].block + blocks[currentBlock].size); uint32_t id = *reinterpret_cast(idPtr); @@ -112,7 +112,7 @@ namespace backend { Reset(); return false; } - currentPtr = Align(blocks[currentBlock].block, alignof(uint32_t)); + currentPtr = AlignPtr(blocks[currentBlock].block, alignof(uint32_t)); return NextCommandId(commandId); } @@ -122,7 +122,7 @@ namespace backend { } void* CommandIterator::NextCommand(size_t commandSize, size_t commandAlignment) { - uint8_t* commandPtr = Align(currentPtr, commandAlignment); + uint8_t* commandPtr = AlignPtr(currentPtr, commandAlignment); ASSERT(commandPtr + sizeof(commandSize) <= blocks[currentBlock].block + blocks[currentBlock].size); currentPtr = commandPtr + commandSize; @@ -173,8 +173,8 @@ namespace backend { ASSERT(currentPtr + sizeof(uint32_t) <= endPtr); uint32_t* idAlloc = reinterpret_cast(currentPtr); - uint8_t* commandAlloc = Align(currentPtr + sizeof(uint32_t), commandAlignment); - uint8_t* nextPtr = Align(commandAlloc + commandSize, alignof(uint32_t)); + uint8_t* commandAlloc = AlignPtr(currentPtr + sizeof(uint32_t), commandAlignment); + uint8_t* nextPtr = AlignPtr(commandAlloc + commandSize, alignof(uint32_t)); // When there is not enough space, we signal the EndOfBlock, so that the iterator nows to // move to the next one. EndOfBlock on the last block means the end of the commands. @@ -211,7 +211,7 @@ namespace backend { } blocks.push_back({lastAllocationSize, block}); - currentPtr = Align(block, alignof(uint32_t)); + currentPtr = AlignPtr(block, alignof(uint32_t)); endPtr = block + lastAllocationSize; return true; } diff --git a/src/common/Math.cpp b/src/common/Math.cpp index 6c542c7f63..ce4d41a190 100644 --- a/src/common/Math.cpp +++ b/src/common/Math.cpp @@ -56,6 +56,7 @@ bool IsAligned(const void* ptr, size_t alignment) { } void* AlignVoidPtr(void* ptr, size_t alignment) { + ASSERT(IsPowerOfTwo(alignment)); ASSERT(alignment != 0); return reinterpret_cast((reinterpret_cast(ptr) + (alignment - 1)) & ~(alignment - 1)); } diff --git a/src/common/Math.h b/src/common/Math.h index fe5ce1accd..e380dca5c9 100644 --- a/src/common/Math.h +++ b/src/common/Math.h @@ -27,12 +27,12 @@ bool IsAligned(const void* ptr, size_t alignment); void* AlignVoidPtr(void* ptr, size_t alignment); template -T* Align(T* ptr, size_t alignment) { +T* AlignPtr(T* ptr, size_t alignment) { return reinterpret_cast(AlignVoidPtr(ptr, alignment)); } template -const T* Align(const T* ptr, size_t alignment) { +const T* AlignPtr(const T* ptr, size_t alignment) { return reinterpret_cast(AlignVoidPtr(const_cast(ptr), alignment)); } diff --git a/src/tests/unittests/MathTests.cpp b/src/tests/unittests/MathTests.cpp index ae4c7838a8..e714729cd6 100644 --- a/src/tests/unittests/MathTests.cpp +++ b/src/tests/unittests/MathTests.cpp @@ -52,15 +52,15 @@ TEST(Math, IsPowerOfTwo) { ASSERT_FALSE(IsPowerOfTwo(0x8000400)); } -// Tests for Align -TEST(Math, Align) { +// Tests for AlignPtr +TEST(Math, AlignPtr) { constexpr size_t kTestAlignment = 8; char buffer[kTestAlignment * 4]; for (size_t i = 0; i < 2 * kTestAlignment; ++i) { char* unaligned = &buffer[i]; - char* aligned = Align(unaligned, kTestAlignment); + char* aligned = AlignPtr(unaligned, kTestAlignment); ASSERT_GE(aligned - unaligned, 0); ASSERT_LT(static_cast(aligned - unaligned), kTestAlignment); @@ -76,7 +76,7 @@ TEST(Math, IsAligned) { for (size_t i = 0; i < 2 * kTestAlignment; ++i) { char* unaligned = &buffer[i]; - char* aligned = Align(unaligned, kTestAlignment); + char* aligned = AlignPtr(unaligned, kTestAlignment); ASSERT_EQ(IsAligned(unaligned, kTestAlignment), unaligned == aligned); }