From d5099efc47d4e6ac60816b5381a5f607ab03f06e Mon Sep 17 00:00:00 2001 From: Michal Schmidt Date: Wed, 13 Aug 2014 01:00:18 +0200 Subject: hashmap: introduce hash_ops to make struct Hashmap smaller It is redundant to store 'hash' and 'compare' function pointers in struct Hashmap separately. The functions always comprise a pair. Store a single pointer to struct hash_ops instead. systemd keeps hundreds of hashmaps, so this saves a little bit of memory. --- src/analyze/analyze.c | 2 +- src/bus-proxyd/bus-policy.c | 4 +-- src/cgtop/cgtop.c | 4 +-- src/core/automount.c | 4 +-- src/core/bus-endpoint.c | 2 +- src/core/dbus-manager.c | 2 +- src/core/dbus.c | 2 +- src/core/device.c | 4 +-- src/core/killall.c | 2 +- src/core/load-fragment.c | 10 +++---- src/core/manager.c | 14 ++++----- src/core/mount-setup.c | 2 +- src/core/swap.c | 4 +-- src/core/transaction.c | 2 +- src/core/unit.c | 22 +++++++-------- src/delta/delta.c | 8 +++--- src/journal-remote/journal-remote.c | 18 ++++-------- src/journal/catalog.c | 11 ++++++-- src/journal/catalog.h | 3 +- src/journal/coredump-vacuum.c | 2 +- src/journal/coredumpctl.c | 2 +- src/journal/journal-file.c | 2 +- src/journal/journalctl.c | 2 +- src/journal/journald-server.c | 2 +- src/journal/mmap-cache.c | 4 +-- src/journal/sd-journal.c | 8 +++--- src/journal/test-catalog.c | 2 +- src/libsystemd-network/sd-dhcp-server.c | 8 ++++-- src/libsystemd/sd-bus/bus-match.c | 4 +-- src/libsystemd/sd-bus/bus-objects.c | 13 ++++++--- src/libsystemd/sd-bus/bus-track.c | 2 +- src/libsystemd/sd-bus/bus-util.c | 2 +- src/libsystemd/sd-bus/busctl.c | 2 +- src/libsystemd/sd-bus/sd-bus.c | 2 +- src/libsystemd/sd-event/sd-event.c | 4 +-- src/libsystemd/sd-rtnl/sd-rtnl.c | 2 +- src/locale/localectl.c | 2 +- src/login/logind-acl.c | 2 +- src/login/logind-session.c | 2 +- src/login/logind.c | 18 ++++++------ src/machine/machined.c | 6 ++-- src/network/networkd-link.c | 2 +- src/network/networkd-manager.c | 8 +++--- src/network/networkd-network.c | 6 ++-- src/network/networkd-wait-online-link.c | 5 ++-- src/readahead/readahead-collect.c | 2 +- src/remount-fs/remount-fs.c | 2 +- src/resolve/resolved-dns-cache.c | 2 +- src/resolve/resolved-dns-domain.c | 5 ++++ src/resolve/resolved-dns-domain.h | 1 + src/resolve/resolved-dns-packet.c | 4 +-- src/resolve/resolved-dns-query.c | 4 +-- src/resolve/resolved-dns-rr.c | 9 ++++-- src/resolve/resolved-dns-rr.h | 4 +-- src/resolve/resolved-dns-scope.c | 2 +- src/resolve/resolved-dns-server.c | 9 ++++-- src/resolve/resolved-dns-server.h | 3 +- src/resolve/resolved-dns-transaction.c | 2 +- src/resolve/resolved-dns-zone.c | 6 ++-- src/resolve/resolved-link.c | 2 +- src/resolve/resolved-manager.c | 4 +-- src/shared/cgroup-util.c | 6 ++-- src/shared/conf-files.c | 2 +- src/shared/fdset.c | 2 +- src/shared/hashmap.c | 42 +++++++++++++++++++-------- src/shared/hashmap.h | 18 ++++++++++-- src/shared/install.c | 8 +++--- src/shared/locale-util.c | 2 +- src/shared/logs-show.c | 2 +- src/shared/set.c | 8 +++--- src/shared/set.h | 4 +-- src/shared/util.c | 6 ++-- src/socket-proxy/socket-proxyd.c | 4 +-- src/sysctl/sysctl.c | 2 +- src/systemctl/systemctl.c | 6 ++-- src/sysusers/sysusers.c | 22 +++++++-------- src/sysv-generator/sysv-generator.c | 8 ++---- src/test/test-hashmap.c | 50 ++++++++++++++++----------------- src/test/test-install.c | 2 +- src/test/test-prioq.c | 7 ++++- src/test/test-unit-file.c | 2 +- src/tmpfiles/tmpfiles.c | 6 ++-- 82 files changed, 278 insertions(+), 226 deletions(-) (limited to 'src') diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index 82f5cf3c5..5e5598806 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -907,7 +907,7 @@ static int analyze_critical_chain(sd_bus *bus, char *names[]) { if (n <= 0) return n; - h = hashmap_new(string_hash_func, string_compare_func); + h = hashmap_new(&string_hash_ops); if (!h) return -ENOMEM; diff --git a/src/bus-proxyd/bus-policy.c b/src/bus-proxyd/bus-policy.c index 06c16a7eb..d2eace940 100644 --- a/src/bus-proxyd/bus-policy.c +++ b/src/bus-proxyd/bus-policy.c @@ -338,7 +338,7 @@ static int file_load(Policy *p, const char *path) { assert_cc(sizeof(uid_t) == sizeof(uint32_t)); - r = hashmap_ensure_allocated(&p->user_items, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&p->user_items, NULL); if (r < 0) return log_oom(); @@ -369,7 +369,7 @@ static int file_load(Policy *p, const char *path) { assert_cc(sizeof(gid_t) == sizeof(uint32_t)); - r = hashmap_ensure_allocated(&p->group_items, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&p->group_items, NULL); if (r < 0) return log_oom(); diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index 509fe4cdc..ab8c4cfda 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -695,8 +695,8 @@ int main(int argc, char *argv[]) { if (r <= 0) goto finish; - a = hashmap_new(string_hash_func, string_compare_func); - b = hashmap_new(string_hash_func, string_compare_func); + a = hashmap_new(&string_hash_ops); + b = hashmap_new(&string_hash_ops); if (!a || !b) { r = log_oom(); goto finish; diff --git a/src/core/automount.c b/src/core/automount.c index 73a8ce17e..f72aca295 100644 --- a/src/core/automount.c +++ b/src/core/automount.c @@ -685,7 +685,7 @@ static int automount_deserialize_item(Unit *u, const char *key, const char *valu log_debug_unit(u->id, "Failed to parse token value %s", value); else { if (!a->tokens) - if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) + if (!(a->tokens = set_new(NULL))) return -ENOMEM; r = set_put(a->tokens, UINT_TO_PTR(token)); @@ -762,7 +762,7 @@ static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, vo } else log_debug_unit(UNIT(a)->id, "Got direct mount request on %s", a->where); - r = set_ensure_allocated(&a->tokens, trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&a->tokens, NULL); if (r < 0) { log_error_unit(UNIT(a)->id, "Failed to allocate token set."); goto fail; diff --git a/src/core/bus-endpoint.c b/src/core/bus-endpoint.c index 8d11974db..1e8f07eb5 100644 --- a/src/core/bus-endpoint.c +++ b/src/core/bus-endpoint.c @@ -55,7 +55,7 @@ int bus_endpoint_add_policy(BusEndpoint *ep, const char *name, BusPolicyAccess a return 0; } } else { - ep->policy_hash = hashmap_new(string_hash_func, string_compare_func); + ep->policy_hash = hashmap_new(&string_hash_ops); if (!ep->policy_hash) return -ENOMEM; } diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c index 2fe9d193f..533ce439a 100644 --- a/src/core/dbus-manager.c +++ b/src/core/dbus-manager.c @@ -1399,7 +1399,7 @@ static int method_list_unit_files(sd_bus *bus, sd_bus_message *message, void *us if (r < 0) return r; - h = hashmap_new(string_hash_func, string_compare_func); + h = hashmap_new(&string_hash_ops); if (!h) return -ENOMEM; diff --git a/src/core/dbus.c b/src/core/dbus.c index e7eee3c6d..09b4a4ac6 100644 --- a/src/core/dbus.c +++ b/src/core/dbus.c @@ -659,7 +659,7 @@ static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void return 0; } - r = set_ensure_allocated(&m->private_buses, trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&m->private_buses, NULL); if (r < 0) { log_oom(); return 0; diff --git a/src/core/device.c b/src/core/device.c index 0f28a1687..11c426108 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -304,7 +304,7 @@ static int device_update_unit(Manager *m, struct udev_device *dev, const char *p goto fail; } - r = hashmap_ensure_allocated(&m->devices_by_sysfs, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&m->devices_by_sysfs, &string_hash_ops); if (r < 0) goto fail; @@ -517,7 +517,7 @@ static int device_following_set(Unit *u, Set **_set) { return 0; } - set = set_new(NULL, NULL); + set = set_new(NULL); if (!set) return -ENOMEM; diff --git a/src/core/killall.c b/src/core/killall.c index 291e1f90e..a6ff50a5a 100644 --- a/src/core/killall.c +++ b/src/core/killall.c @@ -205,7 +205,7 @@ void broadcast_signal(int sig, bool wait_for_exit, bool send_sighup) { _cleanup_set_free_ Set *pids = NULL; if (wait_for_exit) - pids = set_new(trivial_hash_func, trivial_compare_func); + pids = set_new(NULL); assert_se(sigemptyset(&mask) == 0); assert_se(sigaddset(&mask, SIGCHLD) == 0); diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index 7cf19b93c..0620882b4 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -2276,7 +2276,7 @@ int config_parse_syscall_filter( } if (!c->syscall_filter) { - c->syscall_filter = set_new(trivial_hash_func, trivial_compare_func); + c->syscall_filter = set_new(NULL); if (!c->syscall_filter) return log_oom(); @@ -2368,7 +2368,7 @@ int config_parse_syscall_archs( return 0; } - r = set_ensure_allocated(archs, trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(archs, NULL); if (r < 0) return log_oom(); @@ -2474,7 +2474,7 @@ int config_parse_address_families( } if (!c->address_families) { - c->address_families = set_new(trivial_hash_func, trivial_compare_func); + c->address_families = set_new(NULL); if (!c->address_families) return log_oom(); @@ -3092,7 +3092,7 @@ int config_parse_set_status( } } - r = set_ensure_allocated(&status_set->status, NULL, NULL); + r = set_ensure_allocated(&status_set->status, NULL); if (r < 0) return log_oom(); @@ -3423,7 +3423,7 @@ static int load_from_path(Unit *u, const char *path) { assert(u); assert(path); - symlink_names = set_new(string_hash_func, string_compare_func); + symlink_names = set_new(&string_hash_ops); if (!symlink_names) return -ENOMEM; diff --git a/src/core/manager.c b/src/core/manager.c index 095111e8c..0770727cd 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -449,27 +449,27 @@ int manager_new(SystemdRunningAs running_as, bool test_run, Manager **_m) { if (r < 0) goto fail; - r = hashmap_ensure_allocated(&m->units, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&m->units, &string_hash_ops); if (r < 0) goto fail; - r = hashmap_ensure_allocated(&m->jobs, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&m->jobs, NULL); if (r < 0) goto fail; - r = hashmap_ensure_allocated(&m->cgroup_unit, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&m->cgroup_unit, &string_hash_ops); if (r < 0) goto fail; - r = hashmap_ensure_allocated(&m->watch_bus, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&m->watch_bus, &string_hash_ops); if (r < 0) goto fail; - r = set_ensure_allocated(&m->startup_units, trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&m->startup_units, NULL); if (r < 0) goto fail; - r = set_ensure_allocated(&m->failed_units, trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&m->failed_units, NULL); if (r < 0) goto fail; @@ -903,7 +903,7 @@ static void manager_build_unit_path_cache(Manager *m) { set_free_free(m->unit_path_cache); - m->unit_path_cache = set_new(string_hash_func, string_compare_func); + m->unit_path_cache = set_new(&string_hash_ops); if (!m->unit_path_cache) { log_error("Failed to allocate unit path cache."); return; diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c index cc2633e3b..23a66d2e9 100644 --- a/src/core/mount-setup.c +++ b/src/core/mount-setup.c @@ -235,7 +235,7 @@ int mount_cgroup_controllers(char ***join_controllers) { return 0; } - controllers = set_new(string_hash_func, string_compare_func); + controllers = set_new(&string_hash_ops); if (!controllers) return log_oom(); diff --git a/src/core/swap.c b/src/core/swap.c index 019d32ed0..b88a914f7 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -77,7 +77,7 @@ static int swap_set_devnode(Swap *s, const char *devnode) { assert(s); - r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&UNIT(s)->manager->swaps_by_devnode, &string_hash_ops); if (r < 0) return r; @@ -1226,7 +1226,7 @@ static int swap_following_set(Unit *u, Set **_set) { return 0; } - set = set_new(NULL, NULL); + set = set_new(NULL); if (!set) return -ENOMEM; diff --git a/src/core/transaction.c b/src/core/transaction.c index 1f4c9ce41..dbb4133fe 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -1143,7 +1143,7 @@ Transaction *transaction_new(bool irreversible) { if (!tr) return NULL; - tr->jobs = hashmap_new(trivial_hash_func, trivial_compare_func); + tr->jobs = hashmap_new(NULL); if (!tr->jobs) { free(tr); return NULL; diff --git a/src/core/unit.c b/src/core/unit.c index b5c318294..5978e21f5 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -81,7 +81,7 @@ Unit *unit_new(Manager *m, size_t size) { if (!u) return NULL; - u->names = set_new(string_hash_func, string_compare_func); + u->names = set_new(&string_hash_ops); if (!u->names) { free(u); return NULL; @@ -1843,17 +1843,17 @@ int unit_watch_pid(Unit *u, pid_t pid) { /* Watch a specific PID. We only support one or two units * watching each PID for now, not more. */ - r = set_ensure_allocated(&u->pids, trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&u->pids, NULL); if (r < 0) return r; - r = hashmap_ensure_allocated(&u->manager->watch_pids1, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&u->manager->watch_pids1, NULL); if (r < 0) return r; r = hashmap_put(u->manager->watch_pids1, LONG_TO_PTR(pid), u); if (r == -EEXIST) { - r = hashmap_ensure_allocated(&u->manager->watch_pids2, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&u->manager->watch_pids2, NULL); if (r < 0) return r; @@ -2086,22 +2086,22 @@ int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_referen return 0; } - r = set_ensure_allocated(&u->dependencies[d], trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&u->dependencies[d], NULL); if (r < 0) return r; if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID) { - r = set_ensure_allocated(&other->dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&other->dependencies[inverse_table[d]], NULL); if (r < 0) return r; } if (add_reference) { - r = set_ensure_allocated(&u->dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&u->dependencies[UNIT_REFERENCES], NULL); if (r < 0) return r; - r = set_ensure_allocated(&other->dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&other->dependencies[UNIT_REFERENCED_BY], NULL); if (r < 0) return r; } @@ -2847,7 +2847,7 @@ static Set *unit_pid_set(pid_t main_pid, pid_t control_pid) { Set *pid_set; int r; - pid_set = set_new(trivial_hash_func, trivial_compare_func); + pid_set = set_new(NULL); if (!pid_set) return NULL; @@ -3385,7 +3385,7 @@ int unit_require_mounts_for(Unit *u, const char *path) { char *q; if (!u->manager->units_requiring_mounts_for) { - u->manager->units_requiring_mounts_for = hashmap_new(string_hash_func, string_compare_func); + u->manager->units_requiring_mounts_for = hashmap_new(&string_hash_ops); if (!u->manager->units_requiring_mounts_for) return -ENOMEM; } @@ -3394,7 +3394,7 @@ int unit_require_mounts_for(Unit *u, const char *path) { if (!q) return -ENOMEM; - x = set_new(NULL, NULL); + x = set_new(NULL); if (!x) { free(q); return -ENOMEM; diff --git a/src/delta/delta.c b/src/delta/delta.c index cd8bd3571..91f8592b4 100644 --- a/src/delta/delta.c +++ b/src/delta/delta.c @@ -268,7 +268,7 @@ static int enumerate_dir_d(Hashmap *top, Hashmap *bottom, Hashmap *drops, const h = hashmap_get(drops, unit); if (!h) { - h = hashmap_new(string_hash_func, string_compare_func); + h = hashmap_new(&string_hash_ops); if (!h) return -ENOMEM; hashmap_put(drops, unit, h); @@ -372,9 +372,9 @@ static int process_suffix(const char *suffix, const char *onlyprefix) { dropins = nulstr_contains(have_dropins, suffix); - top = hashmap_new(string_hash_func, string_compare_func); - bottom = hashmap_new(string_hash_func, string_compare_func); - drops = hashmap_new(string_hash_func, string_compare_func); + top = hashmap_new(&string_hash_ops); + bottom = hashmap_new(&string_hash_ops); + drops = hashmap_new(&string_hash_ops); if (!top || !bottom || !drops) { r = -ENOMEM; goto finish; diff --git a/src/journal-remote/journal-remote.c b/src/journal-remote/journal-remote.c index 1cc86aeaf..12de82033 100644 --- a/src/journal-remote/journal-remote.c +++ b/src/journal-remote/journal-remote.c @@ -222,20 +222,14 @@ static int open_output(Writer *w, const char* host) { **********************************************************************/ static int init_writer_hashmap(RemoteServer *s) { - static const struct { - hash_func_t hash_func; - compare_func_t compare_func; - } functions[] = { - [JOURNAL_WRITE_SPLIT_NONE] = {trivial_hash_func, - trivial_compare_func}, - [JOURNAL_WRITE_SPLIT_HOST] = {string_hash_func, - string_compare_func}, + static const struct hash_ops *hash_ops[] = { + [JOURNAL_WRITE_SPLIT_NONE] = NULL, + [JOURNAL_WRITE_SPLIT_HOST] = &string_hash_ops, }; - assert(arg_split_mode >= 0 && arg_split_mode < (int) ELEMENTSOF(functions)); + assert(arg_split_mode >= 0 && arg_split_mode < (int) ELEMENTSOF(hash_ops)); - s->writers = hashmap_new(functions[arg_split_mode].hash_func, - functions[arg_split_mode].compare_func); + s->writers = hashmap_new(hash_ops[arg_split_mode]); if (!s->writers) return log_oom(); @@ -695,7 +689,7 @@ static int setup_microhttpd_server(RemoteServer *s, goto error; } - r = hashmap_ensure_allocated(&s->daemons, uint64_hash_func, uint64_compare_func); + r = hashmap_ensure_allocated(&s->daemons, &uint64_hash_ops); if (r < 0) { log_oom(); goto error; diff --git a/src/journal/catalog.c b/src/journal/catalog.c index f03357ded..41d450b15 100644 --- a/src/journal/catalog.c +++ b/src/journal/catalog.c @@ -64,7 +64,7 @@ typedef struct CatalogItem { le64_t offset; } CatalogItem; -unsigned long catalog_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) { +static unsigned long catalog_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) { const CatalogItem *i = p; uint64_t u; size_t l, sz; @@ -81,7 +81,7 @@ unsigned long catalog_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_S return (unsigned long) u; } -int catalog_compare_func(const void *a, const void *b) { +static int catalog_compare_func(const void *a, const void *b) { const CatalogItem *i = a, *j = b; unsigned k; @@ -95,6 +95,11 @@ int catalog_compare_func(const void *a, const void *b) { return strcmp(i->language, j->language); } +const struct hash_ops catalog_hash_ops = { + .hash = catalog_hash_func, + .compare = catalog_compare_func +}; + static int finish_item( Hashmap *h, struct strbuf *sb, @@ -407,7 +412,7 @@ int catalog_update(const char* database, const char* root, const char* const* di unsigned n; long r; - h = hashmap_new(catalog_hash_func, catalog_compare_func); + h = hashmap_new(&catalog_hash_ops); sb = strbuf_new(); if (!h || !sb) { diff --git a/src/journal/catalog.h b/src/journal/catalog.h index fdde67aee..a72ecf6de 100644 --- a/src/journal/catalog.h +++ b/src/journal/catalog.h @@ -28,11 +28,10 @@ #include "strbuf.h" int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path); -unsigned long catalog_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]); -int catalog_compare_func(const void *a, const void *b) _pure_; int catalog_update(const char* database, const char* root, const char* const* dirs); int catalog_get(const char* database, sd_id128_t id, char **data); int catalog_list(FILE *f, const char* database, bool oneline); int catalog_list_items(FILE *f, const char* database, bool oneline, char **items); int catalog_file_lang(const char *filename, char **lang); extern const char * const catalog_file_dirs[]; +extern const struct hash_ops catalog_hash_ops; diff --git a/src/journal/coredump-vacuum.c b/src/journal/coredump-vacuum.c index 125bb3a4b..fec901e8e 100644 --- a/src/journal/coredump-vacuum.c +++ b/src/journal/coredump-vacuum.c @@ -194,7 +194,7 @@ int coredump_vacuum(int exclude_fd, off_t keep_free, off_t max_use) { exclude_st.st_ino == st.st_ino) continue; - r = hashmap_ensure_allocated(&h, NULL, NULL); + r = hashmap_ensure_allocated(&h, NULL); if (r < 0) return log_oom(); diff --git a/src/journal/coredumpctl.c b/src/journal/coredumpctl.c index 34dcae87c..d4756fe67 100644 --- a/src/journal/coredumpctl.c +++ b/src/journal/coredumpctl.c @@ -58,7 +58,7 @@ static Set *new_matches(void) { char *tmp; int r; - set = set_new(trivial_hash_func, trivial_compare_func); + set = set_new(NULL); if (!set) { log_oom(); return NULL; diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index 7286e14dd..f25cda6dd 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -2498,7 +2498,7 @@ int journal_file_open( goto fail; } - f->chain_cache = hashmap_new(uint64_hash_func, uint64_compare_func); + f->chain_cache = hashmap_new(&uint64_hash_ops); if (!f->chain_cache) { r = -ENOMEM; goto fail; diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index d00a815ba..47206d383 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -1029,7 +1029,7 @@ static int get_possible_units(sd_journal *j, const char *field; int r; - found = set_new(string_hash_func, string_compare_func); + found = set_new(&string_hash_ops); if (!found) return log_oom(); diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 01da38b8d..3df741639 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -1483,7 +1483,7 @@ int server_init(Server *s) { mkdir_p("/run/systemd/journal", 0755); - s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func); + s->user_journals = hashmap_new(NULL); if (!s->user_journals) return log_oom(); diff --git a/src/journal/mmap-cache.c b/src/journal/mmap-cache.c index 908562da2..2d268fc33 100644 --- a/src/journal/mmap-cache.c +++ b/src/journal/mmap-cache.c @@ -227,7 +227,7 @@ static Context *context_add(MMapCache *m, unsigned id) { if (c) return c; - r = hashmap_ensure_allocated(&m->contexts, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&m->contexts, NULL); if (r < 0) return NULL; @@ -281,7 +281,7 @@ static FileDescriptor* fd_add(MMapCache *m, int fd) { if (f) return f; - r = hashmap_ensure_allocated(&m->fds, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&m->fds, NULL); if (r < 0) return NULL; diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c index 693707cb3..1fc9f01d0 100644 --- a/src/journal/sd-journal.c +++ b/src/journal/sd-journal.c @@ -70,7 +70,7 @@ static int set_put_error(sd_journal *j, int r) { if (r >= 0) return r; - k = set_ensure_allocated(&j->errors, trivial_hash_func, trivial_compare_func); + k = set_ensure_allocated(&j->errors, NULL); if (k < 0) return k; @@ -1662,7 +1662,7 @@ static int allocate_inotify(sd_journal *j) { } if (!j->directories_by_wd) { - j->directories_by_wd = hashmap_new(trivial_hash_func, trivial_compare_func); + j->directories_by_wd = hashmap_new(NULL); if (!j->directories_by_wd) return -ENOMEM; } @@ -1688,8 +1688,8 @@ static sd_journal *journal_new(int flags, const char *path) { goto fail; } - j->files = hashmap_new(string_hash_func, string_compare_func); - j->directories_by_path = hashmap_new(string_hash_func, string_compare_func); + j->files = hashmap_new(&string_hash_ops); + j->directories_by_path = hashmap_new(&string_hash_ops); j->mmap = mmap_cache_new(); if (!j->files || !j->directories_by_path || !j->mmap) goto fail; diff --git a/src/journal/test-catalog.c b/src/journal/test-catalog.c index 967ab6756..5fe2f6a26 100644 --- a/src/journal/test-catalog.c +++ b/src/journal/test-catalog.c @@ -62,7 +62,7 @@ static void test_catalog_importing(void) { Hashmap *h; struct strbuf *sb; - assert_se(h = hashmap_new(catalog_hash_func, catalog_compare_func)); + assert_se(h = hashmap_new(&catalog_hash_ops)); assert_se(sb = strbuf_new()); #define BUF "xxx" diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index ab683228b..a6d6178e7 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -109,6 +109,11 @@ 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 +}; + static void dhcp_lease_free(DHCPLease *lease) { if (!lease) return; @@ -158,8 +163,7 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) { server->address = htobe32(INADDR_ANY); server->netmask = htobe32(INADDR_ANY); server->index = ifindex; - server->leases_by_client_id = hashmap_new(client_id_hash_func, - client_id_compare_func); + server->leases_by_client_id = hashmap_new(&client_id_hash_ops); *ret = server; server = NULL; diff --git a/src/libsystemd/sd-bus/bus-match.c b/src/libsystemd/sd-bus/bus-match.c index 88b61a75b..18afe0f12 100644 --- a/src/libsystemd/sd-bus/bus-match.c +++ b/src/libsystemd/sd-bus/bus-match.c @@ -450,13 +450,13 @@ static int bus_match_add_compare_value( where->child = c; if (t == BUS_MATCH_MESSAGE_TYPE) { - c->compare.children = hashmap_new(trivial_hash_func, trivial_compare_func); + c->compare.children = hashmap_new(NULL); if (!c->compare.children) { r = -ENOMEM; goto fail; } } else if (BUS_MATCH_CAN_HASH(t)) { - c->compare.children = hashmap_new(string_hash_func, string_compare_func); + c->compare.children = hashmap_new(&string_hash_ops); if (!c->compare.children) { r = -ENOMEM; goto fail; diff --git a/src/libsystemd/sd-bus/bus-objects.c b/src/libsystemd/sd-bus/bus-objects.c index 3a2de6520..81c840f12 100644 --- a/src/libsystemd/sd-bus/bus-objects.c +++ b/src/libsystemd/sd-bus/bus-objects.c @@ -225,7 +225,7 @@ static int get_child_nodes( assert(n); assert(_s); - s = set_new(string_hash_func, string_compare_func); + s = set_new(&string_hash_ops); if (!s) return -ENOMEM; @@ -1420,7 +1420,7 @@ static struct node *bus_node_allocate(sd_bus *bus, const char *path) { if (n) return n; - r = hashmap_ensure_allocated(&bus->nodes, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&bus->nodes, &string_hash_ops); if (r < 0) return NULL; @@ -1590,6 +1590,11 @@ 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 +}; + static int add_object_vtable_internal( sd_bus *bus, sd_bus_slot **slot, @@ -1618,11 +1623,11 @@ static int add_object_vtable_internal( !streq(interface, "org.freedesktop.DBus.Peer") && !streq(interface, "org.freedesktop.DBus.ObjectManager"), -EINVAL); - r = hashmap_ensure_allocated(&bus->vtable_methods, vtable_member_hash_func, vtable_member_compare_func); + r = hashmap_ensure_allocated(&bus->vtable_methods, &vtable_member_hash_ops); if (r < 0) return r; - r = hashmap_ensure_allocated(&bus->vtable_properties, vtable_member_hash_func, vtable_member_compare_func); + r = hashmap_ensure_allocated(&bus->vtable_properties, &vtable_member_hash_ops); if (r < 0) return r; diff --git a/src/libsystemd/sd-bus/bus-track.c b/src/libsystemd/sd-bus/bus-track.c index ffa2cf3d7..0a3322a4e 100644 --- a/src/libsystemd/sd-bus/bus-track.c +++ b/src/libsystemd/sd-bus/bus-track.c @@ -166,7 +166,7 @@ _public_ int sd_bus_track_add_name(sd_bus_track *track, const char *name) { assert_return(track, -EINVAL); assert_return(service_name_is_valid(name), -EINVAL); - r = hashmap_ensure_allocated(&track->names, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&track->names, &string_hash_ops); if (r < 0) return r; diff --git a/src/libsystemd/sd-bus/bus-util.c b/src/libsystemd/sd-bus/bus-util.c index a85d36180..7c6da60cc 100644 --- a/src/libsystemd/sd-bus/bus-util.c +++ b/src/libsystemd/sd-bus/bus-util.c @@ -399,7 +399,7 @@ int bus_verify_polkit_async( if (!sender) return -EBADMSG; - r = hashmap_ensure_allocated(registry, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(registry, NULL); if (r < 0) return r; diff --git a/src/libsystemd/sd-bus/busctl.c b/src/libsystemd/sd-bus/busctl.c index af7180441..f06b74505 100644 --- a/src/libsystemd/sd-bus/busctl.c +++ b/src/libsystemd/sd-bus/busctl.c @@ -76,7 +76,7 @@ static int list_bus_names(sd_bus *bus, char **argv) { pager_open_if_enabled(); - names = hashmap_new(string_hash_func, string_compare_func); + names = hashmap_new(&string_hash_ops); if (!names) return log_oom(); diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index 83b3aa1c3..33b65aba7 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -1756,7 +1756,7 @@ _public_ int sd_bus_call_async( if (!BUS_IS_OPEN(bus->state)) return -ENOTCONN; - r = hashmap_ensure_allocated(&bus->reply_callbacks, uint64_hash_func, uint64_compare_func); + r = hashmap_ensure_allocated(&bus->reply_callbacks, &uint64_hash_ops); if (r < 0) return r; diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c index 985ff2829..b56182dda 100644 --- a/src/libsystemd/sd-event/sd-event.c +++ b/src/libsystemd/sd-event/sd-event.c @@ -1032,7 +1032,7 @@ _public_ int sd_event_add_child( assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(!event_pid_changed(e), -ECHILD); - r = hashmap_ensure_allocated(&e->child_sources, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&e->child_sources, NULL); if (r < 0) return r; @@ -1123,7 +1123,7 @@ _public_ int sd_event_add_post( assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); assert_return(!event_pid_changed(e), -ECHILD); - r = set_ensure_allocated(&e->post_sources, trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&e->post_sources, NULL); if (r < 0) return r; diff --git a/src/libsystemd/sd-rtnl/sd-rtnl.c b/src/libsystemd/sd-rtnl/sd-rtnl.c index a3f314b95..b7f1afe90 100644 --- a/src/libsystemd/sd-rtnl/sd-rtnl.c +++ b/src/libsystemd/sd-rtnl/sd-rtnl.c @@ -557,7 +557,7 @@ int sd_rtnl_call_async(sd_rtnl *nl, assert_return(callback, -EINVAL); assert_return(!rtnl_pid_changed(nl), -ECHILD); - r = hashmap_ensure_allocated(&nl->reply_callbacks, uint64_hash_func, uint64_compare_func); + r = hashmap_ensure_allocated(&nl->reply_callbacks, &uint64_hash_ops); if (r < 0) return r; diff --git a/src/locale/localectl.c b/src/locale/localectl.c index 2bb0d3b6b..bf8b7b2be 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -272,7 +272,7 @@ static int list_vconsole_keymaps(sd_bus *bus, char **args, unsigned n) { _cleanup_strv_free_ char **l = NULL; const char *dir; - keymaps = set_new(string_hash_func, string_compare_func); + keymaps = set_new(&string_hash_ops); if (!keymaps) return log_oom(); diff --git a/src/login/logind-acl.c b/src/login/logind-acl.c index b76e16d90..f7c6f3a4e 100644 --- a/src/login/logind-acl.c +++ b/src/login/logind-acl.c @@ -190,7 +190,7 @@ int devnode_acl_all(struct udev *udev, assert(udev); - nodes = set_new(string_hash_func, string_compare_func); + nodes = set_new(&string_hash_ops); if (!nodes) return -ENOMEM; diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 10a43a4a3..eeb58c903 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -61,7 +61,7 @@ Session* session_new(Manager *m, const char *id) { return NULL; } - s->devices = hashmap_new(devt_hash_func, devt_compare_func); + s->devices = hashmap_new(&devt_hash_ops); if (!s->devices) { free(s->state_file); free(s); diff --git a/src/login/logind.c b/src/login/logind.c index 1f94a97bd..f1b6a8629 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -64,17 +64,17 @@ Manager *manager_new(void) { m->runtime_dir_size = PAGE_ALIGN((size_t) (physical_memory() / 10)); /* 10% */ - m->devices = hashmap_new(string_hash_func, string_compare_func); - m->seats = hashmap_new(string_hash_func, string_compare_func); - m->sessions = hashmap_new(string_hash_func, string_compare_func); - m->users = hashmap_new(trivial_hash_func, trivial_compare_func); - m->inhibitors = hashmap_new(string_hash_func, string_compare_func); - m->buttons = hashmap_new(string_hash_func, string_compare_func); + m->devices = hashmap_new(&string_hash_ops); + m->seats = hashmap_new(&string_hash_ops); + m->sessions = hashmap_new(&string_hash_ops); + m->users = hashmap_new(NULL); + m->inhibitors = hashmap_new(&string_hash_ops); + m->buttons = hashmap_new(&string_hash_ops); - m->user_units = hashmap_new(string_hash_func, string_compare_func); - m->session_units = hashmap_new(string_hash_func, string_compare_func); + m->user_units = hashmap_new(&string_hash_ops); + m->session_units = hashmap_new(&string_hash_ops); - m->busnames = set_new(string_hash_func, string_compare_func); + m->busnames = set_new(&string_hash_ops); if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->busnames || !m->user_units || !m->session_units) diff --git a/src/machine/machined.c b/src/machine/machined.c index aac670ba7..71c8189d5 100644 --- a/src/machine/machined.c +++ b/src/machine/machined.c @@ -44,9 +44,9 @@ Manager *manager_new(void) { if (!m) return NULL; - m->machines = hashmap_new(string_hash_func, string_compare_func); - m->machine_units = hashmap_new(string_hash_func, string_compare_func); - m->machine_leaders = hashmap_new(trivial_hash_func, trivial_compare_func); + m->machines = hashmap_new(&string_hash_ops); + m->machine_units = hashmap_new(&string_hash_ops); + m->machine_leaders = hashmap_new(NULL); if (!m->machines || !m->machine_units || !m->machine_leaders) { manager_free(m); diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 11ac1307d..9bf1a811c 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -206,7 +206,7 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) { if (r < 0) return -ENOMEM; - r = hashmap_ensure_allocated(&manager->links, NULL, NULL); + r = hashmap_ensure_allocated(&manager->links, NULL); if (r < 0) return r; diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 1614bc009..2213ad717 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -115,7 +115,7 @@ int manager_new(Manager **ret) { return -ENOMEM; } - m->netdevs = hashmap_new(string_hash_func, string_compare_func); + m->netdevs = hashmap_new(&string_hash_ops); if (!m->netdevs) return -ENOMEM; @@ -485,15 +485,15 @@ int manager_save(Manager *m) { assert(m->state_file); /* We add all NTP and DNS server to a set, to filter out duplicates */ - dns = set_new(string_hash_func, string_compare_func); + dns = set_new(&string_hash_ops); if (!dns) return -ENOMEM; - ntp = set_new(string_hash_func, string_compare_func); + ntp = set_new(&string_hash_ops); if (!ntp) return -ENOMEM; - domains = set_new(string_hash_func, string_compare_func); + domains = set_new(&string_hash_ops); if (!domains) return -ENOMEM; diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index a2e27e091..aad99236c 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -63,15 +63,15 @@ static int network_load_one(Manager *manager, const char *filename) { LIST_HEAD_INIT(network->static_addresses); LIST_HEAD_INIT(network->static_routes); - network->stacked_netdevs = hashmap_new(string_hash_func, string_compare_func); + network->stacked_netdevs = hashmap_new(&string_hash_ops); if (!network->stacked_netdevs) return log_oom(); - network->addresses_by_section = hashmap_new(NULL, NULL); + network->addresses_by_section = hashmap_new(NULL); if (!network->addresses_by_section) return log_oom(); - network->routes_by_section = hashmap_new(NULL, NULL); + network->routes_by_section = hashmap_new(NULL); if (!network->routes_by_section) return log_oom(); diff --git a/src/network/networkd-wait-online-link.c b/src/network/networkd-wait-online-link.c index f23c7ceb8..268ab676c 100644 --- a/src/network/networkd-wait-online-link.c +++ b/src/network/networkd-wait-online-link.c @@ -34,12 +34,11 @@ int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) { assert(m); assert(ifindex > 0); - r = hashmap_ensure_allocated(&m->links, NULL, NULL); + r = hashmap_ensure_allocated(&m->links, NULL); if (r < 0) return r; - r = hashmap_ensure_allocated(&m->links_by_name, - string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops); if (r < 0) return r; diff --git a/src/readahead/readahead-collect.c b/src/readahead/readahead-collect.c index 1592cc867..822a803a4 100644 --- a/src/readahead/readahead-collect.c +++ b/src/readahead/readahead-collect.c @@ -282,7 +282,7 @@ static int collect(const char *root) { goto finish; } - files = hashmap_new(string_hash_func, string_compare_func); + files = hashmap_new(&string_hash_ops); if (!files) { log_error("Failed to allocate set."); r = -ENOMEM; diff --git a/src/remount-fs/remount-fs.c b/src/remount-fs/remount-fs.c index 847637a12..cd7cfe7a4 100644 --- a/src/remount-fs/remount-fs.c +++ b/src/remount-fs/remount-fs.c @@ -64,7 +64,7 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - pids = hashmap_new(trivial_hash_func, trivial_compare_func); + pids = hashmap_new(NULL); if (!pids) { log_error("Failed to allocate set"); goto finish; diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c index 733ef6348..33ca4d1a4 100644 --- a/src/resolve/resolved-dns-cache.c +++ b/src/resolve/resolved-dns-cache.c @@ -186,7 +186,7 @@ static int dns_cache_init(DnsCache *c) { if (r < 0) return r; - r = hashmap_ensure_allocated(&c->by_key, dns_resource_key_hash_func, dns_resource_key_compare_func); + r = hashmap_ensure_allocated(&c->by_key, &dns_resource_key_hash_ops); if (r < 0) return r; diff --git a/src/resolve/resolved-dns-domain.c b/src/resolve/resolved-dns-domain.c index 8ed1ecf0a..e1eb3ddfe 100644 --- a/src/resolve/resolved-dns-domain.c +++ b/src/resolve/resolved-dns-domain.c @@ -371,6 +371,11 @@ 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 +}; + int dns_name_equal(const char *x, const char *y) { int r, q, k, w; diff --git a/src/resolve/resolved-dns-domain.h b/src/resolve/resolved-dns-domain.h index e674c5d9c..0888a7846 100644 --- a/src/resolve/resolved-dns-domain.h +++ b/src/resolve/resolved-dns-domain.h @@ -37,6 +37,7 @@ int dns_name_normalize(const char *s, char **_ret); unsigned long dns_name_hash_func(const void *s, const uint8_t hash_key[HASH_KEY_SIZE]); int dns_name_compare_func(const void *a, const void *b); +extern const struct hash_ops dns_name_hash_ops; int dns_name_equal(const char *x, const char *y); int dns_name_endswith(const char *name, const char *suffix); diff --git a/src/resolve/resolved-dns-packet.c b/src/resolve/resolved-dns-packet.c index 0d276df8c..7375f7748 100644 --- a/src/resolve/resolved-dns-packet.c +++ b/src/resolve/resolved-dns-packet.c @@ -433,9 +433,7 @@ int dns_packet_append_name(DnsPacket *p, const char *name, goto fail; if (allow_compression) { - r = hashmap_ensure_allocated(&p->names, - dns_name_hash_func, - dns_name_compare_func); + r = hashmap_ensure_allocated(&p->names, &dns_name_hash_ops); if (r < 0) goto fail; diff --git a/src/resolve/resolved-dns-query.c b/src/resolve/resolved-dns-query.c index 669595d7f..f0483c980 100644 --- a/src/resolve/resolved-dns-query.c +++ b/src/resolve/resolved-dns-query.c @@ -144,7 +144,7 @@ static int dns_query_add_transaction(DnsQuery *q, DnsScope *s, DnsResourceKey *k assert(q); assert(s); - r = set_ensure_allocated(&q->transactions, NULL, NULL); + r = set_ensure_allocated(&q->transactions, NULL); if (r < 0) return r; @@ -166,7 +166,7 @@ static int dns_query_add_transaction(DnsQuery *q, DnsScope *s, DnsResourceKey *k return r; } - r = set_ensure_allocated(&t->queries, NULL, NULL); + r = set_ensure_allocated(&t->queries, NULL); if (r < 0) goto gc; diff --git a/src/resolve/resolved-dns-rr.c b/src/resolve/resolved-dns-rr.c index c5f7cb931..fd5ecf413 100644 --- a/src/resolve/resolved-dns-rr.c +++ b/src/resolve/resolved-dns-rr.c @@ -133,7 +133,7 @@ int dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRec return dns_name_equal(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key)); } -unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) { +static unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]) { const DnsResourceKey *k = i; unsigned long ul; @@ -144,7 +144,7 @@ unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[H return ul; } -int dns_resource_key_compare_func(const void *a, const void *b) { +static int dns_resource_key_compare_func(const void *a, const void *b) { const DnsResourceKey *x = a, *y = b; int ret; @@ -165,6 +165,11 @@ 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 +}; + int dns_resource_key_to_string(const DnsResourceKey *key, char **ret) { char cbuf[DECIMAL_STR_MAX(uint16_t)], tbuf[DECIMAL_STR_MAX(uint16_t)]; const char *c, *t; diff --git a/src/resolve/resolved-dns-rr.h b/src/resolve/resolved-dns-rr.h index 3222f1f0e..9d9a89d38 100644 --- a/src/resolve/resolved-dns-rr.h +++ b/src/resolve/resolved-dns-rr.h @@ -159,8 +159,6 @@ DnsResourceKey* dns_resource_key_unref(DnsResourceKey *key); int dns_resource_key_equal(const DnsResourceKey *a, const DnsResourceKey *b); int dns_resource_key_match_rr(const DnsResourceKey *key, const DnsResourceRecord *rr); int dns_resource_key_match_cname(const DnsResourceKey *key, const DnsResourceRecord *rr); -unsigned long dns_resource_key_hash_func(const void *i, const uint8_t hash_key[HASH_KEY_SIZE]); -int dns_resource_key_compare_func(const void *a, const void *b); int dns_resource_key_to_string(const DnsResourceKey *key, char **ret); DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceKey*, dns_resource_key_unref); @@ -175,3 +173,5 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(DnsResourceRecord*, dns_resource_record_unref); const char *dns_class_to_string(uint16_t type); int dns_class_from_string(const char *name, uint16_t *class); + +extern const struct hash_ops dns_resource_key_hash_ops; diff --git a/src/resolve/resolved-dns-scope.c b/src/resolve/resolved-dns-scope.c index 039a754ff..40d59922d 100644 --- a/src/resolve/resolved-dns-scope.c +++ b/src/resolve/resolved-dns-scope.c @@ -709,7 +709,7 @@ int dns_scope_notify_conflict(DnsScope *scope, DnsResourceRecord *rr) { /* We don't send these queries immediately. Instead, we queue * them, and send them after some jitter delay. */ - r = hashmap_ensure_allocated(&scope->conflict_queue, dns_resource_key_hash_func, dns_resource_key_compare_func); + r = hashmap_ensure_allocated(&scope->conflict_queue, &dns_resource_key_hash_ops); if (r < 0) { log_oom(); return r; diff --git a/src/resolve/resolved-dns-server.c b/src/resolve/resolved-dns-server.c index c99768be4..caf06fe45 100644 --- a/src/resolve/resolved-dns-server.c +++ b/src/resolve/resolved-dns-server.c @@ -100,7 +100,7 @@ DnsServer* dns_server_free(DnsServer *s) { return NULL; } -unsigned long dns_server_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) { +static unsigned long dns_server_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) { const DnsServer *s = p; uint64_t u; @@ -110,7 +110,7 @@ unsigned long dns_server_hash_func(const void *p, const uint8_t hash_key[HASH_KE return u; } -int dns_server_compare_func(const void *a, const void *b) { +static int dns_server_compare_func(const void *a, const void *b) { const DnsServer *x = a, *y = b; if (x->family < y->family) @@ -120,3 +120,8 @@ int dns_server_compare_func(const void *a, const void *b) { return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family)); } + +const struct hash_ops dns_server_hash_ops = { + .hash = dns_server_hash_func, + .compare = dns_server_compare_func +}; diff --git a/src/resolve/resolved-dns-server.h b/src/resolve/resolved-dns-server.h index f2361a838..a438a2776 100644 --- a/src/resolve/resolved-dns-server.h +++ b/src/resolve/resolved-dns-server.h @@ -60,5 +60,4 @@ int dns_server_new( DnsServer* dns_server_free(DnsServer *s); -unsigned long dns_server_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]); -int dns_server_compare_func(const void *a, const void *b); +extern const struct hash_ops dns_server_hash_ops; diff --git a/src/resolve/resolved-dns-transaction.c b/src/resolve/resolved-dns-transaction.c index 990b1f2e4..74b063414 100644 --- a/src/resolve/resolved-dns-transaction.c +++ b/src/resolve/resolved-dns-transaction.c @@ -78,7 +78,7 @@ int dns_transaction_new(DnsTransaction **ret, DnsScope *s, DnsQuestion *q) { assert(s); assert(q); - r = hashmap_ensure_allocated(&s->manager->dns_transactions, NULL, NULL); + r = hashmap_ensure_allocated(&s->manager->dns_transactions, NULL); if (r < 0) return r; diff --git a/src/resolve/resolved-dns-zone.c b/src/resolve/resolved-dns-zone.c index b53e957d7..8098ff5e7 100644 --- a/src/resolve/resolved-dns-zone.c +++ b/src/resolve/resolved-dns-zone.c @@ -126,11 +126,11 @@ static int dns_zone_init(DnsZone *z) { assert(z); - r = hashmap_ensure_allocated(&z->by_key, dns_resource_key_hash_func, dns_resource_key_compare_func); + r = hashmap_ensure_allocated(&z->by_key, &dns_resource_key_hash_ops); if (r < 0) return r; - r = hashmap_ensure_allocated(&z->by_name, dns_name_hash_func, dns_name_compare_func); + r = hashmap_ensure_allocated(&z->by_name, &dns_name_hash_ops); if (r < 0) return r; @@ -194,7 +194,7 @@ static int dns_zone_item_probe_start(DnsZoneItem *i) { return r; } - r = set_ensure_allocated(&t->zone_items, NULL, NULL); + r = set_ensure_allocated(&t->zone_items, NULL); if (r < 0) goto gc; diff --git a/src/resolve/resolved-link.c b/src/resolve/resolved-link.c index f47017ced..4def67221 100644 --- a/src/resolve/resolved-link.c +++ b/src/resolve/resolved-link.c @@ -33,7 +33,7 @@ int link_new(Manager *m, Link **ret, int ifindex) { assert(m); assert(ifindex > 0); - r = hashmap_ensure_allocated(&m->links, NULL, NULL); + r = hashmap_ensure_allocated(&m->links, NULL); if (r < 0) return r; diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index 00aaffe44..63d784538 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -737,11 +737,11 @@ int manager_write_resolv_conf(Manager *m) { manager_read_resolv_conf(m); /* Add the full list to a set, to filter out duplicates */ - dns = set_new(dns_server_hash_func, dns_server_compare_func); + dns = set_new(&dns_server_hash_ops); if (!dns) return -ENOMEM; - domains = set_new(dns_name_hash_func, dns_name_compare_func); + domains = set_new(&dns_name_hash_ops); if (!domains) return -ENOMEM; diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 00eac6423..da8e88522 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -161,7 +161,7 @@ int cg_kill(const char *controller, const char *path, int sig, bool sigcont, boo * tasks list, to properly handle forking processes */ if (!s) { - s = allocated_set = set_new(trivial_hash_func, trivial_compare_func); + s = allocated_set = set_new(NULL); if (!s) return -ENOMEM; } @@ -239,7 +239,7 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool si assert(sig >= 0); if (!s) { - s = allocated_set = set_new(trivial_hash_func, trivial_compare_func); + s = allocated_set = set_new(NULL); if (!s) return -ENOMEM; } @@ -290,7 +290,7 @@ int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char assert(cto); assert(pto); - s = set_new(trivial_hash_func, trivial_compare_func); + s = set_new(NULL); if (!s) return -ENOMEM; diff --git a/src/shared/conf-files.c b/src/shared/conf-files.c index c72a099b5..e6ee97a97 100644 --- a/src/shared/conf-files.c +++ b/src/shared/conf-files.c @@ -109,7 +109,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const if (!path_strv_resolve_uniq(dirs, root)) return -ENOMEM; - fh = hashmap_new(string_hash_func, string_compare_func); + fh = hashmap_new(&string_hash_ops); if (!fh) return -ENOMEM; diff --git a/src/shared/fdset.c b/src/shared/fdset.c index d2ea66501..37cbd8526 100644 --- a/src/shared/fdset.c +++ b/src/shared/fdset.c @@ -38,7 +38,7 @@ #define PTR_TO_FD(p) (PTR_TO_INT(p)-1) FDSet *fdset_new(void) { - return MAKE_FDSET(set_new(trivial_hash_func, trivial_compare_func)); + return MAKE_FDSET(set_new(NULL)); } void fdset_free(FDSet *s) { diff --git a/src/shared/hashmap.c b/src/shared/hashmap.c index dbf91c439..715484ce7 100644 --- a/src/shared/hashmap.c +++ b/src/shared/hashmap.c @@ -39,8 +39,7 @@ struct hashmap_entry { }; struct Hashmap { - hash_func_t hash_func; - compare_func_t compare_func; + const struct hash_ops *hash_ops; struct hashmap_entry *iterate_list_head, *iterate_list_tail; @@ -141,6 +140,11 @@ int string_compare_func(const void *a, const void *b) { return strcmp(a, b); } +const struct hash_ops string_hash_ops = { + .hash = string_hash_func, + .compare = string_compare_func +}; + unsigned long trivial_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) { uint64_t u; siphash24((uint8_t*) &u, &p, sizeof(p), hash_key); @@ -151,6 +155,11 @@ int trivial_compare_func(const void *a, const void *b) { return a < b ? -1 : (a > b ? 1 : 0); } +const struct hash_ops trivial_hash_ops = { + .hash = trivial_hash_func, + .compare = trivial_compare_func +}; + unsigned long uint64_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) { uint64_t u; siphash24((uint8_t*) &u, p, sizeof(uint64_t), hash_key); @@ -164,6 +173,11 @@ int uint64_compare_func(const void *_a, const void *_b) { return a < b ? -1 : (a > b ? 1 : 0); } +const struct hash_ops uint64_hash_ops = { + .hash = uint64_hash_func, + .compare = uint64_compare_func +}; + #if SIZEOF_DEV_T != 8 unsigned long devt_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) { uint64_t u; @@ -177,10 +191,15 @@ int devt_compare_func(const void *_a, const void *_b) { b = *(const dev_t*) _b; return a < b ? -1 : (a > b ? 1 : 0); } + +const struct hash_ops devt_hash_ops = { + .hash = devt_hash_func, + .compare = devt_compare_func +}; #endif static unsigned bucket_hash(Hashmap *h, const void *p) { - return (unsigned) (h->hash_func(p, h->hash_key) % h->n_buckets); + return (unsigned) (h->hash_ops->hash(p, h->hash_key) % h->n_buckets); } static void get_hash_key(uint8_t hash_key[HASH_KEY_SIZE], bool reuse_is_ok) { @@ -202,7 +221,7 @@ static void get_hash_key(uint8_t hash_key[HASH_KEY_SIZE], bool reuse_is_ok) { memcpy(hash_key, current, sizeof(current)); } -Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) { +Hashmap *hashmap_new(const struct hash_ops *hash_ops) { bool b; Hashmap *h; size_t size; @@ -224,8 +243,7 @@ Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) { return NULL; } - h->hash_func = hash_func ? hash_func : trivial_hash_func; - h->compare_func = compare_func ? compare_func : trivial_compare_func; + h->hash_ops = hash_ops ? hash_ops : &trivial_hash_ops; h->n_buckets = INITIAL_N_BUCKETS; h->n_entries = 0; @@ -240,7 +258,7 @@ Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) { return h; } -int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func) { +int hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops) { Hashmap *q; assert(h); @@ -248,7 +266,7 @@ int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t if (*h) return 0; - q = hashmap_new(hash_func, compare_func); + q = hashmap_new(hash_ops); if (!q) return -ENOMEM; @@ -406,7 +424,7 @@ static struct hashmap_entry *hash_scan(Hashmap *h, unsigned hash, const void *ke assert(hash < h->n_buckets); for (e = h->buckets[hash]; e; e = e->bucket_next) - if (h->compare_func(e->key, key) == 0) + if (h->hash_ops->compare(e->key, key) == 0) return e; return NULL; @@ -438,7 +456,7 @@ static bool resize_buckets(Hashmap *h) { for (i = h->iterate_list_head; i; i = i->iterate_next) { unsigned long old_bucket, new_bucket; - old_bucket = h->hash_func(i->key, h->hash_key) % h->n_buckets; + old_bucket = h->hash_ops->hash(i->key, h->hash_key) % h->n_buckets; /* First, drop from old bucket table */ if (i->bucket_next) @@ -450,7 +468,7 @@ static bool resize_buckets(Hashmap *h) { h->buckets[old_bucket] = i->bucket_next; /* Then, add to new backet table */ - new_bucket = h->hash_func(i->key, nkey) % m; + new_bucket = h->hash_ops->hash(i->key, nkey) % m; i->bucket_next = n[new_bucket]; i->bucket_previous = NULL; @@ -949,7 +967,7 @@ Hashmap *hashmap_copy(Hashmap *h) { assert(h); - copy = hashmap_new(h->hash_func, h->compare_func); + copy = hashmap_new(h->hash_ops); if (!copy) return NULL; diff --git a/src/shared/hashmap.h b/src/shared/hashmap.h index f89e7e8fb..7385ebc5f 100644 --- a/src/shared/hashmap.h +++ b/src/shared/hashmap.h @@ -43,38 +43,50 @@ typedef _IteratorStruct* Iterator; typedef unsigned long (*hash_func_t)(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]); typedef int (*compare_func_t)(const void *a, const void *b); +struct hash_ops { + hash_func_t hash; + compare_func_t compare; +}; + unsigned long string_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_; int string_compare_func(const void *a, const void *b) _pure_; +extern const struct hash_ops string_hash_ops; /* This will compare the passed pointers directly, and will not * dereference them. This is hence not useful for strings or * suchlike. */ unsigned long trivial_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_; int trivial_compare_func(const void *a, const void *b) _const_; +extern const struct hash_ops trivial_hash_ops; /* 32bit values we can always just embedd in the pointer itself, but * in order to support 32bit archs we need store 64bit values * indirectly, since they don't fit in a pointer. */ unsigned long uint64_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_; int uint64_compare_func(const void *a, const void *b) _pure_; +extern const struct hash_ops uint64_hash_ops; /* On some archs dev_t is 32bit, and on others 64bit. And sometimes * it's 64bit on 32bit archs, and sometimes 32bit on 64bit archs. Yuck! */ #if SIZEOF_DEV_T != 8 unsigned long devt_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_; int devt_compare_func(const void *a, const void *b) _pure_; +extern const struct hash_ops devt_hash_ops = { + .hash = devt_hash_func, + .compare = devt_compare_func +}; #else -/* No need to define a second version of this... */ #define devt_hash_func uint64_hash_func #define devt_compare_func uint64_compare_func +#define devt_hash_ops uint64_hash_ops #endif -Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func); +Hashmap *hashmap_new(const struct hash_ops *hash_ops); void hashmap_free(Hashmap *h); void hashmap_free_free(Hashmap *h); void hashmap_free_free_free(Hashmap *h); Hashmap *hashmap_copy(Hashmap *h); -int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func); +int hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops); int hashmap_put(Hashmap *h, const void *key, void *value); int hashmap_update(Hashmap *h, const void *key, void *value); diff --git a/src/shared/install.c b/src/shared/install.c index 3ef995a92..5d3fcf5e3 100644 --- a/src/shared/install.c +++ b/src/shared/install.c @@ -179,7 +179,7 @@ static int mark_symlink_for_removal( assert(p); - r = set_ensure_allocated(remove_symlinks_to, string_hash_func, string_compare_func); + r = set_ensure_allocated(remove_symlinks_to, &string_hash_ops); if (r < 0) return r; @@ -884,7 +884,7 @@ static int install_info_add( hashmap_get(c->will_install, name)) return 0; - r = hashmap_ensure_allocated(&c->will_install, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&c->will_install, &string_hash_ops); if (r < 0) return r; @@ -1393,7 +1393,7 @@ static int install_context_apply( while ((i = hashmap_first(c->will_install))) { - q = hashmap_ensure_allocated(&c->have_installed, string_hash_func, string_compare_func); + q = hashmap_ensure_allocated(&c->have_installed, &string_hash_ops); if (q < 0) return q; @@ -1434,7 +1434,7 @@ static int install_context_mark_for_removal( while ((i = hashmap_first(c->will_install))) { - q = hashmap_ensure_allocated(&c->have_installed, string_hash_func, string_compare_func); + q = hashmap_ensure_allocated(&c->have_installed, &string_hash_ops); if (q < 0) return q; diff --git a/src/shared/locale-util.c b/src/shared/locale-util.c index 8d2c36303..68851ae13 100644 --- a/src/shared/locale-util.c +++ b/src/shared/locale-util.c @@ -160,7 +160,7 @@ int get_locales(char ***ret) { _cleanup_strv_free_ char **l = NULL; int r; - locales = set_new(string_hash_func, string_compare_func); + locales = set_new(&string_hash_ops); if (!locales) return -ENOMEM; diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 5538dd3b9..5a7bbaf03 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -695,7 +695,7 @@ static int output_json( sd_id128_to_string(boot_id, sid)); } - h = hashmap_new(string_hash_func, string_compare_func); + h = hashmap_new(&string_hash_ops); if (!h) return -ENOMEM; diff --git a/src/shared/set.c b/src/shared/set.c index 02ea83159..d4ffe056d 100644 --- a/src/shared/set.c +++ b/src/shared/set.c @@ -30,8 +30,8 @@ /* For now this is not much more than a wrapper around a hashmap */ -Set *set_new(hash_func_t hash_func, compare_func_t compare_func) { - return MAKE_SET(hashmap_new(hash_func, compare_func)); +Set *set_new(const struct hash_ops *hash_ops) { + return MAKE_SET(hashmap_new(hash_ops)); } void set_free(Set* s) { @@ -42,8 +42,8 @@ void set_free_free(Set *s) { hashmap_free_free(MAKE_HASHMAP(s)); } -int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func) { - return hashmap_ensure_allocated((Hashmap**) s, hash_func, compare_func); +int set_ensure_allocated(Set **s, const struct hash_ops *hash_ops) { + return hashmap_ensure_allocated((Hashmap**) s, hash_ops); } int set_put(Set *s, void *value) { diff --git a/src/shared/set.h b/src/shared/set.h index e0b9c9e0a..e650b7e3f 100644 --- a/src/shared/set.h +++ b/src/shared/set.h @@ -32,12 +32,12 @@ typedef struct Set Set; -Set *set_new(hash_func_t hash_func, compare_func_t compare_func); +Set *set_new(const struct hash_ops *hash_ops); void set_free(Set* s); void set_free_free(Set *s); Set* set_copy(Set *s); -int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func); +int set_ensure_allocated(Set **s, const struct hash_ops *hash_ops); int set_put(Set *s, void *value); int set_consume(Set *s, void *value); diff --git a/src/shared/util.c b/src/shared/util.c index 502b3675b..61d6680dd 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -3911,7 +3911,7 @@ void execute_directory(const char *directory, DIR *d, usec_t timeout, char *argv } } - pids = hashmap_new(NULL, NULL); + pids = hashmap_new(NULL); if (!pids) { log_oom(); _exit(EXIT_FAILURE); @@ -6694,7 +6694,7 @@ int bind_remount_recursive(const char *prefix, bool ro) { path_kill_slashes(cleaned); - done = set_new(string_hash_func, string_compare_func); + done = set_new(&string_hash_ops); if (!done) return -ENOMEM; @@ -6704,7 +6704,7 @@ int bind_remount_recursive(const char *prefix, bool ro) { bool top_autofs = false; char *x; - todo = set_new(string_hash_func, string_compare_func); + todo = set_new(&string_hash_ops); if (!todo) return -ENOMEM; diff --git a/src/socket-proxy/socket-proxyd.c b/src/socket-proxy/socket-proxyd.c index f6e6672cd..81d8457fd 100644 --- a/src/socket-proxy/socket-proxyd.c +++ b/src/socket-proxy/socket-proxyd.c @@ -473,7 +473,7 @@ static int add_connection_socket(Context *context, int fd) { return 0; } - r = set_ensure_allocated(&context->connections, trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&context->connections, NULL); if (r < 0) { log_oom(); return 0; @@ -543,7 +543,7 @@ static int add_listen_socket(Context *context, int fd) { assert(context); assert(fd >= 0); - r = set_ensure_allocated(&context->listen, trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&context->listen, NULL); if (r < 0) { log_oom(); return r; diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 8ce987043..4f9530baf 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -292,7 +292,7 @@ int main(int argc, char *argv[]) { umask(0022); - sysctl_options = hashmap_new(string_hash_func, string_compare_func); + sysctl_options = hashmap_new(&string_hash_ops); if (!sysctl_options) { r = log_oom(); goto finish; diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index de43c879a..88be871f3 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -594,7 +594,7 @@ static int get_unit_list_recursive( assert(_unit_infos); assert(_machines); - replies = set_new(NULL, NULL); + replies = set_new(NULL); if (!replies) return log_oom(); @@ -1338,7 +1338,7 @@ static int list_unit_files(sd_bus *bus, char **args) { Iterator i; unsigned n_units; - h = hashmap_new(string_hash_func, string_compare_func); + h = hashmap_new(&string_hash_ops); if (!h) return log_oom(); @@ -2746,7 +2746,7 @@ static int start_unit(sd_bus *bus, char **args) { return r; } - s = set_new(string_hash_func, string_compare_func); + s = set_new(&string_hash_ops); if (!s) return log_oom(); } diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index b889ed053..ba20d949d 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -107,11 +107,11 @@ static int load_user_database(void) { if (!f) return errno == ENOENT ? 0 : -errno; - r = hashmap_ensure_allocated(&database_user, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&database_user, &string_hash_ops); if (r < 0) return r; - r = hashmap_ensure_allocated(&database_uid, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&database_uid, NULL); if (r < 0) return r; @@ -159,11 +159,11 @@ static int load_group_database(void) { if (!f) return errno == ENOENT ? 0 : -errno; - r = hashmap_ensure_allocated(&database_group, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&database_group, &string_hash_ops); if (r < 0) return r; - r = hashmap_ensure_allocated(&database_gid, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&database_gid, NULL); if (r < 0) return r; @@ -969,7 +969,7 @@ static int add_user(Item *i) { i->uid = search_uid; } - r = hashmap_ensure_allocated(&todo_uids, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&todo_uids, NULL); if (r < 0) return log_oom(); @@ -1122,7 +1122,7 @@ static int add_group(Item *i) { i->gid = search_uid; } - r = hashmap_ensure_allocated(&todo_gids, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&todo_gids, NULL); if (r < 0) return log_oom(); @@ -1210,7 +1210,7 @@ static int add_implicit(void) { if (!i) { _cleanup_(item_freep) Item *j = NULL; - r = hashmap_ensure_allocated(&groups, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&groups, &string_hash_ops); if (r < 0) return log_oom(); @@ -1237,7 +1237,7 @@ static int add_implicit(void) { if (!i) { _cleanup_(item_freep) Item *j = NULL; - r = hashmap_ensure_allocated(&users, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&users, &string_hash_ops); if (r < 0) return log_oom(); @@ -1542,7 +1542,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { return -EINVAL; } - r = hashmap_ensure_allocated(&members, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&members, &string_hash_ops); if (r < 0) return log_oom(); @@ -1584,7 +1584,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { return -EINVAL; } - r = hashmap_ensure_allocated(&users, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&users, &string_hash_ops); if (r < 0) return log_oom(); @@ -1634,7 +1634,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) { return -EINVAL; } - r = hashmap_ensure_allocated(&groups, string_hash_func, string_compare_func); + r = hashmap_ensure_allocated(&groups, &string_hash_ops); if (r < 0) return log_oom(); diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c index 368d420df..6c3281ff1 100644 --- a/src/sysv-generator/sysv-generator.c +++ b/src/sysv-generator/sysv-generator.c @@ -824,8 +824,7 @@ static int set_dependencies_from_rcnd(LookupPaths lp, Hashmap *all_services) { MAX(a*10 + b, service->sysv_start_priority); } - r = set_ensure_allocated(&runlevel_services[i], - trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&runlevel_services[i], NULL); if (r < 0) goto finish; @@ -836,8 +835,7 @@ static int set_dependencies_from_rcnd(LookupPaths lp, Hashmap *all_services) { } else if (de->d_name[0] == 'K' && (rcnd_table[i].type == RUNLEVEL_DOWN)) { - r = set_ensure_allocated(&shutdown_services, - trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&shutdown_services, NULL); if (r < 0) goto finish; @@ -905,7 +903,7 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - all_services = hashmap_new(string_hash_func, string_compare_func); + all_services = hashmap_new(&string_hash_ops); if (!all_services) { log_oom(); return EXIT_FAILURE; diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c index ccef61f55..95a7f8379 100644 --- a/src/test/test-hashmap.c +++ b/src/test/test-hashmap.c @@ -26,7 +26,7 @@ static void test_hashmap_replace(void) { Hashmap *m; char *val1, *val2, *val3, *val4, *val5, *r; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); val1 = strdup("val1"); assert_se(val1); @@ -73,7 +73,7 @@ static void test_hashmap_copy(void) { val4 = strdup("val4"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "key 1", val1); hashmap_put(m, "key 2", val2); @@ -109,7 +109,7 @@ static void test_hashmap_get_strv(void) { val4 = strdup("val4"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "key 1", val1); hashmap_put(m, "key 2", val2); @@ -141,8 +141,8 @@ static void test_hashmap_move_one(void) { val4 = strdup("val4"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); - n = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); + n = hashmap_new(&string_hash_ops); hashmap_put(m, "key 1", val1); hashmap_put(m, "key 2", val2); @@ -168,7 +168,7 @@ static void test_hashmap_next(void) { Hashmap *m; char *val1, *val2, *val3, *val4, *r; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); val1 = strdup("val1"); assert_se(val1); val2 = strdup("val2"); @@ -199,7 +199,7 @@ static void test_hashmap_update(void) { Hashmap *m; char *val1, *val2, *r; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); val1 = strdup("old_value"); assert_se(val1); val2 = strdup("new_value"); @@ -222,7 +222,7 @@ static void test_hashmap_put(void) { Hashmap *m; int valid_hashmap_put; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); valid_hashmap_put = hashmap_put(m, "key 1", (void*) (const char *) "val 1"); assert_se(valid_hashmap_put == 1); @@ -236,7 +236,7 @@ static void test_hashmap_remove_and_put(void) { int valid; char *r; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(m); valid = hashmap_remove_and_put(m, "unvalid key", "new key", NULL); @@ -261,9 +261,9 @@ static void test_hashmap_ensure_allocated(void) { Hashmap *m; int valid_hashmap; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); - valid_hashmap = hashmap_ensure_allocated(&m, string_hash_func, string_compare_func); + valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops); assert_se(valid_hashmap == 0); assert_se(m); @@ -282,7 +282,7 @@ static void test_hashmap_foreach_key(void) { "key 3\0" "key 4\0"; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); NULSTR_FOREACH(key, key_table) hashmap_put(m, key, (void*) (const char*) "my dummy val"); @@ -319,7 +319,7 @@ static void test_hashmap_foreach(void) { val4 = strdup("my val4"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val1); hashmap_put(m, "Key 2", val2); @@ -358,7 +358,7 @@ static void test_hashmap_foreach_backwards(void) { val4 = strdup("my val4"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val1); hashmap_put(m, "Key 2", val2); hashmap_put(m, "Key 3", val3); @@ -395,8 +395,8 @@ static void test_hashmap_merge(void) { val4 = strdup("my val4"); assert_se(val4); - n = hashmap_new(string_hash_func, string_compare_func); - m = hashmap_new(string_hash_func, string_compare_func); + n = hashmap_new(&string_hash_ops); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val1); hashmap_put(m, "Key 2", val2); @@ -422,7 +422,7 @@ static void test_hashmap_contains(void) { val1 = strdup("my val"); assert_se(val1); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(!hashmap_contains(m, "Key 1")); hashmap_put(m, "Key 1", val1); @@ -439,7 +439,7 @@ static void test_hashmap_isempty(void) { val1 = strdup("my val"); assert_se(val1); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(hashmap_isempty(m)); hashmap_put(m, "Key 1", val1); @@ -462,7 +462,7 @@ static void test_hashmap_size(void) { val4 = strdup("my val"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val1); hashmap_put(m, "Key 2", val2); @@ -482,7 +482,7 @@ static void test_hashmap_get(void) { val = strdup("my val"); assert_se(val); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val); @@ -499,7 +499,7 @@ static void test_hashmap_many(void) { #define N_ENTRIES 100000 - assert_se(h = hashmap_new(NULL, NULL)); + assert_se(h = hashmap_new(NULL)); for (i = 1; i < N_ENTRIES*3; i+=3) { assert_se(hashmap_put(h, UINT_TO_PTR(i), UINT_TO_PTR(i)) >= 0); @@ -520,7 +520,7 @@ static void test_hashmap_many(void) { static void test_hashmap_first_key(void) { _cleanup_hashmap_free_ Hashmap *m = NULL; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(m); assert_se(!hashmap_first_key(m)); @@ -535,7 +535,7 @@ static void test_hashmap_first_key(void) { static void test_hashmap_last(void) { _cleanup_hashmap_free_ Hashmap *m = NULL; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(m); assert_se(!hashmap_last(m)); @@ -550,7 +550,7 @@ static void test_hashmap_last(void) { static void test_hashmap_steal_first_key(void) { _cleanup_hashmap_free_ Hashmap *m = NULL; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(m); assert_se(!hashmap_steal_first_key(m)); @@ -563,7 +563,7 @@ static void test_hashmap_steal_first_key(void) { static void test_hashmap_clear_free_free(void) { _cleanup_hashmap_free_ Hashmap *m = NULL; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(m); assert_se(hashmap_put(m, strdup("key 1"), NULL) == 1); diff --git a/src/test/test-install.c b/src/test/test-install.c index 099eb401d..b0f77a18f 100644 --- a/src/test/test-install.c +++ b/src/test/test-install.c @@ -51,7 +51,7 @@ int main(int argc, char* argv[]) { UnitFileChange *changes = NULL; unsigned n_changes = 0; - h = hashmap_new(string_hash_func, string_compare_func); + h = hashmap_new(&string_hash_ops); r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h); assert_se(r == 0); diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c index cdb1e4ad5..dfedc9b8d 100644 --- a/src/test/test-prioq.c +++ b/src/test/test-prioq.c @@ -98,6 +98,11 @@ static unsigned long test_hash(const void *a, const uint8_t hash_key[HASH_KEY_SI return (unsigned long) u; } +static const struct hash_ops test_hash_ops = { + .hash = test_hash, + .compare = test_compare +}; + static void test_struct(void) { Prioq *q; Set *s; @@ -109,7 +114,7 @@ static void test_struct(void) { q = prioq_new(test_compare); assert_se(q); - s = set_new(test_hash, test_compare); + s = set_new(&test_hash_ops); assert_se(s); for (i = 0; i < SET_SIZE; i++) { diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index 34865729f..89f5bdd4e 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -44,7 +44,7 @@ static int test_unit_file_get_set(void) { Iterator i; UnitFileList *p; - h = hashmap_new(string_hash_func, string_compare_func); + h = hashmap_new(&string_hash_ops); assert(h); r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h); diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 3bab7ac13..f9830c431 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -165,7 +165,7 @@ static void load_unix_sockets(void) { /* We maintain a cache of the sockets we found in * /proc/net/unix to speed things up a little. */ - unix_sockets = set_new(string_hash_func, string_compare_func); + unix_sockets = set_new(&string_hash_ops); if (!unix_sockets) return; @@ -1608,8 +1608,8 @@ int main(int argc, char *argv[]) { label_init(NULL); - items = hashmap_new(string_hash_func, string_compare_func); - globs = hashmap_new(string_hash_func, string_compare_func); + items = hashmap_new(&string_hash_ops); + globs = hashmap_new(&string_hash_ops); if (!items || !globs) { r = log_oom(); -- cgit v1.2.3