diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-06-04 19:46:14 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-06-24 10:38:15 +0200 |
commit | fcc1d0315d335ba31d85d6023c79d6404c62e167 (patch) | |
tree | b9873fc9be1b6e05293bbb9e99f051d436c0d6df /src/basic/hashmap.c | |
parent | 6cd55d6f74a49ebf6532f913f6a9cde3efcd34cf (diff) | |
download | systemd-fcc1d0315d335ba31d85d6023c79d6404c62e167.tar.gz |
basic/set: add set_ensure_consume()
This combines set_ensure_allocated() with set_consume(). The cool thing is that
because we know the hash ops, we can correctly free the item if appropriate.
Similarly to set_consume(), the goal is to simplify handling of the case where
the item needs to be freed on error and if already present in the set.
Diffstat (limited to 'src/basic/hashmap.c')
-rw-r--r-- | src/basic/hashmap.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index c876662a0b..ae3235d518 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -1257,6 +1257,20 @@ int _set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key H return set_put(*s, key); } +int _set_ensure_consume(Set **s, const struct hash_ops *hash_ops, void *key HASHMAP_DEBUG_PARAMS) { + int r; + + r = _set_ensure_put(s, hash_ops, key HASHMAP_DEBUG_PASS_ARGS); + if (r <= 0) { + if (hash_ops && hash_ops->free_key) + hash_ops->free_key(key); + else + free(key); + } + + return r; +} + int hashmap_replace(Hashmap *h, const void *key, void *value) { struct swap_entries swap; struct plain_hashmap_entry *e; |