fix: HeapAllocator - ensure getBaseAddress returns initial base address

getBaseAddress was incorrectly returning pLeftBound which changes after
memory allocation.
Added baseAddress field to store and return initial address value.

Signed-off-by: Fabian Zwoliński <fabian.zwolinski@intel.com>
This commit is contained in:
Fabian Zwoliński
2024-12-10 09:23:33 +00:00
committed by Compute-Runtime-Automation
parent 7f412c77a2
commit 5f8e761541
2 changed files with 13 additions and 7 deletions

View File

@@ -31,7 +31,7 @@ class HeapAllocator {
HeapAllocator(uint64_t address, uint64_t size, size_t allocationAlignment) : HeapAllocator(address, size, allocationAlignment, 4 * MemoryConstants::megaByte) {
}
HeapAllocator(uint64_t address, uint64_t size, size_t allocationAlignment, size_t threshold) : size(size), availableSize(size), allocationAlignment(allocationAlignment), sizeThreshold(threshold) {
HeapAllocator(uint64_t address, uint64_t size, size_t allocationAlignment, size_t threshold) : baseAddress(address), size(size), availableSize(size), allocationAlignment(allocationAlignment), sizeThreshold(threshold) {
pLeftBound = address;
pRightBound = address + size;
freedChunksBig.reserve(10);
@@ -59,10 +59,11 @@ class HeapAllocator {
double getUsage() const;
uint64_t getBaseAddress() const {
return this->pLeftBound;
return this->baseAddress;
}
protected:
const uint64_t baseAddress;
const uint64_t size;
uint64_t availableSize;
uint64_t pLeftBound;

View File

@@ -1438,14 +1438,19 @@ TEST(HeapAllocatorTest, givenZeroAlignmentPassedWhenAllocatingMemoryWithCustomAl
EXPECT_EQ(alignUp(heapBase, allocationAlignment), ptr);
}
TEST(HeapAllocatorTest, whenGetBaseAddressIsCalledThenReturnInitialLeftBoundAddress) {
TEST(HeapAllocatorTest, whenGetBaseAddressIsCalledThenReturnInitialBaseAddress) {
const uint64_t heapBase = 0x100000llu;
const size_t heapSize = 1024 * 4096;
HeapAllocatorUnderTest heapAllocator(heapBase, heapSize, allocationAlignment, sizeThreshold);
const size_t heapSize = 16 * MemoryConstants::megaByte;
const size_t sizeThreshold = 4 * MemoryConstants::megaByte;
HeapAllocatorUnderTest heapAllocator(heapBase, heapSize, allocationAlignment, sizeThreshold);
EXPECT_EQ(heapBase, heapAllocator.getBaseAddress());
size_t sizeToAlloc = 4096;
heapAllocator.allocate(sizeToAlloc);
size_t bigChunk = 5 * MemoryConstants::megaByte;
EXPECT_NE(0u, heapAllocator.allocate(bigChunk));
EXPECT_EQ(heapBase, heapAllocator.getBaseAddress());
size_t smallChunk = 4096;
EXPECT_NE(0u, heapAllocator.allocate(smallChunk));
EXPECT_EQ(heapBase, heapAllocator.getBaseAddress());
}