diff --git a/shared/source/utilities/heap_allocator.h b/shared/source/utilities/heap_allocator.h index 948612be3a..7d7496d041 100644 --- a/shared/source/utilities/heap_allocator.h +++ b/shared/source/utilities/heap_allocator.h @@ -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; diff --git a/shared/test/unit_test/utilities/heap_allocator_tests.cpp b/shared/test/unit_test/utilities/heap_allocator_tests.cpp index e4254b2b95..f70d90423b 100644 --- a/shared/test/unit_test/utilities/heap_allocator_tests.cpp +++ b/shared/test/unit_test/utilities/heap_allocator_tests.cpp @@ -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;