1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-22 13:33:56 +03:00

tree-wide: make hash_ops typesafe

This commit is contained in:
Yu Watanabe 2018-11-27 22:25:20 +09:00
parent 25073e5012
commit 7a08d314f2
27 changed files with 85 additions and 223 deletions

View File

@ -28,21 +28,15 @@ char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR
return buffer;
}
int ether_addr_compare(const void *a, const void *b) {
assert(a);
assert(b);
int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b) {
return memcmp(a, b, ETH_ALEN);
}
static void ether_addr_hash_func(const void *p, struct siphash *state) {
static void ether_addr_hash_func(const struct ether_addr *p, struct siphash *state) {
siphash24_compress(p, sizeof(struct ether_addr), state);
}
const struct hash_ops ether_addr_hash_ops = {
.hash = ether_addr_hash_func,
.compare = ether_addr_compare
};
DEFINE_HASH_OPS(ether_addr_hash_ops, struct ether_addr, ether_addr_hash_func, ether_addr_compare);
int ether_addr_from_string(const char *s, struct ether_addr *ret) {
size_t pos = 0, n, field;

View File

@ -12,7 +12,7 @@
#define ETHER_ADDR_TO_STRING_MAX (3*6)
char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]);
int ether_addr_compare(const void *a, const void *b);
int ether_addr_compare(const struct ether_addr *a, const struct ether_addr *b);
static inline bool ether_addr_equal(const struct ether_addr *a, const struct ether_addr *b) {
return ether_addr_compare(a, b) == 0;
}

View File

