Skip to content

Commit 5d3d3ff

Browse files
[16/n] Internal 4GB allocator.
- simplify os agnostic memory manager - remove pointer map - move cpuPtr allocate logic to graphics allocation - do not release tag allocation while injecting memory manager - remove not needed ref count from Memory Allocation Change-Id: I6ad81ee919c9cde939bc754a9dfc2db7568397d2
1 parent ca86fe2 commit 5d3d3ff

File tree

6 files changed

+17
-51
lines changed

6 files changed

+17
-51
lines changed

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
neoDependenciesRev='748020-806'
33
strategy='EQUAL'
44
allowedF=40
5-
allowedCD=337
5+
allowedCD=336

runtime/memory_manager/graphics_allocation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
127127
void incReuseCount() { reuseCount++; }
128128
void decReuseCount() { reuseCount--; }
129129
uint32_t peekReuseCount() const { return reuseCount; }
130+
bool cpuPtrAllocated = false; // flag indicating if cpuPtr is driver-allocated
130131

131132
private:
132133
int allocationType;

runtime/memory_manager/os_agnostic_memory_manager.cpp

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,20 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemory(size_t size,
4444
MemoryAllocation *memoryAllocation = nullptr;
4545

4646
if (fakeBigAllocations && size > bigAllocation) {
47-
memoryAllocation = new MemoryAllocation(true, 1, (void *)dummyAddress, static_cast<uint64_t>(dummyAddress), size, counter);
47+
memoryAllocation = new MemoryAllocation(true, (void *)dummyAddress, static_cast<uint64_t>(dummyAddress), size, counter);
4848
counter++;
4949
memoryAllocation->dummyAllocation = true;
5050
memoryAllocation->uncacheable = uncacheable;
5151
return memoryAllocation;
5252
}
5353
auto ptr = allocateSystemMemory(sizeAligned, alignment ? alignUp(alignment, MemoryConstants::pageSize) : MemoryConstants::pageSize);
54-
DEBUG_BREAK_IF(allocationMap.find(ptr) != allocationMap.end());
5554
if (ptr != nullptr) {
56-
memoryAllocation = new MemoryAllocation(true, 1, ptr, reinterpret_cast<uint64_t>(ptr), size, counter);
55+
memoryAllocation = new MemoryAllocation(true, ptr, reinterpret_cast<uint64_t>(ptr), size, counter);
5756
if (!memoryAllocation) {
5857
alignedFreeWrapper(ptr);
5958
return nullptr;
6059
}
6160
memoryAllocation->uncacheable = uncacheable;
62-
allocationMap.emplace(ptr, memoryAllocation);
6361
}
6462
counter++;
6563
return memoryAllocation;
@@ -77,12 +75,11 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t
7775
return nullptr;
7876
}
7977
uint64_t offset = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr) & MemoryConstants::pageMask);
80-
MemoryAllocation *memAlloc = new MemoryAllocation(false, 1, reinterpret_cast<void *>(ptr), Gmm::canonize(reinterpret_cast<uint64_t>(gpuVirtualAddress) + offset), size, counter);
78+
MemoryAllocation *memAlloc = new MemoryAllocation(false, reinterpret_cast<void *>(ptr), Gmm::canonize(reinterpret_cast<uint64_t>(gpuVirtualAddress) + offset), size, counter);
8179
memAlloc->is32BitAllocation = true;
8280
memAlloc->gpuBaseAddress = Gmm::canonize(allocator32Bit->getBase());
8381
memAlloc->sizeToFree = allocationSize;
8482

85-
allocationMap.emplace(const_cast<void *>(ptr), memAlloc);
8683
counter++;
8784
return memAlloc;
8885
}
@@ -94,22 +91,20 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t
9491
ptrAlloc = alignedMallocWrapper(allocationSize, MemoryConstants::allocationAlignment);
9592
void *gpuPointer = allocator32Bit->allocate(allocationSize);
9693

