diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-10-11 00:45:47 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-10-11 00:45:47 +0200 |
commit | 18387b5983150181dd9dee8edf1573285eecbaa4 (patch) | |
tree | 60818e4f9000af107fc71d785e29cfd0da410360 /src | |
parent | eaa3cbef3b8c214cd5c2d75b04e70ad477187e17 (diff) |
macro: add new assert_return() macro for early parameter checking in functions
For the library functions we expose we currently repeatedly use checks
like the following:
if (!value_is_ok(parameter1))
return -EINVAL;
if (!value_is_ok(parameter2))
return -EINVAL;
And so on. Let's turn this into a macro:
assert_return(value_is_ok(parameter1), -EINVAL);
assert_return(value_is_ok(paramater2), -EINVAL);
This makes our code a bit shorter and simpler, and also allows us to add
a _unlikely_() around the check.
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/macro.h | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/src/shared/macro.h b/src/shared/macro.h index d4f92b60ec..06e16cd453 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -159,6 +159,12 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) { } while (false) #endif +#define assert_return(expr, r) \ + do { \ + if (!(expr)) \ + return (r); \ + } while (false) + #define PTR_TO_INT(p) ((int) ((intptr_t) (p))) #define INT_TO_PTR(u) ((void *) ((intptr_t) (u))) #define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p))) |