@ -603,15 +603,12 @@ int in_addr_prefix_from_string_auto_internal(
}
void in_addr_data_hash_func(const void *p, struct siphash *state) {
const struct in_addr_data *a = p;
static void in_addr_data_hash_func(const struct in_addr_data *a, struct siphash *state) {
siphash24_compress(&a->family, sizeof(a->family), state);
siphash24_compress(&a->address, FAMILY_ADDRESS_SIZE(a->family), state);
}
int in_addr_data_compare_func(const void *a, const void *b) {
const struct in_addr_data *x = a, *y = b;
static int in_addr_data_compare_func(const struct in_addr_data *x, const struct in_addr_data *y) {
int r;
r = CMP(x->family, y->family);
@ -621,7 +618,4 @@ int in_addr_data_compare_func(const void *a, const void *b) {
return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family));
}
const struct hash_ops in_addr_data_hash_ops = {
.hash = in_addr_data_hash_func,
.compare = in_addr_data_compare_func,
};
DEFINE_HASH_OPS(in_addr_data_hash_ops, struct in_addr_data, in_addr_data_hash_func, in_addr_data_compare_func);

View File

@ -69,6 +69,4 @@ static inline size_t FAMILY_ADDRESS_SIZE(int family) {
* See also oss-fuzz#11344. */
#define IN_ADDR_NULL ((union in_addr_union) { .in6 = {} })
void in_addr_data_hash_func(const void *p, struct siphash *state);
int in_addr_data_compare_func(const void *a, const void *b);
extern const struct hash_ops in_addr_data_hash_ops;

View File

@ -713,8 +713,7 @@ typedef struct Member {
uint64_t flags;
} Member;
static void member_hash_func(const void *p, struct siphash *state) {
const Member *m = p;
static void member_hash_func(const Member *m, struct siphash *state) {
uint64_t arity = 1;
assert(m);
@ -921,12 +920,9 @@ static int on_property(const char *interface, const char *name, const char *sign
return 0;
}
static int introspect(int argc, char **argv, void *userdata) {
static const struct hash_ops member_hash_ops = {
.hash = member_hash_func,
.compare = (__compar_fn_t) member_compare_func,
};
DEFINE_PRIVATE_HASH_OPS(member_hash_ops, Member, member_hash_func, member_compare_func);
static int introspect(int argc, char **argv, void *userdata) {
static const XMLIntrospectOps ops = {
.on_interface = on_interface,
.on_method = on_method,

View File

@ -468,9 +468,7 @@ static int socket_verify(Socket *s) {
return 0;
}
static void peer_address_hash_func(const void *p, struct siphash *state) {
const SocketPeer *s = p;
static void peer_address_hash_func(const SocketPeer *s, struct siphash *state) {
assert(s);
if (s->peer.sa.sa_family == AF_INET)
@ -483,8 +481,7 @@ static void peer_address_hash_func(const void *p, struct siphash *state) {
assert_not_reached("Unknown address family.");
}
static int peer_address_compare_func(const void *a, const void *b) {
const SocketPeer *x = a, *y = b;
static int peer_address_compare_func(const SocketPeer *x, const SocketPeer *y) {
int r;
r = CMP(x->peer.sa.sa_family, y->peer.sa.sa_family);
@ -502,10 +499,7 @@ static int peer_address_compare_func(const void *a, const void *b) {
assert_not_reached("Black sheep in the family!");
}
const struct hash_ops peer_address_hash_ops = {
.hash = peer_address_hash_func,
.compare = peer_address_compare_func
};
DEFINE_PRIVATE_HASH_OPS(peer_address_hash_ops, SocketPeer, peer_address_hash_func, peer_address_compare_func);
static int socket_load(Unit *u) {
Socket *s = SOCKET(u);

View File

@ -49,9 +49,7 @@ typedef struct CatalogItem {
le64_t offset;
} CatalogItem;
static void catalog_hash_func(const void *p, struct siphash *state) {
const CatalogItem *i = p;
static void catalog_hash_func(const CatalogItem *i, struct siphash *state) {
siphash24_compress(&i->id, sizeof(i->id), state);
siphash24_compress(i->language, strlen(i->language), state);
}
@ -69,10 +67,7 @@ static int catalog_compare_func(const CatalogItem *a, const CatalogItem *b) {
return strcmp(a->language, b->language);
}
const struct hash_ops catalog_hash_ops = {
.hash = catalog_hash_func,
.compare = (comparison_fn_t) catalog_compare_func,
};
DEFINE_HASH_OPS(catalog_hash_ops, CatalogItem, catalog_hash_func, catalog_compare_func);
static bool next_header(const char **s) {
const char *e;

View File

@ -78,5 +78,5 @@ int dhcp_server_send_packet(sd_dhcp_server *server,
DHCPRequest *req, DHCPPacket *packet,
int type, size_t optoffset);
void client_id_hash_func(const void *p, struct siphash *state);
int client_id_compare_func(const void *_a, const void *_b);
void client_id_hash_func(const DHCPClientId *p, struct siphash *state);
int client_id_compare_func(const DHCPClientId *a, const DHCPClientId *b);

View File

@ -9,9 +9,7 @@
#include "lldp-neighbor.h"
#include "unaligned.h"
static void lldp_neighbor_id_hash_func(const void *p, struct siphash *state) {
const LLDPNeighborID *id = p;
static void lldp_neighbor_id_hash_func(const LLDPNeighborID *id, struct siphash *state) {
siphash24_compress(id->chassis_id, id->chassis_id_size, state);
siphash24_compress(&id->chassis_id_size, sizeof(id->chassis_id_size), state);
siphash24_compress(id->port_id, id->port_id_size, state);
@ -36,10 +34,7 @@ int lldp_neighbor_id_compare_func(const LLDPNeighborID *x, const LLDPNeighborID
return CMP(x->port_id_size, y->port_id_size);
}
const struct hash_ops lldp_neighbor_id_hash_ops = {
.hash = lldp_neighbor_id_hash_func,
.compare = (__compar_fn_t) lldp_neighbor_id_compare_func,
};
DEFINE_HASH_OPS(lldp_neighbor_id_hash_ops, LLDPNeighborID, lldp_neighbor_id_hash_func, lldp_neighbor_id_compare_func);
int lldp_neighbor_prioq_compare_func(const void *a, const void *b) {
const sd_lldp_neighbor *x = a, *y = b;

View File

@ -102,9 +102,7 @@ int sd_dhcp_server_is_running(sd_dhcp_server *server) {
return !!server->receive_message;
}
void client_id_hash_func(const void *p, struct siphash *state) {
const DHCPClientId *id = p;
void client_id_hash_func(const DHCPClientId *id, struct siphash *state) {
assert(id);
assert(id->length);
assert(id->data);
@ -113,13 +111,9 @@ void client_id_hash_func(const void *p, struct siphash *state) {
siphash24_compress(id->data, id->length, state);
}
int client_id_compare_func(const void *_a, const void *_b) {
const DHCPClientId *a, *b;
int client_id_compare_func(const DHCPClientId *a, const DHCPClientId *b) {
int r;
a = _a;
b = _b;
assert(!a->length || a->data);
assert(!b->length || b->data);
@ -130,10 +124,7 @@ int client_id_compare_func(const void *_a, const void *_b) {
return memcmp(a->data, b->data, a->length);
}
static const struct hash_ops client_id_hash_ops = {
.hash = client_id_hash_func,
.compare = client_id_compare_func
};
DEFINE_PRIVATE_HASH_OPS(client_id_hash_ops, DHCPClientId, client_id_hash_func, client_id_compare_func);
static sd_dhcp_server *dhcp_server_free(sd_dhcp_server *server) {
DHCPLease *lease;

View File

@ -1582,9 +1582,7 @@ _public_ int sd_bus_add_fallback(
return bus_add_object(bus, slot, true, prefix, callback, userdata);
}
static void vtable_member_hash_func(const void *a, struct siphash *state) {
const struct vtable_member *m = a;
static void vtable_member_hash_func(const struct vtable_member *m, struct siphash *state) {
assert(m);
string_hash_func(m->path, state);
@ -1592,8 +1590,7 @@ static void vtable_member_hash_func(const void *a, struct siphash *state) {
string_hash_func(m->member, state);
}
static int vtable_member_compare_func(const void *a, const void *b) {
const struct vtable_member *x = a, *y = b;
static int vtable_member_compare_func(const struct vtable_member *x, const struct vtable_member *y) {
int r;
assert(x);
@ -1610,10 +1607,7 @@ static int vtable_member_compare_func(const void *a, const void *b) {
return strcmp(x->member, y->member);
}
static const struct hash_ops vtable_member_hash_ops = {
.hash = vtable_member_hash_func,
.compare = vtable_member_compare_func
};
DEFINE_PRIVATE_HASH_OPS(vtable_member_hash_ops, struct vtable_member, vtable_member_hash_func, vtable_member_compare_func);
static int add_object_vtable_internal(
sd_bus *bus,

View File

@ -1371,8 +1371,7 @@ static int event_make_inotify_data(
return 1;
}
static int inode_data_compare(const void *a, const void *b) {
const struct inode_data *x = a, *y = b;
static int inode_data_compare(const struct inode_data *x, const struct inode_data *y) {
int r;
assert(x);
@ -1385,19 +1384,14 @@ static int inode_data_compare(const void *a, const void *b) {
return CMP(x->ino, y->ino);
}
static void inode_data_hash_func(const void *p, struct siphash *state) {
const struct inode_data *d = p;
assert(p);
static void inode_data_hash_func(const struct inode_data *d, struct siphash *state) {
assert(d);
siphash24_compress(&d->dev, sizeof(d->dev), state);
siphash24_compress(&d->ino, sizeof(d->ino), state);
}
const struct hash_ops inode_data_hash_ops = {
.hash = inode_data_hash_func,
.compare = inode_data_compare
};
DEFINE_PRIVATE_HASH_OPS(inode_data_hash_ops, struct inode_data, inode_data_hash_func, inode_data_compare);
static void event_free_inode_data(
sd_event *e,

View File

@ -182,15 +182,12 @@ int id128_write(const char *p, Id128Format f, sd_id128_t id, bool do_sync) {
return id128_write_fd(fd, f, id, do_sync);
}
void id128_hash_func(const void *p, struct siphash *state) {
siphash24_compress(p, 16, state);
void id128_hash_func(const sd_id128_t *p, struct siphash *state) {
siphash24_compress(p, sizeof(sd_id128_t), state);
}
int id128_compare_func(const void *a, const void *b) {
int id128_compare_func(const sd_id128_t *a, const sd_id128_t *b) {
return memcmp(a, b, 16);
}
const struct hash_ops id128_hash_ops = {
.hash = id128_hash_func,
.compare = id128_compare_func,
};
DEFINE_HASH_OPS(id128_hash_ops, sd_id128_t, id128_hash_func, id128_compare_func);

View File

@ -28,6 +28,6 @@ int id128_read(const char *p, Id128Format f, sd_id128_t *ret);
int id128_write_fd(int fd, Id128Format f, sd_id128_t id, bool do_sync);
int id128_write(const char *p, Id128Format f, sd_id128_t id, bool do_sync);
void id128_hash_func(const void *p, struct siphash *state);
int id128_compare_func(const void *a, const void *b) _pure_;
void id128_hash_func(const sd_id128_t *p, struct siphash *state);
int id128_compare_func(const sd_id128_t *a, const sd_id128_t *b) _pure_;
extern const struct hash_ops id128_hash_ops;

View File

@ -114,9 +114,7 @@ void address_free(Address *address) {
free(address);
}
static void address_hash_func(const void *b, struct siphash *state) {
const Address *a = b;
static void address_hash_func(const Address *a, struct siphash *state) {
assert(a);
siphash24_compress(&a->family, sizeof(a->family), state);
@ -149,8 +147,7 @@ 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;
static int address_compare_func(const Address *a1, const Address *a2) {
int r;
r = CMP(a1->family, a2->family);
@ -194,10 +191,7 @@ static int address_compare_func(const void *c1, const void *c2) {
}
}
static const struct hash_ops address_hash_ops = {
.hash = address_hash_func,
.compare = address_compare_func
};
DEFINE_PRIVATE_HASH_OPS(address_hash_ops, Address, address_hash_func, address_compare_func);
bool address_equal(Address *a1, Address *a2) {
if (a1 == a2)

View File

@ -1250,24 +1250,17 @@ static int dhcp6_route_add_handler(sd_netlink *nl, sd_netlink_message *m, void *
return 0;
}
static void dhcp6_prefixes_hash_func(const void *p, struct siphash *state) {
const struct in6_addr *addr = p;
assert(p);
static void dhcp6_prefixes_hash_func(const struct in6_addr *addr, struct siphash *state) {
assert(addr);
siphash24_compress(addr, sizeof(*addr), state);
}
static int dhcp6_prefixes_compare_func(const void *_a, const void *_b) {
const struct in6_addr *a = _a, *b = _b;
static int dhcp6_prefixes_compare_func(const struct in6_addr *a, const struct in6_addr *b) {
return memcmp(a, b, sizeof(*a));
}
static const struct hash_ops dhcp6_prefixes_hash_ops = {
.hash = dhcp6_prefixes_hash_func,
.compare = dhcp6_prefixes_compare_func,
};
DEFINE_PRIVATE_HASH_OPS(dhcp6_prefixes_hash_ops, struct in6_addr, dhcp6_prefixes_hash_func, dhcp6_prefixes_compare_func);
int manager_dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) {
_cleanup_free_ char *buf = NULL;

View File

@ -322,22 +322,15 @@ static int ndisc_router_process_route(Link *link, sd_ndisc_router *rt) {
return 0;
}
static void ndisc_rdnss_hash_func(const void *p, struct siphash *state) {
const NDiscRDNSS *x = p;
static void ndisc_rdnss_hash_func(const NDiscRDNSS *x, struct siphash *state) {
siphash24_compress(&x->address, sizeof(x->address), state);
}
static int ndisc_rdnss_compare_func(const void *_a, const void *_b) {
const NDiscRDNSS *a = _a, *b = _b;
static int ndisc_rdnss_compare_func(const NDiscRDNSS *a, const NDiscRDNSS *b) {
return memcmp(&a->address, &b->address, sizeof(a->address));
}
static const struct hash_ops ndisc_rdnss_hash_ops = {
.hash = ndisc_rdnss_hash_func,
.compare = ndisc_rdnss_compare_func
};
DEFINE_PRIVATE_HASH_OPS(ndisc_rdnss_hash_ops, NDiscRDNSS, ndisc_rdnss_hash_func, ndisc_rdnss_compare_func);
static int ndisc_router_process_rdnss(Link *link, sd_ndisc_router *rt) {
uint32_t lifetime;
@ -411,22 +404,15 @@ static int ndisc_router_process_rdnss(Link *link, sd_ndisc_router *rt) {
return 0;
}
static void ndisc_dnssl_hash_func(const void *p, struct siphash *state) {
const NDiscDNSSL *x = p;
static void ndisc_dnssl_hash_func(const NDiscDNSSL *x, struct siphash *state) {
siphash24_compress(NDISC_DNSSL_DOMAIN(x), strlen(NDISC_DNSSL_DOMAIN(x)), state);
}
static int ndisc_dnssl_compare_func(const void *_a, const void *_b) {
const NDiscDNSSL *a = _a, *b = _b;
static int ndisc_dnssl_compare_func(const NDiscDNSSL *a, const NDiscDNSSL *b) {
return strcmp(NDISC_DNSSL_DOMAIN(a), NDISC_DNSSL_DOMAIN(b));
}
static const struct hash_ops ndisc_dnssl_hash_ops = {
.hash = ndisc_dnssl_hash_func,
.compare = ndisc_dnssl_compare_func
};
DEFINE_PRIVATE_HASH_OPS(ndisc_dnssl_hash_ops, NDiscDNSSL, ndisc_dnssl_hash_func, ndisc_dnssl_compare_func);
static void ndisc_router_process_dnssl(Link *link, sd_ndisc_router *rt) {
_cleanup_strv_free_ char **l = NULL;

View File

@ -21,15 +21,12 @@
#include "strv.h"
#include "util.h"
static void network_config_hash_func(const void *p, struct siphash *state) {
const NetworkConfigSection *c = p;
static void network_config_hash_func(const NetworkConfigSection *c, struct siphash *state) {
siphash24_compress(c->filename, strlen(c->filename), state);
siphash24_compress(&c->line, sizeof(c->line), state);
}
static int network_config_compare_func(const void *a, const void *b) {
const NetworkConfigSection *x = a, *y = b;
static int network_config_compare_func(const NetworkConfigSection *x, const NetworkConfigSection *y) {
int r;
r = strcmp(x->filename, y->filename);
@ -39,10 +36,7 @@ static int network_config_compare_func(const void *a, const void *b) {
return CMP(x->line, y->line);
}
const struct hash_ops network_config_hash_ops = {
.hash = network_config_hash_func,
.compare = network_config_compare_func,
};
DEFINE_HASH_OPS(network_config_hash_ops, NetworkConfigSection, network_config_hash_func, network_config_compare_func);
int network_config_section_new(const char *filename, unsigned line, NetworkConfigSection **s) {
NetworkConfigSection *cs;

View File

@ -142,9 +142,7 @@ void route_free(Route *route) {
free(route);
}
static void route_hash_func(const void *b, struct siphash *state) {
const Route *route = b;
static void route_hash_func(const Route *route, struct siphash *state) {
assert(route);
siphash24_compress(&route->family, sizeof(route->family), state);
@ -167,8 +165,7 @@ 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;
static int route_compare_func(const Route *a, const Route *b) {
int r;
r = CMP(a->family, b->family);
@ -201,10 +198,7 @@ static int route_compare_func(const void *_a, const void *_b) {
}
}
static const struct hash_ops route_hash_ops = {
.hash = route_hash_func,
.compare = route_compare_func
};
DEFINE_PRIVATE_HASH_OPS(route_hash_ops, Route, route_hash_func, route_compare_func);
bool route_equal(Route *r1, Route *r2) {
if (r1 == r2)

View File

@ -56,9 +56,7 @@ void routing_policy_rule_free(RoutingPolicyRule *rule) {
free(rule);
}
static void routing_policy_rule_hash_func(const void *b, struct siphash *state) {
const RoutingPolicyRule *rule = b;
static void routing_policy_rule_hash_func(const RoutingPolicyRule *rule, struct siphash *state) {
assert(rule);
siphash24_compress(&rule->family, sizeof(rule->family), state);
@ -94,8 +92,7 @@ static void routing_policy_rule_hash_func(const void *b, struct siphash *state)
}
}
static int routing_policy_rule_compare_func(const void *_a, const void *_b) {
const RoutingPolicyRule *a = _a, *b = _b;
static int routing_policy_rule_compare_func(const RoutingPolicyRule *a, const RoutingPolicyRule *b) {
int r;
r = CMP(a->family, b->family);
@ -157,10 +154,7 @@ static int routing_policy_rule_compare_func(const void *_a, const void *_b) {
}
}
const struct hash_ops routing_policy_rule_hash_ops = {
.hash = routing_policy_rule_hash_func,
.compare = routing_policy_rule_compare_func
};
DEFINE_PRIVATE_HASH_OPS(routing_policy_rule_hash_ops, RoutingPolicyRule, routing_policy_rule_hash_func, routing_policy_rule_compare_func);
int routing_policy_rule_get(Manager *m,
int family,

View File

@ -2330,17 +2330,14 @@ int dns_packet_is_reply_for(DnsPacket *p, const DnsResourceKey *key) {
return dns_resource_key_equal(p->question->keys[0], key);
}
static void dns_packet_hash_func(const void *p, struct siphash *state) {
const DnsPacket *s = p;
static void dns_packet_hash_func(const DnsPacket *s, struct siphash *state) {
assert(s);
siphash24_compress(&s->size, sizeof(s->size), state);
siphash24_compress(DNS_PACKET_DATA((DnsPacket*) s), s->size, state);
}
static int dns_packet_compare_func(const void *a, const void *b) {
const DnsPacket *x = a, *y = b;
static int dns_packet_compare_func(const DnsPacket *x, const DnsPacket *y) {
int r;
r = CMP(x->size, y->size);
@ -2350,10 +2347,7 @@ static int dns_packet_compare_func(const void *a, const void *b) {
return memcmp(DNS_PACKET_DATA((DnsPacket*) x), DNS_PACKET_DATA((DnsPacket*) y), x->size);
}
const struct hash_ops dns_packet_hash_ops = {
.hash = dns_packet_hash_func,
.compare = dns_packet_compare_func
};
DEFINE_HASH_OPS(dns_packet_hash_ops, DnsPacket, dns_packet_hash_func, dns_packet_compare_func);
static const char* const dns_rcode_table[_DNS_RCODE_MAX_DEFINED] = {
[DNS_RCODE_SUCCESS] = "SUCCESS",

View File

@ -282,9 +282,7 @@ int dns_resource_key_match_soa(const DnsResourceKey *key, const DnsResourceKey *
return dns_name_endswith(dns_resource_key_name(key), dns_resource_key_name(soa));
}
static void dns_resource_key_hash_func(const void *i, struct siphash *state) {
const DnsResourceKey *k = i;
static void dns_resource_key_hash_func(const DnsResourceKey *k, struct siphash *state) {
assert(k);
dns_name_hash_func(dns_resource_key_name(k), state);
@ -292,8 +290,7 @@ static void dns_resource_key_hash_func(const void *i, struct siphash *state) {
siphash24_compress(&k->type, sizeof(k->type), state);
}
static int dns_resource_key_compare_func(const void *a, const void *b) {
const DnsResourceKey *x = a, *y = b;
static int dns_resource_key_compare_func(const DnsResourceKey *x, const DnsResourceKey *y) {
int ret;
ret = dns_name_compare_func(dns_resource_key_name(x), dns_resource_key_name(y));
@ -311,10 +308,7 @@ static int dns_resource_key_compare_func(const void *a, const void *b) {
return 0;
}
const struct hash_ops dns_resource_key_hash_ops = {
.hash = dns_resource_key_hash_func,
.compare = dns_resource_key_compare_func
};
DEFINE_HASH_OPS(dns_resource_key_hash_ops, DnsResourceKey, dns_resource_key_hash_func, dns_resource_key_compare_func);
char* dns_resource_key_to_string(const DnsResourceKey *key, char *buf, size_t buf_size) {
const char *c, *t;
@ -1343,9 +1337,7 @@ int dns_resource_record_is_synthetic(DnsResourceRecord *rr) {
return !r;
}
void dns_resource_record_hash_func(const void *i, struct siphash *state) {
const DnsResourceRecord *rr = i;
void dns_resource_record_hash_func(const DnsResourceRecord *rr, struct siphash *state) {
assert(rr);
dns_resource_key_hash_func(rr->key, state);
@ -1486,13 +1478,12 @@ void dns_resource_record_hash_func(const void *i, struct siphash *state) {
}
}
static int dns_resource_record_compare_func(const void *a, const void *b) {
const DnsResourceRecord *x = a, *y = b;
int ret;
static int dns_resource_record_compare_func(const DnsResourceRecord *x, const DnsResourceRecord *y) {
int r;
ret = dns_resource_key_compare_func(x->key, y->key);
if (ret != 0)
return ret;
r = dns_resource_key_compare_func(x->key, y->key);
if (r != 0)
return r;
if (dns_resource_record_equal(x, y))
return 0;
@ -1502,10 +1493,7 @@ static int dns_resource_record_compare_func(const void *a, const void *b) {
return CMP(x, y);
}
const struct hash_ops dns_resource_record_hash_ops = {
.hash = dns_resource_record_hash_func,
.compare = dns_resource_record_compare_func,
};
DEFINE_HASH_OPS(dns_resource_record_hash_ops, DnsResourceRecord, dns_resource_record_hash_func, dns_resource_record_compare_func);
DnsResourceRecord *dns_resource_record_copy(DnsResourceRecord *rr) {
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *copy = NULL;

View File

@ -326,7 +326,7 @@ bool dns_txt_item_equal(DnsTxtItem *a, DnsTxtItem *b);
DnsTxtItem *dns_txt_item_copy(DnsTxtItem *i);
int dns_txt_item_new_empty(DnsTxtItem **ret);
void dns_resource_record_hash_func(const void *i, struct siphash *state);
void dns_resource_record_hash_func(const DnsResourceRecord *i, struct siphash *state);
extern const struct hash_ops dns_resource_key_hash_ops;
extern const struct hash_ops dns_resource_record_hash_ops;

View File

@ -595,9 +595,7 @@ bool dns_server_limited_domains(DnsServer *server) {
return domain_restricted;
}
static void dns_server_hash_func(const void *p, struct siphash *state) {
const DnsServer *s = p;
static void dns_server_hash_func(const DnsServer *s, struct siphash *state) {
assert(s);
siphash24_compress(&s->family, sizeof(s->family), state);
@ -605,8 +603,7 @@ static void dns_server_hash_func(const void *p, struct siphash *state) {
siphash24_compress(&s->ifindex, sizeof(s->ifindex), state);
}
static int dns_server_compare_func(const void *a, const void *b) {
const DnsServer *x = a, *y = b;
static int dns_server_compare_func(const DnsServer *x, const DnsServer *y) {
int r;
r = CMP(x->family, y->family);
@ -624,10 +621,7 @@ static int dns_server_compare_func(const void *a, const void *b) {
return 0;
}
const struct hash_ops dns_server_hash_ops = {
.hash = dns_server_hash_func,
.compare = dns_server_compare_func
};
DEFINE_HASH_OPS(dns_server_hash_ops, DnsServer, dns_server_hash_func, dns_server_compare_func);
void dns_server_unlink_all(DnsServer *first) {
DnsServer *next;

View File

@ -460,8 +460,7 @@ finish:
return 0;
}
void dns_name_hash_func(const void *s, struct siphash *state) {
const char *p = s;
void dns_name_hash_func(const char *p, struct siphash *state) {
int r;
assert(p);
@ -484,15 +483,15 @@ void dns_name_hash_func(const void *s, struct siphash *state) {
string_hash_func("", state);
}
int dns_name_compare_func(const void *a, const void *b) {
int dns_name_compare_func(const char *a, const char *b) {
const char *x, *y;
int r, q;
assert(a);
assert(b);
x = (const char *) a + strlen(a);
y = (const char *) b + strlen(b);
x = a + strlen(a);
y = b + strlen(b);
for (;;) {
char la[DNS_LABEL_MAX], lb[DNS_LABEL_MAX];
@ -511,10 +510,7 @@ int dns_name_compare_func(const void *a, const void *b) {
}
}
const struct hash_ops dns_name_hash_ops = {
.hash = dns_name_hash_func,
.compare = dns_name_compare_func
};
DEFINE_HASH_OPS(dns_name_hash_ops, char, dns_name_hash_func, dns_name_compare_func);
int dns_name_equal(const char *x, const char *y) {
int r, q;

View File

@ -57,8 +57,8 @@ static inline int dns_name_is_valid(const char *s) {
return 1;
}
void dns_name_hash_func(const void *s, struct siphash *state);
int dns_name_compare_func(const void *a, const void *b);
void dns_name_hash_func(const char *s, struct siphash *state);
int dns_name_compare_func(const char *a, const char *b);
extern const struct hash_ops dns_name_hash_ops;
int dns_name_between(const char *a, const char *b, const char *c);

View File

@ -48,22 +48,15 @@ struct test {
unsigned idx;
};
static int test_compare(const void *a, const void *b) {
const struct test *x = a, *y = b;
static int test_compare(const struct test *x, const struct test *y) {
return CMP(x->value, y->value);
}
static void test_hash(const void *a, struct siphash *state) {
const struct test *x = a;
static void test_hash(const struct test *x, struct siphash *state) {
siphash24_compress(&x->value, sizeof(x->value), state);
}
static const struct hash_ops test_hash_ops = {
.hash = test_hash,
.compare = test_compare
};
DEFINE_PRIVATE_HASH_OPS(test_hash_ops, struct test, test_hash, test_compare);
static void test_struct(void) {
_cleanup_(prioq_freep) Prioq *q = NULL;
@ -73,7 +66,7 @@ static void test_struct(void) {
srand(0);
assert_se(q = prioq_new(test_compare));
assert_se(q = prioq_new((compare_func_t) test_compare));
assert_se(s = set_new(&test_hash_ops));
for (i = 0; i < SET_SIZE; i++) {