]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: Convert compare_func's to use CMP() macro wherever possible.
authorFilipe Brandenburger <filbranden@google.com>
Thu, 2 Aug 2018 23:43:37 +0000 (16:43 -0700)
committerFilipe Brandenburger <filbranden@google.com>
Tue, 7 Aug 2018 02:26:35 +0000 (19:26 -0700)
Looked for definitions of functions using the *_compare_func() suffix.

Tested:
- Unit tests passed (ninja -C build/ test)
- Installed this build and booted with it.

14 files changed:
src/basic/hash-funcs.c
src/basic/in-addr-util.c
src/basic/process-util.c
src/core/socket.c
src/journal/catalog.c
src/libsystemd-network/lldp-neighbor.c
src/libsystemd-network/sd-dhcp-server.c
src/network/networkd-address.c
src/network/networkd-route.c
src/network/networkd-routing-policy-rule.c
src/resolve/resolved-dns-cache.c
src/resolve/resolved-dns-packet.c
src/resolve/resolved-dns-rr.c
src/resolve/resolved-dns-server.c

index db48437be754ea03d972e1b8bbd561a3b9a8fdf5..6f6832d23734ecb8a279ab81e262553ac5c2bd6b 100644 (file)
@@ -71,7 +71,7 @@ void trivial_hash_func(const void *p, struct siphash *state) {
 }
 
 int trivial_compare_func(const void *a, const void *b) {
-        return a < b ? -1 : (a > b ? 1 : 0);
+        return CMP(a, b);
 }
 
 const struct hash_ops trivial_hash_ops = {
@@ -87,7 +87,7 @@ int uint64_compare_func(const void *_a, const void *_b) {
         uint64_t a, b;
         a = *(const uint64_t*) _a;
         b = *(const uint64_t*) _b;
-        return a < b ? -1 : (a > b ? 1 : 0);
+        return CMP(a, b);
 }
 
 const struct hash_ops uint64_hash_ops = {
@@ -104,7 +104,7 @@ int devt_compare_func(const void *_a, const void *_b) {
         dev_t a, b;
         a = *(const dev_t*) _a;
         b = *(const dev_t*) _b;
-        return a < b ? -1 : (a > b ? 1 : 0);
+        return CMP(a, b);
 }
 
 const struct hash_ops devt_hash_ops = {
index a21aa149f558ccd8469c42434ca6c567d8bc0d56..d83658eaa701ab2a174ea7ee05df80870fa4d4de 100644 (file)
@@ -581,9 +581,11 @@ void in_addr_data_hash_func(const void *p, struct siphash *state) {
 
 int in_addr_data_compare_func(const void *a, const void *b) {
         const struct in_addr_data *x = a, *y = b;
+        int r;
 
-        if (x->family != y->family)
-                return x->family - y->family;
+        r = CMP(x->family, y->family);
+        if (r != 0)
+                return r;
 
         return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family));
 }
index 0a5280eb2420c0bd64b46d84eb470e0628335aa3..d6ee8b62b81272e677b123d9c18d1eb65e52682b 100644 (file)
@@ -1108,12 +1108,7 @@ int pid_compare_func(const void *a, const void *b) {
         const pid_t *p = a, *q = b;
 
         /* Suitable for usage in qsort() */
-
-        if (*p < *q)
-                return -1;
-        if (*p > *q)
-                return 1;
-        return 0;
+        return CMP(*p, *q);
 }
 
 int ioprio_parse_priority(const char *s, int *ret) {
index aedbf51a4cea2d057fa5b56a7fba80e1662243d4..fb17b3be2dfca17379cb215a0ea0743ba3672428 100644 (file)
@@ -484,11 +484,11 @@ static void peer_address_hash_func(const void *p, struct siphash *state) {
 
 static int peer_address_compare_func(const void *a, const void *b) {
         const SocketPeer *x = a, *y = b;
+        int r;
 
-        if (x->peer.sa.sa_family < y->peer.sa.sa_family)
-                return -1;
-        if (x->peer.sa.sa_family > y->peer.sa.sa_family)
-                return 1;
+        r = CMP(x->peer.sa.sa_family, y->peer.sa.sa_family);
+        if (r != 0)
+                return r;
 
         switch(x->peer.sa.sa_family) {
         case AF_INET:
index f9118f0b62807bd750e5477a1faddf9bfea748bb..4ae1136e8604a3d4d4be6325d2413837a81fc200 100644 (file)
@@ -59,12 +59,12 @@ static void catalog_hash_func(const void *p, struct siphash *state) {
 static int catalog_compare_func(const void *a, const void *b) {
         const CatalogItem *i = a, *j = b;
         unsigned k;
+        int r;
 
         for (k = 0; k < ELEMENTSOF(j->id.bytes); k++) {
-                 if (i->id.bytes[k] < j->id.bytes[k])
-                        return -1;
-                 if (i->id.bytes[k] > j->id.bytes[k])
-                        return 1;
+                r = CMP(i->id.bytes[k], j->id.bytes[k]);
+                if (r != 0)
+                        return r;
         }
 
         return strcmp(i->language, j->language);
index 5dcb05137360698e8a2a079988b3df193cbbf316..8295d4d40400e780f9300c7aeac54f589216c4a1 100644 (file)
@@ -26,22 +26,15 @@ static int lldp_neighbor_id_compare_func(const void *a, const void *b) {
         if (r != 0)
                 return r;
 
-        if (x->chassis_id_size < y->chassis_id_size)
-                return -1;
-
-        if (x->chassis_id_size > y->chassis_id_size)
-                return 1;
+        r = CMP(x->chassis_id_size, y->chassis_id_size);
+        if (r != 0)
+                return r;
 
         r = memcmp(x->port_id, y->port_id, MIN(x->port_id_size, y->port_id_size));
         if (r != 0)
                 return r;
 
-        if (x->port_id_size < y->port_id_size)
-                return -1;
-        if (x->port_id_size > y->port_id_size)
-                return 1;
-
-        return 0;
+        return CMP(x->port_id_size, y->port_id_size);
 }
 
 const struct hash_ops lldp_neighbor_id_hash_ops = {
@@ -52,13 +45,7 @@ const struct hash_ops lldp_neighbor_id_hash_ops = {
 int lldp_neighbor_prioq_compare_func(const void *a, const void *b) {
         const sd_lldp_neighbor *x = a, *y = b;
 
-        if (x->until < y->until)
-                return -1;
-
-        if (x->until > y->until)
-                return 1;
-
-        return 0;
+        return CMP(x->until, y->until);
 }
 
 _public_ sd_lldp_neighbor *sd_lldp_neighbor_ref(sd_lldp_neighbor *n) {
index 5ca46b3502ef43f7a8a4b6c90f67c7d359152985..d91849f3796b99701f74035fa5aeeb4a18a10dd1 100644 (file)
@@ -125,6 +125,7 @@ void client_id_hash_func(const void *p, struct siphash *state) {
 
 int client_id_compare_func(const void *_a, const void *_b) {
         const DHCPClientId *a, *b;
+        int r;
 
         a = _a;
         b = _b;
@@ -132,8 +133,9 @@ int client_id_compare_func(const void *_a, const void *_b) {
         assert(!a->length || a->data);
         assert(!b->length || b->data);
 
-        if (a->length != b->length)
-                return a->length < b->length ? -1 : 1;
+        r = CMP(a->length, b->length);
+        if (r != 0)
+                return r;
 
         return memcmp(a->data, b->data, a->length);
 }
index 5bddf88e3643af63f27eeabde9d8d2a32625d5ad..eae129b95cac4e199846f6ad29b32599a4529826 100644 (file)
@@ -144,19 +144,18 @@ static void address_hash_func(const void *b, struct siphash *state) {
 
 static int address_compare_func(const void *c1, const void *c2) {
         const Address *a1 = c1, *a2 = c2;
+        int r;
 
-        if (a1->family < a2->family)
-                return -1;
-        if (a1->family > a2->family)
-                return 1;
+        r = CMP(a1->family, a2->family);
+        if (r != 0)
+                return r;
 
         switch (a1->family) {
         /* use the same notion of equality as the kernel does */
         case AF_INET:
-                if (a1->prefixlen < a2->prefixlen)
-                        return -1;
-                if (a1->prefixlen > a2->prefixlen)
-                        return 1;
+                r = CMP(a1->prefixlen, a2->prefixlen);
+                if (r != 0)
+                        return r;
 
                 /* compare the peer prefixes */
                 if (a1->prefixlen != 0) {
@@ -174,10 +173,9 @@ static int address_compare_func(const void *c1, const void *c2) {
                         else
                                 b2 = be32toh(a2->in_addr.in.s_addr) >> (32 - a1->prefixlen);
 
-                        if (b1 < b2)
-                                return -1;
-                        if (b1 > b2)
-                                return 1;
+                        r = CMP(b1, b2);
+                        if (r != 0)
+                                return r;
                 }
 
                 _fallthrough_;
index b335fdb1bbe7be4b5d7e46cda0b30e08744f97be..aa86ce792d36a587b9703cf9704012b387658fea 100644 (file)
@@ -164,34 +164,30 @@ static void route_hash_func(const void *b, struct siphash *state) {
 
 static int route_compare_func(const void *_a, const void *_b) {
         const Route *a = _a, *b = _b;
+        int r;
 
-        if (a->family < b->family)
-                return -1;
-        if (a->family > b->family)
-                return 1;
+        r = CMP(a->family, b->family);
+        if (r != 0)
+                return r;
 
         switch (a->family) {
         case AF_INET:
         case AF_INET6:
-                if (a->dst_prefixlen < b->dst_prefixlen)
-                        return -1;
-                if (a->dst_prefixlen > b->dst_prefixlen)
-                        return 1;
-
-                if (a->tos < b->tos)
-                        return -1;
-                if (a->tos > b->tos)
-                        return 1;
-
-                if (a->priority < b->priority)
-                        return -1;
-                if (a->priority > b->priority)
-                        return 1;
-
-                if (a->table < b->table)
-                        return -1;
-                if (a->table > b->table)
-                        return 1;
+                r = CMP(a->dst_prefixlen, b->dst_prefixlen);
+                if (r != 0)
+                        return r;
+
+                r = CMP(a->tos, b->tos);
+                if (r != 0)
+                        return r;
+
+                r = CMP(a->priority, b->priority);
+                if (r != 0)
+                        return r;
+
+                r = CMP(a->table, b->table);
+                if (r != 0)
+                        return r;
 
                 return memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
         default:
index 650ea0af52b370240054f1929467455251a0d5d0..dbb06a807469a288ef139a61cd48dc2ea7d83314 100644 (file)
@@ -92,38 +92,32 @@ static int routing_policy_rule_compare_func(const void *_a, const void *_b) {
         const RoutingPolicyRule *a = _a, *b = _b;
         int r;
 
-        if (a->family < b->family)
-                return -1;
-        if (a->family > b->family)
-                return 1;
+        r = CMP(a->family, b->family);
+        if (r != 0)
+                return r;
 
         switch (a->family) {
         case AF_INET:
         case AF_INET6:
-                if (a->from_prefixlen < b->from_prefixlen)
-                        return -1;
-                if (a->from_prefixlen > b->from_prefixlen)
-                        return 1;
+                r = CMP(a->from_prefixlen, b->from_prefixlen);
+                if (r != 0)
+                        return r;
 
-                if (a->to_prefixlen < b->to_prefixlen)
-                        return -1;
-                if (a->to_prefixlen > b->to_prefixlen)
-                        return 1;
+                r = CMP(a->to_prefixlen, b->to_prefixlen);
+                if (r != 0)
+                        return r;
 
-                if (a->tos < b->tos)
-                        return -1;
-                if (a->tos > b->tos)
-                        return 1;
+                r = CMP(a->tos, b->tos);
+                if (r != 0)
+                        return r;
 
-                if (a->fwmask < b->fwmark)
-                        return -1;
-                if (a->fwmask > b->fwmark)
-                        return 1;
+                r = CMP(a->fwmask, b->fwmask);
+                if (r != 0)
+                        return r;
 
-                if (a->table < b->table)
-                        return -1;
-                if (a->table > b->table)
-                        return 1;
+                r = CMP(a->table, b->table);
+                if (r != 0)
+                        return r;
 
                 r = strcmp_ptr(a->iif, b->iif);
                 if (!r)
index 403d23d4fb99f0e4381f68060927c4270616d34f..95e9e072c1c4ab10d075e7f7daed9c437d5395cb 100644 (file)
@@ -228,11 +228,7 @@ void dns_cache_prune(DnsCache *c) {
 static int dns_cache_item_prioq_compare_func(const void *a, const void *b) {
         const DnsCacheItem *x = a, *y = b;
 
-        if (x->until < y->until)
-                return -1;
-        if (x->until > y->until)
-                return 1;
-        return 0;
+        return CMP(x->until, y->until);
 }
 
 static int dns_cache_init(DnsCache *c) {
index 097942ee0a287c2808bffcb3b5fd8883e35485d3..2cc606f363d630da56bbb13bbd0afa1df7033f18 100644 (file)
@@ -2339,11 +2339,11 @@ static void dns_packet_hash_func(const void *p, struct siphash *state) {
 
 static int dns_packet_compare_func(const void *a, const void *b) {
         const DnsPacket *x = a, *y = b;
+        int r;
 
-        if (x->size < y->size)
-                return -1;
-        if (x->size > y->size)
-                return 1;
+        r = CMP(x->size, y->size);
+        if (r != 0)
+                return r;
 
         return memcmp(DNS_PACKET_DATA((DnsPacket*) x), DNS_PACKET_DATA((DnsPacket*) y), x->size);
 }
index cfd0ed214db675feb4c6ff40fcc851938e4c5126..05a4915101e0cd299f5e2f20003bf1e76e0c6e8f 100644 (file)
@@ -300,15 +300,13 @@ static int dns_resource_key_compare_func(const void *a, const void *b) {
         if (ret != 0)
                 return ret;
 
-        if (x->type < y->type)
-                return -1;
-        if (x->type > y->type)
-                return 1;
+        ret = CMP(x->type, y->type);
+        if (ret != 0)
+                return ret;
 
-        if (x->class < y->class)
-                return -1;
-        if (x->class > y->class)
-                return 1;
+        ret = CMP(x->class, y->class);
+        if (ret != 0)
+                return ret;
 
         return 0;
 }
index eff3dbb2752daab006c4544a34338ea79ee96077..24164362c4d819f4c8df7f0f5c60f7bed693fdc7 100644 (file)
@@ -624,19 +624,17 @@ static int dns_server_compare_func(const void *a, const void *b) {
         const DnsServer *x = a, *y = b;
         int r;
 
-        if (x->family < y->family)
-                return -1;
-        if (x->family > y->family)
-                return 1;
+        r = CMP(x->family, y->family);
+        if (r != 0)
+                return r;
 
         r = memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family));
         if (r != 0)
                 return r;
 
-        if (x->ifindex < y->ifindex)
-                return -1;
-        if (x->ifindex > y->ifindex)
-                return 1;
+        r = CMP(x->ifindex, y->ifindex);
+        if (r != 0)
+                return r;
 
         return 0;
 }