4 ms·
I stole this from mongrel2, but the check macro is pretty good for error handling in C #define check(A, M, ...) \ if(!(A)) {
by alexgartrell 15y ago
I stole this from mongrel2, but the check macro is pretty good for error handling in C
#define check(A, M, ...) \
if(!(A)) { \
fprintf(stderr, M "\n", ##__VA_ARGS__); \
goto error; \
} \
}
And you use it like this
FooClass *create_foo_class()
{
FooClass *fc = NULL;
int rc;
fc = malloc(sizeof(*fc));
check(fc != NULL, "allocation failed");
rc = do_something_to_foo_class(fc);
check(rc == 0, "do_something_to_foo_class(...) failed");
return fc;
error:
if(fc != NULL) free(fc);
/* Other cleanup */
return NULL;
}