Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make malloc(0) always return nullptr #1048

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion include/hipSYCL/sycl/usm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ namespace sycl {

inline void *malloc_device(size_t num_bytes, const device &dev,
const context &ctx) {
if(num_bytes == 0)
return nullptr;
return detail::select_device_allocator(dev)->allocate(0, num_bytes);
}

Expand All @@ -72,6 +74,8 @@ T* malloc_device(std::size_t count, const queue &q) {

inline void *aligned_alloc_device(std::size_t alignment, std::size_t num_bytes,
const device &dev, const context &ctx) {
if(num_bytes == 0)
return nullptr;
return detail::select_device_allocator(dev)->allocate(alignment, num_bytes);
}

Expand All @@ -96,6 +100,9 @@ T *aligned_alloc_device(std::size_t alignment, std::size_t count,
// Restricted USM

inline void *malloc_host(std::size_t num_bytes, const context &ctx) {
if(num_bytes == 0)
return nullptr;

return detail::select_usm_allocator(ctx)->allocate_optimized_host(0, num_bytes);
}

Expand All @@ -113,6 +120,9 @@ template <typename T> T *malloc_host(std::size_t count, const queue &q) {

inline void *malloc_shared(std::size_t num_bytes, const device &dev,
const context &ctx) {
if(num_bytes == 0)
return nullptr;

return detail::select_usm_allocator(ctx, dev)->allocate_usm(num_bytes);
}

Expand All @@ -131,6 +141,9 @@ template <typename T> T *malloc_shared(std::size_t count, const queue &q) {

inline void *aligned_alloc_host(std::size_t alignment, std::size_t num_bytes,
const context &ctx) {
if(num_bytes == 0)
return nullptr;

return detail::select_usm_allocator(ctx)->allocate_optimized_host(alignment,
num_bytes);
}
Expand All @@ -154,6 +167,9 @@ T *aligned_alloc_host(std::size_t alignment, std::size_t count,

inline void *aligned_alloc_shared(std::size_t alignment, std::size_t num_bytes,
const device &dev, const context &ctx) {
if(num_bytes == 0)
return nullptr;

return detail::select_usm_allocator(ctx, dev)->allocate_usm(num_bytes);
}

Expand Down Expand Up @@ -241,7 +257,8 @@ T *aligned_alloc(std::size_t alignment, std::size_t count, const sycl::queue &q,
}

inline void free(void *ptr, const sycl::context &ctx) {
return detail::select_usm_allocator(ctx)->free(ptr);
if(ptr)
detail::select_usm_allocator(ctx)->free(ptr);
}

inline void free(void *ptr, const sycl::queue &q) {
Expand Down