Fix alignment to be based on starting address

Related-To: LOCI-2342
Signed-off-by: Young Jin Yoon <young.jin.yoon@intel.com>
This commit is contained in:
Young Jin Yoon
2021-06-15 00:19:35 +00:00
committed by Compute-Runtime-Automation
parent a481c28e55
commit a1036ecc75
2 changed files with 28 additions and 1 deletions

View File

@ -76,7 +76,8 @@ class HeapAllocator {
pLeftBound += sizeToAllocate;
}
} else {
const uint64_t misalignment = pRightBound - alignDown(pRightBound, alignment);
const uint64_t pStart = pRightBound - sizeToAllocate;
const uint64_t misalignment = pStart - alignDown(pStart, alignment);
if (pLeftBound + sizeToAllocate + misalignment <= pRightBound) {
if (misalignment) {
pRightBound -= misalignment;

View File

@ -1074,6 +1074,32 @@ TEST(HeapAllocatorTest, givenAlignedBoundWhenAllocatingMemoryWithCustomAlignment
EXPECT_EQ(heapSize - 2 * ptrSize, heapAllocator.getavailableSize());
}
TEST(HeapAllocatorTest, givenAlignedBoundWhenAllocatingMemoryWithCustomAlignmentBiggerThanPtrSizeFromRightThenReturnAllocations) {
const uint64_t heapBase = 0x100000llu;
const size_t heapSize = 1024u * 4096u;
HeapAllocatorUnderTest heapAllocator(heapBase, heapSize, allocationAlignment, sizeThreshold);
size_t customAlignment = 8 * MemoryConstants::pageSize;
size_t ptrSize = 8 * MemoryConstants::pageSize;
uint64_t ptr = heapAllocator.allocateWithCustomAlignment(ptrSize, customAlignment);
EXPECT_EQ(heapBase + heapSize - ptrSize, heapAllocator.getRightBound());
EXPECT_EQ(heapBase, heapAllocator.getLeftBound());
EXPECT_EQ(8 * MemoryConstants::pageSize, ptrSize);
EXPECT_EQ(heapBase + heapSize - ptrSize, ptr);
EXPECT_EQ(0u, heapAllocator.getFreedChunksBig().size());
EXPECT_EQ(heapSize - ptrSize, heapAllocator.getavailableSize());
ptrSize = 8 * MemoryConstants::pageSize;
customAlignment = 32 * MemoryConstants::pageSize;
ptr = heapAllocator.allocateWithCustomAlignment(ptrSize, customAlignment);
EXPECT_EQ(heapBase + heapSize - customAlignment, heapAllocator.getRightBound());
EXPECT_EQ(heapBase, heapAllocator.getLeftBound());
EXPECT_EQ(8 * MemoryConstants::pageSize, ptrSize);
EXPECT_EQ(heapBase + heapSize - customAlignment, ptr);
EXPECT_EQ(0u, heapAllocator.getFreedChunksBig().size());
EXPECT_EQ(heapSize - 2 * ptrSize, heapAllocator.getavailableSize());
}
TEST(HeapAllocatorTest, givenUnalignedBoundWhenAllocatingWithCustomAlignmentFromLeftThenAlignBoundBeforeAllocation) {
const uint64_t heapBase = 0x100000llu;
const size_t heapSize = 1024u * 4096u;