Skip to content

Instantly share code, notes, and snippets.

@cdwfs
Created July 18, 2014 23:30
Show Gist options
  • Save cdwfs/070c6496fedf04b4873b to your computer and use it in GitHub Desktop.
Save cdwfs/070c6496fedf04b4873b to your computer and use it in GitHub Desktop.
Return value error-checking macro generator
/**
* Handy meta-macro to simplify repetitive error checking for APIs where every function returns
* an error code (e.g. CUDA, C11 threads, most of the Windows API, etc.)
*
* Example usage:
* #define CUDA_CHECK(expr) RETVAL_CHECK(cudaSuccess, expr)
* ...
* CUDA_CHECK( cudaMemcpy(dst, src, size, cudaMemcpyHostToDevice) );
*
* To disable the checks in release builds, just redefine the macro:
* #define CUDA_CHECK(expr) expr
*
* As written, it's Visual C++ specific, but don't let that stop you!
*/
#define RETVAL_CHECK(expected, expr) do { \
int err = (expr); \
if (err != (expected)) { \
printf("%s(%d): error in %s() -- %s returned %d\n", __FILE__, __LINE__, __FUNCTION__, #expr, err); \
__debugbreak(); \
} \
assert(err == (expected)); \
__pragma(warning(push)) \
__pragma(warning(disable:4127)) \
} while(0) \
__pragma(warning(pop))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment