[libvirt] [PATCH 2/5] util: Move the VIR_AUTO(CLEAN|PTR) helper macros into a separate header

Peter Krempa pkrempa at redhat.com
Wed Apr 3 13:06:34 UTC 2019


Keeping them with viralloc.h forcibly pulls in the other stuff from
viralloc.h into other header files. This in turn creates a mess
as more and more headers pull in the 'viral' header file.

If we want to make 'viralloc.h' omnipresent we should pick a different
approach.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/Makefile.am          |  2 +
 src/util/Makefile.inc.am |  1 +
 src/util/viralloc.h      | 66 -----------------------------
 src/util/virauthconfig.h |  1 +
 src/util/virautoclean.h  | 90 ++++++++++++++++++++++++++++++++++++++++
 src/util/virbitmap.h     |  1 +
 src/util/virbuffer.h     |  1 +
 src/util/virerror.h      |  1 +
 src/util/virfirewall.h   |  1 +
 src/util/virhash.h       |  1 +
 src/util/virmacaddr.h    |  1 +
 src/util/virnetdevvlan.h |  1 +
 src/util/virperf.h       |  1 +
 src/util/virsocketaddr.h |  1 +
 14 files changed, 103 insertions(+), 66 deletions(-)
 create mode 100644 src/util/virautoclean.h

diff --git a/src/Makefile.am b/src/Makefile.am
index a73f43c483..b3b1e172ff 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -662,6 +662,7 @@ libvirt_setuid_rpc_client_la_SOURCES = \
 		util/virarch.c \
 		util/viratomic.c \
 		util/viratomic.h \
+		util/virautoclean.h \
 		util/virbitmap.c \
 		util/virbuffer.c \
 		util/vircgroup.c \
@@ -874,6 +875,7 @@ libvirt_nss_la_SOURCES = \
 		util/viralloc.h \
 		util/viratomic.c \
 		util/viratomic.h \
+		util/virautoclean.h \
 		util/virbitmap.c \
 		util/virbitmap.h \
 		util/virbuffer.c \
diff --git a/src/util/Makefile.inc.am b/src/util/Makefile.inc.am
index aa5c6cbe03..6b24245aa1 100644
--- a/src/util/Makefile.inc.am
+++ b/src/util/Makefile.inc.am
@@ -15,6 +15,7 @@ UTIL_SOURCES = \
 	util/virauth.h \
 	util/virauthconfig.c \
 	util/virauthconfig.h \
+	util/virautoclean.h \
 	util/virbitmap.c \
 	util/virbitmap.h \
 	util/virbuffer.c \
diff --git a/src/util/viralloc.h b/src/util/viralloc.h
index 93ec36aae2..92c71d9a1a 100644
--- a/src/util/viralloc.h
+++ b/src/util/viralloc.h
@@ -606,42 +606,6 @@ int virAllocTestCount(void);
 void virAllocTestOOM(int n, int m);
 void virAllocTestHook(void (*func)(int, void*), void *data);

