Merge pull request #16238 from keszybz/set-handling-more
Fix handling of cases where a duplicate item is added to a set and related cleanups
This commit is contained in:
+43
-21
@@ -768,7 +768,7 @@ static void reset_direct_storage(HashmapBase *h) {
|
||||
memset(p, DIB_RAW_INIT, sizeof(dib_raw_t) * hi->n_direct_buckets);
|
||||
}
|
||||
|
||||
static struct HashmapBase *hashmap_base_new(const struct hash_ops *hash_ops, enum HashmapType type HASHMAP_DEBUG_PARAMS) {
|
||||
static struct HashmapBase *hashmap_base_new(const struct hash_ops *hash_ops, enum HashmapType type HASHMAP_DEBUG_PARAMS) {
|
||||
HashmapBase *h;
|
||||
const struct hashmap_type_info *hi = &hashmap_type_info[type];
|
||||
bool up;
|
||||
@@ -808,19 +808,19 @@ static struct HashmapBase *hashmap_base_new(const struct hash_ops *hash_ops, enu
|
||||
}
|
||||
|
||||
Hashmap *_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
|
||||
return (Hashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS);
|
||||
return (Hashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS);
|
||||
}
|
||||
|
||||
OrderedHashmap *_ordered_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
|
||||
return (OrderedHashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS);
|
||||
return (OrderedHashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS);
|
||||
}
|
||||
|
||||
Set *_set_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
|
||||
return (Set*) hashmap_base_new(hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
|
||||
return (Set*) hashmap_base_new(hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
|
||||
}
|
||||
|
||||
static int hashmap_base_ensure_allocated(HashmapBase **h, const struct hash_ops *hash_ops,
|
||||
enum HashmapType type HASHMAP_DEBUG_PARAMS) {
|
||||
enum HashmapType type HASHMAP_DEBUG_PARAMS) {
|
||||
HashmapBase *q;
|
||||
|
||||
assert(h);
|
||||
@@ -828,7 +828,7 @@ static int hashmap_base_ensure_allocated(HashmapBase **h, const struct hash_ops
|
||||
if (*h)
|
||||
return 0;
|
||||
|
||||
q = hashmap_base_new(hash_ops, type HASHMAP_DEBUG_PASS_ARGS);
|
||||
q = hashmap_base_new(hash_ops, type HASHMAP_DEBUG_PASS_ARGS);
|
||||
if (!q)
|
||||
return -ENOMEM;
|
||||
|
||||
@@ -837,15 +837,15 @@ static int hashmap_base_ensure_allocated(HashmapBase **h, const struct hash_ops
|
||||
}
|
||||
|
||||
int _hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
|
||||
return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS);
|
||||
return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS);
|
||||
}
|
||||
|
||||
int _ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
|
||||
return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS);
|
||||
return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS);
|
||||
}
|
||||
|
||||
int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
|
||||
return hashmap_base_ensure_allocated((HashmapBase**)s, hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
|
||||
return hashmap_base_ensure_allocated((HashmapBase**)s, hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
|
||||
}
|
||||
|
||||
static void hashmap_free_no_clear(HashmapBase *h) {
|
||||
@@ -1247,6 +1247,30 @@ int set_put(Set *s, const void *key) {
|
||||
return hashmap_put_boldly(s, hash, &swap, true);
|
||||
}
|
||||
|
||||
int _set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key HASHMAP_DEBUG_PARAMS) {
|
||||
int r;
|
||||
|
||||
r = _set_ensure_allocated(s, hash_ops HASHMAP_DEBUG_PASS_ARGS);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
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;
|
||||
@@ -1687,13 +1711,13 @@ int _hashmap_move_one(HashmapBase *h, HashmapBase *other, const void *key) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
HashmapBase *_hashmap_copy(HashmapBase *h) {
|
||||
HashmapBase *_hashmap_copy(HashmapBase *h HASHMAP_DEBUG_PARAMS) {
|
||||
HashmapBase *copy;
|
||||
int r;
|
||||
|
||||
assert(h);
|
||||
|
||||
copy = hashmap_base_new(h->hash_ops, h->type HASHMAP_DEBUG_SRC_ARGS);
|
||||
copy = hashmap_base_new(h->hash_ops, h->type HASHMAP_DEBUG_PASS_ARGS);
|
||||
if (!copy)
|
||||
return NULL;
|
||||
|
||||
@@ -1709,10 +1733,8 @@ HashmapBase *_hashmap_copy(HashmapBase *h) {
|
||||
assert_not_reached("Unknown hashmap type");
|
||||
}
|
||||
|
||||
if (r < 0) {
|
||||
_hashmap_free(copy, false, false);
|
||||
return NULL;
|
||||
}
|
||||
if (r < 0)
|
||||
return _hashmap_free(copy, false, false);
|
||||
|
||||
return copy;
|
||||
}
|
||||
@@ -1765,10 +1787,10 @@ int set_consume(Set *s, void *value) {
|
||||
return r;
|
||||
}
|
||||
|
||||
int hashmap_put_strdup(Hashmap **h, const char *k, const char *v) {
|
||||
int _hashmap_put_strdup(Hashmap **h, const char *k, const char *v HASHMAP_DEBUG_PARAMS) {
|
||||
int r;
|
||||
|
||||
r = hashmap_ensure_allocated(h, &string_hash_ops_free_free);
|
||||
r = _hashmap_ensure_allocated(h, &string_hash_ops_free_free HASHMAP_DEBUG_PASS_ARGS);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -1799,14 +1821,14 @@ int hashmap_put_strdup(Hashmap **h, const char *k, const char *v) {
|
||||
return r;
|
||||
}
|
||||
|
||||
int set_put_strdup(Set **s, const char *p) {
|
||||
int _set_put_strdup(Set **s, const char *p HASHMAP_DEBUG_PARAMS) {
|
||||
char *c;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
assert(p);
|
||||
|
||||
r = set_ensure_allocated(s, &string_hash_ops_free);
|
||||
r = _set_ensure_allocated(s, &string_hash_ops_free HASHMAP_DEBUG_PASS_ARGS);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -1820,14 +1842,14 @@ int set_put_strdup(Set **s, const char *p) {
|
||||
return set_consume(*s, c);
|
||||
}
|
||||
|
||||
int set_put_strdupv(Set **s, char **l) {
|
||||
int _set_put_strdupv(Set **s, char **l HASHMAP_DEBUG_PARAMS) {
|
||||
int n = 0, r;
|
||||
char **i;
|
||||
|
||||
assert(s);
|
||||
|
||||
STRV_FOREACH(i, l) {
|
||||
r = set_put_strdup(s, *i);
|
||||
r = _set_put_strdup(s, *i HASHMAP_DEBUG_PASS_ARGS);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
||||
+5
-8
@@ -128,13 +128,9 @@ static inline OrderedHashmap *ordered_hashmap_free_free_free(OrderedHashmap *h)
|
||||
IteratedCache *iterated_cache_free(IteratedCache *cache);
|
||||
int iterated_cache_get(IteratedCache *cache, const void ***res_keys, const void ***res_values, unsigned *res_n_entries);
|
||||
|
||||
HashmapBase *_hashmap_copy(HashmapBase *h);
|
||||
static inline Hashmap *hashmap_copy(Hashmap *h) {
|
||||
return (Hashmap*) _hashmap_copy(HASHMAP_BASE(h));
|
||||
}
|
||||
static inline OrderedHashmap *ordered_hashmap_copy(OrderedHashmap *h) {
|
||||
return (OrderedHashmap*) _hashmap_copy(HASHMAP_BASE(h));
|
||||
}
|
||||
HashmapBase *_hashmap_copy(HashmapBase *h HASHMAP_DEBUG_PARAMS);
|
||||
#define hashmap_copy(h) ((Hashmap*) _hashmap_copy(HASHMAP_BASE(h) HASHMAP_DEBUG_SRC_ARGS))
|
||||
#define ordered_hashmap_copy(h) ((OrderedHashmap*) _hashmap_copy(HASHMAP_BASE(h) HASHMAP_DEBUG_SRC_ARGS))
|
||||
|
||||
int _hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
|
||||
int _ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
|
||||
@@ -154,7 +150,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_put_strdup(Hashmap **h, const char *k, const char *v HASHMAP_DEBUG_PARAMS);
|
||||
#define hashmap_put_strdup(h, k, v) _hashmap_put_strdup(h, k, v HASHMAP_DEBUG_SRC_ARGS)
|
||||
|
||||
int hashmap_update(Hashmap *h, const void *key, void *value);
|
||||
static inline int ordered_hashmap_update(OrderedHashmap *h, const void *key, void *value) {
|
||||
|
||||
+13
-5
@@ -26,9 +26,7 @@ static inline Set *set_free_free(Set *s) {
|
||||
|
||||
/* no set_free_free_free */
|
||||
|
||||
static inline Set *set_copy(Set *s) {
|
||||
return (Set*) _hashmap_copy(HASHMAP_BASE(s));
|
||||
}
|
||||
#define set_copy(s) ((Set*) _hashmap_copy(HASHMAP_BASE(h) HASHMAP_DEBUG_SRC_ARGS))
|
||||
|
||||
int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
|
||||
#define set_ensure_allocated(h, ops) _set_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS)
|
||||
@@ -120,9 +118,19 @@ static inline char **set_get_strv(Set *s) {
|
||||
return _hashmap_get_strv(HASHMAP_BASE(s));
|
||||
}
|
||||
|
||||
int _set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key HASHMAP_DEBUG_PARAMS);
|
||||
#define set_ensure_put(s, hash_ops, key) _set_ensure_put(s, hash_ops, key HASHMAP_DEBUG_SRC_ARGS)
|
||||
|
||||
int _set_ensure_consume(Set **s, const struct hash_ops *hash_ops, void *key HASHMAP_DEBUG_PARAMS);
|
||||
#define set_ensure_consume(s, hash_ops, key) _set_ensure_consume(s, hash_ops, key HASHMAP_DEBUG_SRC_ARGS)
|
||||
|
||||
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 HASHMAP_DEBUG_PARAMS);
|
||||
#define set_put_strdup(s, p) _set_put_strdup(s, p HASHMAP_DEBUG_SRC_ARGS)
|
||||
int _set_put_strdupv(Set **s, char **l HASHMAP_DEBUG_PARAMS);
|
||||
#define set_put_strdupv(s, l) _set_put_strdupv(s, l HASHMAP_DEBUG_SRC_ARGS)
|
||||
|
||||
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