diff --git a/runtime/memory_manager/graphics_allocation.h b/runtime/memory_manager/graphics_allocation.h index cf05b57bd8..53820085d3 100644 --- a/runtime/memory_manager/graphics_allocation.h +++ b/runtime/memory_manager/graphics_allocation.h @@ -61,6 +61,8 @@ class GraphicsAllocation : public IDNode { }; virtual ~GraphicsAllocation() = default; + GraphicsAllocation &operator=(const GraphicsAllocation &) = delete; + GraphicsAllocation(const GraphicsAllocation &) = delete; GraphicsAllocation(void *cpuPtrIn, size_t sizeIn) : size(sizeIn), cpuPtr(cpuPtrIn), gpuAddress((uint64_t)cpuPtrIn), diff --git a/runtime/memory_manager/os_agnostic_memory_manager.cpp b/runtime/memory_manager/os_agnostic_memory_manager.cpp index 500e514e1f..269574d835 100644 --- a/runtime/memory_manager/os_agnostic_memory_manager.cpp +++ b/runtime/memory_manager/os_agnostic_memory_manager.cpp @@ -59,7 +59,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemory(size_t size, return nullptr; } memoryAllocation->uncacheable = uncacheable; - allocationMap.insert(std::pair(ptr, *memoryAllocation)); + allocationMap.emplace(ptr, memoryAllocation); } counter++; return memoryAllocation; @@ -82,7 +82,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t memAlloc->gpuBaseAddress = Gmm::canonize(allocator32Bit->getBase()); memAlloc->sizeToFree = allocationSize; - allocationMap.insert(std::pair(const_cast(ptr), *memAlloc)); + allocationMap.emplace(const_cast(ptr), memAlloc); counter++; return memAlloc; } @@ -102,7 +102,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t memoryAllocation->gpuBaseAddress = Gmm::canonize(allocator32Bit->getBase()); memoryAllocation->sizeToFree = allocationSize; memoryAllocation->cpuPtrAllocated = true; - allocationMap.insert(std::pair(ptrAlloc, *memoryAllocation)); + allocationMap.emplace(ptrAlloc, memoryAllocation); } counter++; return memoryAllocation; @@ -139,12 +139,12 @@ void OsAgnosticMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllo auto it = allocationMap.find(ptr); if (it != allocationMap.end()) { - it->second.refCount--; - if (it->second.refCount == 0) { - freeMemory = it->second.cpuPtrAllocated; - is32BitAllocation = it->second.is32BitAllocation; - gpuPtrToFree = reinterpret_cast(it->second.getGpuAddress() & ~MemoryConstants::pageMask); - sizeToFree = it->second.sizeToFree; + it->second->refCount--; + if (it->second->refCount == 0) { + freeMemory = it->second->cpuPtrAllocated; + is32BitAllocation = it->second->is32BitAllocation; + gpuPtrToFree = reinterpret_cast(it->second->getGpuAddress() & ~MemoryConstants::pageMask); + sizeToFree = it->second->sizeToFree; allocationMap.erase(it); } } diff --git a/runtime/memory_manager/os_agnostic_memory_manager.h b/runtime/memory_manager/os_agnostic_memory_manager.h index 474e28b5db..50b1eecce5 100644 --- a/runtime/memory_manager/os_agnostic_memory_manager.h +++ b/runtime/memory_manager/os_agnostic_memory_manager.h @@ -46,7 +46,7 @@ class MemoryAllocation : public GraphicsAllocation { id(count) {} }; -typedef std::map PointerMap; +typedef std::map PointerMap; class OsAgnosticMemoryManager : public MemoryManager { public: diff --git a/runtime/os_interface/windows/wddm_memory_manager.cpp b/runtime/os_interface/windows/wddm_memory_manager.cpp index c2245b922d..cc87de5760 100644 --- a/runtime/os_interface/windows/wddm_memory_manager.cpp +++ b/runtime/os_interface/windows/wddm_memory_manager.cpp @@ -70,80 +70,68 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryForImage(ImageInfo delete gmm; return allocateGraphicsMemory(imgInfo.size, MemoryConstants::preferredAlignment); } - WddmAllocation allocation(nullptr, imgInfo.size, nullptr); - allocation.gmm = gmm; - auto status = WddmMemoryManager::createWddmAllocation(&allocation, MemoryType::EXTERNAL_ALLOCATION); - if (status) { - auto *wddmAllocation = new WddmAllocation(allocation); - return wddmAllocation; - } else { + auto allocation = new WddmAllocation(nullptr, imgInfo.size, nullptr); + allocation->gmm = gmm; + + if (!WddmMemoryManager::createWddmAllocation(allocation, MemoryType::EXTERNAL_ALLOCATION)) { + delete allocation; return nullptr; } + return allocation; } GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin) { size_t sizeAligned = alignUp(size, MemoryConstants::pageSize64k); - bool success = true; Gmm *gmm = nullptr; - WddmAllocation allocation(nullptr, sizeAligned, nullptr, sizeAligned, nullptr); + auto wddmAllocation = new WddmAllocation(nullptr, sizeAligned, nullptr, sizeAligned, nullptr); gmm = Gmm::create(nullptr, sizeAligned, false); + wddmAllocation->gmm = gmm; - while (success) { - allocation.gmm = gmm; - success = wddm->createAllocation64k(&allocation); - - if (!success) - break; - - auto *wddmAllocation = new WddmAllocation(allocation); - auto cpuPtr = lockResource(wddmAllocation); - wddmAllocation->setLocked(true); - - wddmAllocation->setAlignedCpuPtr(cpuPtr); - // 64kb map is not needed - auto status = wddm->mapGpuVirtualAddress(wddmAllocation, cpuPtr, sizeAligned, false, false, false); - DEBUG_BREAK_IF(!status); - wddmAllocation->setCpuPtrAndGpuAddress(cpuPtr, (uint64_t)wddmAllocation->gpuPtr); - - return wddmAllocation; + if (!wddm->createAllocation64k(wddmAllocation)) { + delete gmm; + delete wddmAllocation; + return nullptr; } - delete gmm; - return nullptr; + auto cpuPtr = lockResource(wddmAllocation); + wddmAllocation->setLocked(true); + + wddmAllocation->setAlignedCpuPtr(cpuPtr); + // 64kb map is not needed + auto status = wddm->mapGpuVirtualAddress(wddmAllocation, cpuPtr, sizeAligned, false, false, false); + DEBUG_BREAK_IF(!status); + wddmAllocation->setCpuPtrAndGpuAddress(cpuPtr, (uint64_t)wddmAllocation->gpuPtr); + + return wddmAllocation; } GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) { size_t newAlignment = alignment ? alignUp(alignment, MemoryConstants::pageSize) : MemoryConstants::pageSize; size_t sizeAligned = size ? alignUp(size, MemoryConstants::pageSize) : MemoryConstants::pageSize; void *pSysMem = allocateSystemMemory(sizeAligned, newAlignment); - bool success = true; Gmm *gmm = nullptr; if (pSysMem == nullptr) { return nullptr; } - WddmAllocation allocation(pSysMem, sizeAligned, pSysMem, sizeAligned, nullptr); - allocation.cpuPtrAllocated = true; + auto wddmAllocation = new WddmAllocation(pSysMem, sizeAligned, pSysMem, sizeAligned, nullptr); + wddmAllocation->cpuPtrAllocated = true; gmm = Gmm::create(pSysMem, sizeAligned, uncacheable); - while (success) { - allocation.gmm = gmm; + wddmAllocation->gmm = gmm; - bool success = createWddmAllocation(&allocation, MemoryType::EXTERNAL_ALLOCATION); - if (!success) - break; - - auto *wddmAllocation = new WddmAllocation(allocation); - return wddmAllocation; + if (!createWddmAllocation(wddmAllocation, MemoryType::EXTERNAL_ALLOCATION)) { + delete gmm; + delete wddmAllocation; + freeSystemMemory(pSysMem); + return nullptr; } - delete gmm; - freeSystemMemory(pSysMem); - return nullptr; + return wddmAllocation; } GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, const void *ptrArg) { @@ -180,8 +168,6 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, const } GraphicsAllocation *WddmMemoryManager::allocate32BitGraphicsMemory(size_t size, void *ptr, MemoryType memoryType) { - GraphicsAllocation *graphicsAllocation = nullptr; - bool success = true; Gmm *gmm = nullptr; const void *ptrAligned = nullptr; size_t sizeAligned = size; @@ -203,66 +189,61 @@ GraphicsAllocation *WddmMemoryManager::allocate32BitGraphicsMemory(size_t size, cpuPtrAllocated = true; } - WddmAllocation allocation(const_cast(ptrAligned), sizeAligned, const_cast(ptrAligned), sizeAligned, nullptr); - allocation.cpuPtrAllocated = cpuPtrAllocated; - allocation.is32BitAllocation = true; - allocation.allocationOffset = offset; + auto wddmAllocation = new WddmAllocation(const_cast(ptrAligned), sizeAligned, const_cast(ptrAligned), sizeAligned, nullptr); + wddmAllocation->cpuPtrAllocated = cpuPtrAllocated; + wddmAllocation->is32BitAllocation = true; + wddmAllocation->allocationOffset = offset; gmm = Gmm::create(ptrAligned, sizeAligned, false); + wddmAllocation->gmm = gmm; - while (success) { - allocation.gmm = gmm; - - success = createWddmAllocation(&allocation, memoryType); - if (!success) - break; - - auto *wddmAllocation = new WddmAllocation(allocation); - graphicsAllocation = wddmAllocation; - graphicsAllocation->is32BitAllocation = true; - auto baseAddress = memoryType == MemoryType::EXTERNAL_ALLOCATION ? allocator32Bit->getBase() : this->wddm->getAdapterInfo()->GfxPartition.Heap32[1].Base; - graphicsAllocation->gpuBaseAddress = Gmm::canonize(baseAddress); - - return graphicsAllocation; + if (!createWddmAllocation(wddmAllocation, memoryType)) { + delete gmm; + delete wddmAllocation; + freeSystemMemory(pSysMem); + return nullptr; } - delete gmm; - freeSystemMemory(pSysMem); - return nullptr; + wddmAllocation->is32BitAllocation = true; + auto baseAddress = memoryType == MemoryType::EXTERNAL_ALLOCATION ? allocator32Bit->getBase() : this->wddm->getAdapterInfo()->GfxPartition.Heap32[1].Base; + wddmAllocation->gpuBaseAddress = Gmm::canonize(baseAddress); + + return wddmAllocation; } GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle) { - WddmAllocation allocation(nullptr, 0, handle); + auto allocation = new WddmAllocation(nullptr, 0, handle); bool is32BitAllocation = false; - if (ntHandle) { - wddm->openNTHandle((HANDLE)((UINT_PTR)handle), &allocation); - } else { - if (wddm->openSharedHandle(handle, &allocation) == false) { - return nullptr; - } + bool status = ntHandle ? wddm->openNTHandle((HANDLE)((UINT_PTR)handle), allocation) + : wddm->openSharedHandle(handle, allocation); + + if (!status) { + delete allocation; + return nullptr; } // Shared objects are passed without size - size_t size = allocation.gmm->gmmResourceInfo->getSizeAllocation(); - allocation.setSize(size); + size_t size = allocation->gmm->gmmResourceInfo->getSizeAllocation(); + allocation->setSize(size); void *ptr = nullptr; if (is32bit) { if (!wddm->reserveValidAddressRange(size, ptr)) { + delete allocation; return nullptr; } - allocation.setReservedAddress(ptr); + allocation->setReservedAddress(ptr); } else if (requireSpecificBitness && this->force32bitAllocations) { is32BitAllocation = true; - allocation.is32BitAllocation = true; - allocation.gpuBaseAddress = Gmm::canonize(allocator32Bit->getBase()); + allocation->is32BitAllocation = true; + allocation->gpuBaseAddress = Gmm::canonize(allocator32Bit->getBase()); } - auto status = wddm->mapGpuVirtualAddress(&allocation, ptr, size, is32BitAllocation, false, false); + status = wddm->mapGpuVirtualAddress(allocation, ptr, size, is32BitAllocation, false, false); DEBUG_BREAK_IF(!status); - allocation.setGpuAddress(allocation.gpuPtr); + allocation->setGpuAddress(allocation->gpuPtr); - return new WddmAllocation(allocation); + return allocation; } GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness, bool /*isReused*/) { diff --git a/unit_tests/command_stream/aub_command_stream_receiver_tests.cpp b/unit_tests/command_stream/aub_command_stream_receiver_tests.cpp index 5288b99b25..a4a017d543 100644 --- a/unit_tests/command_stream/aub_command_stream_receiver_tests.cpp +++ b/unit_tests/command_stream/aub_command_stream_receiver_tests.cpp @@ -279,7 +279,7 @@ HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenGraphic HWTEST_F(AubCommandStreamReceiverTests, givenAubCommandStreamReceiverWhenGraphicsAllocationSizeIsZeroThenWriteMemoryIsNotAllowed) { std::unique_ptr> aubCsr(new AUBCommandStreamReceiverHw(*platformDevices[0], true)); - auto gfxAllocation = GraphicsAllocation((void *)0x1234, 0); + GraphicsAllocation gfxAllocation((void *)0x1234, 0); EXPECT_FALSE(aubCsr->writeMemory(gfxAllocation)); } diff --git a/unit_tests/command_stream/command_stream_receiver_hw_tests.cpp b/unit_tests/command_stream/command_stream_receiver_hw_tests.cpp index cb2ef84774..b63c3ca573 100644 --- a/unit_tests/command_stream/command_stream_receiver_hw_tests.cpp +++ b/unit_tests/command_stream/command_stream_receiver_hw_tests.cpp @@ -931,7 +931,7 @@ HWTEST_F(CommandStreamReceiverCQFlushTaskTests, getCSShouldReturnACSWithEnoughSi EXPECT_GE(commandStream.getAvailableSpace(), sizeRequested); commandStream.getSpace(sizeRequested - sizeCQReserves); - GraphicsAllocation allocation = GraphicsAllocation((void *)0x1234, 1); + GraphicsAllocation allocation((void *)0x1234, 1); LinearStream linear(&allocation); auto blocking = true; diff --git a/unit_tests/memory_manager/memory_manager_tests.cpp b/unit_tests/memory_manager/memory_manager_tests.cpp index ca89ff3ac8..7e6d39afe4 100644 --- a/unit_tests/memory_manager/memory_manager_tests.cpp +++ b/unit_tests/memory_manager/memory_manager_tests.cpp @@ -37,11 +37,17 @@ #include "unit_tests/mocks/mock_memory_manager.h" #include +#include using namespace OCLRT; typedef Test MemoryAllocatorTest; +TEST(GraphicsAllocationTest, defaultTypeTraits) { + EXPECT_FALSE(std::is_copy_constructible::value); + EXPECT_FALSE(std::is_copy_assignable::value); +} + TEST(GraphicsAllocationTest, Ctor) { void *cpuPtr = (void *)0x30000; size_t size = 0x1000; diff --git a/unit_tests/os_interface/linux/drm_command_stream_tests.cpp b/unit_tests/os_interface/linux/drm_command_stream_tests.cpp index e8ff2755bd..62b79c84e7 100644 --- a/unit_tests/os_interface/linux/drm_command_stream_tests.cpp +++ b/unit_tests/os_interface/linux/drm_command_stream_tests.cpp @@ -116,7 +116,7 @@ TEST_F(DrmCommandStreamTest, makeResident) { .Times(0); EXPECT_CALL(*mock, ioctl(DRM_IOCTL_I915_GEM_WAIT, ::testing::_)) .Times(0); - auto graphicsAllocation = DrmAllocation(nullptr, nullptr, 1024); + DrmAllocation graphicsAllocation(nullptr, nullptr, 1024); csr->makeResident(graphicsAllocation); } @@ -130,7 +130,7 @@ TEST_F(DrmCommandStreamTest, makeResidentTwiceTheSame) { EXPECT_CALL(*mock, ioctl(DRM_IOCTL_I915_GEM_WAIT, ::testing::_)) .Times(0); - auto graphicsAllocation = DrmAllocation(nullptr, nullptr, 1024); + DrmAllocation graphicsAllocation(nullptr, nullptr, 1024); csr->makeResident(graphicsAllocation); csr->makeResident(graphicsAllocation); @@ -146,7 +146,7 @@ TEST_F(DrmCommandStreamTest, makeResidentSizeZero) { EXPECT_CALL(*mock, ioctl(DRM_IOCTL_I915_GEM_WAIT, ::testing::_)) .Times(0); - auto graphicsAllocation = DrmAllocation(nullptr, nullptr, 0); + DrmAllocation graphicsAllocation(nullptr, nullptr, 0); csr->makeResident(graphicsAllocation); } @@ -161,8 +161,8 @@ TEST_F(DrmCommandStreamTest, makeResidentResized) { EXPECT_CALL(*mock, ioctl(DRM_IOCTL_I915_GEM_WAIT, ::testing::_)) .Times(0); - auto graphicsAllocation = DrmAllocation(nullptr, nullptr, 1024); - auto graphicsAllocation2 = DrmAllocation(nullptr, nullptr, 8192); + DrmAllocation graphicsAllocation(nullptr, nullptr, 1024); + DrmAllocation graphicsAllocation2(nullptr, nullptr, 8192); csr->makeResident(graphicsAllocation); csr->makeResident(graphicsAllocation2); @@ -413,8 +413,8 @@ TEST_F(DrmCommandStreamTest, FlushCheckFlags) { .Times(1) .WillRepeatedly(::testing::Return(0)); - auto allocation = DrmAllocation(nullptr, (void *)0x7FFFFFFF, 1024); - auto allocation2 = DrmAllocation(nullptr, (void *)0x307FFFFFFF, 1024); + DrmAllocation allocation(nullptr, (void *)0x7FFFFFFF, 1024); + DrmAllocation allocation2(nullptr, (void *)0x307FFFFFFF, 1024); csr->makeResident(allocation); csr->makeResident(allocation2); csr->addBatchBufferEnd(cs, nullptr); @@ -448,7 +448,7 @@ TEST_F(DrmCommandStreamTest, CheckDrmFree) { EXPECT_CALL(*mock, ioctl(DRM_IOCTL_I915_GEM_WAIT, ::testing::_)) .Times(1); - auto allocation = DrmAllocation(nullptr, nullptr, 1024); + DrmAllocation allocation(nullptr, nullptr, 1024); csr->makeResident(allocation); csr->addBatchBufferEnd(cs, nullptr); @@ -493,7 +493,7 @@ TEST_F(DrmCommandStreamTest, CheckDrmFreeCloseFailed) { .WillOnce(::testing::Return(-1)); EXPECT_CALL(*mock, ioctl(DRM_IOCTL_I915_GEM_WAIT, ::testing::_)) .Times(1); - auto allocation = DrmAllocation(nullptr, nullptr, 1024); + DrmAllocation allocation(nullptr, nullptr, 1024); csr->makeResident(allocation); csr->addBatchBufferEnd(cs, nullptr); @@ -1341,7 +1341,7 @@ TEST_F(DrmCommandStreamLeaksTest, GivenAllocationsContainingDifferentCountOfFrag TEST_F(DrmCommandStreamLeaksTest, makeResidentSizeZero) { std::unique_ptr buffer(this->createBO(0)); - auto allocation = DrmAllocation(buffer.get(), nullptr, buffer->peekSize()); + DrmAllocation allocation(buffer.get(), nullptr, buffer->peekSize()); EXPECT_EQ(nullptr, allocation.getUnderlyingBuffer()); EXPECT_EQ(buffer->peekSize(), allocation.getUnderlyingBufferSize());