Rename Align to AlignPtr

This commit is contained in:
Austin Eng 2017-07-14 18:53:07 -04:00 committed by Austin Eng
parent 0bcf0e8e74
commit 8867e5d8df
4 changed files with 14 additions and 13 deletions

View File

@ -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<uint32_t*>(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<uint32_t*>(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;
}

View File

@ -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<void*>((reinterpret_cast<intptr_t>(ptr) + (alignment - 1)) & ~(alignment - 1));
}

View File

@ -27,12 +27,12 @@ bool IsAligned(const void* ptr, size_t alignment);
void* AlignVoidPtr(void* ptr, size_t alignment);
template<typename T>
T* Align(T* ptr, size_t alignment) {
T* AlignPtr(T* ptr, size_t alignment) {
return reinterpret_cast<T*>(AlignVoidPtr(ptr, alignment));
}
template<typename T>
const T* Align(const T* ptr, size_t alignment) {
const T* AlignPtr(const T* ptr, size_t alignment) {
return reinterpret_cast<const T*>(AlignVoidPtr(const_cast<T*>(ptr), alignment));
}

View File

@ -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<size_t>(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);
}