fix: change freed chunk size when ptr is aligned

Signed-off-by: Maciej Plewka <maciej.plewka@intel.com>

Related-To: NEO-9945
This commit is contained in:
Maciej Plewka
2024-01-30 10:42:56 +01:00
committed by Compute-Runtime-Automation
parent fa9c79fb63
commit 620ad5fa89
2 changed files with 29 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2023 Intel Corporation
* Copyright (C) 2019-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -147,11 +147,11 @@ uint64_t HeapAllocator::getFromFreedChunks(size_t size, std::vector<HeapChunk> &
DEBUG_BREAK_IF(!(size <= sizeThreshold || (size > sizeThreshold && sizeDelta > sizeThreshold)));
auto ptr = freedChunks[bestFitIndex].ptr + sizeDelta;
freedChunks[bestFitIndex].size = sizeDelta;
if (!isAligned(ptr, requiredAlignment)) {
return 0llu;
}
freedChunks[bestFitIndex].size = sizeDelta;
return ptr;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2023 Intel Corporation
* Copyright (C) 2018-2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
@@ -200,6 +200,31 @@ TEST(HeapAllocatorTest, GivenMoreThanTwiceBiggerSizeChunksInFreedChunksWhenGetIs
EXPECT_EQ(2u, freedChunks.size());
}
TEST(HeapAllocatorTest, GivenMoreThanTwiceBiggerSizeChunksInFreedChunksWhenGetIsCalledAndAlignmentDoesNotThenChuksSizeDoesNotChange) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;
auto pLowerBound = ptrBase;
auto allocAlign = 8162u;
auto heapAllocator = std::make_unique<HeapAllocatorUnderTest>(ptrBase, size, allocationAlignment, sizeThreshold);
std::vector<HeapChunk> freedChunks;
size_t requestedSize = 2 * 4096;
size_t sizeChunk1 = 9 * 4096;
size_t sizeChunk2 = 3 * 4096;
freedChunks.emplace_back(pLowerBound, sizeChunk1);
pLowerBound += sizeChunk1;
freedChunks.emplace_back(pLowerBound, sizeChunk2);
EXPECT_EQ(2u, freedChunks.size());
heapAllocator->getFromFreedChunks(requestedSize, freedChunks, allocAlign);
EXPECT_EQ(freedChunks[0].size, sizeChunk1);
EXPECT_EQ(freedChunks[1].size, sizeChunk2);
}
TEST(HeapAllocatorTest, GivenStoredChunkAdjacentToLeftBoundaryOfIncomingChunkWhenStoreIsCalledThenChunkIsMerged) {
uint64_t ptrBase = 0x100000llu;
size_t size = 1024 * 4096;