Blame SOURCES/libvirt-util-alloc-add-macros-for-implementing-automatic-cleanup-functionality.patch

9c6c51
From 78f99c03b31dd7850a2eeebbf88e47e391376293 Mon Sep 17 00:00:00 2001
9c6c51
Message-Id: <78f99c03b31dd7850a2eeebbf88e47e391376293@dist-git>
9c6c51
From: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
9c6c51
Date: Fri, 3 May 2019 13:54:30 +0200
9c6c51
Subject: [PATCH] util: alloc: add macros for implementing automatic cleanup
9c6c51
 functionality
9c6c51
9c6c51
New macros are introduced which help in adding GNU C's cleanup
9c6c51
attribute to variable declarations. Variables declared with these
9c6c51
macros will have their allocated memory freed automatically when
9c6c51
they go out of scope.
9c6c51
9c6c51
Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
9c6c51
Reviewed-by: Erik Skultety <eskultet@redhat.com>
9c6c51
(cherry picked from commit dcec13f5a2ba17223d403ff9e9fed916a4dd9c04)
9c6c51
9c6c51
https: //bugzilla.redhat.com/show_bug.cgi?id=1505998
9c6c51
Signed-off-by: Erik Skultety <eskultet@redhat.com>
9c6c51
Message-Id: <7ca85d7157eda723aac994f7c9f0f04ed9a35ab5.1556884442.git.eskultet@redhat.com>
9c6c51
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
9c6c51
---
9c6c51
 src/util/viralloc.h | 42 ++++++++++++++++++++++++++++++++++++++++++
9c6c51
 1 file changed, 42 insertions(+)
9c6c51
9c6c51
diff --git a/src/util/viralloc.h b/src/util/viralloc.h
9c6c51
index 69d0f904f1..a23aa188bb 100644
9c6c51
--- a/src/util/viralloc.h
9c6c51
+++ b/src/util/viralloc.h
9c6c51
@@ -596,4 +596,46 @@ void virAllocTestInit(void);
9c6c51
 int virAllocTestCount(void);
9c6c51
 void virAllocTestOOM(int n, int m);
9c6c51
 void virAllocTestHook(void (*func)(int, void*), void *data);
9c6c51
+
9c6c51
+# define VIR_AUTOPTR_FUNC_NAME(type) type##AutoPtrFree
9c6c51
+
9c6c51
+/**
9c6c51
+ * VIR_DEFINE_AUTOPTR_FUNC:
9c6c51
+ * @type: type of the variable to be freed automatically
9c6c51
+ * @func: cleanup function to be automatically called
9c6c51
+ *
9c6c51
+ * This macro defines a function for automatic freeing of
9c6c51
+ * resources allocated to a variable of type @type. This newly
9c6c51
+ * defined function works as a necessary wrapper around @func.
9c6c51
+ */
9c6c51
+# define VIR_DEFINE_AUTOPTR_FUNC(type, func) \
9c6c51
+    static inline void VIR_AUTOPTR_FUNC_NAME(type)(type **_ptr) \
9c6c51
+    { \
9c6c51
+        if (*_ptr) \
9c6c51
+            (func)(*_ptr); \
9c6c51
+        *_ptr = NULL; \
9c6c51
+    } \
9c6c51
+
9c6c51
+/**
9c6c51
+ * VIR_AUTOFREE:
9c6c51
+ * @type: type of the variable to be freed automatically
9c6c51
+ *
9c6c51
+ * Macro to automatically free the memory allocated to
9c6c51
+ * the variable declared with it by calling virFree
9c6c51
+ * when the variable goes out of scope.
9c6c51
+ */
9c6c51
+# define VIR_AUTOFREE(type) __attribute__((cleanup(virFree))) type
9c6c51
+
9c6c51
+/**
9c6c51
+ * VIR_AUTOPTR:
9c6c51
+ * @type: type of the variable to be freed automatically
9c6c51
+ *
9c6c51
+ * Macro to automatically free the memory allocated to
9c6c51
+ * the variable declared with it by calling the function
9c6c51
+ * defined by VIR_DEFINE_AUTOPTR_FUNC when the variable
9c6c51
+ * goes out of scope.
9c6c51
+ */
9c6c51
+# define VIR_AUTOPTR(type) \
9c6c51
+    __attribute__((cleanup(VIR_AUTOPTR_FUNC_NAME(type)))) type *
9c6c51
+
9c6c51
 #endif /* __VIR_MEMORY_H_ */
9c6c51
-- 
9c6c51
2.21.0
9c6c51