#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 */
#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) {
#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)
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);