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