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 14, 2023
1 parent a1b3cff commit ff5ebc1
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 58 deletions.
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
11 changes: 7 additions & 4 deletions source/adapters/opencl/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ urDeviceGet(ur_platform_handle_t hPlatform, ur_device_type_t DeviceType,
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 +999,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 +1034,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
15 changes: 10 additions & 5 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 Down Expand Up @@ -344,7 +347,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 +361,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
10 changes: 6 additions & 4 deletions source/adapters/opencl/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ UR_APIEXPORT ur_result_t UR_APICALL
urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
ur_platform_handle_t *phPlatforms, uint32_t *pNumPlatforms) {

static std::vector<ur_platform_handle_t> URPlatforms;
static std::vector<std::unique_ptr<ur_platform_handle_t_>> URPlatforms;
static std::once_flag InitFlag;
static uint32_t NumPlatforms = 0;
cl_int Result = CL_SUCCESS;
Expand All @@ -107,7 +107,8 @@ urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
}
URPlatforms.resize(NumPlatforms);
for (uint32_t i = 0; i < NumPlatforms; i++) {
URPlatforms[i] = new ur_platform_handle_t_(CLPlatforms[i]);
URPlatforms[i] =
std::make_unique<ur_platform_handle_t_>(CLPlatforms[i]);
}
return Result;
},
Expand All @@ -125,7 +126,7 @@ urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
}
if (NumEntries && phPlatforms) {
for (uint32_t i = 0; i < NumEntries; i++) {
phPlatforms[i] = URPlatforms[i];
phPlatforms[i] = URPlatforms[i].get();
}
}
return mapCLErrorToUR(Result);
Expand All @@ -142,7 +143,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle(
ur_platform_handle_t *phPlatform) {
cl_platform_id NativeHandle =
reinterpret_cast<cl_platform_id>(hNativePlatform);
*phPlatform = new ur_platform_handle_t_(NativeHandle);
auto URPlatform = std::make_unique<ur_platform_handle_t_>(NativeHandle);
*phPlatform = URPlatform.release();
return UR_RESULT_SUCCESS;
}

Expand Down
Loading

0 comments on commit ff5ebc1

Please sign in to comment.