99cbc7
From 61ed471220064f39afe088eb76630a4f7fe35b43 Mon Sep 17 00:00:00 2001
99cbc7
Message-Id: <61ed471220064f39afe088eb76630a4f7fe35b43@dist-git>
99cbc7
From: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
99cbc7
Date: Tue, 4 Jun 2019 16:22:02 +0200
99cbc7
Subject: [PATCH] util: alloc: add macros for implementing automatic cleanup
99cbc7
 functionality
99cbc7
MIME-Version: 1.0
99cbc7
Content-Type: text/plain; charset=UTF-8
99cbc7
Content-Transfer-Encoding: 8bit
99cbc7
99cbc7
New macros are introduced which help in adding GNU C's cleanup
99cbc7
attribute to variable declarations. Variables declared with these
99cbc7
macros will have their allocated memory freed automatically when
99cbc7
they go out of scope.
99cbc7
99cbc7
Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
99cbc7
Reviewed-by: Erik Skultety <eskultet@redhat.com>
99cbc7
(cherry picked from commit dcec13f5a2ba17223d403ff9e9fed916a4dd9c04)
99cbc7
99cbc7
https://bugzilla.redhat.com/show_bug.cgi?id=1703661
99cbc7
99cbc7
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
99cbc7
Message-Id: <20190604142207.2036-2-abologna@redhat.com>
99cbc7
Reviewed-by: Ján Tomko <jtomko@redhat.com>
99cbc7
---
99cbc7
 src/util/viralloc.h | 42 ++++++++++++++++++++++++++++++++++++++++++
99cbc7
 1 file changed, 42 insertions(+)
99cbc7
99cbc7
diff --git a/src/util/viralloc.h b/src/util/viralloc.h
99cbc7
index 69d0f904f1..a23aa188bb 100644
99cbc7
--- a/src/util/viralloc.h
99cbc7
+++ b/src/util/viralloc.h
99cbc7
@@ -596,4 +596,46 @@ void virAllocTestInit(void);
99cbc7
 int virAllocTestCount(void);
99cbc7
 void virAllocTestOOM(int n, int m);
99cbc7
 void virAllocTestHook(void (*func)(int, void*), void *data);
99cbc7
+
99cbc7
+# define VIR_AUTOPTR_FUNC_NAME(type) type##AutoPtrFree
99cbc7
+
99cbc7
+/**
99cbc7
+ * VIR_DEFINE_AUTOPTR_FUNC:
99cbc7
+ * @type: type of the variable to be freed automatically
99cbc7
+ * @func: cleanup function to be automatically called
99cbc7
+ *
99cbc7
+ * This macro defines a function for automatic freeing of
99cbc7
+ * resources allocated to a variable of type @type. This newly
99cbc7
+ * defined function works as a necessary wrapper around @func.
99cbc7
+ */
99cbc7
+# define VIR_DEFINE_AUTOPTR_FUNC(type, func) \
99cbc7
+    static inline void VIR_AUTOPTR_FUNC_NAME(type)(type **_ptr) \
99cbc7
+    { \
99cbc7
+        if (*_ptr) \
99cbc7
+            (func)(*_ptr); \
99cbc7
+        *_ptr = NULL; \
99cbc7
+    } \
99cbc7
+
99cbc7
+/**
99cbc7
+ * VIR_AUTOFREE:
99cbc7
+ * @type: type of the variable to be freed automatically
99cbc7
+ *
99cbc7
+ * Macro to automatically free the memory allocated to
99cbc7
+ * the variable declared with it by calling virFree
99cbc7
+ * when the variable goes out of scope.
99cbc7
+ */
99cbc7
+# define VIR_AUTOFREE(type) __attribute__((cleanup(virFree))) type
99cbc7
+
99cbc7
+/**
99cbc7
+ * VIR_AUTOPTR:
99cbc7
+ * @type: type of the variable to be freed automatically
99cbc7
+ *
99cbc7
+ * Macro to automatically free the memory allocated to
99cbc7
+ * the variable declared with it by calling the function
99cbc7
+ * defined by VIR_DEFINE_AUTOPTR_FUNC when the variable
99cbc7
+ * goes out of scope.
99cbc7
+ */
99cbc7
+# define VIR_AUTOPTR(type) \
99cbc7
+    __attribute__((cleanup(VIR_AUTOPTR_FUNC_NAME(type)))) type *
99cbc7
+
99cbc7
 #endif /* __VIR_MEMORY_H_ */
99cbc7
-- 
99cbc7
2.21.0
99cbc7