From: Daan De Meyer Date: Tue, 6 May 2025 07:53:56 +0000 (+0200) Subject: macro: Introduce ABS() macro and use it in header files X-Git-Tag: v258-rc1~690^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=40a6cdc29e94aa2daa606467466ca9eb92069750;p=thirdparty%2Fsystemd.git macro: Introduce ABS() macro and use it in header files abs() requires including the entirety of stdlib.h just for a single trivial function. Let's introduce the ABS() macro and use it in header files instead so we can get rid of stdlib.h transitive includes in header files in a later commit. --- diff --git a/src/basic/errno-list.h b/src/basic/errno-list.h index 3c15afdaab6..afe54d0e8c4 100644 --- a/src/basic/errno-list.h +++ b/src/basic/errno-list.h @@ -2,9 +2,10 @@ #pragma once #include -#include #include +#include "macro.h" + /* * MAX_ERRNO is defined as 4095 in linux/err.h * We use the same value here. @@ -16,7 +17,7 @@ static inline const char* errno_to_name(int id) { if (id == 0) /* To stay in line with our own impl */ return NULL; - return strerrorname_np(abs(id)); + return strerrorname_np(ABS(id)); } #else const char* errno_to_name(int id); diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h index 01b4d59e045..10118c6901b 100644 --- a/src/basic/errno-util.h +++ b/src/basic/errno-util.h @@ -2,7 +2,6 @@ #pragma once #include -#include #include #include "assert-util.h" @@ -16,7 +15,7 @@ * https://stackoverflow.com/questions/34880638/compound-literal-lifetime-and-if-blocks * * Note that we use the GNU variant of strerror_r() here. */ -#define STRERROR(errnum) strerror_r(abs(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN) +#define STRERROR(errnum) strerror_r(ABS(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN) /* A helper to print an error message or message for functions that return 0 on EOF. * Note that we can't use ({ … }) to define a temporary variable, so errnum is @@ -41,7 +40,7 @@ static inline void _reset_errno_(int *saved_errno) { #define LOCAL_ERRNO(value) \ PROTECT_ERRNO; \ - errno = abs(value) + errno = ABS(value) static inline int negative_errno(void) { /* This helper should be used to shut up gcc if you know 'errno' is @@ -93,15 +92,14 @@ static inline int errno_or_else(int fallback) { if (errno > 0) return -errno; - return -abs(fallback); + return -ABS(fallback); } -/* abs(3) says: Trying to take the absolute value of the most negative integer is not defined. */ #define _DEFINE_ABS_WRAPPER(name) \ static inline bool ERRNO_IS_##name(intmax_t r) { \ if (r == INTMAX_MIN) \ return false; \ - return ERRNO_IS_NEG_##name(-imaxabs(r)); \ + return ERRNO_IS_NEG_##name(-ABS(r)); \ } assert_cc(INT_MAX <= INTMAX_MAX); diff --git a/src/basic/log.h b/src/basic/log.h index 9759b28dc03..845a458f3ed 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -33,9 +33,9 @@ typedef enum LogTarget{ #define LOG_NULL (LOG_EMERG - 1) assert_cc(LOG_NULL == -1); -#define SYNTHETIC_ERRNO(num) (abs(num) | (1 << 30)) +#define SYNTHETIC_ERRNO(num) (ABS(num) | (1 << 30)) #define IS_SYNTHETIC_ERRNO(val) (((val) >> 30) == 1) -#define ERRNO_VALUE(val) (abs(val) & ~(1 << 30)) +#define ERRNO_VALUE(val) (ABS(val) & ~(1 << 30)) /* The callback function to be invoked when syntax warnings are seen * in the unit files. */ diff --git a/src/basic/macro.h b/src/basic/macro.h index 4ee1ad4d578..cf1c1a46175 100644 --- a/src/basic/macro.h +++ b/src/basic/macro.h @@ -105,7 +105,7 @@ static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) { #define return_with_errno(r, err) \ do { \ - errno = abs(err); \ + errno = ABS(err); \ return r; \ } while (false) diff --git a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-util.h b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-util.h index 4e9535a9951..51d30c845e3 100644 --- a/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-util.h +++ b/src/cryptsetup/cryptsetup-tokens/cryptsetup-token-util.h @@ -6,6 +6,8 @@ #include #include +#include "macro.h" + /* crypt_dump() internal indentation magic */ #define CRYPT_DUMP_LINE_SEP "\n\t " @@ -15,7 +17,7 @@ #define crypt_log(cd, ...) crypt_logf(cd, CRYPT_LOG_NORMAL, __VA_ARGS__) #define crypt_log_full_errno(cd, e, lvl, ...) ({ \ - int _e = abs(e), _s = errno; \ + int _e = ABS(e), _s = errno; \ errno = _e; \ crypt_logf(cd, lvl, __VA_ARGS__); \ errno = _s; \ diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index dbf0a7d8466..417a7853d4c 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -176,6 +176,13 @@ UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \ }) +#define ABS(a) __ABS(UNIQ, (a)) +#define __ABS(aq, a) \ +({ \ + const typeof(a) UNIQ_T(A, aq) = (a); \ + UNIQ_T(A, aq) < 0 ? -UNIQ_T(A, aq) : UNIQ_T(A, aq); \ +}) + #define IS_UNSIGNED_INTEGER_TYPE(type) \ (__builtin_types_compatible_p(typeof(type), unsigned char) || \ __builtin_types_compatible_p(typeof(type), unsigned short) || \ diff --git a/src/libsystemd/sd-journal/journal-internal.h b/src/libsystemd/sd-journal/journal-internal.h index b13f7a45dc4..badec0e9438 100644 --- a/src/libsystemd/sd-journal/journal-internal.h +++ b/src/libsystemd/sd-journal/journal-internal.h @@ -149,7 +149,7 @@ int journal_add_matchf(sd_journal *j, const char *format, ...) _printf_(2, 3); /* All errors that we might encounter while extracting a field that are not real errors, * but only mean that the field is too large or we don't support the compression. */ static inline bool JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(int r) { - return IN_SET(abs(r), + return IN_SET(ABS(r), ENOBUFS, /* Field or decompressed field too large */ E2BIG, /* Field too large for pointer width */ EPROTONOSUPPORT); /* Unsupported compression */ diff --git a/src/shared/plymouth-util.h b/src/shared/plymouth-util.h index d7600767b58..9949e5dc83e 100644 --- a/src/shared/plymouth-util.h +++ b/src/shared/plymouth-util.h @@ -10,5 +10,5 @@ int plymouth_send_raw(const void *raw, size_t size, int flags); int plymouth_send_msg(const char *text, bool pause_spinner); static inline bool ERRNO_IS_NO_PLYMOUTH(int r) { - return IN_SET(abs(r), EAGAIN, ENOENT) || ERRNO_IS_DISCONNECT(r); + return IN_SET(ABS(r), EAGAIN, ENOENT) || ERRNO_IS_DISCONNECT(r); } diff --git a/src/shared/tests.h b/src/shared/tests.h index 5a37c56324b..d19ffcea8c2 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -40,7 +40,7 @@ static inline void log_set_assert_return_is_criticalp(bool *p) { #define ASSERT_RETURN_EXPECTED_SE(expr) ASSERT_RETURN_EXPECTED(assert_se(expr)); static inline bool manager_errno_skip_test(int r) { - return IN_SET(abs(r), + return IN_SET(ABS(r), EPERM, EACCES, EADDRINUSE,