Skip to content

Commit ffb33f5

Browse files
refactor virtualAlloc methods
Change-Id: I06e93471b395818ad172048d545f1439066207d5
1 parent 9f5f0f6 commit ffb33f5

File tree

8 files changed

+82
-32
lines changed

8 files changed

+82
-32
lines changed

runtime/os_interface/windows/wddm.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@
4343
namespace OCLRT {
4444
extern Wddm::CreateDXGIFactoryFcn getCreateDxgiFactory();
4545
extern Wddm::GetSystemInfoFcn getGetSystemInfo();
46+
extern Wddm::VirtualAllocFcn getVirtualAlloc();
47+
extern Wddm::VirtualFreeFcn getVirtualFree();
4648

4749
class WddmMemoryManager;
4850

4951
Wddm::CreateDXGIFactoryFcn Wddm::createDxgiFactory = getCreateDxgiFactory();
5052
Wddm::GetSystemInfoFcn Wddm::getSystemInfo = getGetSystemInfo();
53+
Wddm::VirtualAllocFcn Wddm::virtualAllocFnc = getVirtualAlloc();
54+
Wddm::VirtualFreeFcn Wddm::virtualFreeFnc = getVirtualFree();
5155

5256
Wddm::Wddm(Gdi *gdi) : initialized(false),
5357
gdiAllocated(false),
@@ -900,7 +904,7 @@ void Wddm::registerTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, Wdd
900904

901905
void Wddm::releaseReservedAddress(void *reservedAddress) {
902906
if (reservedAddress) {
903-
auto status = virtualFreeWrapper(reservedAddress, 0, MEM_RELEASE);
907+
auto status = virtualFree(reservedAddress, 0, MEM_RELEASE);
904908
DEBUG_BREAK_IF(!status);
905909
}
906910
}
@@ -922,22 +926,22 @@ void Wddm::resetPageTableManager(GmmPageTableMngr *newPageTableManager) {
922926
}
923927

924928
bool Wddm::reserveValidAddressRange(size_t size, void *&reservedMem) {
925-
reservedMem = virtualAllocWrapper(nullptr, size, MEM_RESERVE, PAGE_READWRITE);
929+
reservedMem = virtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE);
926930
if (reservedMem == nullptr) {
927931
return false;
928932
} else if (minAddress > reinterpret_cast<uintptr_t>(reservedMem)) {
929933
StackVec<void *, 100> invalidAddrVector;
930934
invalidAddrVector.push_back(reservedMem);
931935
do {
932-
reservedMem = virtualAllocWrapper(nullptr, size, MEM_RESERVE | MEM_TOP_DOWN, PAGE_READWRITE);
936+
reservedMem = virtualAlloc(nullptr, size, MEM_RESERVE | MEM_TOP_DOWN, PAGE_READWRITE);
933937
if (minAddress > reinterpret_cast<uintptr_t>(reservedMem) && reservedMem != nullptr) {
934938
invalidAddrVector.push_back(reservedMem);
935939
} else {
936940
break;
937941
}
938942
} while (1);
939943
for (auto &it : invalidAddrVector) {
940-
auto status = virtualFreeWrapper(it, 0, MEM_RELEASE);
944+
auto status = virtualFree(it, 0, MEM_RELEASE);
941945
DEBUG_BREAK_IF(!status);
942946
}
943947
if (reservedMem == nullptr) {
@@ -947,4 +951,11 @@ bool Wddm::reserveValidAddressRange(size_t size, void *&reservedMem) {
947951
return true;
948952
}
949953

954+
void *Wddm::virtualAlloc(void *inPtr, size_t size, unsigned long flags, unsigned long type) {
955+
return virtualAllocFnc(inPtr, size, flags, type);
956+
}
957+
int Wddm::virtualFree(void *ptr, size_t size, unsigned long flags) {
958+
return virtualFreeFnc(ptr, size, flags);
959+
}
960+
950961
} // namespace OCLRT

runtime/os_interface/windows/wddm.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class Wddm {
6666
public:
6767
typedef HRESULT(WINAPI *CreateDXGIFactoryFcn)(REFIID riid, void **ppFactory);
6868
typedef void(WINAPI *GetSystemInfoFcn)(SYSTEM_INFO *pSystemInfo);
69+
typedef BOOL(WINAPI *VirtualFreeFcn)(LPVOID ptr, SIZE_T size, DWORD flags);
70+
typedef LPVOID(WINAPI *VirtualAllocFcn)(LPVOID inPtr, SIZE_T size, DWORD flags, DWORD type);
6971

7072
virtual ~Wddm();
7173

@@ -101,12 +103,9 @@ class Wddm {
101103
void registerTrimCallback(PFND3DKMT_TRIMNOTIFICATIONCALLBACK callback, WddmMemoryManager *memoryManager);
102104
MOCKABLE_VIRTUAL void releaseReservedAddress(void *reservedAddress);
103105
MOCKABLE_VIRTUAL bool reserveValidAddressRange(size_t size, void *&reservedMem);
104-
MOCKABLE_VIRTUAL bool virtualFreeWrapper(void *ptr, size_t size, uint32_t flags) {
105-
return VirtualFree(ptr, size, flags) != 0 ? true : false;
106-
}
107-
MOCKABLE_VIRTUAL void *virtualAllocWrapper(void *inPtr, size_t size, uint32_t flags, uint32_t type) {
108-
return reinterpret_cast<void *>(VirtualAlloc(inPtr, size, flags, type));
109-
}
106+
107+
MOCKABLE_VIRTUAL void *virtualAlloc(void *inPtr, size_t size, unsigned long flags, unsigned long type);
108+
MOCKABLE_VIRTUAL int virtualFree(void *ptr, size_t size, unsigned long flags);
110109

111110
template <typename GfxFamily>
112111
bool configureDeviceAddressSpace();
@@ -258,6 +257,8 @@ class Wddm {
258257

259258
static CreateDXGIFactoryFcn createDxgiFactory;
260259
static GetSystemInfoFcn getSystemInfo;
260+
static VirtualFreeFcn virtualFreeFnc;
261+
static VirtualAllocFcn virtualAllocFnc;
261262

262263
std::unique_ptr<GmmPageTableMngr> pageTableManager;
263264

runtime/os_interface/windows/wddm_create.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -22,7 +22,6 @@
2222

2323
#include "runtime/os_interface/windows/wddm.h"
2424
#include <dxgi.h>
25-
2625
namespace OCLRT {
2726

2827
Wddm *Wddm::createWddm(Gdi *gdi) {
@@ -36,4 +35,13 @@ Wddm::CreateDXGIFactoryFcn getCreateDxgiFactory() {
3635
Wddm::GetSystemInfoFcn getGetSystemInfo() {
3736
return GetSystemInfo;
3837
}
38+
39+
Wddm::VirtualFreeFcn getVirtualFree() {
40+
return VirtualFree;
41+
}
42+
43+
Wddm::VirtualAllocFcn getVirtualAlloc() {
44+
return VirtualAlloc;
45+
}
46+
3947
} // namespace OCLRT

unit_tests/os_interface/windows/device_command_stream_tests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,13 @@ TEST_F(WddmCommandStreamTest, createAllocationAndMakeResident) {
532532
}
533533

534534
TEST_F(WddmCommandStreamTest, givenHostPtrWhenPtrBelowRestrictionThenCreateAllocationAndMakeResident) {
535-
void *hostPtr = reinterpret_cast<void *>(wddm->virtualAllocAddress - 0x3000);
535+
void *hostPtr = reinterpret_cast<void *>(memManager->getAlignedMallocRestrictions()->minAddress - 0x1000);
536536
auto size = 0x2000u;
537-
void *expectedReserve = reinterpret_cast<void *>(wddm->virtualAllocAddress);
537+
538538
WddmAllocation *gfxAllocation = static_cast<WddmAllocation *>(csr->createAllocationAndHandleResidency(hostPtr, size));
539539

540+
void *expectedReserve = reinterpret_cast<void *>(wddm->virtualAllocAddress);
541+
540542
ASSERT_NE(nullptr, gfxAllocation);
541543

542544
EXPECT_EQ(1u, memManager->getResidencyAllocations().size());

unit_tests/os_interface/windows/wddm_create.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -24,6 +24,16 @@
2424
#include "unit_tests/os_interface/windows/ult_dxgi_factory.h"
2525

2626
namespace OCLRT {
27+
28+
BOOL WINAPI ULTVirtualFree(LPVOID ptr, SIZE_T size, DWORD flags) {
29+
free(ptr);
30+
return 1;
31+
}
32+
33+
LPVOID WINAPI ULTVirtualAlloc(LPVOID inPtr, SIZE_T size, DWORD flags, DWORD type) {
34+
return malloc(size);
35+
}
36+
2737
Wddm *Wddm::createWddm(Gdi *gdi) {
2838
if (gdi == nullptr)
2939
return new WddmMock();
@@ -37,4 +47,12 @@ Wddm::CreateDXGIFactoryFcn getCreateDxgiFactory() {
3747
Wddm::GetSystemInfoFcn getGetSystemInfo() {
3848
return ULTGetSystemInfo;
3949
}
50+
51+
Wddm::VirtualFreeFcn getVirtualFree() {
52+
return ULTVirtualFree;
53+
}
54+
55+
Wddm::VirtualAllocFcn getVirtualAlloc() {
56+
return ULTVirtualAlloc;
57+
}
4058
} // namespace OCLRT

unit_tests/os_interface/windows/wddm_fixture.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,14 @@ class WddmMock : public Wddm {
277277
return waitFromCpuResult.success = Wddm::waitFromCpu(lastFenceValue);
278278
}
279279

280-
bool virtualFreeWrapper(void *ptr, size_t size, uint32_t flags) {
281-
return true;
280+
void *virtualAlloc(void *inPtr, size_t size, unsigned long flags, unsigned long type) override {
281+
void *address = Wddm::virtualAlloc(inPtr, size, flags, type);
282+
virtualAllocAddress = (uintptr_t)address;
283+
return address;
282284
}
283-
284-
void *virtualAllocWrapper(void *inPtr, size_t size, uint32_t flags, uint32_t type) {
285-
void *tmp = reinterpret_cast<void *>(virtualAllocAddress);
286-
size += MemoryConstants::pageSize;
287-
size -= size % MemoryConstants::pageSize;
288-
virtualAllocAddress += size;
289-
return tmp;
285+
int virtualFree(void *ptr, size_t size, unsigned long flags) override {
286+
int success = Wddm::virtualFree(ptr, size, flags);
287+
return success;
290288
}
291289

292290
void releaseReservedAddress(void *reservedAddress) override {
@@ -361,15 +359,15 @@ class WddmMockReserveAddress : public WddmMock {
361359
returnNullIter = 0;
362360
}
363361

364-
void *virtualAllocWrapper(void *inPtr, size_t size, uint32_t flags, uint32_t type) {
362+
void *virtualAlloc(void *inPtr, size_t size, unsigned long flags, unsigned long type) override {
365363
if (returnGood != 0) {
366-
return WddmMock::virtualAllocWrapper(inPtr, size, flags, type);
364+
return WddmMock::virtualAlloc(inPtr, size, flags, type);
367365
}
368366

369367
if (returnInvalidCount != 0) {
370368
returnInvalidIter++;
371369
if (returnInvalidIter > returnInvalidCount) {
372-
return WddmMock::virtualAllocWrapper(inPtr, size, flags, type);
370+
return WddmMock::virtualAlloc(inPtr, size, flags, type);
373371
}
374372
if (returnNullCount != 0) {
375373
returnNullIter++;
@@ -384,6 +382,15 @@ class WddmMockReserveAddress : public WddmMock {
384382
return nullptr;
385383
}
386384

385+
int virtualFree(void *ptr, size_t size, unsigned long flags) override {
386+
387+
if ((ptr == reinterpret_cast<void *>(0x1000)) || (ptr == reinterpret_cast<void *>(0x0))) {
388+
return 1;
389+
}
390+
391+
return WddmMock::virtualFree(ptr, size, flags);
392+
}
393+
387394
uint32_t returnGood;
388395
uint32_t returnInvalidCount;
389396
uint32_t returnInvalidIter;

unit_tests/os_interface/windows/wddm_memory_manager_tests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,11 +618,13 @@ HWTEST_F(WddmMemoryManagerTest, givenDefaultWddmMemoryManagerWhenAskedForAligned
618618
HWTEST_F(WddmMemoryManagerTest, givenWddmMemoryManagerWhenCpuMemNotMeetRestrictionsThenReserveMemRangeForMap) {
619619
SetUpMm<FamilyType>();
620620
WddmMock *mockWddm = static_cast<WddmMock *>(wddm);
621-
void *cpuPtr = reinterpret_cast<void *>(mockWddm->virtualAllocAddress - 0x3000);
621+
void *cpuPtr = reinterpret_cast<void *>(memoryManager->getAlignedMallocRestrictions()->minAddress - 0x1000);
622622
size_t size = 0x1000;
623-
void *expectReserve = reinterpret_cast<void *>(mockWddm->virtualAllocAddress);
624623

625624
WddmAllocation *allocation = static_cast<WddmAllocation *>(memoryManager->allocateGraphicsMemory(size, cpuPtr));
625+
626+
void *expectReserve = reinterpret_cast<void *>(mockWddm->virtualAllocAddress);
627+
626628
ASSERT_NE(nullptr, allocation);
627629
EXPECT_EQ(expectReserve, allocation->getReservedAddress());
628630
EXPECT_EQ(expectReserve, reinterpret_cast<void *>(allocation->gpuPtr));

unit_tests/os_interface/windows/wddm_tests.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,8 +1001,9 @@ HWTEST_F(WddmReserveAddressTest, givenWddmWhenFirstIsSuccessfulThenReturnReserve
10011001
EXPECT_TRUE(ret);
10021002

10031003
wddmMock->returnGood = 1;
1004-
uintptr_t expectedReserve = wddmMock->virtualAllocAddress;
1004+
10051005
ret = wddmMock->reserveValidAddressRange(size, reserve);
1006+
uintptr_t expectedReserve = wddmMock->virtualAllocAddress;
10061007
EXPECT_TRUE(ret);
10071008
EXPECT_EQ(expectedReserve, reinterpret_cast<uintptr_t>(reserve));
10081009
wddmMock->releaseReservedAddress(reserve);
@@ -1032,9 +1033,9 @@ HWTEST_F(WddmReserveAddressTest, givenWddmWhenFirstIsInvalidSecondSuccessfulThen
10321033
EXPECT_TRUE(ret);
10331034

10341035
wddmMock->returnInvalidCount = 1;
1035-
uintptr_t expectedReserve = wddmMock->virtualAllocAddress;
10361036

10371037
ret = wddmMock->reserveValidAddressRange(size, reserve);
1038+
uintptr_t expectedReserve = wddmMock->virtualAllocAddress;
10381039
EXPECT_TRUE(ret);
10391040
EXPECT_EQ(expectedReserve, reinterpret_cast<uintptr_t>(reserve));
10401041
wddmMock->releaseReservedAddress(reserve);
@@ -1050,9 +1051,9 @@ HWTEST_F(WddmReserveAddressTest, givenWddmWhenSecondIsInvalidThirdSuccessfulThen
10501051
EXPECT_TRUE(ret);
10511052

10521053
wddmMock->returnInvalidCount = 2;
1053-
uintptr_t expectedReserve = wddmMock->virtualAllocAddress;
10541054

10551055
ret = wddmMock->reserveValidAddressRange(size, reserve);
1056+
uintptr_t expectedReserve = wddmMock->virtualAllocAddress;
10561057
EXPECT_TRUE(ret);
10571058
EXPECT_EQ(expectedReserve, reinterpret_cast<uintptr_t>(reserve));
10581059
wddmMock->releaseReservedAddress(reserve);

0 commit comments

Comments
 (0)