shared: move NM_CMP*() macros to "nm-std-aux.h"

I would need these macros earlier in "nm-glib-aux/nm-shared-utils.h",
so it would be sufficient to just move them.

However, when I already move them, move them to "nm-std-aux/nm-std-aux.h"
because they don't need a glib dependency.
This commit is contained in:
Thomas Haller 2020-09-24 12:51:05 +02:00
parent 1c3f7d823e
commit f37dd5d394
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 102 additions and 120 deletions

View file

@ -248,126 +248,6 @@ gboolean nm_utils_ipaddr_is_normalized(int addr_family, const char *str_addr);
/*****************************************************************************/
#define NM_CMP_RETURN(c) \
G_STMT_START \
{ \
const int _cc = (c); \
if (_cc) \
return _cc < 0 ? -1 : 1; \
} \
G_STMT_END
#define NM_CMP_RETURN_DIRECT(c) \
G_STMT_START \
{ \
const int _cc = (c); \
if (_cc) \
return _cc; \
} \
G_STMT_END
#define NM_CMP_SELF(a, b) \
G_STMT_START \
{ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
\
if (_a == _b) \
return 0; \
if (!_a) \
return -1; \
if (!_b) \
return 1; \
} \
G_STMT_END
#define NM_CMP_DIRECT(a, b) \
G_STMT_START \
{ \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
\
if (_a != _b) \
return (_a < _b) ? -1 : 1; \
} \
G_STMT_END
#define NM_CMP_DIRECT_UNSAFE(a, b) \
G_STMT_START \
{ \
if ((a) != (b)) \
return ((a) < (b)) ? -1 : 1; \
} \
G_STMT_END
/* In the general case, direct pointer comparison is undefined behavior in C.
* Avoid that by casting pointers to void* and then to uintptr_t. This comparison
* is not really meaningful, except that it provides some kind of stable sort order
* between pointers (that can otherwise not be compared). */
#define NM_CMP_DIRECT_PTR(a, b) NM_CMP_DIRECT((uintptr_t)((void *) (a)), (uintptr_t)((void *) (b)))
#define NM_CMP_DIRECT_MEMCMP(a, b, size) NM_CMP_RETURN(memcmp((a), (b), (size)))
#define NM_CMP_DIRECT_STRCMP(a, b) NM_CMP_RETURN_DIRECT(strcmp((a), (b)))
#define NM_CMP_DIRECT_STRCMP0(a, b) NM_CMP_RETURN_DIRECT(nm_strcmp0((a), (b)))
#define NM_CMP_DIRECT_IN6ADDR(a, b) \
G_STMT_START \
{ \
const struct in6_addr *const _a = (a); \
const struct in6_addr *const _b = (b); \
NM_CMP_RETURN(memcmp(_a, _b, sizeof(struct in6_addr))); \
} \
G_STMT_END
#define NM_CMP_FIELD(a, b, field) NM_CMP_DIRECT(((a)->field), ((b)->field))
#define NM_CMP_FIELD_UNSAFE(a, b, field) \
G_STMT_START \
{ \
/* it's unsafe, because it evaluates the arguments more then once.
* This is necessary for bitfields, for which typeof() doesn't work. */ \
if (((a)->field) != ((b)->field)) \
return ((a)->field < ((b)->field)) ? -1 : 1; \
} \
G_STMT_END
#define NM_CMP_FIELD_BOOL(a, b, field) NM_CMP_DIRECT(!!((a)->field), !!((b)->field))
#define NM_CMP_FIELD_STR(a, b, field) NM_CMP_RETURN(strcmp(((a)->field), ((b)->field)))
#define NM_CMP_FIELD_STR_INTERNED(a, b, field) \
G_STMT_START \
{ \
const char *_a = ((a)->field); \
const char *_b = ((b)->field); \
\
if (_a != _b) { \
NM_CMP_RETURN_DIRECT(nm_strcmp0(_a, _b)); \
} \
} \
G_STMT_END
#define NM_CMP_FIELD_STR0(a, b, field) NM_CMP_RETURN_DIRECT(nm_strcmp0(((a)->field), ((b)->field)))
#define NM_CMP_FIELD_MEMCMP_LEN(a, b, field, len) \
NM_CMP_RETURN(memcmp(&((a)->field), &((b)->field), NM_MIN(len, sizeof((a)->field))))
#define NM_CMP_FIELD_MEMCMP(a, b, field) \
NM_CMP_RETURN(memcmp(&((a)->field), &((b)->field), sizeof((a)->field)))
#define NM_CMP_FIELD_IN6ADDR(a, b, field) \
G_STMT_START \
{ \
const struct in6_addr *const _a = &((a)->field); \
const struct in6_addr *const _b = &((b)->field); \
NM_CMP_RETURN(memcmp(_a, _b, sizeof(struct in6_addr))); \
} \
G_STMT_END
/*****************************************************************************/
gboolean nm_utils_memeqzero(gconstpointer data, gsize length);
/*****************************************************************************/

