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 9d0d48e
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 239 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
11 changes: 5 additions & 6 deletions source/adapters/opencl/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgMemObj(
ur_kernel_handle_t hKernel, uint32_t argIndex,
const ur_kernel_arg_mem_obj_properties_t *, ur_mem_handle_t hArgValue) {

cl_mem CLArgValue = hArgValue->get();
cl_int RetErr = clSetKernelArg(cl_adapter::cast<cl_kernel>(hKernel),
cl_adapter::cast<cl_uint>(argIndex),
sizeof(hArgValue), &CLArgValue);
CL_RETURN_ON_FAILURE(RetErr);
cl_mem CLArgValue = hArgValue ? hArgValue->get() : nullptr;
CL_RETURN_ON_FAILURE(clSetKernelArg(cl_adapter::cast<cl_kernel>(hKernel),
cl_adapter::cast<cl_uint>(argIndex),
sizeof(CLArgValue), &CLArgValue));
return UR_RESULT_SUCCESS;
}

Expand All @@ -408,7 +407,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgSampler(
cl_sampler CLArgSampler = hArgValue->get();
cl_int RetErr = clSetKernelArg(cl_adapter::cast<cl_kernel>(hKernel),
cl_adapter::cast<cl_uint>(argIndex),
sizeof(hArgValue), &CLArgSampler);
sizeof(CLArgSampler), &CLArgSampler);
CL_RETURN_ON_FAILURE(RetErr);
return UR_RESULT_SUCCESS;
}
Loading

0 comments on commit 9d0d48e

Please sign in to comment.