]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: alloc: Introduce 'VIR_AUTOCLEAN' macros for clearing stack'd structs
authorPeter Krempa <pkrempa@redhat.com>
Thu, 21 Feb 2019 14:49:29 +0000 (15:49 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 22 Feb 2019 09:05:45 +0000 (10:05 +0100)
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@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
src/util/viralloc.h

index 1d67fff95c247e47e1b4466945fa29a71e380bde..15451d467384d9674160c0694c90994ab6c011bb 100644 (file)
@@ -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 automatically
+ *
+ * Macro to automatically call clearing function registered for variable of @type
+ * when the variable goes out of scope.
+ * The cleanup function is registered 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