Skip to content

Commit ff5ebc1

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

File tree

12 files changed

+147
-58
lines changed

12 files changed

+147
-58
lines changed

source/adapters/opencl/command_buffer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
324324
NumberOfQueues, &CLQueue, hCommandBuffer->CLCommandBuffer,
325325
numEventsInWaitList, CLWaitEvents.data(), &Event));
326326
if (phEvent) {
327-
*phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue);
327+
auto UREvent =
328+
std::make_unique<ur_event_handle_t_>(Event, hQueue->Context, hQueue);
329+
*phEvent = UREvent.release();
328330
}
329331
return UR_RESULT_SUCCESS;
330332
}

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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ urDeviceGet(ur_platform_handle_t hPlatform, ur_device_type_t DeviceType,
8686
cl_device_type DeviceType = hPlatform->Devices[i]->Type;
8787
if (DeviceType == Type || Type == CL_DEVICE_TYPE_ALL) {
8888
if (phDevices) {
89-
phDevices[DeviceNumIter] = hPlatform->Devices[i];
89+
phDevices[DeviceNumIter] = hPlatform->Devices[i].get();
9090
}
9191
DeviceNumIter++;
9292
}
@@ -999,8 +999,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition(
999999
CLNumDevicesRet,
10001000
CLSubDevices.data(), nullptr));
10011001
for (uint32_t i = 0; i < NumDevices; i++) {
1002-
phSubDevices[i] =
1003-
new ur_device_handle_t_(CLSubDevices[i], hDevice->Platform, hDevice);
1002+
auto URSubDevice = std::make_unique<ur_device_handle_t_>(
1003+
CLSubDevices[i], hDevice->Platform, hDevice);
1004+
phSubDevices[i] = URSubDevice.release();
10041005
}
10051006
}
10061007

@@ -1033,7 +1034,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
10331034
ur_native_handle_t hNativeDevice, ur_platform_handle_t hPlatform,
10341035
const ur_device_native_properties_t *, ur_device_handle_t *phDevice) {
10351036
cl_device_id NativeHandle = reinterpret_cast<cl_device_id>(hNativeDevice);
1036-
*phDevice = new ur_device_handle_t_(NativeHandle, hPlatform, nullptr);
1037+
auto URDevice =
1038+
std::make_unique<ur_device_handle_t_>(NativeHandle, hPlatform, nullptr);
1039+
*phDevice = URDevice.release();
10371040
return UR_RESULT_SUCCESS;
10381041
}
10391042

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/memory.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
266266
cl_mem Buffer = FuncPtr(
267267
CLContext, PropertiesIntel.data(), static_cast<cl_mem_flags>(flags),
268268
size, pProperties->pHost, cl_adapter::cast<cl_int *>(&RetErr));
269-
*phBuffer = new ur_mem_handle_t_(Buffer, hContext);
269+
auto URMem = std::make_unique<ur_mem_handle_t_>(Buffer, hContext);
270+
*phBuffer = URMem.release();
270271
return mapCLErrorToUR(RetErr);
271272
}
272273
}
@@ -276,7 +277,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
276277
clCreateBuffer(hContext->get(), static_cast<cl_mem_flags>(flags), size,
277278
HostPtr, cl_adapter::cast<cl_int *>(&RetErr));
278279
CL_RETURN_ON_FAILURE(RetErr);
279-
*phBuffer = new ur_mem_handle_t_(Buffer, hContext);
280+
auto URMem = std::make_unique<ur_mem_handle_t_>(Buffer, hContext);
281+
*phBuffer = URMem.release();
280282

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

301304
return UR_RESULT_SUCCESS;
302305
}
@@ -344,7 +347,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreateWithNativeHandle(
344347
ur_native_handle_t hNativeMem, ur_context_handle_t hContext,
345348
const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) {
346349
cl_mem NativeHandle = reinterpret_cast<cl_mem>(hNativeMem);
347-
*phMem = new ur_mem_handle_t_(NativeHandle, hContext);
350+
auto URMem = std::make_unique<ur_mem_handle_t_>(NativeHandle, hContext);
351+
*phMem = URMem.release();
348352
if (!pProperties || !pProperties->isNativeHandleOwned) {
349353
return urMemRetain(*phMem);
350354
}
@@ -357,7 +361,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreateWithNativeHandle(
357361
[[maybe_unused]] const ur_image_desc_t *pImageDesc,
358362
const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) {
359363
cl_mem NativeHandle = reinterpret_cast<cl_mem>(hNativeMem);
360-
*phMem = new ur_mem_handle_t_(NativeHandle, hContext);
364+
auto URMem = std::make_unique<ur_mem_handle_t_>(NativeHandle, hContext);
365+
*phMem = URMem.release();
361366
if (!pProperties || !pProperties->isNativeHandleOwned) {
362367
return urMemRetain(*phMem);
363368
}

source/adapters/opencl/platform.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ UR_APIEXPORT ur_result_t UR_APICALL
8787
urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
8888
ur_platform_handle_t *phPlatforms, uint32_t *pNumPlatforms) {
8989

90-
static std::vector<ur_platform_handle_t> URPlatforms;
90+
static std::vector<std::unique_ptr<ur_platform_handle_t_>> URPlatforms;
9191
static std::once_flag InitFlag;
9292
static uint32_t NumPlatforms = 0;
9393
cl_int Result = CL_SUCCESS;
@@ -107,7 +107,8 @@ urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
107107
}
108108
URPlatforms.resize(NumPlatforms);
109109
for (uint32_t i = 0; i < NumPlatforms; i++) {
110-
URPlatforms[i] = new ur_platform_handle_t_(CLPlatforms[i]);
110+
URPlatforms[i] =
111+
std::make_unique<ur_platform_handle_t_>(CLPlatforms[i]);
111112
}
112113
return Result;
113114
},
@@ -125,7 +126,7 @@ urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries,
125126
}
126127
if (NumEntries && phPlatforms) {
127128
for (uint32_t i = 0; i < NumEntries; i++) {
128-
phPlatforms[i] = URPlatforms[i];
129+
phPlatforms[i] = URPlatforms[i].get();
129130
}
130131
}
131132
return mapCLErrorToUR(Result);
@@ -142,7 +143,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle(
142143
ur_platform_handle_t *phPlatform) {
143144
cl_platform_id NativeHandle =
144145
reinterpret_cast<cl_platform_id>(hNativePlatform);
145-
*phPlatform = new ur_platform_handle_t_(NativeHandle);
146+
auto URPlatform = std::make_unique<ur_platform_handle_t_>(NativeHandle);
147+
*phPlatform = URPlatform.release();
146148
return UR_RESULT_SUCCESS;
147149
}
148150

0 commit comments

Comments
 (0)