Skip to content

Commit 5083f4f

Browse files
authored
Merge pull request oneapi-src#1397 from GeorgeWeb/georgi/check-allocation-error-on-event-from-native-handle
[CUDA][HIP] Catch and report bad_alloc errors for event object creation
2 parents a97eed1 + 4c3f9ab commit 5083f4f

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

source/adapters/cuda/event.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,17 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle(
280280

281281
std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr};
282282

283-
*phEvent = ur_event_handle_t_::makeWithNative(
284-
hContext, reinterpret_cast<CUevent>(hNativeEvent));
283+
try {
284+
EventPtr =
285+
std::unique_ptr<ur_event_handle_t_>(ur_event_handle_t_::makeWithNative(
286+
hContext, reinterpret_cast<CUevent>(hNativeEvent)));
287+
} catch (const std::bad_alloc &) {
288+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
289+
} catch (...) {
290+
return UR_RESULT_ERROR_UNKNOWN;
291+
}
292+
293+
*phEvent = EventPtr.release();
285294

286295
return UR_RESULT_SUCCESS;
287296
}

source/adapters/hip/event.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle(
327327
ur_event_handle_t *phEvent) {
328328
std::ignore = pProperties;
329329

330-
*phEvent = ur_event_handle_t_::makeWithNative(
331-
hContext, reinterpret_cast<hipEvent_t>(hNativeEvent));
330+
std::unique_ptr<ur_event_handle_t_> EventPtr{nullptr};
331+
332+
try {
333+
EventPtr =
334+
std::unique_ptr<ur_event_handle_t_>(ur_event_handle_t_::makeWithNative(
335+
hContext, reinterpret_cast<hipEvent_t>(hNativeEvent)));
336+
} catch (const std::bad_alloc &) {
337+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
338+
} catch (...) {
339+
return UR_RESULT_ERROR_UNKNOWN;
340+
}
341+
342+
*phEvent = EventPtr.release();
332343

333344
return UR_RESULT_SUCCESS;
334345
}

0 commit comments

Comments
 (0)