diff options
Diffstat (limited to 'src/basic')
-rw-r--r-- | src/basic/hash-funcs.c | 3 | ||||
-rw-r--r-- | src/basic/hash-funcs.h | 1 | ||||
-rw-r--r-- | src/basic/hashmap.c | 26 | ||||
-rw-r--r-- | src/basic/hashmap.h | 2 |
4 files changed, 32 insertions, 0 deletions
diff --git a/src/basic/hash-funcs.c b/src/basic/hash-funcs.c index 1be43d41a9..11cd371fad 100644 --- a/src/basic/hash-funcs.c +++ b/src/basic/hash-funcs.c @@ -10,6 +10,9 @@ void string_hash_func(const char *p, struct siphash *state) { } DEFINE_HASH_OPS(string_hash_ops, char, string_hash_func, string_compare_func); +DEFINE_HASH_OPS_FULL(string_hash_ops_free_free, + char, string_hash_func, string_compare_func, free, + char, free); void path_hash_func(const char *q, struct siphash *state) { size_t n; diff --git a/src/basic/hash-funcs.h b/src/basic/hash-funcs.h index 3d2ae4b55e..0d2d428389 100644 --- a/src/basic/hash-funcs.h +++ b/src/basic/hash-funcs.h @@ -76,6 +76,7 @@ struct hash_ops { void string_hash_func(const char *p, struct siphash *state); #define string_compare_func strcmp extern const struct hash_ops string_hash_ops; +extern const struct hash_ops string_hash_ops_free_free; void path_hash_func(const char *p, struct siphash *state); int path_compare_func(const char *a, const char *b) _pure_; diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index f244d767da..c33e00fc59 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -1768,6 +1768,32 @@ int set_consume(Set *s, void *value) { return r; } +int hashmap_put_strdup(Hashmap **h, const char *k, const char *v) { + int r; + + r = hashmap_ensure_allocated(h, &string_hash_ops_free_free); + if (r < 0) + return r; + + _cleanup_free_ char *kdup = NULL, *vdup = NULL; + kdup = strdup(k); + vdup = strdup(v); + if (!kdup || !vdup) + return -ENOMEM; + + r = hashmap_put(*h, kdup, vdup); + if (r < 0) { + if (r == -EEXIST && streq(v, hashmap_get(*h, kdup))) + return 0; + return r; + } + + assert(r > 0); /* 0 would mean vdup is already in the hashmap, which cannot be */ + kdup = vdup = NULL; + + return 0; +} + int set_put_strdup(Set *s, const char *p) { char *c; diff --git a/src/basic/hashmap.h b/src/basic/hashmap.h index 1b071c230e..65adc92513 100644 --- a/src/basic/hashmap.h +++ b/src/basic/hashmap.h @@ -147,6 +147,8 @@ static inline int ordered_hashmap_put(OrderedHashmap *h, const void *key, void * return hashmap_put(PLAIN_HASHMAP(h), key, value); } +int hashmap_put_strdup(Hashmap **h, const char *k, const char *v); + int hashmap_update(Hashmap *h, const void *key, void *value); static inline int ordered_hashmap_update(OrderedHashmap *h, const void *key, void *value) { return hashmap_update(PLAIN_HASHMAP(h), key, value); |