diff options
author | Thomas Haller <thaller@redhat.com> | 2017-07-26 11:23:35 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-07-31 15:13:31 +0200 |
commit | 8d03caf599e6f6737a333aeccda0ee22dc66beeb (patch) | |
tree | f38836a418f5cc9aa6c12965c9633de297b2b619 | |
parent | b9fd352eca365a5f21d969bee653a6420033fb6e (diff) | |
download | NetworkManager-8d03caf599e6f6737a333aeccda0ee22dc66beeb.tar.gz |
shared: cleanup NM_CMP_*() macros
-rw-r--r-- | shared/nm-utils/nm-shared-utils.h | 86 | ||||
-rw-r--r-- | src/platform/nm-platform.c | 52 |
2 files changed, 74 insertions, 64 deletions
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h index 8ff4c3cf05..c4306f4120 100644 --- a/shared/nm-utils/nm-shared-utils.h +++ b/shared/nm-utils/nm-shared-utils.h @@ -24,79 +24,89 @@ /*****************************************************************************/ +#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_SELF(a, b) \ G_STMT_START { \ - if ((a) == (b)) \ + typeof (a) _a = (a); \ + typeof (b) _b = (b); \ + \ + if (_a == _b) \ return 0; \ - if (!(a)) \ + if (!_a) \ return -1; \ - if (!(b)) \ + if (!_b) \ return 1; \ } G_STMT_END #define NM_CMP_DIRECT(a, b) \ G_STMT_START { \ - if ((a) != (b)) \ - return ((a) < (b)) ? -1 : 1; \ + typeof (a) _a = (a); \ + typeof (b) _b = (b); \ + \ + if (_a != _b) \ + return (_a < _b) ? -1 : 1; \ } G_STMT_END #define NM_CMP_DIRECT_MEMCMP(a, b, size) \ + NM_CMP_RETURN (memcmp ((a), (b), (size))) + +#define NM_CMP_DIRECT_IN6ADDR(a, b) \ G_STMT_START { \ - int c = memcmp ((a), (b), (size)); \ - if (c != 0) \ - return c < 0 ? -1 : 1; \ + 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; \ + return ((a)->field < ((b)->field)) ? -1 : 1; \ } G_STMT_END #define NM_CMP_FIELD_BOOL(a, b, field) \ - G_STMT_START { \ - if ((!((a)->field)) != (!((b)->field))) \ - return ((!((a)->field)) < (!((b)->field))) ? -1 : 1; \ - } G_STMT_END + NM_CMP_DIRECT (!!((a)->field), !!((b)->field)) #define NM_CMP_FIELD_STR(a, b, field) \ - G_STMT_START { \ - int c = strcmp ((a)->field, (b)->field); \ - if (c != 0) \ - return c < 0 ? -1 : 1; \ - } G_STMT_END + NM_CMP_RETURN (strcmp (((a)->field), ((b)->field))) #define NM_CMP_FIELD_STR_INTERNED(a, b, field) \ G_STMT_START { \ - if (((a)->field) != ((b)->field)) { \ - /* just to be sure, also do a strcmp() if the pointers don't match */ \ - int c = g_strcmp0 ((a)->field, (b)->field); \ - if (c != 0) \ - return c < 0 ? -1 : 1; \ + const char *_a = ((a)->field); \ + const char *_b = ((b)->field); \ + \ + if (_a != _b) { \ + NM_CMP_RETURN (g_strcmp0 (_a, _b)); \ } \ } G_STMT_END #define NM_CMP_FIELD_STR0(a, b, field) \ - G_STMT_START { \ - int c = g_strcmp0 ((a)->field, (b)->field); \ - if (c != 0) \ - return c < 0 ? -1 : 1; \ - } G_STMT_END + NM_CMP_RETURN (g_strcmp0 (((a)->field), ((b)->field))) #define NM_CMP_FIELD_MEMCMP_LEN(a, b, field, len) \ - G_STMT_START { \ - int c = memcmp (&((a)->field), &((b)->field), \ - MIN (len, sizeof ((a)->field))); \ - if (c != 0) \ - return c < 0 ? -1 : 1; \ - } G_STMT_END + NM_CMP_RETURN (memcmp (&((a)->field), &((b)->field), \ + 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 { \ - int c = memcmp (&((a)->field), &((b)->field), \ - sizeof ((a)->field)); \ - if (c != 0) \ - return c < 0 ? -1 : 1; \ + 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 /*****************************************************************************/ diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 590ac101f1..3cdfba882a 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -4363,7 +4363,7 @@ nm_platform_link_cmp (const NMPlatformLink *a, const NMPlatformLink *b) NM_CMP_FIELD (a, b, master); NM_CMP_FIELD (a, b, parent); NM_CMP_FIELD (a, b, n_ifi_flags); - NM_CMP_FIELD (a, b, connected); + NM_CMP_FIELD_UNSAFE (a, b, connected); NM_CMP_FIELD (a, b, mtu); NM_CMP_FIELD_BOOL (a, b, initialized); NM_CMP_FIELD (a, b, arptype); @@ -4524,12 +4524,12 @@ nm_platform_lnk_macsec_cmp (const NMPlatformLnkMacsec *a, const NMPlatformLnkMac NM_CMP_FIELD (a, b, window); NM_CMP_FIELD (a, b, encoding_sa); NM_CMP_FIELD (a, b, validation); - NM_CMP_FIELD (a, b, encrypt); - NM_CMP_FIELD (a, b, protect); - NM_CMP_FIELD (a, b, include_sci); - NM_CMP_FIELD (a, b, es); - NM_CMP_FIELD (a, b, scb); - NM_CMP_FIELD (a, b, replay_protect); + NM_CMP_FIELD_UNSAFE (a, b, encrypt); + NM_CMP_FIELD_UNSAFE (a, b, protect); + NM_CMP_FIELD_UNSAFE (a, b, include_sci); + NM_CMP_FIELD_UNSAFE (a, b, es); + NM_CMP_FIELD_UNSAFE (a, b, scb); + NM_CMP_FIELD_UNSAFE (a, b, replay_protect); return 0; } @@ -4549,8 +4549,8 @@ nm_platform_lnk_macvlan_cmp (const NMPlatformLnkMacvlan *a, const NMPlatformLnkM { NM_CMP_SELF (a, b); NM_CMP_FIELD (a, b, mode); - NM_CMP_FIELD (a, b, no_promisc); - NM_CMP_FIELD (a, b, tap); + NM_CMP_FIELD_UNSAFE (a, b, no_promisc); + NM_CMP_FIELD_UNSAFE (a, b, tap); return 0; } @@ -4780,13 +4780,13 @@ nm_platform_ip4_route_cmp_full (const NMPlatformIP4Route *a, const NMPlatformIP4 NM_CMP_FIELD (a, b, mss); NM_CMP_FIELD (a, b, scope_inv); NM_CMP_FIELD (a, b, pref_src); - NM_CMP_FIELD (a, b, rt_cloned); + NM_CMP_FIELD_UNSAFE (a, b, rt_cloned); NM_CMP_FIELD (a, b, tos); - NM_CMP_FIELD (a, b, lock_window); - NM_CMP_FIELD (a, b, lock_cwnd); - NM_CMP_FIELD (a, b, lock_initcwnd); - NM_CMP_FIELD (a, b, lock_initrwnd); - NM_CMP_FIELD (a, b, lock_mtu); + NM_CMP_FIELD_UNSAFE (a, b, lock_window); + NM_CMP_FIELD_UNSAFE (a, b, lock_cwnd); + NM_CMP_FIELD_UNSAFE (a, b, lock_initcwnd); + NM_CMP_FIELD_UNSAFE (a, b, lock_initrwnd); + NM_CMP_FIELD_UNSAFE (a, b, lock_mtu); NM_CMP_FIELD (a, b, window); NM_CMP_FIELD (a, b, cwnd); NM_CMP_FIELD (a, b, initcwnd); @@ -4833,29 +4833,29 @@ nm_platform_ip6_route_cmp_full (const NMPlatformIP6Route *a, const NMPlatformIP6 NM_CMP_SELF (a, b); NM_CMP_FIELD (a, b, ifindex); if (consider_host_part) - NM_CMP_FIELD_MEMCMP (a, b, network); + NM_CMP_FIELD_IN6ADDR (a, b, network); else { struct in6_addr n1, n2; nm_utils_ip6_address_clear_host_address (&n1, &a->network, a->plen); nm_utils_ip6_address_clear_host_address (&n2, &b->network, b->plen); - NM_CMP_DIRECT_MEMCMP (&n1, &n2, sizeof (struct in6_addr)); + NM_CMP_DIRECT_IN6ADDR (&n1, &n2); } NM_CMP_FIELD (a, b, plen); NM_CMP_FIELD (a, b, metric); - NM_CMP_FIELD_MEMCMP (a, b, gateway); - NM_CMP_FIELD_MEMCMP (a, b, pref_src); - NM_CMP_FIELD_MEMCMP (a, b, src); + NM_CMP_FIELD_IN6ADDR (a, b, gateway); + NM_CMP_FIELD_IN6ADDR (a, b, pref_src); + NM_CMP_FIELD_IN6ADDR (a, b, src); NM_CMP_FIELD (a, b, src_plen); NM_CMP_FIELD (a, b, rt_source); NM_CMP_FIELD (a, b, mss); - NM_CMP_FIELD (a, b, rt_cloned); + NM_CMP_FIELD_UNSAFE (a, b, rt_cloned); NM_CMP_FIELD (a, b, tos); - NM_CMP_FIELD (a, b, lock_window); - NM_CMP_FIELD (a, b, lock_cwnd); - NM_CMP_FIELD (a, b, lock_initcwnd); - NM_CMP_FIELD (a, b, lock_initrwnd); - NM_CMP_FIELD (a, b, lock_mtu); + NM_CMP_FIELD_UNSAFE (a, b, lock_window); + NM_CMP_FIELD_UNSAFE (a, b, lock_cwnd); + NM_CMP_FIELD_UNSAFE (a, b, lock_initcwnd); + NM_CMP_FIELD_UNSAFE (a, b, lock_initrwnd); + NM_CMP_FIELD_UNSAFE (a, b, lock_mtu); NM_CMP_FIELD (a, b, window); NM_CMP_FIELD (a, b, cwnd); NM_CMP_FIELD (a, b, initcwnd); |