-# define VIR_AUTOPTR_FUNC_NAME(type) type##AutoPtrFree
-
-/**
- * VIR_DEFINE_AUTOPTR_FUNC:
- * @type: type of the variable to be freed automatically
- * @func: cleanup function to be automatically called
- *
- * This macro defines a function for automatic freeing of
- * resources allocated to a variable of type @type. This newly
- * defined function works as a necessary wrapper around @func.
- */
-# define VIR_DEFINE_AUTOPTR_FUNC(type, func) \
-    static inline void VIR_AUTOPTR_FUNC_NAME(type)(type **_ptr) \
-    { \
-        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. Note that @func must
- * take pointer to @type.
- */
-# define VIR_DEFINE_AUTOCLEAN_FUNC(type, func) \
-    static inline void VIR_AUTOCLEAN_FUNC_NAME(type)(type *_ptr) \
-    { \
-        (func)(_ptr); \
-    }
-
 /**
  * VIR_AUTOFREE:
  * @type: type of the variable to be freed automatically
@@ -652,34 +616,4 @@ void virAllocTestHook(void (*func)(int, void*), void *data);
  */
 # define VIR_AUTOFREE(type) __attribute__((cleanup(virFree))) type

-/**
- * VIR_AUTOPTR:
- * @type: type of the variable to be freed automatically
- *
- * Macro to automatically free the memory allocated to
- * the variable declared with it by calling the function
- * defined by VIR_DEFINE_AUTOPTR_FUNC when the variable
- * goes out of scope.
- *
- * Note that this macro must NOT be used with vectors! The freeing function
- * will not free any elements beyond the first.
- */
-# 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.
- *
- * Note that this macro must NOT be used with vectors! The cleaning function
- * will not clean any elements beyond the first.
- */
-# define VIR_AUTOCLEAN(type) \
-    __attribute__((cleanup(VIR_AUTOCLEAN_FUNC_NAME(type)))) type
-
 #endif /* LIBVIRT_VIRALLOC_H */
diff --git a/src/util/virauthconfig.h b/src/util/virauthconfig.h
index 603ef89d25..9da0366e81 100644
--- a/src/util/virauthconfig.h
+++ b/src/util/virauthconfig.h
@@ -23,6 +23,7 @@

 # include "internal.h"
 # include "viralloc.h"
+# include "virautoclean.h"

 typedef struct _virAuthConfig virAuthConfig;
 typedef virAuthConfig *virAuthConfigPtr;
diff --git a/src/util/virautoclean.h b/src/util/virautoclean.h
new file mode 100644
index 0000000000..b632023c73
--- /dev/null
+++ b/src/util/virautoclean.h
@@ -0,0 +1,90 @@
+/*
+ * virautoclean.h: automatic scope-based memory clearing helper macros for
+ *                 use in header files
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef LIBVIRT_VIRAUTOCLEAN_H
+# define LIBVIRT_VIRAUTOCLEAN_H
+
+# define VIR_AUTOPTR_FUNC_NAME(type) type##AutoPtrFree
+
+/**
+ * VIR_DEFINE_AUTOPTR_FUNC:
+ * @type: type of the variable to be freed automatically
+ * @func: cleanup function to be automatically called
+ *
+ * This macro defines a function for automatic freeing of
+ * resources allocated to a variable of type @type. This newly
+ * defined function works as a necessary wrapper around @func.
+ */
+# define VIR_DEFINE_AUTOPTR_FUNC(type, func) \
+    static inline void VIR_AUTOPTR_FUNC_NAME(type)(type **_ptr) \
+    { \
+        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. Note that @func must
+ * take pointer to @type.
+ */
+# define VIR_DEFINE_AUTOCLEAN_FUNC(type, func) \
+    static inline void VIR_AUTOCLEAN_FUNC_NAME(type)(type *_ptr) \
+    { \
+        (func)(_ptr); \
+    }
+
+/**
+ * VIR_AUTOPTR:
+ * @type: type of the variable to be freed automatically
+ *
+ * Macro to automatically free the memory allocated to
+ * the variable declared with it by calling the function
+ * defined by VIR_DEFINE_AUTOPTR_FUNC when the variable
+ * goes out of scope.
+ *
+ * Note that this macro must NOT be used with vectors! The freeing function
+ * will not free any elements beyond the first.
+ */
+# 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.
+ *
+ * Note that this macro must NOT be used with vectors! The cleaning function
+ * will not clean any elements beyond the first.
+ */
+# define VIR_AUTOCLEAN(type) \
+    __attribute__((cleanup(VIR_AUTOCLEAN_FUNC_NAME(type)))) type
+
+#endif /* LIBVIRT_VIRAUTOCLEAN_H */
diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h
index c54e203971..b4d3673292 100644
--- a/src/util/virbitmap.h
+++ b/src/util/virbitmap.h
@@ -24,6 +24,7 @@

 # include "internal.h"
 # include "viralloc.h"
+# include "virautoclean.h"

 # include <sys/types.h>

diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h
index 18957ae02c..2f5efdf2b7 100644
--- a/src/util/virbuffer.h
+++ b/src/util/virbuffer.h
@@ -25,6 +25,7 @@

 # include "internal.h"
 # include "viralloc.h"
+# include "virautoclean.h"


 /**
diff --git a/src/util/virerror.h b/src/util/virerror.h
index 213bc3f606..4f691550bc 100644
--- a/src/util/virerror.h
+++ b/src/util/virerror.h
@@ -24,6 +24,7 @@

 # include "internal.h"
 # include "viralloc.h"
+# include "virautoclean.h"

 # define VIR_ERROR_MAX_LENGTH 1024

diff --git a/src/util/virfirewall.h b/src/util/virfirewall.h
index e3ad1adb6b..63bf915233 100644
--- a/src/util/virfirewall.h
+++ b/src/util/virfirewall.h
@@ -23,6 +23,7 @@

 # include "internal.h"
 # include "viralloc.h"
+# include "virautoclean.h"

 typedef struct _virFirewall virFirewall;
 typedef virFirewall *virFirewallPtr;
diff --git a/src/util/virhash.h b/src/util/virhash.h
index 2df1a5d12a..b1de808b18 100644
--- a/src/util/virhash.h
+++ b/src/util/virhash.h
@@ -12,6 +12,7 @@


 # include "viralloc.h"
+# include "virautoclean.h"

 /*
  * The hash table.
diff --git a/src/util/virmacaddr.h b/src/util/virmacaddr.h
index 1deaf087e0..6cb0ce3264 100644
--- a/src/util/virmacaddr.h
+++ b/src/util/virmacaddr.h
@@ -23,6 +23,7 @@

 # include "internal.h"
 # include "viralloc.h"
+# include "virautoclean.h"

 # define VIR_MAC_BUFLEN 6
 # define VIR_MAC_HEXLEN (VIR_MAC_BUFLEN * 2)
diff --git a/src/util/virnetdevvlan.h b/src/util/virnetdevvlan.h
index 2a13759550..ff4fb94abc 100644
--- a/src/util/virnetdevvlan.h
+++ b/src/util/virnetdevvlan.h
@@ -22,6 +22,7 @@
 # include <virutil.h>

 # include "viralloc.h"
+# include "virautoclean.h"

 typedef enum {
     VIR_NATIVE_VLAN_MODE_DEFAULT = 0,
diff --git a/src/util/virperf.h b/src/util/virperf.h
index 3cd26563b4..b625bb6846 100644
--- a/src/util/virperf.h
+++ b/src/util/virperf.h
@@ -21,6 +21,7 @@

 # include "virutil.h"
 # include "viralloc.h"
+# include "virautoclean.h"

 /* Some Intel processor families introduced some RDT (Resource Director
  * Technology) features to monitor or control shared resource based on
diff --git a/src/util/virsocketaddr.h b/src/util/virsocketaddr.h
index f6caa4fcf5..2cc3f94b80 100644
--- a/src/util/virsocketaddr.h
+++ b/src/util/virsocketaddr.h
@@ -27,6 +27,7 @@

 # include "internal.h"
 # include "viralloc.h"
+# include "virautoclean.h"

 /* On architectures which lack these limits, define them (ie. Cygwin).
  * Note that the libvirt code should be robust enough to handle the
-- 
2.20.1




More information about the libvir-list mailing list