Skip to content

Commit 3f59acf

Browse files
[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
1 parent 2cbb76a commit 3f59acf

File tree

8 files changed

+267
-280
lines changed

8 files changed

+267
-280
lines changed

runtime/memory_manager/os_agnostic_memory_manager.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t
7575
return nullptr;
7676
}
7777
uint64_t offset = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr) & MemoryConstants::pageMask);
78-
MemoryAllocation *memAlloc = new MemoryAllocation(false, 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(gpuVirtualAddress + offset), size, counter);
7979
memAlloc->is32BitAllocation = true;
8080
memAlloc->gpuBaseAddress = Gmm::canonize(allocator32Bit->getBase());
8181
memAlloc->sizeToFree = allocationSize;
@@ -89,11 +89,11 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t
8989

9090
if (size < 0xfffff000)
9191
ptrAlloc = alignedMallocWrapper(allocationSize, MemoryConstants::allocationAlignment);
92-
void *gpuPointer = allocator32Bit->allocate(allocationSize);
92+
auto gpuAddress = allocator32Bit->allocate(allocationSize);
9393

9494
MemoryAllocation *memoryAllocation = nullptr;
9595
if (ptrAlloc != nullptr) {
96-
memoryAllocation = new MemoryAllocation(true, ptrAlloc, Gmm::canonize(reinterpret_cast<uint64_t>(gpuPointer)), size, counter);
96+
memoryAllocation = new MemoryAllocation(true, ptrAlloc, Gmm::canonize(gpuAddress), size, counter);
9797
memoryAllocation->is32BitAllocation = true;
9898
memoryAllocation->gpuBaseAddress = Gmm::canonize(allocator32Bit->getBase());
9999
memoryAllocation->sizeToFree = allocationSize;
@@ -129,8 +129,8 @@ void OsAgnosticMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllo
129129
void *ptr = gfxAllocation->getUnderlyingBuffer();
130130

131131
if (gfxAllocation->is32BitAllocation) {
132-
void *gpuPtrToFree = reinterpret_cast<void *>(gfxAllocation->getGpuAddress() & ~MemoryConstants::pageMask);
133-
allocator32Bit->free(gpuPtrToFree, static_cast<MemoryAllocation *>(gfxAllocation)->sizeToFree);
132+
auto gpuAddressToFree = gfxAllocation->getGpuAddress() & ~MemoryConstants::pageMask;
133+
allocator32Bit->free(gpuAddressToFree, static_cast<MemoryAllocation *>(gfxAllocation)->sizeToFree);
134134
}
135135
if (gfxAllocation->cpuPtrAllocated) {
136136
alignedFreeWrapper(ptr);

runtime/os_interface/32bit_memory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class Allocator32bit {
3838
Allocator32bit();
3939
~Allocator32bit();
4040

41-
void *allocate(size_t &size);
41+
uint64_t allocate(size_t &size);
4242
uintptr_t getBase();
43-
int free(void *ptr, size_t size);
43+
int free(uint64_t ptr, size_t size);
4444

4545
protected:
4646
std::unique_ptr<OsInternals> osInternals;

runtime/os_interface/linux/drm_32bit_memory.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ bool OCLRT::is32BitOsAllocatorAvailable = true;
9696
Allocator32bit::Allocator32bit(uint64_t base, uint64_t size) {
9797
this->base = base;
9898
this->size = size;
99-
heapAllocator = std::unique_ptr<HeapAllocator>(new HeapAllocator((void *)base, size));
99+
heapAllocator = std::unique_ptr<HeapAllocator>(new HeapAllocator(base, size));
100100
}
101101

102102
OCLRT::Allocator32bit::Allocator32bit() : Allocator32bit(new OsInternals) {
@@ -133,7 +133,7 @@ OCLRT::Allocator32bit::Allocator32bit(Allocator32bit::OsInternals *osInternalsIn
133133
base = (uint64_t)ptr;
134134
size = sizeToMap;
135135

136-
heapAllocator = std::unique_ptr<HeapAllocator>(new HeapAllocator(ptr, sizeToMap));
136+
heapAllocator = std::unique_ptr<HeapAllocator>(new HeapAllocator(base, sizeToMap));
137137
} else {
138138
this->osInternals->drmAllocator = new Allocator32bit::OsInternals::Drm32BitAllocator(*this->osInternals);
139139
}
@@ -149,24 +149,24 @@ OCLRT::Allocator32bit::~Allocator32bit() {
149149
}
150150
}
151151

152-
void *OCLRT::Allocator32bit::allocate(size_t &size) {
153-
void *ptr = nullptr;
152+
uint64_t OCLRT::Allocator32bit::allocate(size_t &size) {
153+
uint64_t ptr = 0llu;
154154
if (DebugManager.flags.UseNewHeapAllocator.get()) {
155155
ptr = this->heapAllocator->allocate(size);
156156
} else {
157-
ptr = this->osInternals->drmAllocator->allocate(size);
157+
ptr = reinterpret_cast<uint64_t>(this->osInternals->drmAllocator->allocate(size));
158158
}
159159
return ptr;
160160
}
161161

162-
int Allocator32bit::free(void *ptr, size_t size) {
163-
if ((ptr == MAP_FAILED) || (ptr == nullptr))
162+
int Allocator32bit::free(uint64_t ptr, size_t size) {
163+
if ((ptr == reinterpret_cast<uint64_t>(MAP_FAILED)) || (ptr == 0llu))
164164
return 0;
165165

166166
if (DebugManager.flags.UseNewHeapAllocator.get()) {
167167
this->heapAllocator->free(ptr, size);
168168
} else {
169-
return this->osInternals->drmAllocator->free(ptr, size);
169+
return this->osInternals->drmAllocator->free(reinterpret_cast<void *>(ptr), size);
170170
}
171171
return 0;
172172
}

runtime/os_interface/linux/drm_memory_manager.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,12 @@ uint32_t DrmMemoryManager::unreference(OCLRT::BufferObject *bo, bool synchronous
128128
if (allocatorType == MMAP_ALLOCATOR) {
129129
munmapFunction(address, unmapSize);
130130
} else {
131+
uint64_t graphicsAddress = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(address));
131132
if (allocatorType == BIT32_ALLOCATOR_EXTERNAL) {
132-
allocator32Bit->free(address, unmapSize);
133+
allocator32Bit->free(graphicsAddress, unmapSize);
133134
} else {
134135
UNRECOVERABLE_IF(allocatorType != BIT32_ALLOCATOR_INTERNAL)
135-
internal32bitAllocator->free(address, unmapSize);
136+
internal32bitAllocator->free(graphicsAddress, unmapSize);
136137
}
137138
}
138139

@@ -276,7 +277,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemory(size_t size, void *
276277

277278
bo->isAllocated = false;
278279
bo->setUnmapSize(realAllocationSize);
279-
bo->address = gpuVirtualAddress;
280+
bo->address = reinterpret_cast<void *>(gpuVirtualAddress);
280281
uintptr_t offset = (uintptr_t)bo->address;
281282
bo->softPin((uint64_t)offset);
282283
bo->setAllocationType(allocationType);
@@ -300,7 +301,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemory(size_t size, void *
300301
return nullptr;
301302
}
302303

303-
BufferObject *bo = allocUserptr(reinterpret_cast<uintptr_t>(res), alignedAllocationSize, 0, true);
304+
BufferObject *bo = allocUserptr(res, alignedAllocationSize, 0, true);
304305

305306
if (!bo) {
306307
allocatorToUse->free(res, allocationSize);
@@ -312,7 +313,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemory(size_t size, void *
312313

313314
bo->setAllocationType(allocationType);
314315

315-
auto drmAllocation = new DrmAllocation(bo, res, alignedAllocationSize);
316+
auto drmAllocation = new DrmAllocation(bo, reinterpret_cast<void *>(res), alignedAllocationSize);
316317
drmAllocation->is32BitAllocation = true;
317318
drmAllocation->gpuBaseAddress = allocatorToUse->getBase();
318319
return drmAllocation;
@@ -338,18 +339,18 @@ BufferObject *DrmMemoryManager::findAndReferenceSharedBufferObject(int boHandle)
338339
}
339340

340341
BufferObject *DrmMemoryManager::createSharedBufferObject(int boHandle, size_t size, bool requireSpecificBitness) {
341-
void *gpuRange = nullptr;
342+
uint64_t gpuRange = 0llu;
342343
StorageAllocatorType storageType = UNKNOWN_ALLOCATOR;
343344

344345
if (requireSpecificBitness && this->force32bitAllocations) {
345346
gpuRange = this->allocator32Bit->allocate(size);
346347
storageType = BIT32_ALLOCATOR_EXTERNAL;
347348
} else {
348-
gpuRange = mmapFunction(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
349+
gpuRange = reinterpret_cast<uint64_t>(mmapFunction(nullptr, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0));
349350
storageType = MMAP_ALLOCATOR;
350351
}
351352

352-
DEBUG_BREAK_IF(gpuRange == MAP_FAILED);
353+
DEBUG_BREAK_IF(gpuRange == reinterpret_cast<uint64_t>(MAP_FAILED));
353354

354355
auto bo = new (std::nothrow) BufferObject(this->drm, boHandle, true);
355356
if (!bo) {
@@ -358,7 +359,7 @@ BufferObject *DrmMemoryManager::createSharedBufferObject(int boHandle, size_t si
358359

359360
bo->size = size;
360361
bo->address = reinterpret_cast<void *>(gpuRange);
361-
bo->softPin(reinterpret_cast<uint64_t>(gpuRange));
362+
bo->softPin(gpuRange);
362363
bo->setUnmapSize(size);
363364
bo->setAllocationType(storageType);
364365
return bo;

runtime/os_interface/windows/wddm_32bit_memory.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Allocator32bit::OsInternals {
3434
Allocator32bit::Allocator32bit(uint64_t base, uint64_t size) {
3535
this->base = base;
3636
this->size = size;
37-
heapAllocator = std::unique_ptr<HeapAllocator>(new HeapAllocator((void *)base, size));
37+
heapAllocator = std::unique_ptr<HeapAllocator>(new HeapAllocator(base, size));
3838
}
3939

4040
OCLRT::Allocator32bit::Allocator32bit() {
@@ -43,7 +43,7 @@ OCLRT::Allocator32bit::Allocator32bit() {
4343
osInternals = std::unique_ptr<OsInternals>(new OsInternals);
4444
osInternals.get()->allocatedRange = (void *)((uintptr_t)this->base);
4545

46-
heapAllocator = std::unique_ptr<HeapAllocator>(new HeapAllocator((void *)this->base, sizeToMap));
46+
heapAllocator = std::unique_ptr<HeapAllocator>(new HeapAllocator(this->base, sizeToMap));
4747
}
4848

4949
OCLRT::Allocator32bit::~Allocator32bit() {
@@ -52,13 +52,13 @@ OCLRT::Allocator32bit::~Allocator32bit() {
5252
}
5353
}
5454

55-
void *Allocator32bit::allocate(size_t &size) {
55+
uint64_t Allocator32bit::allocate(size_t &size) {
5656
if (size >= 0xfffff000)
57-
return nullptr;
57+
return 0llu;
5858
return this->heapAllocator->allocate(size);
5959
}
6060

61-
int Allocator32bit::free(void *ptr, size_t size) {
61+
int Allocator32bit::free(uint64_t ptr, size_t size) {
6262
this->heapAllocator->free(ptr, size);
6363
return 0;
6464
}

0 commit comments

Comments
 (0)