Introduce RET_GATHER and use it in src/shared/

The idea is to make it easier to implement the common pattern of
accumulating errors (negative values) in an accumulator to return
the first error.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-07-13 11:12:00 +02:00
parent c8e02e408f
commit 809c3a84e1
5 changed files with 30 additions and 21 deletions
+10
View File
@@ -73,6 +73,16 @@ static inline int RET_NERRNO(int ret) {
return ret;
}
/* Collect possible errors in <acc>, so that the first error can be returned.
* Returns (possibly updated) <acc>. */
#define RET_GATHER(acc, err) \
({ \
int *__a = &(acc), __e = (err); \
if (*__a >= 0 && __e < 0) \
*__a = __e; \
*__a; \
})
static inline int errno_or_else(int fallback) {
/* To be used when invoking library calls where errno handling is not defined clearly: we return
* errno if it is set, and the specified error otherwise. The idea is that the caller initializes