Skip to content

Commit 708a0bc

Browse files
author
Hugh Delaney
committed
Don't check for nullptr
If new fails then std::bad_alloc will be thrown. Also add the same checking for urMemBufferCreate.
1 parent 2b501a1 commit 708a0bc

File tree

2 files changed

+4
-14
lines changed

2 files changed

+4
-14
lines changed

source/adapters/cuda/memory.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
5656

5757
auto URMemObj = std::unique_ptr<ur_mem_handle_t_>(
5858
new ur_mem_handle_t_{hContext, flags, AllocMode, HostPtr, size});
59-
if (URMemObj == nullptr) {
60-
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
61-
}
6259

6360
// First allocation will be made at urMemBufferCreate if context only
6461
// has one device
@@ -74,6 +71,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
7471
MemObj = URMemObj.release();
7572
} catch (ur_result_t Err) {
7673
return Err;
74+
} catch (std::bad_alloc &Err) {
75+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
7776
} catch (...) {
7877
return UR_RESULT_ERROR_OUT_OF_RESOURCES;
7978
}
@@ -245,10 +244,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreate(
245244
}
246245
}
247246

248-
if (URMemObj == nullptr) {
249-
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
250-
}
251-
252247
*phMem = URMemObj.release();
253248
} catch (ur_result_t Err) {
254249
return Err;

source/adapters/hip/memory.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
133133

134134
auto URMemObj = std::unique_ptr<ur_mem_handle_t_>(
135135
new ur_mem_handle_t_{hContext, flags, AllocMode, HostPtr, size});
136-
if (URMemObj == nullptr) {
137-
throw UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
138-
}
139136

140137
// First allocation will be made at urMemBufferCreate if context only
141138
// has one device
@@ -157,6 +154,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate(
157154
RetMemObj = URMemObj.release();
158155
} catch (ur_result_t Err) {
159156
Result = Err;
157+
} catch (std::bad_alloc &Err) {
158+
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
160159
} catch (...) {
161160
Result = UR_RESULT_ERROR_OUT_OF_RESOURCES;
162161
}
@@ -384,10 +383,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreate(
384383
auto URMemObj = std::unique_ptr<ur_mem_handle_t_>(new ur_mem_handle_t_{
385384
hContext, flags, *pImageFormat, *pImageDesc, pHost});
386385

387-
if (URMemObj == nullptr) {
388-
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
389-
}
390-
391386
if (PerformInitialCopy) {
392387
for (const auto &Dev : hContext->getDevices()) {
393388
ScopedContext Active(Dev);

0 commit comments

Comments
 (0)