Skip to content

Commit

Permalink
Use unique_ptr instead of new to auto handle memory allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahmed1111 committed Dec 15, 2023
1 parent 8552a40 commit f11e091
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 233 deletions.
3 changes: 1 addition & 2 deletions source/adapters/opencl/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters,
uint32_t *pNumAdapters) {
if (NumEntries > 0 && phAdapters) {
std::lock_guard<std::mutex> Lock{adapter.Mutex};
// adapter.RefCount++;
if (adapter.RefCount++ == 0) {
cl_ext::ExtFuncPtrCache = std::make_unique<cl_ext::ExtFuncPtrCacheT>();
}

*phAdapters = &adapter;
}

Expand All @@ -43,7 +43,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {

UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
std::lock_guard<std::mutex> Lock{adapter.Mutex};
// --adapter.RefCount;
if (--adapter.RefCount == 0) {
cl_ext::ExtFuncPtrCache.reset();
}
Expand Down
4 changes: 3 additions & 1 deletion source/adapters/opencl/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
NumberOfQueues, &CLQueue, hCommandBuffer->CLCommandBuffer,
numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
9 changes: 6 additions & 3 deletions source/adapters/opencl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(
cl_context Ctx = clCreateContext(
nullptr, cl_adapter::cast<cl_uint>(DeviceCount), CLDevices.data(),
nullptr, nullptr, cl_adapter::cast<cl_int *>(&Ret));

*phContext = new ur_context_handle_t_(Ctx, DeviceCount, phDevices);
auto URContext =
std::make_unique<ur_context_handle_t_>(Ctx, DeviceCount, phDevices);
*phContext = URContext.release();
return mapCLErrorToUR(Ret);
}

Expand Down Expand Up @@ -142,7 +143,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle(
ur_context_handle_t *phContext) {

cl_context NativeHandle = reinterpret_cast<cl_context>(hNativeContext);
*phContext = new ur_context_handle_t_(NativeHandle, numDevices, phDevices);
auto URContext = std::make_unique<ur_context_handle_t_>(
NativeHandle, numDevices, phDevices);
*phContext = URContext.release();
return UR_RESULT_SUCCESS;
}

Expand Down
12 changes: 8 additions & 4 deletions source/adapters/opencl/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ urDeviceGet(ur_platform_handle_t hPlatform, ur_device_type_t DeviceType,
default:
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}
UR_RETURN_ON_FAILURE(hPlatform->InitDevices());
try {
uint32_t AllDevicesNum = hPlatform->Devices.size();
uint32_t DeviceNumIter = 0;
for (uint32_t i = 0; i < AllDevicesNum; i++) {
cl_device_type DeviceType = hPlatform->Devices[i]->Type;
if (DeviceType == Type || Type == CL_DEVICE_TYPE_ALL) {
if (phDevices) {
phDevices[DeviceNumIter] = hPlatform->Devices[i];
phDevices[DeviceNumIter] = hPlatform->Devices[i].get();
}
DeviceNumIter++;
}
Expand Down Expand Up @@ -999,8 +1000,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition(
CLNumDevicesRet,
CLSubDevices.data(), nullptr));
for (uint32_t i = 0; i < NumDevices; i++) {
phSubDevices[i] =
new ur_device_handle_t_(CLSubDevices[i], hDevice->Platform, hDevice);
auto URSubDevice = std::make_unique<ur_device_handle_t_>(
CLSubDevices[i], hDevice->Platform, hDevice);
phSubDevices[i] = URSubDevice.release();
}
}

Expand Down Expand Up @@ -1033,7 +1035,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
ur_native_handle_t hNativeDevice, ur_platform_handle_t hPlatform,
const ur_device_native_properties_t *, ur_device_handle_t *phDevice) {
cl_device_id NativeHandle = reinterpret_cast<cl_device_id>(hNativeDevice);
*phDevice = new ur_device_handle_t_(NativeHandle, hPlatform, nullptr);
auto URDevice =
std::make_unique<ur_device_handle_t_>(NativeHandle, hPlatform, nullptr);
*phDevice = URDevice.release();
return UR_RESULT_SUCCESS;
}

Expand Down
80 changes: 60 additions & 20 deletions source/adapters/opencl/enqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueKernelLaunch(
pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize, numEventsInWaitList,
CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -61,7 +63,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWait(
CL_RETURN_ON_FAILURE(clEnqueueMarkerWithWaitList(
hQueue->get(), numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -77,7 +81,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWaitWithBarrier(
CL_RETURN_ON_FAILURE(clEnqueueBarrierWithWaitList(
hQueue->get(), numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -95,7 +101,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferRead(
hQueue->get(), hBuffer->get(), blockingRead, offset, size, pDst,
numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -113,7 +121,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferWrite(
hQueue->get(), hBuffer->get(), blockingWrite, offset, size, pSrc,
numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -139,7 +149,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferReadRect(
Region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch,
pDst, numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -165,7 +177,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferWriteRect(
Region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch,
pSrc, numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -184,7 +198,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferCopy(
hQueue->get(), hBufferSrc->get(), hBufferDst->get(), srcOffset, dstOffset,
size, numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -209,7 +225,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferCopyRect(
Region, srcRowPitch, srcSlicePitch, dstRowPitch, dstSlicePitch,
numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -231,7 +249,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferFill(
hQueue->get(), hBuffer->get(), pPattern, patternSize, offset, size,
numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -271,7 +291,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferFill(
}

if (phEvent) {
*phEvent = new ur_event_handle_t_(WriteEvent, hQueue->Context, hQueue);
auto UREvent = std::make_unique<ur_event_handle_t_>(
WriteEvent, hQueue->Context, hQueue);
*phEvent = UREvent.release();
} else {
CL_RETURN_ON_FAILURE(clReleaseEvent(WriteEvent));
}
Expand All @@ -295,7 +317,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageRead(
hQueue->get(), hImage->get(), blockingRead, Origin, Region, rowPitch,
slicePitch, pDst, numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -316,7 +340,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageWrite(
hQueue->get(), hImage->get(), blockingWrite, Origin, Region, rowPitch,
slicePitch, pSrc, numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -339,7 +365,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageCopy(
hQueue->get(), hImageSrc->get(), hImageDst->get(), SrcOrigin, DstOrigin,
Region, numEventsInWaitList, CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand All @@ -360,7 +388,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferMap(
numEventsInWaitList, CLWaitEvents.data(),
&Event, &Err);
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return mapCLErrorToUR(Err);
}
Expand All @@ -378,7 +408,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemUnmap(
pMappedPtr, numEventsInWaitList,
CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -406,7 +438,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite(
Res = F(hQueue->get(), hProgram->get(), name, blockingWrite, count, offset,
pSrc, numEventsInWaitList, CLWaitEvents.data(), &Event);
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return mapCLErrorToUR(Res);
}
Expand Down Expand Up @@ -434,7 +468,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead(
Res = F(hQueue->get(), hProgram->get(), name, blockingRead, count, offset,
pDst, numEventsInWaitList, CLWaitEvents.data(), &Event);
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
return mapCLErrorToUR(Res);
}
Expand Down Expand Up @@ -463,7 +499,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueReadHostPipe(
blocking, pDst, size, numEventsInWaitList,
CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
}

Expand Down Expand Up @@ -494,7 +532,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueWriteHostPipe(
blocking, pSrc, size, numEventsInWaitList,
CLWaitEvents.data(), &Event));
if (phEvent) {
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
auto UREvent =
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
*phEvent = UREvent.release();
}
}

Expand Down
4 changes: 3 additions & 1 deletion source/adapters/opencl/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle(
const ur_event_native_properties_t *pProperties,
ur_event_handle_t *phEvent) {
cl_event NativeHandle = reinterpret_cast<cl_event>(hNativeEvent);
*phEvent = new ur_event_handle_t_(NativeHandle, hContext, nullptr);
auto UREvent =
std::make_unique<ur_event_handle_t_>(NativeHandle, hContext, nullptr);
*phEvent = UREvent.release();
if (!pProperties || !pProperties->isNativeHandleOwned) {
return urEventRetain(*phEvent);
}
Expand Down
22 changes: 14 additions & 8 deletions source/adapters/opencl/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
cl_mem Buffer = FuncPtr(
CLContext, PropertiesIntel.data(), static_cast<cl_mem_flags>(flags),
size, pProperties->pHost, cl_adapter::cast<cl_int *>(&RetErr));
*phBuffer = new ur_mem_handle_t_(Buffer, hContext);
auto URMem = std::make_unique<ur_mem_handle_t_>(Buffer, hContext);
*phBuffer = URMem.release();
return mapCLErrorToUR(RetErr);
}
}
Expand All @@ -276,7 +277,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
clCreateBuffer(hContext->get(), static_cast<cl_mem_flags>(flags), size,
HostPtr, cl_adapter::cast<cl_int *>(&RetErr));
CL_RETURN_ON_FAILURE(RetErr);
*phBuffer = new ur_mem_handle_t_(Buffer, hContext);
auto URMem = std::make_unique<ur_mem_handle_t_>(Buffer, hContext);
*phBuffer = URMem.release();

return UR_RESULT_SUCCESS;
}
Expand All @@ -296,7 +298,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreate(
clCreateImage(hContext->get(), MapFlags, &ImageFormat, &ImageDesc, pHost,
cl_adapter::cast<cl_int *>(&RetErr));
CL_RETURN_ON_FAILURE(RetErr);
*phMem = new ur_mem_handle_t_(Mem, hContext);
auto URMem = std::make_unique<ur_mem_handle_t_>(Mem, hContext);
*phMem = URMem.release();

return UR_RESULT_SUCCESS;
}
Expand All @@ -321,10 +324,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferPartition(
BufferRegion.origin = pRegion->origin;
BufferRegion.size = pRegion->size;

*phMem = reinterpret_cast<ur_mem_handle_t>(clCreateSubBuffer(
cl_mem Buffer = clCreateSubBuffer(
hBuffer->get(), static_cast<cl_mem_flags>(flags), BufferCreateType,
&BufferRegion, cl_adapter::cast<cl_int *>(&RetErr)));

&BufferRegion, cl_adapter::cast<cl_int *>(&RetErr));
auto URMem = std::make_unique<ur_mem_handle_t_>(Buffer, hBuffer->Context);
*phMem = URMem.release();
if (RetErr == CL_INVALID_VALUE) {
size_t BufferSize = 0;
CL_RETURN_ON_FAILURE(clGetMemObjectInfo(
Expand All @@ -344,7 +348,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreateWithNativeHandle(
ur_native_handle_t hNativeMem, ur_context_handle_t hContext,
const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) {
cl_mem NativeHandle = reinterpret_cast<cl_mem>(hNativeMem);
*phMem = new ur_mem_handle_t_(NativeHandle, hContext);
auto URMem = std::make_unique<ur_mem_handle_t_>(NativeHandle, hContext);
*phMem = URMem.release();
if (!pProperties || !pProperties->isNativeHandleOwned) {
return urMemRetain(*phMem);
}
Expand All @@ -357,7 +362,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreateWithNativeHandle(
[[maybe_unused]] const ur_image_desc_t *pImageDesc,
const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) {
cl_mem NativeHandle = reinterpret_cast<cl_mem>(hNativeMem);
*phMem = new ur_mem_handle_t_(NativeHandle, hContext);
auto URMem = std::make_unique<ur_mem_handle_t_>(NativeHandle, hContext);
*phMem = URMem.release();
if (!pProperties || !pProperties->isNativeHandleOwned) {
return urMemRetain(*phMem);
}
Expand Down
Loading

0 comments on commit f11e091

Please sign in to comment.