From 3f59acf54a43fc046f56aad097fe0796f9299de1 Mon Sep 17 00:00:00 2001 From: "Mrozek, Michal" Date: Tue, 27 Mar 2018 16:43:47 +0200 Subject: [PATCH] [19/n] Internal 4GB allocator. - Allocator now uses uint64_t instead of void*. - This is due to the fact that it is required to work on 64 bit addresses in 32 bit dll. Change-Id: Ia715ea7913efc95a2974aff8dff390203d8125a8 --- .../os_agnostic_memory_manager.cpp | 10 +- runtime/os_interface/32bit_memory.h | 4 +- .../os_interface/linux/drm_32bit_memory.cpp | 16 +- .../os_interface/linux/drm_memory_manager.cpp | 19 +- .../windows/wddm_32bit_memory.cpp | 10 +- runtime/utilities/heap_allocator.h | 78 ++-- .../linux/drm_memory_manager_tests.cpp | 24 +- unit_tests/utilities/heap_allocator_tests.cpp | 386 +++++++++--------- 8 files changed, 267 insertions(+), 280 deletions(-) diff --git a/runtime/memory_manager/os_agnostic_memory_manager.cpp b/runtime/memory_manager/os_agnostic_memory_manager.cpp index 1f92a7ffc6..700ed3c8cc 100644 --- a/runtime/memory_manager/os_agnostic_memory_manager.cpp +++ b/runtime/memory_manager/os_agnostic_memory_manager.cpp @@ -75,7 +75,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t return nullptr; } uint64_t offset = static_cast(reinterpret_cast(ptr) & MemoryConstants::pageMask); - MemoryAllocation *memAlloc = new MemoryAllocation(false, reinterpret_cast(ptr), Gmm::canonize(reinterpret_cast(gpuVirtualAddress) + offset), size, counter); + MemoryAllocation *memAlloc = new MemoryAllocation(false, reinterpret_cast(ptr), Gmm::canonize(gpuVirtualAddress + offset), size, counter); memAlloc->is32BitAllocation = true; memAlloc->gpuBaseAddress = Gmm::canonize(allocator32Bit->getBase()); memAlloc->sizeToFree = allocationSize; @@ -89,11 +89,11 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t if (size < 0xfffff000) ptrAlloc = alignedMallocWrapper(allocationSize, MemoryConstants::allocationAlignment); - void *gpuPointer = allocator32Bit->allocate(allocationSize); + auto gpuAddress = allocator32Bit->allocate(allocationSize); MemoryAllocation *memoryAllocation = nullptr; if (ptrAlloc != nullptr) { - memoryAllocation = new MemoryAllocation(true, ptrAlloc, Gmm::canonize(reinterpret_cast(gpuPointer)), size, counter); + memoryAllocation = new MemoryAllocation(true, ptrAlloc, Gmm::canonize(gpuAddress), size, counter); memoryAllocation->is32BitAllocation = true; memoryAllocation->gpuBaseAddress = Gmm::canonize(allocator32Bit->getBase()); memoryAllocation->sizeToFree = allocationSize; @@ -129,8 +129,8 @@ void OsAgnosticMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllo void *ptr = gfxAllocation->getUnderlyingBuffer(); if (gfxAllocation->is32BitAllocation) { - void *gpuPtrToFree = reinterpret_cast(gfxAllocation->getGpuAddress() & ~MemoryConstants::pageMask); - allocator32Bit->free(gpuPtrToFree, static_cast(gfxAllocation)->sizeToFree); + auto gpuAddressToFree = gfxAllocation->getGpuAddress() & ~MemoryConstants::pageMask; + allocator32Bit->free(gpuAddressToFree, static_cast(gfxAllocation)->sizeToFree); } if (gfxAllocation->cpuPtrAllocated) { alignedFreeWrapper(ptr); diff --git a/runtime/os_interface/32bit_memory.h b/runtime/os_interface/32bit_memory.h index eb89edb616..dc7ddb65a9 100644 --- a/runtime/os_interface/32bit_memory.h +++ b/runtime/os_interface/32bit_memory.h @@ -38,9 +38,9 @@ class Allocator32bit { Allocator32bit(); ~Allocator32bit(); - void *allocate(size_t &size); + uint64_t allocate(size_t &size); uintptr_t getBase(); - int free(void *ptr, size_t size); + int free(uint64_t ptr, size_t size); protected: std::unique_ptr osInternals; diff --git a/runtime/os_interface/linux/drm_32bit_memory.cpp b/runtime/os_interface/linux/drm_32bit_memory.cpp index 95cd8fe13c..3f26211d32 100644 --- a/runtime/os_interface/linux/drm_32bit_memory.cpp +++ b/runtime/os_interface/linux/drm_32bit_memory.cpp @@ -96,7 +96,7 @@ bool OCLRT::is32BitOsAllocatorAvailable = true; Allocator32bit::Allocator32bit(uint64_t base, uint64_t size) { this->base = base; this->size = size; - heapAllocator = std::unique_ptr(new HeapAllocator((void *)base, size)); + heapAllocator = std::unique_ptr(new HeapAllocator(base, size)); } OCLRT::Allocator32bit::Allocator32bit() : Allocator32bit(new OsInternals) { @@ -133,7 +133,7 @@ OCLRT::Allocator32bit::Allocator32bit(Allocator32bit::OsInternals *osInternalsIn base = (uint64_t)ptr; size = sizeToMap; - heapAllocator = std::unique_ptr(new HeapAllocator(ptr, sizeToMap)); + heapAllocator = std::unique_ptr(new HeapAllocator(base, sizeToMap)); } else { this->osInternals->drmAllocator = new Allocator32bit::OsInternals::Drm32BitAllocator(*this->osInternals); } @@ -149,24 +149,24 @@ OCLRT::Allocator32bit::~Allocator32bit() { } } -void *OCLRT::Allocator32bit::allocate(size_t &size) { - void *ptr = nullptr; +uint64_t OCLRT::Allocator32bit::allocate(size_t &size) { + uint64_t ptr = 0llu; if (DebugManager.flags.UseNewHeapAllocator.get()) { ptr = this->heapAllocator->allocate(size); } else { - ptr = this->osInternals->drmAllocator->allocate(size); + ptr = reinterpret_cast(this->osInternals->drmAllocator->allocate(size)); } return ptr; } -int Allocator32bit::free(void *ptr, size_t size) { - if ((ptr == MAP_FAILED) || (ptr == nullptr)) +int Allocator32bit::free(uint64_t ptr, size_t size) { + if ((ptr == reinterpret_cast(MAP_FAILED)) || (ptr == 0llu)) return 0; if (DebugManager.flags.UseNewHeapAllocator.get()) { this->heapAllocator->free(ptr, size); } else { - return this->osInternals->drmAllocator->free(ptr, size); + return this->osInternals->drmAllocator->free(reinterpret_cast(ptr), size); } return 0; } diff --git a/runtime/os_interface/linux/drm_memory_manager.cpp b/runtime/os_interface/linux/drm_memory_manager.cpp index 458e2efc45..75ab42228b 100644 --- a/runtime/os_interface/linux/drm_memory_manager.cpp +++ b/runtime/os_interface/linux/drm_memory_manager.cpp @@ -128,11 +128,12 @@ uint32_t DrmMemoryManager::unreference(OCLRT::BufferObject *bo, bool synchronous if (allocatorType == MMAP_ALLOCATOR) { munmapFunction(address, unmapSize); } else { + uint64_t graphicsAddress = static_cast(reinterpret_cast(address)); if (allocatorType == BIT32_ALLOCATOR_EXTERNAL) { - allocator32Bit->free(address, unmapSize); + allocator32Bit->free(graphicsAddress, unmapSize); } else { UNRECOVERABLE_IF(allocatorType != BIT32_ALLOCATOR_INTERNAL) - internal32bitAllocator->free(address, unmapSize); + internal32bitAllocator->free(graphicsAddress, unmapSize); } } @@ -276,7 +277,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemory(size_t size, void * bo->isAllocated = false; bo->setUnmapSize(realAllocationSize); - bo->address = gpuVirtualAddress; + bo->address = reinterpret_cast(gpuVirtualAddress); uintptr_t offset = (uintptr_t)bo->address; bo->softPin((uint64_t)offset); bo->setAllocationType(allocationType); @@ -300,7 +301,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemory(size_t size, void * return nullptr; } - BufferObject *bo = allocUserptr(reinterpret_cast(res), alignedAllocationSize, 0, true); + BufferObject *bo = allocUserptr(res, alignedAllocationSize, 0, true); if (!bo) { allocatorToUse->free(res, allocationSize); @@ -312,7 +313,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemory(size_t size, void * bo->setAllocationType(allocationType); - auto drmAllocation = new DrmAllocation(bo, res, alignedAllocationSize); + auto drmAllocation = new DrmAllocation(bo, reinterpret_cast(res), alignedAllocationSize); drmAllocation->is32BitAllocation = true; drmAllocation->gpuBaseAddress = allocatorToUse->getBase(); return drmAllocation; @@ -338,18 +339,18 @@ BufferObject *DrmMemoryManager::findAndReferenceSharedBufferObject(int boHandle) } BufferObject *DrmMemoryManager::createSharedBufferObject(int boHandle, size_t size, bool requireSpecificBitness) { - void *gpuRange = nullptr; + uint64_t gpuRange = 0llu; StorageAllocatorType storageType = UNKNOWN_ALLOCATOR; if (requireSpecificBitness && this->force32bitAllocations) { gpuRange = this->allocator32Bit->allocate(size); storageType = BIT32_ALLOCATOR_EXTERNAL; } else { - gpuRange = mmapFunction(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); + gpuRange = reinterpret_cast(mmapFunction(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0)); storageType = MMAP_ALLOCATOR; } - DEBUG_BREAK_IF(gpuRange == MAP_FAILED); + DEBUG_BREAK_IF(gpuRange == reinterpret_cast(MAP_FAILED)); auto bo = new (std::nothrow) BufferObject(this->drm, boHandle, true); if (!bo) { @@ -358,7 +359,7 @@ BufferObject *DrmMemoryManager::createSharedBufferObject(int boHandle, size_t si bo->size = size; bo->address = reinterpret_cast(gpuRange); - bo->softPin(reinterpret_cast(gpuRange)); + bo->softPin(gpuRange); bo->setUnmapSize(size); bo->setAllocationType(storageType); return bo; diff --git a/runtime/os_interface/windows/wddm_32bit_memory.cpp b/runtime/os_interface/windows/wddm_32bit_memory.cpp index 3416a7f0af..42eae9b76f 100644 --- a/runtime/os_interface/windows/wddm_32bit_memory.cpp +++ b/runtime/os_interface/windows/wddm_32bit_memory.cpp @@ -34,7 +34,7 @@ class Allocator32bit::OsInternals { Allocator32bit::Allocator32bit(uint64_t base, uint64_t size) { this->base = base; this->size = size; - heapAllocator = std::unique_ptr(new HeapAllocator((void *)base, size)); + heapAllocator = std::unique_ptr(new HeapAllocator(base, size)); } OCLRT::Allocator32bit::Allocator32bit() { @@ -43,7 +43,7 @@ OCLRT::Allocator32bit::Allocator32bit() { osInternals = std::unique_ptr(new OsInternals); osInternals.get()->allocatedRange = (void *)((uintptr_t)this->base); - heapAllocator = std::unique_ptr(new HeapAllocator((void *)this->base, sizeToMap)); + heapAllocator = std::unique_ptr(new HeapAllocator(this->base, sizeToMap)); } OCLRT::Allocator32bit::~Allocator32bit() { @@ -52,13 +52,13 @@ OCLRT::Allocator32bit::~Allocator32bit() { } } -void *Allocator32bit::allocate(size_t &size) { +uint64_t Allocator32bit::allocate(size_t &size) { if (size >= 0xfffff000) - return nullptr; + return 0llu; return this->heapAllocator->allocate(size); } -int Allocator32bit::free(void *ptr, size_t size) { +int Allocator32bit::free(uint64_t ptr, size_t size) { this->heapAllocator->free(ptr, size); return 0; } diff --git a/runtime/utilities/heap_allocator.h b/runtime/utilities/heap_allocator.h index e5ec6b6052..455d4f25d4 100644 --- a/runtime/utilities/heap_allocator.h +++ b/runtime/utilities/heap_allocator.h @@ -33,8 +33,8 @@ namespace OCLRT { struct HeapChunk { - HeapChunk(void *ptr, size_t size) : ptr(ptr), size(size) {} - void *ptr; + HeapChunk(uint64_t ptr, size_t size) : ptr(ptr), size(size) {} + uint64_t ptr; size_t size; }; @@ -42,16 +42,16 @@ bool operator<(const HeapChunk &hc1, const HeapChunk &hc2); class HeapAllocator { public: - HeapAllocator(void *address, uint64_t size) : address(address), size(size), availableSize(size), sizeThreshold(defaultSizeThreshold) { - pLeftBound = static_cast(reinterpret_cast(address)); - pRightBound = static_cast(reinterpret_cast(address) + (size_t)size); + HeapAllocator(uint64_t address, uint64_t size) : address(address), size(size), availableSize(size), sizeThreshold(defaultSizeThreshold) { + pLeftBound = address; + pRightBound = address + size; freedChunksBig.reserve(10); freedChunksSmall.reserve(50); } - HeapAllocator(void *address, uint64_t size, size_t threshold) : address(address), size(size), availableSize(size), sizeThreshold(threshold) { - pLeftBound = reinterpret_cast(address); - pRightBound = reinterpret_cast(address) + size; + HeapAllocator(uint64_t address, uint64_t size, size_t threshold) : address(address), size(size), availableSize(size), sizeThreshold(threshold) { + pLeftBound = address; + pRightBound = address + size; freedChunksBig.reserve(10); freedChunksSmall.reserve(50); } @@ -59,40 +59,40 @@ class HeapAllocator { ~HeapAllocator() { } - void *allocate(size_t &sizeToAllocate) { + uint64_t allocate(size_t &sizeToAllocate) { std::lock_guard lock(mtx); sizeToAllocate = alignUp(sizeToAllocate, allocationAlignment); - void *ptrReturn = nullptr; + uint64_t ptrReturn = 0llu; DBG_LOG(PrintDebugMessages, __FUNCTION__, "Allocator usage == ", this->getUsage()); if (availableSize < sizeToAllocate) { - return nullptr; + return 0llu; } std::vector &freedChunks = (sizeToAllocate > sizeThreshold) ? freedChunksBig : freedChunksSmall; size_t sizeOfFreedChunk = 0; uint32_t defragmentCount = 0; - while (ptrReturn == nullptr) { + while (ptrReturn == 0llu) { ptrReturn = getFromFreedChunks(sizeToAllocate, freedChunks, sizeOfFreedChunk); - if (ptrReturn == nullptr) { + if (ptrReturn == 0llu) { if (sizeToAllocate > sizeThreshold) { if (pLeftBound + sizeToAllocate <= pRightBound) { - ptrReturn = reinterpret_cast(pLeftBound); + ptrReturn = pLeftBound; pLeftBound += sizeToAllocate; } } else { if (pRightBound - sizeToAllocate >= pLeftBound) { pRightBound -= sizeToAllocate; - ptrReturn = reinterpret_cast(pRightBound); + ptrReturn = pRightBound; } } } - if (ptrReturn != nullptr) { + if (ptrReturn != 0llu) { if (sizeOfFreedChunk > 0) { availableSize -= sizeOfFreedChunk; sizeToAllocate = sizeOfFreedChunk; @@ -101,7 +101,7 @@ class HeapAllocator { } } - if (ptrReturn == nullptr) { + if (ptrReturn == 0llu) { if (defragmentCount == 1) break; defragment(); @@ -112,10 +112,10 @@ class HeapAllocator { return ptrReturn; } - void free(void *ptr, size_t size) { + void free(uint64_t ptr, size_t size) { std::lock_guard lock(mtx); - uintptr_t ptrIn = reinterpret_cast(ptr); - if (ptrIn == 0u) + auto ptrIn = ptr; + if (ptrIn == 0llu) return; DBG_LOG(PrintDebugMessages, __FUNCTION__, "Allocator usage == ", this->getUsage()); @@ -151,7 +151,7 @@ class HeapAllocator { } protected: - void *address; + uint64_t address; uint64_t size; uint64_t availableSize; uint64_t pLeftBound, pRightBound; @@ -163,7 +163,7 @@ class HeapAllocator { std::vector freedChunksBig; std::mutex mtx; - void *getFromFreedChunks(size_t size, std::vector &freedChunks, size_t &sizeOfFreedChunk) { + uint64_t getFromFreedChunks(size_t size, std::vector &freedChunks, size_t &sizeOfFreedChunk) { size_t elements = freedChunks.size(); size_t bestFitIndex = -1; size_t bestFitSize = 0; @@ -171,7 +171,7 @@ class HeapAllocator { for (size_t i = 0; i < elements; i++) { if (freedChunks[i].size == size) { - void *ptr = freedChunks[i].ptr; + auto ptr = freedChunks[i].ptr; freedChunks.erase(freedChunks.begin() + i); return ptr; } @@ -186,7 +186,7 @@ class HeapAllocator { if (bestFitSize != 0) { if (bestFitSize < (size << 1)) { - void *ptr = freedChunks[bestFitIndex].ptr; + auto ptr = freedChunks[bestFitIndex].ptr; sizeOfFreedChunk = freedChunks[bestFitIndex].size; freedChunks.erase(freedChunks.begin() + bestFitIndex); return ptr; @@ -195,26 +195,26 @@ class HeapAllocator { DEBUG_BREAK_IF(!((size <= sizeThreshold) || ((size > sizeThreshold) && (sizeDelta > sizeThreshold)))); - void *ptr = reinterpret_cast(reinterpret_cast(freedChunks[bestFitIndex].ptr) + sizeDelta); + auto ptr = freedChunks[bestFitIndex].ptr + sizeDelta; freedChunks[bestFitIndex].size = sizeDelta; return ptr; } } - return nullptr; + return 0llu; } - void storeInFreedChunks(void *ptr, size_t size, std::vector &freedChunks) { + void storeInFreedChunks(uint64_t ptr, size_t size, std::vector &freedChunks) { size_t elements = freedChunks.size(); - uintptr_t pLeft = reinterpret_cast(ptr); - uintptr_t pRight = reinterpret_cast(ptr) + size; + uint64_t pLeft = ptr; + uint64_t pRight = ptr + size; bool freedChunkStored = false; for (size_t i = 0; i < elements; i++) { - if (freedChunks[i].ptr == reinterpret_cast(pRight)) { - freedChunks[i].ptr = reinterpret_cast(pLeft); + if (freedChunks[i].ptr == pRight) { + freedChunks[i].ptr = pLeft; freedChunks[i].size += size; freedChunkStored = true; - } else if ((reinterpret_cast(freedChunks[i].ptr) + freedChunks[i].size) == pLeft) { + } else if ((freedChunks[i].ptr + freedChunks[i].size) == pLeft) { freedChunks[i].size += size; freedChunkStored = true; } @@ -234,7 +234,7 @@ class HeapAllocator { size_t maxSizeOfSmallChunks = freedChunksSmall.size(); if (maxSizeOfSmallChunks > 0) { - uintptr_t ptr = reinterpret_cast(freedChunksSmall[maxSizeOfSmallChunks - 1].ptr); + auto ptr = freedChunksSmall[maxSizeOfSmallChunks - 1].ptr; size_t chunkSize = freedChunksSmall[maxSizeOfSmallChunks - 1].size; if (ptr == pRightBound) { pRightBound = ptr + chunkSize; @@ -247,7 +247,7 @@ class HeapAllocator { size_t maxSizeOfBigChunks = freedChunksBig.size(); if (maxSizeOfBigChunks > 0) { - uintptr_t ptr = reinterpret_cast(freedChunksBig[maxSizeOfBigChunks - 1].ptr); + auto ptr = freedChunksBig[maxSizeOfBigChunks - 1].ptr; size_t chunkSize = freedChunksBig[maxSizeOfBigChunks - 1].size; if (ptr == (pLeftBound - chunkSize)) { @@ -263,11 +263,11 @@ class HeapAllocator { std::sort(freedChunksSmall.rbegin(), freedChunksSmall.rend()); size_t maxSize = freedChunksSmall.size(); for (size_t i = maxSize - 1; i > 0; --i) { - uintptr_t ptr = reinterpret_cast(freedChunksSmall[i].ptr); + auto ptr = freedChunksSmall[i].ptr; size_t chunkSize = freedChunksSmall[i].size; - if (reinterpret_cast(freedChunksSmall[i - 1].ptr) == ptr + chunkSize) { - freedChunksSmall[i - 1].ptr = reinterpret_cast(ptr); + if (freedChunksSmall[i - 1].ptr == ptr + chunkSize) { + freedChunksSmall[i - 1].ptr = ptr; freedChunksSmall[i - 1].size += chunkSize; freedChunksSmall.erase(freedChunksSmall.begin() + i); } @@ -279,9 +279,9 @@ class HeapAllocator { size_t maxSize = freedChunksBig.size(); for (size_t i = maxSize - 1; i > 0; --i) { - uintptr_t ptr = reinterpret_cast(freedChunksBig[i].ptr); + auto ptr = freedChunksBig[i].ptr; size_t chunkSize = freedChunksBig[i].size; - if ((reinterpret_cast(freedChunksBig[i - 1].ptr) + freedChunksBig[i - 1].size) == ptr) { + if ((freedChunksBig[i - 1].ptr + freedChunksBig[i - 1].size) == ptr) { freedChunksBig[i - 1].size += chunkSize; freedChunksBig.erase(freedChunksBig.begin() + i); } diff --git a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp index 5952b4c66d..56b0371c3d 100644 --- a/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp +++ b/unit_tests/os_interface/linux/drm_memory_manager_tests.cpp @@ -1131,7 +1131,7 @@ TEST_F(DrmMemoryManagerTest, Given32BitDeviceWithMemoryManagerWhenInternalHeapIs auto allocator = memoryManager->getDrmInternal32BitAllocator(); size_t size = 4 * GB - 4096; auto alloc = allocator->allocate(size); - EXPECT_NE(nullptr, alloc); + EXPECT_NE(0llu, alloc); size_t allocationSize = 4096 * 3; auto graphicsAllocation = memoryManager->createInternalGraphicsAllocation(nullptr, allocationSize); @@ -2041,7 +2041,7 @@ TEST(Allocator32BitUsingHeapAllocator, given32BitAllocatorWhenMMapFailsThenNullp mockAllocator32Bit *mock32BitAllocator = new mockAllocator32Bit(osInternals); size_t size = 100u; auto ptr = mock32BitAllocator->allocate(size); - EXPECT_EQ(nullptr, ptr); + EXPECT_EQ(0llu, ptr); EXPECT_EQ(2u, mmapCallCount); delete mock32BitAllocator; DebugManager.flags.UseNewHeapAllocator.set(flagToRestore); @@ -2059,7 +2059,7 @@ TEST(Allocator32BitUsingHeapAllocator, given32BitAllocatorWhenFirstMMapFailsThen mockAllocator32Bit *mock32BitAllocator = new mockAllocator32Bit(osInternals); size_t size = 100u; auto ptr = mock32BitAllocator->allocate(size); - EXPECT_NE(nullptr, ptr); + EXPECT_NE(0llu, ptr); EXPECT_EQ(2u, mmapCallCount); EXPECT_NE(nullptr, osInternals->heapBasePtr); @@ -2088,7 +2088,7 @@ TEST(DrmAllocator32Bit, freeMapFailedPointer) { mockAllocator32Bit mock32BitAllocator; size_t size = 100u; - int result = mock32BitAllocator.free(MAP_FAILED, size); + int result = mock32BitAllocator.free(reinterpret_cast(MAP_FAILED), size); EXPECT_EQ(0, result); DebugManager.flags.UseNewHeapAllocator.set(flagToRestore); @@ -2100,7 +2100,7 @@ TEST(DrmAllocator32Bit, freeNullPtrPointer) { mockAllocator32Bit mock32BitAllocator; uint32_t size = 100u; - int result = mock32BitAllocator.free(nullptr, size); + int result = mock32BitAllocator.free(0llu, size); EXPECT_EQ(0, result); DebugManager.flags.UseNewHeapAllocator.set(flagToRestore); @@ -2162,12 +2162,12 @@ TEST(DrmAllocator32Bit, given32bitRegionExhaustedWhenTwoAllocationsAreCreatedThe EXPECT_EQ(maxMmap32BitAddress + alignedSize, ptr2); EXPECT_EQ(4u, mmapCallCount); - mock32BitAllocator.free(reinterpret_cast(ptr2), size); + mock32BitAllocator.free(ptr2, size); auto getInternals = mock32BitAllocator.getosInternal(); EXPECT_EQ(ptr2, getInternals->upperRangeAddress); - mock32BitAllocator.free(reinterpret_cast(ptr), size); + mock32BitAllocator.free(ptr, size); EXPECT_EQ(ptr, getInternals->upperRangeAddress); EXPECT_EQ(2u, unmapCallCount); @@ -2190,12 +2190,12 @@ TEST(DrmAllocator32Bit, given32bitRegionAndUpperRegionExhaustedWhenTwoAllocation EXPECT_EQ(lowerRangeStart + alignedSize, ptr2); EXPECT_EQ(6u, mmapCallCount); - mock32BitAllocator.free(reinterpret_cast(ptr2), size); + mock32BitAllocator.free(ptr2, size); auto getInternals = mock32BitAllocator.getosInternal(); EXPECT_EQ(ptr2, getInternals->lowerRangeAddress); - mock32BitAllocator.free(reinterpret_cast(ptr), size); + mock32BitAllocator.free(ptr, size); EXPECT_EQ(ptr, getInternals->lowerRangeAddress); EXPECT_EQ(4u, unmapCallCount); @@ -2214,7 +2214,7 @@ TEST(DrmAllocator32Bit, given32bitAllocatorWithAllHeapsExhaustedWhenAskedForAllo auto ptr = mock32BitAllocator.allocate(size); - EXPECT_EQ(nullptr, ptr); + EXPECT_EQ(0llu, ptr); EXPECT_EQ(3u, mmapCallCount); EXPECT_EQ(2u, unmapCallCount); @@ -2242,7 +2242,7 @@ TEST(DrmAllocator32Bit, givenMapFailedAsInputToFreeFunctionWhenItIsCalledThenUnm DebugManager.flags.UseNewHeapAllocator.set(false); mockAllocator32Bit mock32BitAllocator; - mock32BitAllocator.free(MAP_FAILED, 100u); + mock32BitAllocator.free(reinterpret_cast(MAP_FAILED), 100u); EXPECT_EQ(0u, unmapCallCount); DebugManager.flags.UseNewHeapAllocator.set(flagToRestore); } @@ -2252,7 +2252,7 @@ TEST(DrmAllocator32Bit, givenNullptrAsInputToFreeFunctionWhenItIsCalledThenUnmap DebugManager.flags.UseNewHeapAllocator.set(false); mockAllocator32Bit mock32BitAllocator; - mock32BitAllocator.free(nullptr, 100u); + mock32BitAllocator.free(0llu, 100u); EXPECT_EQ(0u, unmapCallCount); DebugManager.flags.UseNewHeapAllocator.set(flagToRestore); } diff --git a/unit_tests/utilities/heap_allocator_tests.cpp b/unit_tests/utilities/heap_allocator_tests.cpp index 31baa087f0..b8e060814c 100644 --- a/unit_tests/utilities/heap_allocator_tests.cpp +++ b/unit_tests/utilities/heap_allocator_tests.cpp @@ -34,8 +34,8 @@ const size_t sizeThreshold = 16 * 4096; class HeapAllocatorUnderTest : public HeapAllocator { public: - HeapAllocatorUnderTest(void *address, uint64_t size, size_t threshold) : HeapAllocator(address, size, threshold) {} - HeapAllocatorUnderTest(void *address, uint64_t size) : HeapAllocator(address, size) {} + HeapAllocatorUnderTest(uint64_t address, uint64_t size, size_t threshold) : HeapAllocator(address, size, threshold) {} + HeapAllocatorUnderTest(uint64_t address, uint64_t size) : HeapAllocator(address, size) {} uint64_t getLeftBound() { return this->pLeftBound; } uint64_t getRightBound() { return this->pRightBound; } @@ -43,11 +43,11 @@ class HeapAllocatorUnderTest : public HeapAllocator { size_t getThresholdSize() { return this->sizeThreshold; } void defragment() { return HeapAllocator::defragment(); } - void *getFromFreedChunks(size_t size, std::vector &vec) { + uint64_t getFromFreedChunks(size_t size, std::vector &vec) { size_t sizeOfFreedChunk; return HeapAllocator::getFromFreedChunks(size, vec, sizeOfFreedChunk); } - void storeInFreedChunks(void *ptr, size_t size, std::vector &vec) { return HeapAllocator::storeInFreedChunks(ptr, size, vec); } + void storeInFreedChunks(uint64_t ptr, size_t size, std::vector &vec) { return HeapAllocator::storeInFreedChunks(ptr, size, vec); } std::vector &getFreedChunksSmall() { return this->freedChunksSmall; }; std::vector &getFreedChunksBig() { return this->freedChunksBig; }; @@ -57,7 +57,7 @@ class HeapAllocatorUnderTest : public HeapAllocator { }; TEST(HeapAllocatorTest, DefaultCtorHasThresholdSet) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size); EXPECT_NE(0u, heapAllocator->getThresholdSize()); @@ -67,10 +67,10 @@ TEST(HeapAllocatorTest, DefaultCtorHasThresholdSet) { //this test is no longer valid as pt TEST(HeapAllocatorTest, DISABLED_FreeNotAllocatedPointer) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size); - heapAllocator->free(reinterpret_cast(0x123000), size); + heapAllocator->free(0x123000llu, size); EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size()); EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size()); @@ -78,7 +78,7 @@ TEST(HeapAllocatorTest, DISABLED_FreeNotAllocatedPointer) { } TEST(HeapAllocatorTest, StatisticsMethods) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size); EXPECT_EQ(heapAllocator->getavailableSize(), heapAllocator->getLeftSize()); @@ -86,7 +86,7 @@ TEST(HeapAllocatorTest, StatisticsMethods) { EXPECT_EQ((double)0.0, heapAllocator->getUsage()); size_t ptrSize = 4096; - void *ptr = heapAllocator->allocate(ptrSize); + auto ptr = heapAllocator->allocate(ptrSize); EXPECT_EQ(4096u, heapAllocator->getUsedSize()); EXPECT_LT((double)0.0, heapAllocator->getUsage()); @@ -96,16 +96,16 @@ TEST(HeapAllocatorTest, StatisticsMethods) { } TEST(HeapAllocatorTest, GivenExactSizeChunkInFreedChunksWhenGetIsCalledThenChunkIsReturned) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); std::vector freedChunks; - void *ptrFreed = reinterpret_cast(0x101000); + uint64_t ptrFreed = 0x101000llu; size_t sizeFreed = MemoryConstants::pageSize * 2; freedChunks.emplace_back(ptrFreed, sizeFreed); - void *ptrReturned = heapAllocator->getFromFreedChunks(sizeFreed, freedChunks); + auto ptrReturned = heapAllocator->getFromFreedChunks(sizeFreed, freedChunks); EXPECT_EQ(ptrFreed, ptrReturned); // ptr returned is the one that was stored EXPECT_EQ(0u, freedChunks.size()); // entry in freed container is removed @@ -114,56 +114,56 @@ TEST(HeapAllocatorTest, GivenExactSizeChunkInFreedChunksWhenGetIsCalledThenChunk } TEST(HeapAllocatorTest, GivenOnlySmallerSizeChunksInFreedChunksWhenGetIsCalledThenNullptrIsReturned) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); std::vector freedChunks; - freedChunks.emplace_back(reinterpret_cast(0x100000), 4096); - freedChunks.emplace_back(reinterpret_cast(0x101000), 4096); - freedChunks.emplace_back(reinterpret_cast(0x105000), 4096); - freedChunks.emplace_back(reinterpret_cast(0x104000), 4096); - freedChunks.emplace_back(reinterpret_cast(0x102000), 8192); - freedChunks.emplace_back(reinterpret_cast(0x109000), 8192); - freedChunks.emplace_back(reinterpret_cast(0x107000), 4096); + freedChunks.emplace_back(0x100000llu, 4096); + freedChunks.emplace_back(0x101000llu, 4096); + freedChunks.emplace_back(0x105000llu, 4096); + freedChunks.emplace_back(0x104000llu, 4096); + freedChunks.emplace_back(0x102000llu, 8192); + freedChunks.emplace_back(0x109000llu, 8192); + freedChunks.emplace_back(0x107000llu, 4096); EXPECT_EQ(7u, freedChunks.size()); - void *ptrReturned = heapAllocator->getFromFreedChunks(4 * 4096, freedChunks); + auto ptrReturned = heapAllocator->getFromFreedChunks(4 * 4096, freedChunks); - EXPECT_EQ(nullptr, ptrReturned); + EXPECT_EQ(0llu, ptrReturned); EXPECT_EQ(7u, freedChunks.size()); delete heapAllocator; } TEST(HeapAllocatorTest, GivenOnlyBiggerSizeChunksInFreedChunksWhenGetIsCalledThenBestFitChunkIsReturned) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; - uintptr_t pUpperBound = reinterpret_cast(ptrBase) + size; + auto pUpperBound = ptrBase + size; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); std::vector freedChunks; - void *ptrExpected = nullptr; + uint64_t ptrExpected = 0llu; pUpperBound -= 4096; - freedChunks.emplace_back(reinterpret_cast(pUpperBound), 4096); + freedChunks.emplace_back(pUpperBound, 4096); pUpperBound -= 5 * 4096; - freedChunks.emplace_back(reinterpret_cast(pUpperBound), 5 * 4096); + freedChunks.emplace_back(pUpperBound, 5 * 4096); pUpperBound -= 4 * 4096; - freedChunks.emplace_back(reinterpret_cast(pUpperBound), 4 * 4096); - ptrExpected = reinterpret_cast(pUpperBound); + freedChunks.emplace_back(pUpperBound, 4 * 4096); + ptrExpected = pUpperBound; pUpperBound -= 5 * 4096; - freedChunks.emplace_back(reinterpret_cast(pUpperBound), 5 * 4096); + freedChunks.emplace_back(pUpperBound, 5 * 4096); pUpperBound -= 4 * 4096; - freedChunks.emplace_back(reinterpret_cast(pUpperBound), 4 * 4096); + freedChunks.emplace_back(pUpperBound, 4 * 4096); EXPECT_EQ(5u, freedChunks.size()); - void *ptrReturned = heapAllocator->getFromFreedChunks(3 * 4096, freedChunks); + auto ptrReturned = heapAllocator->getFromFreedChunks(3 * 4096, freedChunks); EXPECT_EQ(ptrExpected, ptrReturned); EXPECT_EQ(4u, freedChunks.size()); @@ -172,53 +172,53 @@ TEST(HeapAllocatorTest, GivenOnlyBiggerSizeChunksInFreedChunksWhenGetIsCalledThe } TEST(HeapAllocatorTest, GivenOnlyMoreThanTwiceBiggerSizeChunksInFreedChunksWhenGetIsCalledThenSplittedChunkIsReturned) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; - uintptr_t pLowerBound = reinterpret_cast(ptrBase); + auto pLowerBound = ptrBase; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); std::vector freedChunks; - void *ptrExpected = nullptr; + uint64_t ptrExpected = 0llu; size_t requestedSize = 3 * 4096; - freedChunks.emplace_back(reinterpret_cast(pLowerBound), 4096); + freedChunks.emplace_back(pLowerBound, 4096); pLowerBound += 4096; - freedChunks.emplace_back(reinterpret_cast(pLowerBound), 9 * 4096); + freedChunks.emplace_back(pLowerBound, 9 * 4096); pLowerBound += 9 * 4096; - freedChunks.emplace_back(reinterpret_cast(pLowerBound), 7 * 4096); + freedChunks.emplace_back(pLowerBound, 7 * 4096); size_t deltaSize = 7 * 4096 - requestedSize; - ptrExpected = reinterpret_cast(pLowerBound + deltaSize); + ptrExpected = pLowerBound + deltaSize; EXPECT_EQ(3u, freedChunks.size()); - void *ptrReturned = heapAllocator->getFromFreedChunks(requestedSize, freedChunks); + auto ptrReturned = heapAllocator->getFromFreedChunks(requestedSize, freedChunks); EXPECT_EQ(ptrExpected, ptrReturned); EXPECT_EQ(3u, freedChunks.size()); - EXPECT_EQ(reinterpret_cast(pLowerBound), freedChunks[2].ptr); + EXPECT_EQ(pLowerBound, freedChunks[2].ptr); EXPECT_EQ(deltaSize, freedChunks[2].size); delete heapAllocator; } TEST(HeapAllocatorTest, GivenStoredChunkAdjacentToLeftBoundaryOfIncomingChunkWhenStoreIsCalledThenChunkIsMerged) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; - uintptr_t pLowerBound = reinterpret_cast(ptrBase); + auto pLowerBound = ptrBase; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); std::vector freedChunks; - void *ptrExpected = nullptr; + uint64_t ptrExpected = 0llu; size_t expectedSize = 9 * 4096; - freedChunks.emplace_back(reinterpret_cast(pLowerBound), 4096); + freedChunks.emplace_back(pLowerBound, 4096); pLowerBound += 4096; - freedChunks.emplace_back(reinterpret_cast(pLowerBound), 9 * 4096); - ptrExpected = reinterpret_cast(pLowerBound); + freedChunks.emplace_back(pLowerBound, 9 * 4096); + ptrExpected = pLowerBound; pLowerBound += 9 * 4096; EXPECT_EQ(ptrExpected, freedChunks[1].ptr); @@ -226,7 +226,7 @@ TEST(HeapAllocatorTest, GivenStoredChunkAdjacentToLeftBoundaryOfIncomingChunkWhe EXPECT_EQ(2u, freedChunks.size()); - void *ptrToStore = reinterpret_cast(pLowerBound); + auto ptrToStore = pLowerBound; size_t sizeToStore = 2 * 4096; expectedSize += sizeToStore; @@ -242,26 +242,26 @@ TEST(HeapAllocatorTest, GivenStoredChunkAdjacentToLeftBoundaryOfIncomingChunkWhe } TEST(HeapAllocatorTest, GivenStoredChunkAdjacentToRightBoundaryOfIncomingChunkWhenStoreIsCalledThenChunkIsMerged) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; - uintptr_t pLowerBound = reinterpret_cast(ptrBase); + auto pLowerBound = ptrBase; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); std::vector freedChunks; - void *ptrExpected = nullptr; + uint64_t ptrExpected = 0llu; size_t expectedSize = 9 * 4096; - freedChunks.emplace_back(reinterpret_cast(pLowerBound), 4096); + freedChunks.emplace_back(pLowerBound, 4096); pLowerBound += 4096; pLowerBound += 4096; // space between stored chunk and chunk to store - void *ptrToStore = reinterpret_cast(pLowerBound); + auto ptrToStore = pLowerBound; size_t sizeToStore = 2 * 4096; pLowerBound += sizeToStore; - freedChunks.emplace_back(reinterpret_cast(pLowerBound), 9 * 4096); - ptrExpected = reinterpret_cast(pLowerBound); + freedChunks.emplace_back(pLowerBound, 9 * 4096); + ptrExpected = pLowerBound; EXPECT_EQ(ptrExpected, freedChunks[1].ptr); EXPECT_EQ(expectedSize, freedChunks[1].size); @@ -282,22 +282,22 @@ TEST(HeapAllocatorTest, GivenStoredChunkAdjacentToRightBoundaryOfIncomingChunkWh } TEST(HeapAllocatorTest, GivenStoredChunkNotAdjacentToIncomingChunkWhenStoreIsCalledThenNewFreeChunkIsCreated) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; - uintptr_t pLowerBound = reinterpret_cast(ptrBase); + auto pLowerBound = ptrBase; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); std::vector freedChunks; - freedChunks.emplace_back(reinterpret_cast(pLowerBound), 4096); + freedChunks.emplace_back(pLowerBound, 4096); pLowerBound += 4096; - freedChunks.emplace_back(reinterpret_cast(pLowerBound), 9 * 4096); + freedChunks.emplace_back(pLowerBound, 9 * 4096); pLowerBound += 9 * 4096; pLowerBound += 9 * 4096; - void *ptrToStore = reinterpret_cast(pLowerBound); + uint64_t ptrToStore = pLowerBound; size_t sizeToStore = 4096; EXPECT_EQ(2u, freedChunks.size()); @@ -313,43 +313,41 @@ TEST(HeapAllocatorTest, GivenStoredChunkNotAdjacentToIncomingChunkWhenStoreIsCal } TEST(HeapAllocatorTest, AllocateReturnsPointerAndAddsEntryToMap) { - - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); size_t ptrSize = 4096; - void *ptr = heapAllocator->allocate(ptrSize); + uint64_t ptr = heapAllocator->allocate(ptrSize); - EXPECT_NE(nullptr, ptr); + EXPECT_NE(0llu, ptr); EXPECT_LE(ptrBase, ptr); size_t ptrSize2 = sizeThreshold + 4096; ptr = heapAllocator->allocate(ptrSize2); - EXPECT_NE(nullptr, ptr); + EXPECT_NE(0llu, ptr); EXPECT_LE(ptrBase, ptr); delete heapAllocator; } TEST(HeapAllocatorTest, FreeReclaimsSpaceAndRemovesEntriesFromMap) { - - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024u * 4096u; - uintptr_t pLeftBound = reinterpret_cast(ptrBase); - uintptr_t pRightBound = pLeftBound + size; + auto pLeftBound = ptrBase; + auto pRightBound = pLeftBound + size; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); size_t ptrSize = 4096; - void *ptr = heapAllocator->allocate(ptrSize); + uint64_t ptr = heapAllocator->allocate(ptrSize); - EXPECT_NE(nullptr, ptr); + EXPECT_NE(0llu, ptr); EXPECT_LE(ptrBase, ptr); size_t ptrSize2 = sizeThreshold + 4096; - void *ptr2 = heapAllocator->allocate(ptrSize2); + uint64_t ptr2 = heapAllocator->allocate(ptrSize2); EXPECT_EQ(heapAllocator->getLeftBound(), pLeftBound + sizeThreshold + 4096u); EXPECT_EQ(heapAllocator->getRightBound(), pRightBound - 4096u); @@ -368,8 +366,7 @@ TEST(HeapAllocatorTest, FreeReclaimsSpaceAndRemovesEntriesFromMap) { } TEST(HeapAllocatorTest, AllocateMultiple) { - - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; size_t allocSize = 4096; @@ -378,21 +375,21 @@ TEST(HeapAllocatorTest, AllocateMultiple) { for (uint32_t i = 0u; i < 2u; i++) { HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); doubleAllocSize = allocSize * 2; - uintptr_t pLeftBound = reinterpret_cast(ptrBase); - uintptr_t pRightBound = pLeftBound + size; + auto pLeftBound = ptrBase; + auto pRightBound = pLeftBound + size; - void *ptr1 = heapAllocator->allocate(allocSize); - EXPECT_NE(nullptr, ptr1); + uint64_t ptr1 = heapAllocator->allocate(allocSize); + EXPECT_NE(0llu, ptr1); EXPECT_LE(ptrBase, ptr1); - void *ptr2 = heapAllocator->allocate(allocSize); - EXPECT_NE(nullptr, ptr2); + uint64_t ptr2 = heapAllocator->allocate(allocSize); + EXPECT_NE(0llu, ptr2); - void *ptr3 = heapAllocator->allocate(doubleAllocSize); - EXPECT_NE(nullptr, ptr3); + uint64_t ptr3 = heapAllocator->allocate(doubleAllocSize); + EXPECT_NE(0llu, ptr3); - void *ptr4 = heapAllocator->allocate(allocSize); - EXPECT_NE(nullptr, ptr4); + uint64_t ptr4 = heapAllocator->allocate(allocSize); + EXPECT_NE(0llu, ptr4); EXPECT_NE(ptr1, ptr2); EXPECT_NE(ptr1, ptr3); @@ -416,51 +413,49 @@ TEST(HeapAllocatorTest, AllocateMultiple) { } TEST(HeapAllocatorTest, AllocateWholeSpace) { - - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); size_t ptrSize = 4096; - void *ptr1 = heapAllocator->allocate(ptrSize); - EXPECT_NE(nullptr, ptr1); + uint64_t ptr1 = heapAllocator->allocate(ptrSize); + EXPECT_NE(0llu, ptr1); EXPECT_LE(ptrBase, ptr1); size_t ptrSize2 = 1023 * 4096; - void *ptr2 = heapAllocator->allocate(ptrSize2); - EXPECT_NE(nullptr, ptr2); + uint64_t ptr2 = heapAllocator->allocate(ptrSize2); + EXPECT_NE(0llu, ptr2); EXPECT_EQ(heapAllocator->getLeftBound(), heapAllocator->getRightBound()); EXPECT_EQ(0u, heapAllocator->getavailableSize()); size_t ptrSize3 = 8192; - void *ptr3 = heapAllocator->allocate(ptrSize3); - EXPECT_EQ(nullptr, ptr3); + uint64_t ptr3 = heapAllocator->allocate(ptrSize3); + EXPECT_EQ(0llu, ptr3); delete heapAllocator; } TEST(HeapAllocatorTest, FreeInReverseOrder) { - - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); - uintptr_t pLeftBound = reinterpret_cast(ptrBase); - uintptr_t pRightBound = pLeftBound + size; + auto pLeftBound = ptrBase; + auto pRightBound = pLeftBound + size; size_t ptr1Size = 4096; - void *ptr1 = heapAllocator->allocate(ptr1Size); - EXPECT_NE(nullptr, ptr1); + uint64_t ptr1 = heapAllocator->allocate(ptr1Size); + EXPECT_NE(0llu, ptr1); EXPECT_LE(ptrBase, ptr1); size_t ptrSize2 = sizeThreshold + 4096; - void *ptr2 = heapAllocator->allocate(ptrSize2); - EXPECT_NE(nullptr, ptr2); + uint64_t ptr2 = heapAllocator->allocate(ptrSize2); + EXPECT_NE(0llu, ptr2); size_t ptrSize3 = 8192; - void *ptr3 = heapAllocator->allocate(ptrSize3); - EXPECT_NE(nullptr, ptr3); + uint64_t ptr3 = heapAllocator->allocate(ptrSize3); + EXPECT_NE(0llu, ptr3); heapAllocator->free(ptr3, ptrSize3); heapAllocator->free(ptr2, ptrSize2); @@ -478,47 +473,47 @@ TEST(HeapAllocatorTest, FreeInReverseOrder) { } TEST(HeapAllocatorTest, SizeNotAvailable) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 0; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); size_t ptrSize = 4096; - void *ptr = heapAllocator->allocate(ptrSize); + uint64_t ptr = heapAllocator->allocate(ptrSize); - EXPECT_EQ(nullptr, ptr); + EXPECT_EQ(0llu, ptr); EXPECT_EQ(0u, heapAllocator->getavailableSize()); delete heapAllocator; } TEST(HeapAllocatorTest, SizeAvailableButInsufficient) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 11 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, 3 * 4096); size_t remainingSize = size; // first small succeeds size_t ptrSize = 4096; - void *ptr = heapAllocator->allocate(ptrSize); - EXPECT_NE(nullptr, ptr); + uint64_t ptr = heapAllocator->allocate(ptrSize); + EXPECT_NE(0llu, ptr); // second small suceeds size_t ptrSize1 = 4096; - void *ptr1 = heapAllocator->allocate(ptrSize1); + uint64_t ptr1 = heapAllocator->allocate(ptrSize1); // free first to store on free list heapAllocator->free(ptr, ptrSize); remainingSize -= 4096; - EXPECT_NE(nullptr, ptr1); + EXPECT_NE(0llu, ptr1); EXPECT_EQ(remainingSize, heapAllocator->getavailableSize()); // first big succeeds size_t ptrSize2 = 4 * 4096; - void *ptr2 = heapAllocator->allocate(ptrSize2); - EXPECT_NE(nullptr, ptr2); + uint64_t ptr2 = heapAllocator->allocate(ptrSize2); + EXPECT_NE(0llu, ptr2); // second big succeeds size_t ptrSize3 = 4 * 4096; - void *ptr3 = heapAllocator->allocate(ptrSize3); - EXPECT_NE(nullptr, ptr3); + uint64_t ptr3 = heapAllocator->allocate(ptrSize3); + EXPECT_NE(0llu, ptr3); // free first big to store on free list heapAllocator->free(ptr2, 4 * 4096); remainingSize -= 4 * 4096; @@ -527,21 +522,21 @@ TEST(HeapAllocatorTest, SizeAvailableButInsufficient) { // third small fails size_t ptrSize4 = 2 * 4096; - void *ptr4 = heapAllocator->allocate(ptrSize4); - EXPECT_EQ(nullptr, ptr4); + uint64_t ptr4 = heapAllocator->allocate(ptrSize4); + EXPECT_EQ(0llu, ptr4); // third big fails size_t ptrSize5 = 5 * 4096; - void *ptr5 = heapAllocator->allocate(ptrSize5); - EXPECT_EQ(nullptr, ptr5); + uint64_t ptr5 = heapAllocator->allocate(ptrSize5); + EXPECT_EQ(0llu, ptr5); delete heapAllocator; } TEST(HeapAllocatorTest, FreeNullDoesNothing) { - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, sizeThreshold, sizeThreshold); - heapAllocator->free(nullptr, 0); + heapAllocator->free(0llu, 0); EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size()); EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size()); @@ -550,27 +545,26 @@ TEST(HeapAllocatorTest, FreeNullDoesNothing) { } TEST(HeapAllocatorTest, AllocateAfterFree) { - - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); - uintptr_t pLeftBound = reinterpret_cast(ptrBase); - uintptr_t pRightBound = pLeftBound + size; + auto pLeftBound = ptrBase; + auto pRightBound = pLeftBound + size; size_t ptrSize = 8192; - void *ptr = heapAllocator->allocate(ptrSize); - EXPECT_NE(nullptr, ptr); + uint64_t ptr = heapAllocator->allocate(ptrSize); + EXPECT_NE(0llu, ptr); EXPECT_LE(ptrBase, ptr); size_t ptrSize1 = 8192; - void *ptr1 = heapAllocator->allocate(ptrSize1); - EXPECT_NE(nullptr, ptr1); + uint64_t ptr1 = heapAllocator->allocate(ptrSize1); + EXPECT_NE(0llu, ptr1); EXPECT_LE(ptrBase, ptr1); size_t ptrSize2 = 8192; - void *ptr2 = heapAllocator->allocate(ptrSize2); - EXPECT_NE(nullptr, ptr2); + uint64_t ptr2 = heapAllocator->allocate(ptrSize2); + EXPECT_NE(0llu, ptr2); heapAllocator->free(ptr1, ptrSize1); @@ -578,8 +572,8 @@ TEST(HeapAllocatorTest, AllocateAfterFree) { EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size()); size_t ptrSize3 = 8192; - void *ptr3 = heapAllocator->allocate(ptrSize3); - EXPECT_NE(nullptr, ptr3); + uint64_t ptr3 = heapAllocator->allocate(ptrSize3); + EXPECT_NE(0llu, ptr3); EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size()); EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size()); @@ -603,33 +597,32 @@ TEST(HeapAllocatorTest, AllocateAfterFree) { } TEST(HeapAllocatorTest, AllocateFromFreedBiggerChunk) { - - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); - uintptr_t pLeftBound = reinterpret_cast(ptrBase); - uintptr_t pRightBound = pLeftBound + size; + auto pLeftBound = ptrBase; + auto pRightBound = pLeftBound + size; size_t sizeAllocated = 0; size_t ptrSize = 8192; - void *ptr = heapAllocator->allocate(ptrSize); - EXPECT_NE(nullptr, ptr); + uint64_t ptr = heapAllocator->allocate(ptrSize); + EXPECT_NE(0llu, ptr); EXPECT_LE(ptrBase, ptr); sizeAllocated += 8192; size_t ptrSize1 = 4 * 4096; - void *ptr1 = heapAllocator->allocate(ptrSize1); - EXPECT_NE(nullptr, ptr1); + uint64_t ptr1 = heapAllocator->allocate(ptrSize1); + EXPECT_NE(0llu, ptr1); EXPECT_LE(ptrBase, ptr1); sizeAllocated += 4 * 4096; size_t ptrSize2 = 8192; - void *ptr2 = heapAllocator->allocate(ptrSize2); - EXPECT_NE(nullptr, ptr2); + uint64_t ptr2 = heapAllocator->allocate(ptrSize2); + EXPECT_NE(0llu, ptr2); sizeAllocated += 8192; EXPECT_EQ(size - sizeAllocated, heapAllocator->getavailableSize()); @@ -643,8 +636,8 @@ TEST(HeapAllocatorTest, AllocateFromFreedBiggerChunk) { EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size()); size_t ptrSize3 = 3 * 4096; - void *ptr3 = heapAllocator->allocate(ptrSize3); - EXPECT_NE(nullptr, ptr3); + uint64_t ptr3 = heapAllocator->allocate(ptrSize3); + EXPECT_NE(0llu, ptr3); EXPECT_EQ(0u, heapAllocator->getFreedChunksSmall().size()); EXPECT_EQ(0u, heapAllocator->getFreedChunksBig().size()); @@ -672,41 +665,39 @@ TEST(HeapAllocatorTest, AllocateFromFreedBiggerChunk) { } TEST(HeapAllocatorTest, AllocateWhenNoSpaceForSmallAllocation) { - - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); size_t ptrSize1 = size - 4096; - void *ptr1 = heapAllocator->allocate(ptrSize1); - EXPECT_NE(nullptr, ptr1); + uint64_t ptr1 = heapAllocator->allocate(ptrSize1); + EXPECT_NE(0llu, ptr1); EXPECT_LE(ptrBase, ptr1); EXPECT_EQ(4096u, heapAllocator->getavailableSize()); size_t ptrSize2 = 8192; - void *ptr2 = heapAllocator->allocate(ptrSize2); - EXPECT_EQ(nullptr, ptr2); + uint64_t ptr2 = heapAllocator->allocate(ptrSize2); + EXPECT_EQ(0llu, ptr2); delete heapAllocator; } TEST(HeapAllocatorTest, AllocateWhenNoSpaceForBigAllocation) { - - void *ptrBase = reinterpret_cast(0x100000); + uint64_t ptrBase = 0x100000llu; size_t size = 1024 * 4096; HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(ptrBase, size, sizeThreshold); size_t ptrSize1 = 8192; - void *ptr1 = heapAllocator->allocate(ptrSize1); - EXPECT_NE(nullptr, ptr1); + uint64_t ptr1 = heapAllocator->allocate(ptrSize1); + EXPECT_NE(0llu, ptr1); EXPECT_LE(ptrBase, ptr1); EXPECT_EQ(size - 8192u, heapAllocator->getavailableSize()); size_t ptrSize2 = size - 4096; - void *ptr2 = heapAllocator->allocate(ptrSize2); - EXPECT_EQ(nullptr, ptr2); + uint64_t ptr2 = heapAllocator->allocate(ptrSize2); + EXPECT_EQ(0llu, ptr2); delete heapAllocator; } @@ -716,10 +707,10 @@ TEST(HeapAllocatorTest, AllocationsDoNotOverlap) { const uint32_t maxIndex = 2000; - unique_ptr ptrs(new void *[maxIndex]); + unique_ptr ptrs(new uint64_t[maxIndex]); unique_ptr sizes(new size_t[maxIndex]); - memset(ptrs.get(), 0, sizeof(void *) * maxIndex); + memset(ptrs.get(), 0, sizeof(uint64_t) * maxIndex); memset(sizes.get(), 0, sizeof(size_t) * maxIndex); uint16_t *freeIndexes = new uint16_t[maxIndex]; @@ -740,11 +731,12 @@ TEST(HeapAllocatorTest, AllocationsDoNotOverlap) { delete[] freeIndexes; uint64_t allocatorSize = 1024llu * 1024llu; // 1 MB void *pBasePtr = alignedMalloc(static_cast(allocatorSize), 4096); + uint64_t basePtr = static_cast(reinterpret_cast(pBasePtr)); constexpr size_t reqAlignment = 4; size_t bigAllocationThreshold = (512 + 256) * reqAlignment; memset(pBasePtr, 0, static_cast(allocatorSize)); - HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(pBasePtr, allocatorSize, bigAllocationThreshold); + HeapAllocatorUnderTest *heapAllocator = new HeapAllocatorUnderTest(basePtr, allocatorSize, bigAllocationThreshold); heapAllocator->overrideAlignement(reqAlignment); @@ -755,7 +747,7 @@ TEST(HeapAllocatorTest, AllocationsDoNotOverlap) { sizes[i] = sizeToAllocate; ptrs[i] = heapAllocator->allocate(sizes[i]); - if (ptrs[i] == nullptr) + if (ptrs[i] == 0llu) break; uint8_t *pTemp = reinterpret_cast(ptrs[i]); @@ -765,14 +757,12 @@ TEST(HeapAllocatorTest, AllocationsDoNotOverlap) { } uint32_t indexToFree = indexes[i] % (i * 2 + 1); - //if (indexToFree < i) { - if (ptrs[indexToFree] != nullptr) { - memset(ptrs[indexToFree], 0, sizes[indexToFree]); + if (ptrs[indexToFree]) { + memset(reinterpret_cast(ptrs[indexToFree]), 0, sizes[indexToFree]); heapAllocator->free(ptrs[indexToFree], sizes[indexToFree]); - ptrs[indexToFree] = nullptr; + ptrs[indexToFree] = 0llu; sizes[indexToFree] = 0; } - //} } } @@ -793,22 +783,18 @@ TEST(HeapAllocatorTest, AllocationsDoNotOverlap) { //at this point we should be able to allocate full size size_t totalSize = (size_t)(allocatorSize - reqAlignment); auto finalPtr = heapAllocator->allocate(totalSize); - EXPECT_NE(nullptr, finalPtr); + EXPECT_NE(0llu, finalPtr); heapAllocator->free(finalPtr, totalSize); alignedFree(pBasePtr); - // delete[] indexes; - // delete[] ptrs; - // delete[] sizes; - delete heapAllocator; } TEST(HeapAllocatorTest, defragmentBig) { - void *ptrBase = reinterpret_cast(0x100000); - uintptr_t basePtr = 0x100000; + uint64_t ptrBase = 0x100000llu; + uint64_t basePtr = 0x100000llu; size_t size = 1024 * 4096; size_t threshold = 4096; @@ -823,17 +809,17 @@ TEST(HeapAllocatorTest, defragmentBig) { // 0, 1, 2 - can be merged to one // 6,7,8,10 - can be merged to one - void *ptrs[12]; + uint64_t ptrs[12]; ptrs[0] = heapAllocator->allocate(allocSize); ptrs[1] = heapAllocator->allocate(allocSize); ptrs[2] = heapAllocator->allocate(allocSize); ptrs[3] = heapAllocator->allocate(tripleallocSize); - ptrs[4] = nullptr; - ptrs[5] = nullptr; + ptrs[4] = 0llu; + ptrs[5] = 0llu; ptrs[6] = heapAllocator->allocate(allocSize); ptrs[7] = heapAllocator->allocate(allocSize); ptrs[8] = heapAllocator->allocate(doubleallocSize); - ptrs[9] = nullptr; + ptrs[9] = 0llu; ptrs[10] = heapAllocator->allocate(allocSize); ptrs[11] = heapAllocator->allocate(allocSize); @@ -855,21 +841,21 @@ TEST(HeapAllocatorTest, defragmentBig) { ASSERT_EQ(2u, freedChunks.size()); - EXPECT_EQ(reinterpret_cast(basePtr), freedChunks[0].ptr); + EXPECT_EQ(basePtr, freedChunks[0].ptr); EXPECT_EQ(3 * allocSize, freedChunks[0].size); - EXPECT_EQ(reinterpret_cast(basePtr + 6 * allocSize), freedChunks[1].ptr); + EXPECT_EQ((basePtr + 6 * allocSize), freedChunks[1].ptr); EXPECT_EQ(5 * allocSize, freedChunks[1].size); delete heapAllocator; } TEST(HeapAllocatorTest, defragmentSmall) { - void *ptrBase = reinterpret_cast(0x100000); - uintptr_t basePtr = 0x100000; + uint64_t ptrBase = 0x100000llu; + uint64_t basePtr = 0x100000; size_t size = 1024 * 4096; - uintptr_t upperLimitPtr = basePtr + size; + uint64_t upperLimitPtr = basePtr + size; size_t threshold = 2 * MemoryConstants::pageSize; size_t allocSize = MemoryConstants::pageSize; @@ -882,17 +868,17 @@ TEST(HeapAllocatorTest, defragmentSmall) { // 0, 1, 2 - can be merged to one // 6,7,8,10 - can be merged to one - void *ptrs[12]; + uint64_t ptrs[12]; ptrs[0] = heapAllocator->allocate(allocSize); ptrs[1] = heapAllocator->allocate(allocSize); ptrs[2] = heapAllocator->allocate(allocSize); ptrs[3] = heapAllocator->allocate(doubleallocSize); - ptrs[4] = nullptr; - ptrs[5] = nullptr; + ptrs[4] = 0llu; + ptrs[5] = 0llu; ptrs[6] = heapAllocator->allocate(allocSize); ptrs[7] = heapAllocator->allocate(allocSize); ptrs[8] = heapAllocator->allocate(doubleallocSize); - ptrs[9] = nullptr; + ptrs[9] = 0llu; ptrs[10] = heapAllocator->allocate(allocSize); ptrs[11] = heapAllocator->allocate(allocSize); @@ -914,17 +900,17 @@ TEST(HeapAllocatorTest, defragmentSmall) { ASSERT_EQ(2u, freedChunks.size()); - EXPECT_EQ(reinterpret_cast(upperLimitPtr - 3 * allocSize), freedChunks[0].ptr); + EXPECT_EQ((upperLimitPtr - 3 * allocSize), freedChunks[0].ptr); EXPECT_EQ(3 * allocSize, freedChunks[0].size); - EXPECT_EQ(reinterpret_cast(upperLimitPtr - 10 * allocSize), freedChunks[1].ptr); + EXPECT_EQ((upperLimitPtr - 10 * allocSize), freedChunks[1].ptr); EXPECT_EQ(5 * allocSize, freedChunks[1].size); delete heapAllocator; } TEST(HeapAllocatorTest, Given10SmallAllocationsWhenFreedInTheSameOrderThenLastChunkFreedReturnsWholeSpaceToFreeRange) { - void *ptrBase = reinterpret_cast(0); + uint64_t ptrBase = 0llu; size_t size = 1024 * 4096; size_t threshold = 2 * 4096; @@ -932,7 +918,7 @@ TEST(HeapAllocatorTest, Given10SmallAllocationsWhenFreedInTheSameOrderThenLastCh std::vector &freedChunks = heapAllocator->getFreedChunksSmall(); - void *ptrs[10]; + uint64_t ptrs[10]; size_t sizes[10]; for (uint32_t i = 0; i < 10; i++) { @@ -956,7 +942,7 @@ TEST(HeapAllocatorTest, Given10SmallAllocationsWhenFreedInTheSameOrderThenLastCh } TEST(HeapAllocatorTest, Given10SmallAllocationsWhenMergedToBigAllocatedAsSmallSplittedAndReleasedThenItDoesNotGoToFreedBigChunksList) { - void *ptrBase = reinterpret_cast(0); + uint64_t ptrBase = 0llu; uintptr_t basePtr = 0; // Size for 10 small allocs plus one single 2 page plus some space @@ -970,12 +956,12 @@ TEST(HeapAllocatorTest, Given10SmallAllocationsWhenMergedToBigAllocatedAsSmallSp std::vector &freedChunksSmall = heapAllocator->getFreedChunksSmall(); std::vector &freedChunksBig = heapAllocator->getFreedChunksBig(); - void *ptrs[10]; + uint64_t ptrs[10]; size_t sizes[10]; // size smaller than threshold size_t sizeOfSmallAlloc = 2 * 4096; - void *smallAlloc = nullptr; + uint64_t smallAlloc = 0llu; for (uint32_t i = 0; i < 10; i++) { sizes[i] = 4096; @@ -993,8 +979,8 @@ TEST(HeapAllocatorTest, Given10SmallAllocationsWhenMergedToBigAllocatedAsSmallSp // Allocate small chunk, should be taken from freed list smallAlloc = heapAllocator->allocate(sizeOfSmallAlloc); - EXPECT_NE(nullptr, smallAlloc); - EXPECT_LE(upperLimitPtr - (8 * 4096), reinterpret_cast(smallAlloc)); + EXPECT_NE(0llu, smallAlloc); + EXPECT_LE(upperLimitPtr - (8 * 4096), smallAlloc); EXPECT_EQ(1u, freedChunksSmall.size()); @@ -1019,12 +1005,12 @@ TEST(HeapAllocatorTest, Given10SmallAllocationsWhenMergedToBigAllocatedAsSmallSp } TEST(HeapAllocatorTest, Given10SmallAllocationsWhenMergedToBigAllocatedAsSmallNotSplittedAndReleasedThenItDoesNotGoToFreedBigChunksList) { - void *ptrBase = reinterpret_cast(0); + uint64_t ptrBase = 0llu; uintptr_t basePtr = 0; // Size for 10 small allocs plus one single 3 page plus some space size_t size = (10 + 3 + 1) * 4096; - uintptr_t upperLimitPtr = basePtr + size; + uint64_t upperLimitPtr = basePtr + size; size_t threshold = 4 * 4096; @@ -1033,12 +1019,12 @@ TEST(HeapAllocatorTest, Given10SmallAllocationsWhenMergedToBigAllocatedAsSmallNo std::vector &freedChunksSmall = heapAllocator->getFreedChunksSmall(); std::vector &freedChunksBig = heapAllocator->getFreedChunksBig(); - void *ptrs[10]; + uint64_t ptrs[10]; size_t sizes[10]; // size smaller than threshold size_t sizeOfSmallAlloc = 3 * 4096; - void *smallAlloc = nullptr; + uint64_t smallAlloc = 0llu; for (uint32_t i = 0; i < 10; i++) { sizes[i] = 4096; @@ -1056,8 +1042,8 @@ TEST(HeapAllocatorTest, Given10SmallAllocationsWhenMergedToBigAllocatedAsSmallNo // Allocate small chunk, should be taken from freed list smallAlloc = heapAllocator->allocate(sizeOfSmallAlloc); - EXPECT_NE(nullptr, smallAlloc); - EXPECT_LE(upperLimitPtr - (5 * 4096), reinterpret_cast(smallAlloc)); + EXPECT_NE(0llu, smallAlloc); + EXPECT_LE(upperLimitPtr - (5 * 4096), smallAlloc); EXPECT_EQ(0u, freedChunksSmall.size());