]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: Introduce free_and_replace_full()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 20 Jul 2022 15:11:34 +0000 (17:11 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 21 Jul 2022 00:45:55 +0000 (09:45 +0900)
Let's have all our free_and_replace() functions use a single
implementation.

src/basic/alloc-util.h
src/basic/hashmap.h
src/basic/set.h
src/basic/strv.h

index 13dab0304fe557fc50a6c34d55794a43fc5ba85a..cf4b8b166a9dabe57b5d13c8c89830a7dd221b7b 100644 (file)
@@ -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 */
 
index eafc08f658624d7ea1a2a548b00284987e5b81b6..91b3fe862b02325808edf615fe77a6689a054095 100644 (file)
@@ -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) {
index 52cf63e2ddaefa289f6e88665a36a5811b14352b..618e729744662b29938db978573cd49da8b6cfc0 100644 (file)
@@ -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)
index 072739df355518abff28da8bb391ea1ea409e845..87ec6337bde12083f63f439aa7f16345ebe561cb 100644 (file)
@@ -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);