View file

@ -749,6 +749,108 @@ nm_steal_fd(int *p_fd)
/*****************************************************************************/
#define NM_CMP_RETURN(c) \
do { \
const int _cc = (c); \
if (_cc) \
return _cc < 0 ? -1 : 1; \
} while (0)
#define NM_CMP_RETURN_DIRECT(c) \
do { \
const int _cc = (c); \
if (_cc) \
return _cc; \
} while (0)
#define NM_CMP_SELF(a, b) \
do { \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
\
if (_a == _b) \
return 0; \
if (!_a) \
return -1; \
if (!_b) \
return 1; \
} while (0)
#define NM_CMP_DIRECT(a, b) \
do { \
typeof(a) _a = (a); \
typeof(b) _b = (b); \
\
if (_a != _b) \
return (_a < _b) ? -1 : 1; \
} while (0)
#define NM_CMP_DIRECT_UNSAFE(a, b) \
do { \
if ((a) != (b)) \
return ((a) < (b)) ? -1 : 1; \
} while (0)
/* In the general case, direct pointer comparison is undefined behavior in C.
* Avoid that by casting pointers to void* and then to uintptr_t. This comparison
* is not really meaningful, except that it provides some kind of stable sort order
* between pointers (that can otherwise not be compared). */
#define NM_CMP_DIRECT_PTR(a, b) NM_CMP_DIRECT((uintptr_t)((void *) (a)), (uintptr_t)((void *) (b)))
#define NM_CMP_DIRECT_MEMCMP(a, b, size) NM_CMP_RETURN(memcmp((a), (b), (size)))
#define NM_CMP_DIRECT_STRCMP(a, b) NM_CMP_RETURN_DIRECT(strcmp((a), (b)))
#define NM_CMP_DIRECT_STRCMP0(a, b) NM_CMP_RETURN_DIRECT(nm_strcmp0((a), (b)))
#define NM_CMP_DIRECT_IN6ADDR(a, b) \
do { \
const struct in6_addr *const _a = (a); \
const struct in6_addr *const _b = (b); \
NM_CMP_RETURN(memcmp(_a, _b, sizeof(struct in6_addr))); \
} while (0)
#define NM_CMP_FIELD(a, b, field) NM_CMP_DIRECT(((a)->field), ((b)->field))
#define NM_CMP_FIELD_UNSAFE(a, b, field) \
do { \
/* it's unsafe, because it evaluates the arguments more then once.
* This is necessary for bitfields, for which typeof() doesn't work. */ \
if (((a)->field) != ((b)->field)) \
return ((a)->field < ((b)->field)) ? -1 : 1; \
} while (0)
#define NM_CMP_FIELD_BOOL(a, b, field) NM_CMP_DIRECT(!!((a)->field), !!((b)->field))
#define NM_CMP_FIELD_STR(a, b, field) NM_CMP_RETURN(strcmp(((a)->field), ((b)->field)))
#define NM_CMP_FIELD_STR_INTERNED(a, b, field) \
do { \
const char *_a = ((a)->field); \
const char *_b = ((b)->field); \
\
if (_a != _b) { \
NM_CMP_RETURN_DIRECT(nm_strcmp0(_a, _b)); \
} \
} while (0)
#define NM_CMP_FIELD_STR0(a, b, field) NM_CMP_RETURN_DIRECT(nm_strcmp0(((a)->field), ((b)->field)))
#define NM_CMP_FIELD_MEMCMP_LEN(a, b, field, len) \
NM_CMP_RETURN(memcmp(&((a)->field), &((b)->field), NM_MIN(len, sizeof((a)->field))))
#define NM_CMP_FIELD_MEMCMP(a, b, field) \
NM_CMP_RETURN(memcmp(&((a)->field), &((b)->field), sizeof((a)->field)))
#define NM_CMP_FIELD_IN6ADDR(a, b, field) \
do { \
const struct in6_addr *const _a = &((a)->field); \
const struct in6_addr *const _b = &((b)->field); \
NM_CMP_RETURN(memcmp(_a, _b, sizeof(struct in6_addr))); \
} while (0)
/*****************************************************************************/
#define NM_AF_UNSPEC 0 /* AF_UNSPEC */
#define NM_AF_INET 2 /* AF_INET */
#define NM_AF_INET6 10 /* AF_INET6 */