From fdfcc9bf1c66283e2031caaf78a32aa6684a8c46 Mon Sep 17 00:00:00 2001
From: Michal Schmidt <mschmidt@redhat.com>
Date: Fri, 15 Feb 2013 09:36:58 +0100
Subject: [PATCH] add _cleanup_free_, _cleanup_close_
part of upstream commit 2fbe635a83a79f8889afec421ae3990ea106fb91
---
src/shared/macro.h | 3 +++
src/shared/util.c | 9 +++++++++
src/shared/util.h | 4 ++++
3 files changed, 16 insertions(+)
diff --git a/src/shared/macro.h b/src/shared/macro.h
index 2572a96..f82df89 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -188,6 +188,9 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
return k;
}
+#define _cleanup_free_ __attribute__((cleanup(freep)))
+#define _cleanup_fclose_ __attribute__((cleanup(fclosep)))
+
#include "log.h"
#endif
diff --git a/src/shared/util.c b/src/shared/util.c
index faf428f..c41b9e0 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -5781,3 +5781,12 @@ bool in_initrd(void) {
return saved;
}
+
+void freep(void *p) {
+ free(*(void**) p);
+}
+
+void fclosep(FILE **f) {
+ if (*f)
+ fclose(*f);
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index 5459810..81392f5 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -542,4 +542,8 @@ _malloc_ static inline void *memdup_multiply(const void *p, size_t a, size_t b)
return memdup(p, a * b);
}
+
+void freep(void *p);
+void fclosep(FILE **f);
+
#endif