From e1226a9b488d5677b10f086cabf610131c66d5aa Mon Sep 17 00:00:00 2001 From: Matteo Croce Date: Fri, 28 Jun 2024 17:19:26 +0200 Subject: [PATCH] Use hashmap_ensure_replace() Use the new function `hashmap_ensure_replace()` where we're using `hashmap_ensure_allocated()` and `hashmap_ensure_replace()`. Signed-off-by: Matteo Croce --- src/core/unit.c | 7 +------ src/libsystemd/sd-hwdb/sd-hwdb.c | 6 +----- src/libsystemd/sd-netlink/netlink-socket.c | 8 +------- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/core/unit.c b/src/core/unit.c index 0e931be4846..a4e96116fc2 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -2805,13 +2805,8 @@ int unit_watch_pidref(Unit *u, const PidRef *pid, bool exclusive) { new_array[n] = u; new_array[n+1] = NULL; - /* Make sure the hashmap is allocated */ - r = hashmap_ensure_allocated(&u->manager->watch_pids_more, &pidref_hash_ops_free); - if (r < 0) - return r; - /* Add or replace the old array */ - r = hashmap_replace(u->manager->watch_pids_more, old_pid ?: pid, new_array); + r = hashmap_ensure_replace(&u->manager->watch_pids_more, &pidref_hash_ops_free, old_pid ?: pid, new_array); if (r < 0) return r; diff --git a/src/libsystemd/sd-hwdb/sd-hwdb.c b/src/libsystemd/sd-hwdb/sd-hwdb.c index f623d470648..11c5092bc97 100644 --- a/src/libsystemd/sd-hwdb/sd-hwdb.c +++ b/src/libsystemd/sd-hwdb/sd-hwdb.c @@ -166,11 +166,7 @@ static int hwdb_add_property(sd_hwdb *hwdb, const struct trie_value_entry_f *ent } } - r = ordered_hashmap_ensure_allocated(&hwdb->properties, &string_hash_ops); - if (r < 0) - return r; - - r = ordered_hashmap_replace(hwdb->properties, key, (void *)entry); + r = ordered_hashmap_ensure_replace(&hwdb->properties, &string_hash_ops, key, (void *) entry); if (r < 0) return r; diff --git a/src/libsystemd/sd-netlink/netlink-socket.c b/src/libsystemd/sd-netlink/netlink-socket.c index 64cde89ecfa..d09dc31fa32 100644 --- a/src/libsystemd/sd-netlink/netlink-socket.c +++ b/src/libsystemd/sd-netlink/netlink-socket.c @@ -86,15 +86,9 @@ static unsigned broadcast_group_get_ref(sd_netlink *nl, unsigned group) { } static int broadcast_group_set_ref(sd_netlink *nl, unsigned group, unsigned n_ref) { - int r; - assert(nl); - r = hashmap_ensure_allocated(&nl->broadcast_group_refs, NULL); - if (r < 0) - return r; - - return hashmap_replace(nl->broadcast_group_refs, UINT_TO_PTR(group), UINT_TO_PTR(n_ref)); + return hashmap_ensure_replace(&nl->broadcast_group_refs, NULL, UINT_TO_PTR(group), UINT_TO_PTR(n_ref)); } static int broadcast_group_join(sd_netlink *nl, unsigned group) { -- 2.47.3