summaryrefslogtreecommitdiff
path: root/src/basic/alloc-util.h
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2016-10-16 19:23:35 -0400
committerSven Eden <yamakuzure@gmx.net>2017-07-05 08:50:55 +0200
commita0db25e25b5691e4fc0afa5f5a9a8d1ce586decf (patch)
treec20e7ac755a6778eff4629885b6307fb093995a4 /src/basic/alloc-util.h
parent2aebec83237867717ad3ba0096a82670d82d911a (diff)
tree-wide: introduce free_and_replace helper
It's a common pattern, so add a helper for it. A macro is necessary because a function that takes a pointer to a pointer would be type specific, similarly to cleanup functions. Seems better to use a macro.
Diffstat (limited to 'src/basic/alloc-util.h')
-rw-r--r--src/basic/alloc-util.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h
index ceeee519b..a44dd473c 100644
--- a/src/basic/alloc-util.h
+++ b/src/basic/alloc-util.h
@@ -43,6 +43,14 @@ static inline void *mfree(void *memory) {
return NULL;
}
+#define free_and_replace(a, b) \
+ ({ \
+ free(a); \
+ (a) = (b); \
+ (b) = NULL; \
+ 0; \
+ })
+
void* memdup(const void *p, size_t l) _alloc_(2);
static inline void freep(void *p) {