Skip to content

Commit b3fd249

Browse files
committed
Use unique_ptr instead of new to auto handle memory allocations
1 parent 1643280 commit b3fd249

File tree

14 files changed

+178
-239
lines changed

14 files changed

+178
-239
lines changed

source/adapters/opencl/adapter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters,
2222
uint32_t *pNumAdapters) {
2323
if (NumEntries > 0 && phAdapters) {
2424
std::lock_guard<std::mutex> Lock{adapter.Mutex};
25-
// adapter.RefCount++;
2625
if (adapter.RefCount++ == 0) {
2726
cl_ext::ExtFuncPtrCache = std::make_unique<cl_ext::ExtFuncPtrCacheT>();
2827
}
28+
2929
*phAdapters = &adapter;
3030
}
3131

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

4444
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
4545
std::lock_guard<std::mutex> Lock{adapter.Mutex};
46-
// --adapter.RefCount;
4746
if (--adapter.RefCount == 0) {
4847
cl_ext::ExtFuncPtrCache.reset();
4948
}

source/adapters/opencl/command_buffer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
358358
NumberOfQueues, &CLQueue, hCommandBuffer->CLCommandBuffer,
359359
numEventsInWaitList, CLWaitEvents.data(), &Event));
360360
if (phEvent) {
361-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
361+
auto UREvent =
362+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
363+
*phEvent = UREvent.release();
362364
}
363365
return UR_RESULT_SUCCESS;
364366
}

source/adapters/opencl/context.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(
4545
cl_context Ctx = clCreateContext(
4646
nullptr, cl_adapter::cast<cl_uint>(DeviceCount), CLDevices.data(),
4747
nullptr, nullptr, cl_adapter::cast<cl_int *>(&Ret));
48-
49-
*phContext = new ur_context_handle_t_(Ctx, DeviceCount, phDevices);
48+
auto URContext =
49+
std::make_unique<ur_context_handle_t_>(Ctx, DeviceCount, phDevices);
50+
*phContext = URContext.release();
5051
return mapCLErrorToUR(Ret);
5152
}
5253

@@ -142,7 +143,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle(
142143
ur_context_handle_t *phContext) {
143144

144145
cl_context NativeHandle = reinterpret_cast<cl_context>(hNativeContext);
145-
*phContext = new ur_context_handle_t_(NativeHandle, numDevices, phDevices);
146+
auto URContext = std::make_unique<ur_context_handle_t_>(
147+
NativeHandle, numDevices, phDevices);
148+
*phContext = URContext.release();
146149
return UR_RESULT_SUCCESS;
147150
}
148151

source/adapters/opencl/device.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ urDeviceGet(ur_platform_handle_t hPlatform, ur_device_type_t DeviceType,
7979
default:
8080
return UR_RESULT_ERROR_INVALID_ENUMERATION;
8181
}
82+
UR_RETURN_ON_FAILURE(hPlatform->InitDevices());
8283
try {
8384
uint32_t AllDevicesNum = hPlatform->Devices.size();
8485
uint32_t DeviceNumIter = 0;
8586
for (uint32_t i = 0; i < AllDevicesNum; i++) {
8687
cl_device_type DeviceType = hPlatform->Devices[i]->Type;
8788
if (DeviceType == Type || Type == CL_DEVICE_TYPE_ALL) {
8889
if (phDevices) {
89-
phDevices[DeviceNumIter] = hPlatform->Devices[i];
90+
phDevices[DeviceNumIter] = hPlatform->Devices[i].get();
9091
}
9192
DeviceNumIter++;
9293
}
@@ -999,8 +1000,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition(
9991000
CLNumDevicesRet,
10001001
CLSubDevices.data(), nullptr));
10011002
for (uint32_t i = 0; i < NumDevices; i++) {
1002-
phSubDevices[i] =
1003-
new ur_device_handle_t_(CLSubDevices[i], hDevice->Platform, hDevice);
1003+
auto URSubDevice = std::make_unique<ur_device_handle_t_>(
1004+
CLSubDevices[i], hDevice->Platform, hDevice);
1005+
phSubDevices[i] = URSubDevice.release();
10041006
}
10051007
}
10061008

@@ -1033,7 +1035,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
10331035
ur_native_handle_t hNativeDevice, ur_platform_handle_t hPlatform,
10341036
const ur_device_native_properties_t *, ur_device_handle_t *phDevice) {
10351037
cl_device_id NativeHandle = reinterpret_cast<cl_device_id>(hNativeDevice);
1036-
*phDevice = new ur_device_handle_t_(NativeHandle, hPlatform, nullptr);
1038+
auto URDevice =
1039+
std::make_unique<ur_device_handle_t_>(NativeHandle, hPlatform, nullptr);
1040+
*phDevice = URDevice.release();
10371041
return UR_RESULT_SUCCESS;
10381042
}
10391043

