Remove alignedCpuPtr from WddmAllocation

- also switch to make_unique in wddm memory manager functions.

Change-Id: I2f7cf412a993040439466f1971d935fb8429ce7c
This commit is contained in:
Mrozek, Michal
2018-10-31 10:05:34 +01:00
committed by sys_ocldev
parent 1822fb0747
commit 8bec1906ec
6 changed files with 43 additions and 56 deletions

View File

@@ -31,11 +31,10 @@ class WddmAllocation : public GraphicsAllocation {
D3DKMT_HANDLE resourceHandle = 0u; // used by shared resources
D3DGPU_VIRTUAL_ADDRESS gpuPtr; // set by mapGpuVA
WddmAllocation(void *cpuPtrIn, size_t sizeIn, void *alignedCpuPtr, void *reservedAddr, MemoryPool::Type pool, size_t osContextsCount)
WddmAllocation(void *cpuPtrIn, size_t sizeIn, void *reservedAddr, MemoryPool::Type pool, size_t osContextsCount)
: GraphicsAllocation(cpuPtrIn, castToUint64(cpuPtrIn), 0llu, sizeIn),
handle(0),
gpuPtr(0),
alignedCpuPtr(alignedCpuPtr),
trimCandidateListPositions(osContextsCount, trimListUnusedPosition) {
reservedAddressSpace = reservedAddr;
this->memoryPool = pool;
@@ -45,18 +44,13 @@ class WddmAllocation : public GraphicsAllocation {
: GraphicsAllocation(cpuPtrIn, sizeIn, sharedHandle),
handle(0),
gpuPtr(0),
alignedCpuPtr(nullptr),
trimCandidateListPositions(osContextsCount, trimListUnusedPosition) {
reservedAddressSpace = nullptr;
this->memoryPool = pool;
}
WddmAllocation(void *alignedCpuPtr, size_t sizeIn, void *reservedAddress, MemoryPool::Type pool, size_t osContextsCount)
: WddmAllocation(alignedCpuPtr, sizeIn, alignedCpuPtr, reservedAddress, pool, osContextsCount) {
}
void *getAlignedCpuPtr() const {
return this->alignedCpuPtr;
return alignDown(this->cpuPtr, MemoryConstants::pageSize);
}
size_t getAlignedSize() const {
@@ -88,7 +82,6 @@ class WddmAllocation : public GraphicsAllocation {
void setGpuAddress(uint64_t graphicsAddress) { this->gpuAddress = graphicsAddress; }
protected:
void *alignedCpuPtr;
ResidencyData residency;
std::vector<size_t> trimCandidateListPositions;
void *reservedAddressSpace;

View File

@@ -71,39 +71,38 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryForImage(ImageInfo
delete gmm;
return allocateGraphicsMemory(imgInfo.size);
}
auto allocation = new WddmAllocation(nullptr, imgInfo.size, nullptr, MemoryPool::SystemCpuInaccessible, getOsContextCount());
auto allocation = std::make_unique<WddmAllocation>(nullptr, imgInfo.size, nullptr, MemoryPool::SystemCpuInaccessible, getOsContextCount());
allocation->gmm = gmm;
if (!WddmMemoryManager::createWddmAllocation(allocation, AllocationOrigin::EXTERNAL_ALLOCATION)) {
delete allocation;
if (!WddmMemoryManager::createWddmAllocation(allocation.get(), AllocationOrigin::EXTERNAL_ALLOCATION)) {
return nullptr;
}
return allocation;
return allocation.release();
}
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory64kb(size_t size, size_t alignment, bool forcePin, bool preferRenderCompressed) {
size_t sizeAligned = alignUp(size, MemoryConstants::pageSize64k);
Gmm *gmm = nullptr;
auto wddmAllocation = new WddmAllocation(nullptr, sizeAligned, nullptr, nullptr, MemoryPool::System64KBPages, getOsContextCount());
auto wddmAllocation = std::make_unique<WddmAllocation>(nullptr, sizeAligned, nullptr, MemoryPool::System64KBPages, getOsContextCount());
gmm = new Gmm(nullptr, sizeAligned, false, preferRenderCompressed, true);
wddmAllocation->gmm = gmm;
if (!wddm->createAllocation64k(wddmAllocation)) {
if (!wddm->createAllocation64k(wddmAllocation.get())) {
delete gmm;
delete wddmAllocation;
return nullptr;
}
auto cpuPtr = lockResource(wddmAllocation);
auto cpuPtr = lockResource(wddmAllocation.get());
wddmAllocation->setLocked(true);
// 64kb map is not needed
auto status = wddm->mapGpuVirtualAddress(wddmAllocation, cpuPtr, false, false, false);
auto status = wddm->mapGpuVirtualAddress(wddmAllocation.get(), cpuPtr, false, false, false);
DEBUG_BREAK_IF(!status);
wddmAllocation->setCpuPtrAndGpuAddress(cpuPtr, (uint64_t)wddmAllocation->gpuPtr);
return wddmAllocation;
return wddmAllocation.release();
}
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, size_t alignment, bool forcePin, bool uncacheable) {
@@ -116,20 +115,19 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, size_
return nullptr;
}
auto wddmAllocation = new WddmAllocation(pSysMem, sizeAligned, pSysMem, nullptr, MemoryPool::System4KBPages, getOsContextCount());
auto wddmAllocation = std::make_unique<WddmAllocation>(pSysMem, sizeAligned, nullptr, MemoryPool::System4KBPages, getOsContextCount());
wddmAllocation->driverAllocatedCpuPointer = pSysMem;
gmm = new Gmm(pSysMem, sizeAligned, uncacheable);
wddmAllocation->gmm = gmm;
if (!createWddmAllocation(wddmAllocation, AllocationOrigin::EXTERNAL_ALLOCATION)) {
if (!createWddmAllocation(wddmAllocation.get(), AllocationOrigin::EXTERNAL_ALLOCATION)) {
delete gmm;
delete wddmAllocation;
freeSystemMemory(pSysMem);
return nullptr;
}
return wddmAllocation;
return wddmAllocation.release();
}
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryForNonSvmHostPtr(size_t size, void *cpuPtr) {
@@ -137,19 +135,18 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemoryForNonSvmHostPtr(si
auto offsetInPage = ptrDiff(cpuPtr, alignedPtr);
auto alignedSize = alignSizeWholePage(cpuPtr, size);
auto wddmAllocation = new WddmAllocation(cpuPtr, size, alignedPtr, nullptr, MemoryPool::System4KBPages, getOsContextCount());
auto wddmAllocation = std::make_unique<WddmAllocation>(cpuPtr, size, nullptr, MemoryPool::System4KBPages, getOsContextCount());
wddmAllocation->allocationOffset = offsetInPage;
auto gmm = new Gmm(alignedPtr, alignedSize, false);
wddmAllocation->gmm = gmm;
if (!createWddmAllocation(wddmAllocation, AllocationOrigin::EXTERNAL_ALLOCATION)) {
if (!createWddmAllocation(wddmAllocation.get(), AllocationOrigin::EXTERNAL_ALLOCATION)) {
delete gmm;
delete wddmAllocation;
return nullptr;
}
return wddmAllocation;
return wddmAllocation.release();
}
GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, const void *ptrArg) {
@@ -170,7 +167,7 @@ GraphicsAllocation *WddmMemoryManager::allocateGraphicsMemory(size_t size, const
return nullptr;
}
auto allocation = new WddmAllocation(ptr, size, ptrAligned, reserve, MemoryPool::System4KBPages, getOsContextCount());
auto allocation = new WddmAllocation(ptr, size, reserve, MemoryPool::System4KBPages, getOsContextCount());
allocation->allocationOffset = offset;
Gmm *gmm = new Gmm(ptrAligned, sizeAligned, false);
@@ -205,7 +202,7 @@ GraphicsAllocation *WddmMemoryManager::allocate32BitGraphicsMemory(size_t size,
ptrAligned = pSysMem;
}
auto wddmAllocation = new WddmAllocation(const_cast<void *>(ptrAligned), sizeAligned, const_cast<void *>(ptrAligned), nullptr, MemoryPool::System4KBPagesWith32BitGpuAddressing, getOsContextCount());
auto wddmAllocation = std::make_unique<WddmAllocation>(const_cast<void *>(ptrAligned), sizeAligned, nullptr, MemoryPool::System4KBPagesWith32BitGpuAddressing, getOsContextCount());
wddmAllocation->driverAllocatedCpuPointer = pSysMem;
wddmAllocation->is32BitAllocation = true;
wddmAllocation->allocationOffset = offset;
@@ -213,9 +210,8 @@ GraphicsAllocation *WddmMemoryManager::allocate32BitGraphicsMemory(size_t size,
gmm = new Gmm(ptrAligned, sizeAligned, false);
wddmAllocation->gmm = gmm;
if (!createWddmAllocation(wddmAllocation, allocationOrigin)) {
if (!createWddmAllocation(wddmAllocation.get(), allocationOrigin)) {
delete gmm;
delete wddmAllocation;
freeSystemMemory(pSysMem);
return nullptr;
}
@@ -224,18 +220,17 @@ GraphicsAllocation *WddmMemoryManager::allocate32BitGraphicsMemory(size_t size,
auto baseAddress = allocationOrigin == AllocationOrigin::EXTERNAL_ALLOCATION ? allocator32Bit->getBase() : this->wddm->getGfxPartition().Heap32[1].Base;
wddmAllocation->gpuBaseAddress = GmmHelper::canonize(baseAddress);
return wddmAllocation;
return wddmAllocation.release();
}
GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handle, bool requireSpecificBitness, bool ntHandle) {
auto allocation = new WddmAllocation(nullptr, 0, handle, MemoryPool::SystemCpuInaccessible, getOsContextCount());
auto allocation = std::make_unique<WddmAllocation>(nullptr, 0, handle, MemoryPool::SystemCpuInaccessible, getOsContextCount());
bool is32BitAllocation = false;
bool status = ntHandle ? wddm->openNTHandle((HANDLE)((UINT_PTR)handle), allocation)
: wddm->openSharedHandle(handle, allocation);
bool status = ntHandle ? wddm->openNTHandle((HANDLE)((UINT_PTR)handle), allocation.get())
: wddm->openSharedHandle(handle, allocation.get());
if (!status) {
delete allocation;
return nullptr;
}
@@ -246,7 +241,6 @@ GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handl
void *ptr = nullptr;
if (is32bit) {
if (!wddm->reserveValidAddressRange(size, ptr)) {
delete allocation;
return nullptr;
}
allocation->setReservedAddress(ptr);
@@ -255,10 +249,10 @@ GraphicsAllocation *WddmMemoryManager::createAllocationFromHandle(osHandle handl
allocation->is32BitAllocation = true;
allocation->gpuBaseAddress = GmmHelper::canonize(allocator32Bit->getBase());
}
status = wddm->mapGpuVirtualAddress(allocation, ptr, is32BitAllocation, false, false);
status = wddm->mapGpuVirtualAddress(allocation.get(), ptr, is32BitAllocation, false, false);
DEBUG_BREAK_IF(!status);
allocation->setGpuAddress(allocation->gpuPtr);
return allocation;
return allocation.release();
}
GraphicsAllocation *WddmMemoryManager::createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) {
@@ -448,7 +442,7 @@ void WddmMemoryManager::obtainGpuAddresFromFragments(WddmAllocation *allocation,
}
GraphicsAllocation *WddmMemoryManager::createGraphicsAllocation(OsHandleStorage &handleStorage, size_t hostPtrSize, const void *hostPtr) {
auto allocation = new WddmAllocation(const_cast<void *>(hostPtr), hostPtrSize, const_cast<void *>(hostPtr), nullptr, MemoryPool::System4KBPages, getOsContextCount());
auto allocation = new WddmAllocation(const_cast<void *>(hostPtr), hostPtrSize, nullptr, MemoryPool::System4KBPages, getOsContextCount());
allocation->fragmentsStorage = handleStorage;
obtainGpuAddresFromFragments(allocation, handleStorage);
return allocation;

View File

@@ -12,7 +12,7 @@ namespace OCLRT {
class MockWddmAllocation : public WddmAllocation {
public:
MockWddmAllocation() : WddmAllocation(nullptr, 0, nullptr, nullptr, MemoryPool::MemoryNull, 1u) {
MockWddmAllocation() : WddmAllocation(nullptr, 0, nullptr, MemoryPool::MemoryNull, 1u) {
}
};

View File

@@ -76,7 +76,7 @@ TEST_F(Wddm20Tests, givenNullPageTableManagerAndRenderCompressedResourceWhenMapp
mockGmmRes->setUnifiedAuxTranslationCapable();
void *fakePtr = reinterpret_cast<void *>(0x100);
WddmAllocation allocation(fakePtr, 0x2100, fakePtr, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation(fakePtr, 0x2100, nullptr, MemoryPool::MemoryNull, 1u);
allocation.gmm = gmm.get();
allocation.handle = ALLOCATION_HANDLE;
@@ -194,7 +194,7 @@ TEST_F(Wddm20WithMockGdiDllTests, givenAllocationSmallerUnderlyingThanAlignedSiz
size_t underlyingPages = underlyingSize / MemoryConstants::pageSize;
size_t alignedPages = alignedSize / MemoryConstants::pageSize;
WddmAllocation allocation(ptr, 0x2100, ptr, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation(ptr, 0x2100, nullptr, MemoryPool::MemoryNull, 1u);
Gmm *gmm = GmmHelperFunctions::getGmm(allocation.getAlignedCpuPtr(), allocation.getAlignedSize());
allocation.gmm = gmm;
@@ -217,7 +217,7 @@ TEST_F(Wddm20WithMockGdiDllTests, givenAllocationSmallerUnderlyingThanAlignedSiz
TEST_F(Wddm20WithMockGdiDllTests, givenWddmAllocationWhenMappingGpuVaThenUseGmmSize) {
void *fakePtr = reinterpret_cast<void *>(0x123);
WddmAllocation allocation(fakePtr, 100, fakePtr, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation(fakePtr, 100, nullptr, MemoryPool::MemoryNull, 1u);
std::unique_ptr<Gmm> gmm(GmmHelperFunctions::getGmm(allocation.getAlignedCpuPtr(), allocation.getAlignedSize()));
allocation.gmm = gmm.get();
@@ -739,7 +739,7 @@ TEST_F(Wddm20Tests, whenCreateAllocation64kFailsThenReturnFalse) {
void *fakePtr = reinterpret_cast<void *>(0x123);
auto gmm = std::make_unique<Gmm>(fakePtr, 100, false);
WddmAllocation allocation(fakePtr, 100, fakePtr, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation(fakePtr, 100, nullptr, MemoryPool::MemoryNull, 1u);
allocation.gmm = gmm.get();
EXPECT_FALSE(wddm->createAllocation64k(&allocation));

View File

@@ -56,7 +56,7 @@ TEST(WddmMemoryManager, NonAssignable) {
}
TEST(WddmAllocationTest, givenAllocationIsTrimCandidateInOneOsContextWhenGettingTrimCandidatePositionThenReturnItsPositionAndUnusedPositionInOtherContexts) {
WddmAllocation allocation{nullptr, 0, nullptr, nullptr, MemoryPool::MemoryNull, 3u};
WddmAllocation allocation{nullptr, 0, nullptr, MemoryPool::MemoryNull, 3u};
OsContext osContext{nullptr, 1u};
allocation.setTrimCandidateListPosition(osContext.getContextId(), 700u);
EXPECT_EQ(trimListUnusedPosition, allocation.getTrimCandidateListPosition(0u));
@@ -65,7 +65,7 @@ TEST(WddmAllocationTest, givenAllocationIsTrimCandidateInOneOsContextWhenGetting
}
TEST(WddmAllocationTest, givenRequestedContextIdTooLargeWhenGettingTrimCandidateListPositionThenReturnUnusedPosition) {
WddmAllocation allocation{nullptr, 0, nullptr, nullptr, MemoryPool::MemoryNull, 1u};
WddmAllocation allocation{nullptr, 0, nullptr, MemoryPool::MemoryNull, 1u};
EXPECT_EQ(trimListUnusedPosition, allocation.getTrimCandidateListPosition(1u));
EXPECT_EQ(trimListUnusedPosition, allocation.getTrimCandidateListPosition(1000u));
}
@@ -1213,7 +1213,7 @@ TEST_F(WddmMemoryManagerTest2, makeResidentResidencyAllocationsSucceedsWhenMakeR
MockWddmAllocation allocation1;
void *cpuPtr = reinterpret_cast<void *>(wddm->getWddmMinAddress() + 0x1000);
size_t allocationSize = 0x1000;
WddmAllocation allocationToTrim(cpuPtr, allocationSize, cpuPtr, nullptr, MemoryPool::MemoryNull, memoryManager->getOsContextCount());
WddmAllocation allocationToTrim(cpuPtr, allocationSize, nullptr, MemoryPool::MemoryNull, memoryManager->getOsContextCount());
allocationToTrim.getResidencyData().updateCompletionData(osContext->get()->getResidencyController().getMonitoredFence().lastSubmittedFence, osContext->getContextId());

View File

@@ -506,9 +506,9 @@ TEST_F(WddmResidencyControllerWithGdiTest, trimToBudgetReturnsFalseWhenNumBytesT
}
TEST_F(WddmResidencyControllerWithGdiTest, trimToBudgetStopsEvictingWhenNumBytesToTrimIsZero) {
WddmAllocation allocation1(reinterpret_cast<void *>(0x1000), 0x1000, reinterpret_cast<void *>(0x1000), nullptr, MemoryPool::MemoryNull, 1u),
allocation2(reinterpret_cast<void *>(0x1000), 0x3000, reinterpret_cast<void *>(0x1000), nullptr, MemoryPool::MemoryNull, 1u),
allocation3(reinterpret_cast<void *>(0x1000), 0x1000, reinterpret_cast<void *>(0x1000), nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation1(reinterpret_cast<void *>(0x1000), 0x1000, nullptr, MemoryPool::MemoryNull, 1u),
allocation2(reinterpret_cast<void *>(0x1000), 0x3000, nullptr, MemoryPool::MemoryNull, 1u),
allocation3(reinterpret_cast<void *>(0x1000), 0x1000, nullptr, MemoryPool::MemoryNull, 1u);
allocation1.getResidencyData().resident = true;
allocation1.getResidencyData().updateCompletionData(0, osContextId);
@@ -601,8 +601,8 @@ TEST_F(WddmResidencyControllerWithGdiTest, trimToBudgetWaitsFromCpuWhenLastFence
TEST_F(WddmResidencyControllerWithGdiAndMemoryManagerTest, trimToBudgetEvictsDoneFragmentsOnly) {
gdi->setNonZeroNumBytesToTrimInEvict();
void *ptr = reinterpret_cast<void *>(wddm->virtualAllocAddress + 0x1000);
WddmAllocation allocation1(ptr, 0x1000, ptr, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation2(ptr, 0x1000, ptr, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation1(ptr, 0x1000, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation2(ptr, 0x1000, nullptr, MemoryPool::MemoryNull, 1u);
allocation1.getResidencyData().resident = true;
allocation1.getResidencyData().updateCompletionData(0, osContextId);
@@ -660,9 +660,9 @@ TEST_F(WddmResidencyControllerWithGdiTest, givenThreeAllocationsAlignedSizeBigge
void *ptr2 = reinterpret_cast<void *>(wddm->virtualAllocAddress + 0x3000);
void *ptr3 = reinterpret_cast<void *>(wddm->virtualAllocAddress + 0x5000);
WddmAllocation allocation1(ptr1, underlyingSize, ptr1, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation2(ptr2, underlyingSize, ptr2, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation3(ptr3, underlyingSize, ptr3, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation1(ptr1, underlyingSize, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation2(ptr2, underlyingSize, nullptr, MemoryPool::MemoryNull, 1u);
WddmAllocation allocation3(ptr3, underlyingSize, nullptr, MemoryPool::MemoryNull, 1u);
allocation1.getResidencyData().resident = true;
allocation1.getResidencyData().updateCompletionData(0, osContextId);