1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-01 17:51:22 +03:00

tree-wide: Convert compare_func's to use CMP() macro wherever possible.

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.
This commit is contained in:
Filipe Brandenburger 2018-08-02 16:43:37 -07:00
parent 26cdf3e50b
commit a0edd02e43
14 changed files with 86 additions and 120 deletions

View 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 = {

View 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));
}

View 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) {

View 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:

View 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);

View 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) {

View 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);
}

View 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_;

View 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;
r = CMP(a->dst_prefixlen, b->dst_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->priority < b->priority)
return -1;
if (a->priority > b->priority)
return 1;
r = CMP(a->priority, b->priority);
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;
return memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
default:

View 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)

View 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) {

View 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);
}

View 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;
}

View 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;
}