Add set_consume which always takes ownership

Freeing in error path is the common pattern with set_put().
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2013-04-24 00:25:04 -04:00
parent c79bb9e4e2
commit ef42202ac8
14 changed files with 40 additions and 59 deletions
+2 -4
View File
@@ -178,11 +178,9 @@ static int mark_symlink_for_removal(
path_kill_slashes(n);
r = set_put(*remove_symlinks_to, n);
if (r < 0) {
free(n);
r = set_consume(*remove_symlinks_to, n);
if (r < 0)
return r == -EEXIST ? 0 : r;
}
return 0;
}
+7
View File
@@ -49,6 +49,13 @@ int set_put(Set *s, void *value) {
return hashmap_put(MAKE_HASHMAP(s), value, value);
}
int set_consume(Set *s, void *value) {
int r = set_put(s, value);
if (r < 0)
free(value);
return r;
}
int set_replace(Set *s, void *value) {
return hashmap_replace(MAKE_HASHMAP(s), value, value);
}
+1
View File
@@ -46,6 +46,7 @@ Set* set_copy(Set *s);
int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func);
int set_put(Set *s, void *value);
int set_consume(Set *s, void *value);
int set_replace(Set *s, void *value);
void *set_get(Set *s, void *value);
bool set_contains(Set *s, void *value);