render / rpms / libvirt

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