diff --git a/shared/source/utilities/heap_allocator.h b/shared/source/utilities/heap_allocator.h index 669dd6321d..ee2fae6345 100644 --- a/shared/source/utilities/heap_allocator.h +++ b/shared/source/utilities/heap_allocator.h @@ -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; diff --git a/shared/test/unit_test/utilities/heap_allocator_tests.cpp b/shared/test/unit_test/utilities/heap_allocator_tests.cpp index eae44d2d5f..ae572a6472 100644 --- a/shared/test/unit_test/utilities/heap_allocator_tests.cpp +++ b/shared/test/unit_test/utilities/heap_allocator_tests.cpp @@ -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()); } \ No newline at end of file