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.
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
|
||||
+2
-2
@@ -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. */
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user