source/adapters/opencl/enqueue.cpp

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueKernelLaunch(
4545
pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize, numEventsInWaitList,
4646
CLWaitEvents.data(), &Event));
4747
if (phEvent) {
48-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
48+
auto UREvent =
49+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
50+
*phEvent = UREvent.release();
4951
}
5052
return UR_RESULT_SUCCESS;
5153
}
@@ -61,7 +63,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWait(
6163
CL_RETURN_ON_FAILURE(clEnqueueMarkerWithWaitList(
6264
hQueue->get(), numEventsInWaitList, CLWaitEvents.data(), &Event));
6365
if (phEvent) {
64-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
66+
auto UREvent =
67+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
68+
*phEvent = UREvent.release();
6569
}
6670
return UR_RESULT_SUCCESS;
6771
}
@@ -77,7 +81,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWaitWithBarrier(
7781
CL_RETURN_ON_FAILURE(clEnqueueBarrierWithWaitList(
7882
hQueue->get(), numEventsInWaitList, CLWaitEvents.data(), &Event));
7983
if (phEvent) {
80-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
84+
auto UREvent =
85+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
86+
*phEvent = UREvent.release();
8187
}
8288
return UR_RESULT_SUCCESS;
8389
}
@@ -95,7 +101,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferRead(
95101
hQueue->get(), hBuffer->get(), blockingRead, offset, size, pDst,
96102
numEventsInWaitList, CLWaitEvents.data(), &Event));
97103
if (phEvent) {
98-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
104+
auto UREvent =
105+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
106+
*phEvent = UREvent.release();
99107
}
100108
return UR_RESULT_SUCCESS;
101109
}
@@ -113,7 +121,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferWrite(
113121
hQueue->get(), hBuffer->get(), blockingWrite, offset, size, pSrc,
114122
numEventsInWaitList, CLWaitEvents.data(), &Event));
115123
if (phEvent) {
116-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
124+
auto UREvent =
125+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
126+
*phEvent = UREvent.release();
117127
}
118128
return UR_RESULT_SUCCESS;
119129
}
@@ -139,7 +149,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferReadRect(
139149
Region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch,
140150
pDst, numEventsInWaitList, CLWaitEvents.data(), &Event));
141151
if (phEvent) {
142-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
152+
auto UREvent =
153+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
154+
*phEvent = UREvent.release();
143155
}
144156
return UR_RESULT_SUCCESS;
145157
}
@@ -165,7 +177,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferWriteRect(
165177
Region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch,
166178
pSrc, numEventsInWaitList, CLWaitEvents.data(), &Event));
167179
if (phEvent) {
168-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
180+
auto UREvent =
181+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
182+
*phEvent = UREvent.release();
169183
}
170184
return UR_RESULT_SUCCESS;
171185
}
@@ -184,7 +198,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferCopy(
184198
hQueue->get(), hBufferSrc->get(), hBufferDst->get(), srcOffset, dstOffset,
185199
size, numEventsInWaitList, CLWaitEvents.data(), &Event));
186200
if (phEvent) {
187-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
201+
auto UREvent =
202+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
203+
*phEvent = UREvent.release();
188204
}
189205
return UR_RESULT_SUCCESS;
190206
}
@@ -209,7 +225,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferCopyRect(
209225
Region, srcRowPitch, srcSlicePitch, dstRowPitch, dstSlicePitch,
210226
numEventsInWaitList, CLWaitEvents.data(), &Event));
211227
if (phEvent) {
212-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
228+
auto UREvent =
229+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
230+
*phEvent = UREvent.release();
213231
}
214232
return UR_RESULT_SUCCESS;
215233
}
@@ -231,7 +249,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferFill(
231249
hQueue->get(), hBuffer->get(), pPattern, patternSize, offset, size,
232250
numEventsInWaitList, CLWaitEvents.data(), &Event));
233251
if (phEvent) {
234-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
252+
auto UREvent =
253+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
254+
*phEvent = UREvent.release();
235255
}
236256
return UR_RESULT_SUCCESS;
237257
}
@@ -271,7 +291,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferFill(
271291
}
272292

