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