Skip to content

Commit c06721c

Browse files
committed
Make host device return nullptr for bad mallocs (huge, 0, etc)
Signed-off-by: James Brodman <[email protected]>
1 parent e247bf6 commit c06721c

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

sycl/include/CL/sycl/detail/aligned_allocator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ template <typename T> class aligned_allocator {
5656

5757
pointer Result = reinterpret_cast<pointer>(
5858
detail::OSUtil::alignedAlloc(MAlignment, NumBytes));
59-
if (!Result)
59+
if (!Result || NumBytes == 0)
6060
throw std::bad_alloc();
6161
return Result;
6262
}

sycl/source/detail/usm/usm_impl.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,21 @@ void *alignedAlloc(size_t Alignment, size_t Size, const context &Ctxt,
7171
const device &Dev, alloc Kind) {
7272
void *RetVal = nullptr;
7373
if (Ctxt.is_host()) {
74-
if (!Alignment) {
75-
// worst case default
76-
Alignment = 128;
77-
}
78-
79-
aligned_allocator<char> Alloc(Alignment);
80-
try {
81-
RetVal = Alloc.allocate(Size);
82-
} catch (const std::bad_alloc &) {
83-
// Conform with Specification behavior
74+
if (Kind == alloc::unknown) {
8475
RetVal = nullptr;
76+
} else {
77+
if (!Alignment) {
78+
// worst case default
79+
Alignment = 128;
80+
}
81+
82+
aligned_allocator<char> Alloc(Alignment);
83+
try {
84+
RetVal = Alloc.allocate(Size);
85+
} catch (const std::bad_alloc &) {
86+
// Conform with Specification behavior
87+
RetVal = nullptr;
88+
}
8589
}
8690
} else {
8791
std::shared_ptr<context_impl> CtxImpl = detail::getSyclObjImpl(Ctxt);

0 commit comments

Comments
 (0)