97-
DEBUG_BREAK_IF(allocationMap.find(ptrAlloc) != allocationMap.end());
9894
MemoryAllocation *memoryAllocation = nullptr;
9995
if (ptrAlloc != nullptr) {
100-
memoryAllocation = new MemoryAllocation(true, 1, ptrAlloc, Gmm::canonize(reinterpret_cast<uint64_t>(gpuPointer)), size, counter);
96+
memoryAllocation = new MemoryAllocation(true, ptrAlloc, Gmm::canonize(reinterpret_cast<uint64_t>(gpuPointer)), size, counter);
10197
memoryAllocation->is32BitAllocation = true;
10298
memoryAllocation->gpuBaseAddress = Gmm::canonize(allocator32Bit->getBase());
10399
memoryAllocation->sizeToFree = allocationSize;
104100
memoryAllocation->cpuPtrAllocated = true;
105-
allocationMap.emplace(ptrAlloc, memoryAllocation);
106101
}
107102
counter++;
108103
return memoryAllocation;
109104
}
110105

111106
GraphicsAllocation *OsAgnosticMemoryManager::createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness, bool reuseBO) {
112-
auto graphicsAllocation = new MemoryAllocation(false, 1, reinterpret_cast<void *>(1), 1, 4096u, static_cast<uint64_t>(handle));
107+
auto graphicsAllocation = new MemoryAllocation(false, reinterpret_cast<void *>(1), 1, 4096u, static_cast<uint64_t>(handle));
113108
graphicsAllocation->setSharedHandle(handle);
114109
graphicsAllocation->is32BitAllocation = requireSpecificBitness;
115110
return graphicsAllocation;
@@ -131,29 +126,13 @@ void OsAgnosticMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllo
131126
return;
132127
}
133128

