Merge pull request #15635 from keszybz/set-put-strdup
Let set_put_strdup() allocate the set and related changes
This commit is contained in:
@@ -77,8 +77,10 @@ static int files_add(
|
||||
/* Is this a masking entry? */
|
||||
if ((flags & CONF_FILES_FILTER_MASKED))
|
||||
if (null_or_empty(&st)) {
|
||||
assert(masked);
|
||||
|
||||
/* Mark this one as masked */
|
||||
r = set_put_strdup(masked, de->d_name);
|
||||
r = set_put_strdup(&masked, de->d_name);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ 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_WITH_KEY_DESTRUCTOR(string_hash_ops_free,
|
||||
char, string_hash_func, string_compare_func, free);
|
||||
DEFINE_HASH_OPS_FULL(string_hash_ops_free_free,
|
||||
char, string_hash_func, string_compare_func, free,
|
||||
char, free);
|
||||
|
||||
@@ -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;
|
||||
extern const struct hash_ops string_hash_ops_free_free;
|
||||
|
||||
void path_hash_func(const char *p, struct siphash *state);
|
||||
|
||||
+23
-10
@@ -1775,41 +1775,54 @@ int hashmap_put_strdup(Hashmap **h, const char *k, const char *v) {
|
||||
return r;
|
||||
|
||||
_cleanup_free_ char *kdup = NULL, *vdup = NULL;
|
||||
|
||||
kdup = strdup(k);
|
||||
vdup = strdup(v);
|
||||
if (!kdup || !vdup)
|
||||
if (!kdup)
|
||||
return -ENOMEM;
|
||||
|
||||
if (v) {
|
||||
vdup = strdup(v);
|
||||
if (!vdup)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
r = hashmap_put(*h, kdup, vdup);
|
||||
if (r < 0) {
|
||||
if (r == -EEXIST && streq(v, hashmap_get(*h, kdup)))
|
||||
if (r == -EEXIST && streq_ptr(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;
|
||||
/* 0 with non-null vdup would mean vdup is already in the hashmap, which cannot be */
|
||||
assert(vdup == NULL || r > 0);
|
||||
if (r > 0)
|
||||
kdup = vdup = NULL;
|
||||
|
||||
return 0;
|
||||
return r;
|
||||
}
|
||||
|
||||
int set_put_strdup(Set *s, const char *p) {
|
||||
int set_put_strdup(Set **s, const char *p) {
|
||||
char *c;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
assert(p);
|
||||
|
||||
if (set_contains(s, (char*) p))
|
||||
r = set_ensure_allocated(s, &string_hash_ops_free);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (set_contains(*s, (char*) p))
|
||||
return 0;
|
||||
|
||||
c = strdup(p);
|
||||
if (!c)
|
||||
return -ENOMEM;
|
||||
|
||||
return set_consume(s, c);
|
||||
return set_consume(*s, c);
|
||||
}
|
||||
|
||||
int set_put_strdupv(Set *s, char **l) {
|
||||
int set_put_strdupv(Set **s, char **l) {
|
||||
int n = 0, r;
|
||||
char **i;
|
||||
|
||||
|
||||
+2
-2
@@ -113,8 +113,8 @@ static inline char **set_get_strv(Set *s) {
|
||||
}
|
||||
|
||||
int set_consume(Set *s, void *value);
|
||||
int set_put_strdup(Set *s, const char *p);
|
||||
int set_put_strdupv(Set *s, char **l);
|
||||
int set_put_strdup(Set **s, const char *p);
|
||||
int set_put_strdupv(Set **s, char **l);
|
||||
int set_put_strsplit(Set *s, const char *v, const char *separators, ExtractFlags flags);
|
||||
|
||||
#define SET_FOREACH(e, s, i) \
|
||||
|
||||
Reference in New Issue
Block a user