Short version: Is there an official/correct way to query for the size of strings like CL_PLATFORM_VENDOR?
Long version:
Looking at OpenCL functions like clGetPlatformInfo I want to find out how much space to allocate for the result. The function has this signature:
cl_int clGetPlatformInfo(cl_platform_id platform,
cl_platform_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret)
The docs say:
param_value_size
Specifies the size in bytes of memory pointed to by
param_value. This size in bytes must be greater
than or equal to size of return type specified in the
table below.
All of the return types are listed as char[]. I wanted to know how much space to reserve so I called it like this, where I pass 0 for param_value_size and NULL for param_value, hoping to get the correct size return in param_value_size_ret:
size_t size = 0;
l_success = clGetPlatformInfo(platform_id,
CL_PLATFORM_VENDOR, 0, NULL, &size);
if( l_success != CL_SUCCESS)
{
printf("Failed getting vendor name size.\n");
return -1;
}
printf("l_success = %d, size = %d\n", l_success, size);
char* vendor = NULL;
vendor = malloc(size);
if( vendor )
{
l_success = clGetPlatformInfo(platform_id,
CL_PLATFORM_VENDOR, size, vendor, &size);
if( l_success != CL_SUCCESS )
{
printf("Failed getting vendor name.\n");
return -1;
}
printf("Vendor name is '%s', length is %d\n", vendor, strlen(vendor));
} else {
printf("malloc failed.\n");
return -1;
}
It behaved as I had hoped, it return a size of 19 for the string, “NVIDIA Corporation” (size included null terminator) and strlen return 18. Is this the “right” way to query for parameter size or am I just getting lucky with my vendor’s implementation? Has anyone seen this idiom fail on some vendor?
Edit: The bit that is tripping me up is this, “This size in bytes must be greater than or equal to size of return type”, it seems like when I pass 0 and NULL the call should fail because that’s not greater than or equal to the size of the returned value. I’m not sure why they say “return type”.
Yes, it is the right way.
In the same doc you’ve mentioned:
And below:
So even if it is not stated explicitly, if
param_valueis NULL no error should be produced, so the code is supposed to work properly.Here is a piece of code from Khronos OpenCL C++ bindings (specs). They too do it this way, and I think it counts as “official”: