From: Daan De Meyer Date: Wed, 20 Jul 2022 15:11:34 +0000 (+0200) Subject: tree-wide: Introduce free_and_replace_full() X-Git-Tag: v252-rc1~610 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc4c10b28b585be0ff235b5c155078324a057426;p=thirdparty%2Fsystemd.git tree-wide: Introduce free_and_replace_full() Let's have all our free_and_replace() functions use a single implementation. --- diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index 13dab0304fe..cf4b8b166a9 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -50,16 +50,19 @@ typedef void* (*mfree_func_t)(void *p); #define malloc0(n) (calloc(1, (n) ?: 1)) -#define free_and_replace(a, b) \ +#define free_and_replace_full(a, b, free_func) \ ({ \ typeof(a)* _a = &(a); \ typeof(b)* _b = &(b); \ - free(*_a); \ + free_func(*_a); \ *_a = *_b; \ *_b = NULL; \ 0; \ }) +#define free_and_replace(a, b) \ + free_and_replace_full(a, b, free) + void* memdup(const void *p, size_t l) _alloc_(2); void* memdup_suffix0(const void *p, size_t l); /* We can't use _alloc_() here, since we return a buffer one byte larger than the specified size */ diff --git a/src/basic/hashmap.h b/src/basic/hashmap.h index eafc08f6586..91b3fe862b0 100644 --- a/src/basic/hashmap.h +++ b/src/basic/hashmap.h @@ -90,12 +90,7 @@ OrderedHashmap* _ordered_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DE #define ordered_hashmap_new(ops) _ordered_hashmap_new(ops HASHMAP_DEBUG_SRC_ARGS) #define hashmap_free_and_replace(a, b) \ - ({ \ - hashmap_free(a); \ - (a) = (b); \ - (b) = NULL; \ - 0; \ - }) + free_and_replace_full(a, b, hashmap_free) HashmapBase* _hashmap_free(HashmapBase *h, free_func_t default_free_key, free_func_t default_free_value); static inline Hashmap* hashmap_free(Hashmap *h) { diff --git a/src/basic/set.h b/src/basic/set.h index 52cf63e2dda..618e7297446 100644 --- a/src/basic/set.h +++ b/src/basic/set.h @@ -6,12 +6,7 @@ #include "macro.h" #define set_free_and_replace(a, b) \ - ({ \ - set_free(a); \ - (a) = (b); \ - (b) = NULL; \ - 0; \ - }) + free_and_replace_full(a, b, set_free) Set* _set_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS); #define set_new(ops) _set_new(ops HASHMAP_DEBUG_SRC_ARGS) diff --git a/src/basic/strv.h b/src/basic/strv.h index 072739df355..87ec6337bde 100644 --- a/src/basic/strv.h +++ b/src/basic/strv.h @@ -258,14 +258,7 @@ int strv_extend_n(char ***l, const char *value, size_t n); int fputstrv(FILE *f, char * const *l, const char *separator, bool *space); #define strv_free_and_replace(a, b) \ - ({ \ - char ***_a = &(a); \ - char ***_b = &(b); \ - strv_free(*_a); \ - (*_a) = (*_b); \ - (*_b) = NULL; \ - 0; \ - }) + free_and_replace_full(a, b, strv_free) extern const struct hash_ops string_strv_hash_ops; int _string_strv_hashmap_put(Hashmap **h, const char *key, const char *value HASHMAP_DEBUG_PARAMS);