The new utility macros are useful for variables we put on the stack but
require some cleanup. The most prominent of those is virBuffer which is
used almost exclusively in that way.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/util/viralloc.h | 32 +++++++++++++++++++++++++++++++-
1 file changed, 31 insertions(+), 1 deletion(-)
diff --git a/src/util/viralloc.h b/src/util/viralloc.h
index 1d67fff95c..705c2636fa 100644
--- a/src/util/viralloc.h
+++ b/src/util/viralloc.h
@@ -613,7 +613,24 @@ void virAllocTestHook(void (*func)(int, void*), void *data);
if (*_ptr) \
(func)(*_ptr); \
*_ptr = NULL; \
- } \
+ }
+
+# define VIR_AUTOCLEAN_FUNC_NAME(type) type##AutoClean
+
+/**
+ * VIR_DEFINE_AUTOCLEAN_FUNC:
+ * @type: type of the variable to be cleared automatically
+ * @func: cleanup function to be automatically called
+ *
+ * This macro defines a function for automatic clearing of
+ * resources in a stack'd variable of type @type. This newly
+ * defined function works as a necessary wrapper around @func.
+ */
+# define VIR_DEFINE_AUTOCLEAN_FUNC(type, func) \
+ static inline void VIR_AUTOCLEAN_FUNC_NAME(type)(type *_ptr) \
+ { \
+ (func)(_ptr); \
+ }
/**
* VIR_AUTOFREE:
@@ -637,6 +654,19 @@ void virAllocTestHook(void (*func)(int, void*), void *data);
# define VIR_AUTOPTR(type) \
__attribute__((cleanup(VIR_AUTOPTR_FUNC_NAME(type)))) type *
+/**
+ * VIR_AUTOCLEAN:
+ * @type: type of the variable to be cleared
+ *
+ * Macro to automatically calls destructor of @type variable declared directly
+ * when the variable goes out of scope.
+ * The destructor is defined by VIR_DEFINE_AUTOCLEAN_FUNC macro for the given
+ * type.
+ */
+# define VIR_AUTOCLEAN(type) \
+ __attribute__((cleanup(VIR_AUTOCLEAN_FUNC_NAME(type)))) type
+
+
/**
* VIR_AUTOUNREF:
* @type: type of an virObject subclass to be unref'd automatically
--
2.20.1