134-
bool freeMemory = false;
135-
bool is32BitAllocation = false;
136129
void *ptr = gfxAllocation->getUnderlyingBuffer();
137-
void *gpuPtrToFree = nullptr;
138-
size_t sizeToFree = 0;
139-
auto it = allocationMap.find(ptr);
140-
141-
if (it != allocationMap.end()) {
142-
it->second->refCount--;
143-
if (it->second->refCount == 0) {
144-
freeMemory = it->second->cpuPtrAllocated;
145-
is32BitAllocation = it->second->is32BitAllocation;
146-
gpuPtrToFree = reinterpret_cast<void *>(it->second->getGpuAddress() & ~MemoryConstants::pageMask);
147-
sizeToFree = it->second->sizeToFree;
148-
allocationMap.erase(it);
149-
}
130+
131+
if (gfxAllocation->is32BitAllocation) {
132+
void *gpuPtrToFree = reinterpret_cast<void *>(gfxAllocation->getGpuAddress() & ~MemoryConstants::pageMask);
133+
allocator32Bit->free(gpuPtrToFree, static_cast<MemoryAllocation *>(gfxAllocation)->sizeToFree);
150134
}
151-
if (is32BitAllocation) {
152-
allocator32Bit->free(gpuPtrToFree, sizeToFree);
153-
if (freeMemory) {
154-
alignedFreeWrapper(ptr);
155-
}
156-
} else if (freeMemory) {
135+
if (gfxAllocation->cpuPtrAllocated) {
157136
alignedFreeWrapper(ptr);
158137
}
159138
delete gfxAllocation;
@@ -172,7 +151,7 @@ uint64_t OsAgnosticMemoryManager::getInternalHeapBaseAddress() {
172151
}
173152

174153
GraphicsAllocation *OsAgnosticMemoryManager::createGraphicsAllocation(OsHandleStorage &handleStorage, size_t hostPtrSize, const void *hostPtr) {
175-
auto allocation = new MemoryAllocation(false, 0, const_cast<void *>(hostPtr), reinterpret_cast<uint64_t>(hostPtr), hostPtrSize, counter++);
154+
auto allocation = new MemoryAllocation(false, const_cast<void *>(hostPtr), reinterpret_cast<uint64_t>(hostPtr), hostPtrSize, counter++);
176155
allocation->fragmentsStorage = handleStorage;
177156
return allocation;
178157
}

runtime/memory_manager/os_agnostic_memory_manager.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,17 @@ constexpr size_t bigAllocation = 1 * MB;
3131
constexpr uintptr_t dummyAddress = 0xFFFFF000u;
3232
class MemoryAllocation : public GraphicsAllocation {
3333
public:
34-
bool cpuPtrAllocated;
35-
unsigned int refCount;
3634
unsigned long long id;
3735
size_t sizeToFree = 0;
3836
bool dummyAllocation = false;
3937
bool uncacheable = false;
4038

4139
void setSharedHandle(osHandle handle) { this->sharedHandle = handle; }
4240

43-
MemoryAllocation(bool cpuPtrAllocated, int refCount, void *pMem, uint64_t gpuAddress, size_t memSize, uint64_t count) : GraphicsAllocation(pMem, gpuAddress, 0u, memSize),
44-
cpuPtrAllocated(cpuPtrAllocated),
45-
refCount(refCount),
46-
id(count) {}
41+
MemoryAllocation(bool cpuPtrAllocated, void *pMem, uint64_t gpuAddress, size_t memSize, uint64_t count) : GraphicsAllocation(pMem, gpuAddress, 0u, memSize),
42+
id(count) {
43+
this->cpuPtrAllocated = cpuPtrAllocated;
44+
}
4745
};
4846

4947
typedef std::map<void *, MemoryAllocation *> PointerMap;
@@ -83,7 +81,6 @@ class OsAgnosticMemoryManager : public MemoryManager {
8381
void turnOnFakingBigAllocations();
8482

8583
private:
86-
PointerMap allocationMap;
8784
unsigned long long counter = 0;
8885
bool fakeBigAllocations = false;
8986
};

runtime/os_interface/windows/wddm_allocation.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,13 @@ const size_t trimListUnusedPosition = (size_t)-1;
4040

4141
class WddmAllocation : public GraphicsAllocation {
4242
public:
43-
// runtime assigned fields
44-
bool cpuPtrAllocated; // flag indicating if cpuPtr is driver-allocated
45-
4643
// OS assigned fields
4744
D3DKMT_HANDLE handle; // set by createAllocation
4845
D3DKMT_HANDLE resourceHandle = 0u; // used by shared resources
4946

5047
D3DGPU_VIRTUAL_ADDRESS gpuPtr; // set by mapGpuVA
5148
WddmAllocation(void *cpuPtrIn, size_t sizeIn, void *alignedCpuPtr, size_t alignedSize, void *reservedAddr)
5249
: GraphicsAllocation(cpuPtrIn, sizeIn),
53-
cpuPtrAllocated(false),
5450
handle(0),
5551
gpuPtr(0),
5652
alignedCpuPtr(alignedCpuPtr),
@@ -60,7 +56,6 @@ class WddmAllocation : public GraphicsAllocation {
6056
}
6157

6258
WddmAllocation(void *cpuPtrIn, size_t sizeIn, osHandle sharedHandle) : GraphicsAllocation(cpuPtrIn, sizeIn, sharedHandle),
63-
cpuPtrAllocated(false),
6459
handle(0),
6560
gpuPtr(0),
6661
alignedCpuPtr(nullptr),

unit_tests/mocks/mock_device.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,7 @@ bool MockDevice::hasDriverInfo() {
5656

5757
void MockDevice::injectMemoryManager(MockMemoryManager *memoryManager) {
5858
memoryManager->setCommandStreamReceiver(commandStreamReceiver);
59-
this->memoryManager->freeGraphicsMemory(tagAllocation);
60-
tagAllocation = memoryManager->allocateGraphicsMemory(sizeof(uint32_t), sizeof(uint32_t));
61-
auto pTagMemory = reinterpret_cast<uint32_t *>(tagAllocation->getUnderlyingBuffer());
62-
*pTagMemory = initialHardwareTag;
63-
tagAddress = pTagMemory;
6459
commandStreamReceiver->setMemoryManager(memoryManager);
65-
commandStreamReceiver->setTagAllocation(tagAllocation);
6660
setMemoryManager(memoryManager);
6761
memoryManager->setDevice(this);
6862
}

0 commit comments

Comments
 (0)