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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2020-06-24 10:38:15 +02:00
parent 6cd55d6f74
commit fcc1d0315d
4 changed files with 42 additions and 0 deletions
+14
View File
@@ -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;