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.
This commit is contained in:
Michal Schmidt 2014-08-13 01:00:18 +02:00
parent f44541bc93
commit d5099efc47
82 changed files with 278 additions and 226 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

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

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

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

View File

@ -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;

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

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

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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();

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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"

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

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

View File

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

View File

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

View File

@ -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;

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

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

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

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

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

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

View File

@ -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;

View File

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

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

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

View File

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

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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();
}

View File

@ -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();

View File

@ -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;

View File

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

View File

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

View File

@ -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++) {

View File

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

View File

@ -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();