using namespace clover;
+namespace {
+ const cl_mem_flags dev_access_flags =
+ CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY;
+ const cl_mem_flags host_ptr_flags =
+ CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR;
+ const cl_mem_flags all_mem_flags =
+ dev_access_flags | host_ptr_flags;
+
+ void
+ validate_flags(cl_mem_flags flags, cl_mem_flags valid) {
+ if ((flags & ~valid) ||
+ util_bitcount(flags & dev_access_flags) > 1)
+ throw error(CL_INVALID_VALUE);
+
+ if ((flags & CL_MEM_USE_HOST_PTR) &&
+ (flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_ALLOC_HOST_PTR)))
+ throw error(CL_INVALID_VALUE);
+ }
+}
+
CLOVER_API cl_mem
clCreateBuffer(cl_context d_ctx, cl_mem_flags flags, size_t size,
void *host_ptr, cl_int *r_errcode) try {
auto &ctx = obj(d_ctx);
+ validate_flags(flags, all_mem_flags);
+
if (bool(host_ptr) != bool(flags & (CL_MEM_USE_HOST_PTR |
CL_MEM_COPY_HOST_PTR)))
throw error(CL_INVALID_HOST_PTR);
))
throw error(CL_INVALID_BUFFER_SIZE);
- if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
- CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |
- CL_MEM_COPY_HOST_PTR))
- throw error(CL_INVALID_VALUE);
-
- if (util_bitcount(flags & (CL_MEM_READ_ONLY | CL_MEM_WRITE_ONLY |
- CL_MEM_READ_WRITE)) > 1)
- throw error(CL_INVALID_VALUE);
-
- if ((flags & CL_MEM_USE_HOST_PTR) &&
- (flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_ALLOC_HOST_PTR)))
- throw error(CL_INVALID_VALUE);
-
ret_error(r_errcode, CL_SUCCESS);
return new root_buffer(ctx, flags, size, host_ptr);
const void *op_info, cl_int *r_errcode) try {
auto &parent = obj<root_buffer>(d_mem);
- if ((flags & (CL_MEM_USE_HOST_PTR |
- CL_MEM_ALLOC_HOST_PTR |
- CL_MEM_COPY_HOST_PTR)) ||
- (~flags & parent.flags() & (CL_MEM_READ_ONLY |
- CL_MEM_WRITE_ONLY)))
+ validate_flags(flags, dev_access_flags);
+
+ if (~flags & parent.flags() &
+ (dev_access_flags & ~CL_MEM_READ_WRITE))
throw error(CL_INVALID_VALUE);
if (op == CL_BUFFER_CREATE_TYPE_REGION) {
void *host_ptr, cl_int *r_errcode) try {
auto &ctx = obj(d_ctx);
+ validate_flags(flags, all_mem_flags);
+
if (!any_of(std::mem_fn(&device::image_support), ctx.devices()))
throw error(CL_INVALID_OPERATION);
- if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
- CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |
- CL_MEM_COPY_HOST_PTR))
- throw error(CL_INVALID_VALUE);
-
if (!format)
throw error(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR);
void *host_ptr, cl_int *r_errcode) try {
auto &ctx = obj(d_ctx);
+ validate_flags(flags, all_mem_flags);
+
if (!any_of(std::mem_fn(&device::image_support), ctx.devices()))
throw error(CL_INVALID_OPERATION);
- if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
- CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |
- CL_MEM_COPY_HOST_PTR))
- throw error(CL_INVALID_VALUE);
-
if (!format)
throw error(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR);
cl_mem_object_type type, cl_uint count,
cl_image_format *r_buf, cl_uint *r_count) try {
auto &ctx = obj(d_ctx);
+ auto formats = supported_formats(ctx, type);
- if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY |
- CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR |
- CL_MEM_COPY_HOST_PTR))
- throw error(CL_INVALID_VALUE);
+ validate_flags(flags, all_mem_flags);
if (r_buf && !r_count)
throw error(CL_INVALID_VALUE);
- auto formats = supported_formats(ctx, type);
-
if (r_buf)
std::copy_n(formats.begin(),
std::min((cl_uint)formats.size(), count),