@@ -120,11 +120,15 @@ hipError_t hipMemCreate(hipMemGenericAllocationHandle_t* handle, size_t size,
120120
121121 // Add this to amd::Memory object, so this ptr is accesible for other hipmemory operations.
122122 size_t offset = 0 ; // this is ignored
123- amd::Memory* memObj = getMemoryObject (ptr, offset);
123+ amd::Memory* phys_mem_obj = getMemoryObject (ptr, offset);
124124 // saves the current device id so that it can be accessed later
125- memObj->getUserData ().deviceId = prop->location .id ;
126- memObj->getUserData ().data = new hip::GenericAllocation (ptr, size, *prop);
127- *handle = reinterpret_cast <hipMemGenericAllocationHandle_t>(memObj->getUserData ().data );
125+ phys_mem_obj->getUserData ().deviceId = prop->location .id ;
126+ phys_mem_obj->getUserData ().data = new hip::GenericAllocation (*phys_mem_obj, size, *prop);
127+ *handle = reinterpret_cast <hipMemGenericAllocationHandle_t>(phys_mem_obj->getUserData ().data );
128+
129+ // Remove because the entry of 0x1 is not needed in MemObjMap.
130+ // We save the copy of Phy mem obj in virtual mem obj during mapping.
131+ amd::MemObjMap::RemoveMemObj (ptr);
128132
129133 HIP_RETURN (hipSuccess);
130134}
@@ -225,9 +229,6 @@ hipError_t hipMemMap(void* ptr, size_t size, size_t offset, hipMemGenericAllocat
225229 cmd->awaitCompletion ();
226230 cmd->release ();
227231
228- // update the internal svm address to ptr
229- ga->asAmdMemory ().setSvmPtr (ptr);
230-
231232 HIP_RETURN (hipSuccess);
232233}
233234
@@ -268,7 +269,8 @@ hipError_t hipMemRetainAllocationHandle(hipMemGenericAllocationHandle_t* handle,
268269 HIP_RETURN (hipErrorInvalidValue);
269270 }
270271
271- *handle = reinterpret_cast <hipMemGenericAllocationHandle_t>(mem->getUserData ().data );
272+ *handle = reinterpret_cast <hipMemGenericAllocationHandle_t>(
273+ mem->getUserData ().phys_mem_obj ->getUserData ().data );
272274
273275 if (*handle == nullptr ) {
274276 HIP_RETURN (hipErrorInvalidValue);
@@ -312,17 +314,17 @@ hipError_t hipMemUnmap(void* ptr, size_t size) {
312314 HIP_RETURN (hipErrorInvalidValue);
313315 }
314316
315- amd::Memory* pa = amd::MemObjMap::FindMemObj (ptr);
316- if (pa == nullptr ) {
317+ amd::Memory* vaddr_mem_obj = amd::MemObjMap::FindVirtualMemObj (ptr);
318+ if (vaddr_mem_obj == nullptr && vaddr_mem_obj-> getSize () != size ) {
317319 HIP_RETURN (hipErrorInvalidValue);
318320 }
319321
320- amd::Memory* va = amd::MemObjMap::FindVirtualMemObj (ptr) ;
321- if (va == nullptr && va-> getSize () != size ) {
322+ amd::Memory* phys_mem_obj = vaddr_mem_obj-> getUserData (). phys_mem_obj ;
323+ if (phys_mem_obj == nullptr ) {
322324 HIP_RETURN (hipErrorInvalidValue);
323325 }
324326
325- auto & queue = *g_devices[pa ->getUserData ().deviceId ]->NullStream ();
327+ auto & queue = *g_devices[phys_mem_obj ->getUserData ().deviceId ]->NullStream ();
326328
327329 amd::Command* cmd = new amd::VirtualMapCommand (queue, amd::Command::EventWaitList{}, ptr, size,
328330 nullptr );
@@ -331,9 +333,8 @@ hipError_t hipMemUnmap(void* ptr, size_t size) {
331333 cmd->release ();
332334
333335 // restore the original pa of the generic allocation
334- hip::GenericAllocation* ga = reinterpret_cast <hip::GenericAllocation*>(pa->getUserData ().data );
335- pa->setSvmPtr (ga->genericAddress ());
336-
336+ hip::GenericAllocation* ga
337+ = reinterpret_cast <hip::GenericAllocation*>(phys_mem_obj->getUserData ().data );
337338 ga->release ();
338339
339340 HIP_RETURN (hipSuccess);
0 commit comments