273293
if (phEvent) {
274-
*phEvent = new ur_event_handle_t_(WriteEvent, hQueue->Context, hQueue);
294+
auto UREvent = std::make_unique<ur_event_handle_t_>(
295+
WriteEvent, hQueue->Context, hQueue);
296+
*phEvent = UREvent.release();
275297
} else {
276298
CL_RETURN_ON_FAILURE(clReleaseEvent(WriteEvent));
277299
}
@@ -295,7 +317,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageRead(
295317
hQueue->get(), hImage->get(), blockingRead, Origin, Region, rowPitch,
296318
slicePitch, pDst, numEventsInWaitList, CLWaitEvents.data(), &Event));
297319
if (phEvent) {
298-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
320+
auto UREvent =
321+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
322+
*phEvent = UREvent.release();
299323
}
300324
return UR_RESULT_SUCCESS;
301325
}
@@ -316,7 +340,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageWrite(
316340
hQueue->get(), hImage->get(), blockingWrite, Origin, Region, rowPitch,
317341
slicePitch, pSrc, numEventsInWaitList, CLWaitEvents.data(), &Event));
318342
if (phEvent) {
319-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
343+
auto UREvent =
344+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
345+
*phEvent = UREvent.release();
320346
}
321347
return UR_RESULT_SUCCESS;
322348
}
@@ -339,7 +365,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageCopy(
339365
hQueue->get(), hImageSrc->get(), hImageDst->get(), SrcOrigin, DstOrigin,
340366
Region, numEventsInWaitList, CLWaitEvents.data(), &Event));
341367
if (phEvent) {
342-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
368+
auto UREvent =
369+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
370+
*phEvent = UREvent.release();
343371
}
344372
return UR_RESULT_SUCCESS;
345373
}
@@ -360,7 +388,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferMap(
360388
numEventsInWaitList, CLWaitEvents.data(),
361389
&Event, &Err);
362390
if (phEvent) {
363-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
391+
auto UREvent =
392+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
393+
*phEvent = UREvent.release();
364394
}
365395
return mapCLErrorToUR(Err);
366396
}
@@ -378,7 +408,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemUnmap(
378408
pMappedPtr, numEventsInWaitList,
379409
CLWaitEvents.data(), &Event));
380410
if (phEvent) {
381-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
411+
auto UREvent =
412+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
413+
*phEvent = UREvent.release();
382414
}
383415
return UR_RESULT_SUCCESS;
384416
}
@@ -406,7 +438,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite(
406438
Res = F(hQueue->get(), hProgram->get(), name, blockingWrite, count, offset,
407439
pSrc, numEventsInWaitList, CLWaitEvents.data(), &Event);
408440
if (phEvent) {
409-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
441+
auto UREvent =
442+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
443+
*phEvent = UREvent.release();
410444
}
411445
return mapCLErrorToUR(Res);
412446
}
@@ -434,7 +468,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead(
434468
Res = F(hQueue->get(), hProgram->get(), name, blockingRead, count, offset,
435469
pDst, numEventsInWaitList, CLWaitEvents.data(), &Event);
436470
if (phEvent) {
437-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
471+
auto UREvent =
472+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
473+
*phEvent = UREvent.release();
438474
}
439475
return mapCLErrorToUR(Res);
440476
}
@@ -463,7 +499,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueReadHostPipe(
463499
blocking, pDst, size, numEventsInWaitList,
464500
CLWaitEvents.data(), &Event));
465501
if (phEvent) {
466-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
502+
auto UREvent =
503+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
504+
*phEvent = UREvent.release();
467505
}
468506
}
469507

@@ -494,7 +532,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueWriteHostPipe(
494532
blocking, pSrc, size, numEventsInWaitList,
495533
CLWaitEvents.data(), &Event));
496534
if (phEvent) {
497-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
535+
auto UREvent =
536+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
537+
*phEvent = UREvent.release();
498538
}
499539
}
500540

source/adapters/opencl/event.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle(
115115
const ur_event_native_properties_t *pProperties,
116116
ur_event_handle_t *phEvent) {
117117
cl_event NativeHandle = reinterpret_cast<cl_event>(hNativeEvent);
118-
*phEvent = new ur_event_handle_t_(NativeHandle, hContext, nullptr);
118+
auto UREvent =
119+
std::make_unique<ur_event_handle_t_>(NativeHandle, hContext, nullptr);
120+
*phEvent = UREvent.release();
119121
if (!pProperties || !pProperties->isNativeHandleOwned) {
120122
return urEventRetain(*phEvent);
121123
}

source/adapters/opencl/kernel.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgMemObj(
393393
ur_kernel_handle_t hKernel, uint32_t argIndex,
394394
const ur_kernel_arg_mem_obj_properties_t *, ur_mem_handle_t hArgValue) {
395395

396-
cl_mem CLArgValue = hArgValue->get();
397-
cl_int RetErr = clSetKernelArg(cl_adapter::cast<cl_kernel>(hKernel),
398-
cl_adapter::cast<cl_uint>(argIndex),
399-
sizeof(hArgValue), &CLArgValue);
400-
CL_RETURN_ON_FAILURE(RetErr);
396+
cl_mem CLArgValue = hArgValue ? hArgValue->get() : nullptr;
397+
CL_RETURN_ON_FAILURE(clSetKernelArg(cl_adapter::cast<cl_kernel>(hKernel),
398+
cl_adapter::cast<cl_uint>(argIndex),
399+
sizeof(CLArgValue), &CLArgValue));
401400
return UR_RESULT_SUCCESS;
402401
}
403402

@@ -408,7 +407,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgSampler(
408407
cl_sampler CLArgSampler = hArgValue->get();
409408
cl_int RetErr = clSetKernelArg(cl_adapter::cast<cl_kernel>(hKernel),
410409
cl_adapter::cast<cl_uint>(argIndex),
411-
sizeof(hArgValue), &CLArgSampler);
410+
sizeof(CLArgSampler), &CLArgSampler);
412411
CL_RETURN_ON_FAILURE(RetErr);
413412
return UR_RESULT_SUCCESS;
414413
}

0 commit comments

Comments
 (0)