[Libguestfs] [PATCH v7 08/13] common/utils: Refactor stdlib, gnulib and libxml2 cleanup functions.

Richard W.M. Jones rjones at redhat.com
Mon Jun 19 13:31:24 UTC 2017


This refactoring change just moves the cleanup functions around in the
common/utils directory.

libxml2 cleanups are moved to a separate object file, so that we can
still link to libutils even if the main program is not using libxml2
anywhere.

cleanup.c is renamed to cleanups.c.

A new header file cleanups.h is introduced which will replace
guestfs-internal-frontend-cleanups.h (fully replaced in a later commit).
---
 .gitignore                               |   3 +-
 common/utils/Makefile.am                 |   4 +-
 common/utils/{cleanup.c => cleanups.c}   | 105 ++++++-------------------------
 common/utils/cleanups.h                  |  82 ++++++++++++++++++++++++
 common/utils/guestfs-internal-frontend.h |  58 +----------------
 common/utils/libxml2-cleanups.c          |  94 +++++++++++++++++++++++++++
 docs/C_SOURCE_FILES                      |   4 +-
 ocaml/Makefile.am                        |   1 +
 python/Makefile.am                       |  15 +++--
 9 files changed, 216 insertions(+), 150 deletions(-)

diff --git a/.gitignore b/.gitignore
index c347e31bd..ef3b9997f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -488,6 +488,8 @@ Makefile.in
 /python/bindtests.py
 /python/build
 /python/c-ctype.h
+/python/cleanups.c
+/python/cleanups.h
 /python/config.h
 /python/dist
 /python/examples/guestfs-python.3
@@ -496,7 +498,6 @@ Makefile.in
 /python/guestfs.pyc
 /python/guestfs.pyo
 /python/guestfs-internal-all.h
-/python/guestfs-internal-frontend-cleanups.h
 /python/guestfs-internal-frontend.h
 /python/ignore-value.h
 /python/MANIFEST
diff --git a/common/utils/Makefile.am b/common/utils/Makefile.am
index 93f42293a..5c9728797 100644
--- a/common/utils/Makefile.am
+++ b/common/utils/Makefile.am
@@ -33,9 +33,11 @@ noinst_LTLIBRARIES = libutils.la
 
 libutils_la_SOURCES = \
 	../../lib/guestfs.h \
-	cleanup.c \
+	cleanups.c \
+	cleanups.h \
 	guestfs-internal-frontend.h \
 	guestfs-internal-frontend-cleanups.h \
+	libxml2-cleanups.c \
 	structs-cleanup.c \
 	structs-print.c \
 	structs-print.h \
diff --git a/common/utils/cleanup.c b/common/utils/cleanups.c
similarity index 70%
rename from common/utils/cleanup.c
rename to common/utils/cleanups.c
index 6c4558c39..c9a34c2ef 100644
--- a/common/utils/cleanup.c
+++ b/common/utils/cleanups.c
@@ -1,5 +1,5 @@
 /* libguestfs
- * Copyright (C) 2013 Red Hat Inc.
+ * Copyright (C) 2013-2017 Red Hat Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -63,16 +63,12 @@
 #include <unistd.h>
 #include <string.h>
 
-#include <libxml/uri.h>
-#include <libxml/tree.h>
-#include <libxml/xpath.h>
-#include <libxml/xmlwriter.h>
-
 #include "hash.h"
 
-#include "guestfs.h"
 #include "guestfs-internal-frontend.h"
 
+/* Stdlib cleanups. */
+
 void
 guestfs_int_cleanup_free (void *ptr)
 {
@@ -80,21 +76,6 @@ guestfs_int_cleanup_free (void *ptr)
 }
 
 void
-guestfs_int_cleanup_free_string_list (char ***ptr)
-{
-  guestfs_int_free_string_list (*ptr);
-}
-
-void
-guestfs_int_cleanup_hash_free (void *ptr)
-{
-  Hash_table *h = * (Hash_table **) ptr;
-
-  if (h)
-    hash_free (h);
-}
-
-void
 guestfs_int_cleanup_unlink_free (char **ptr)
 {
   char *filename = *ptr;
@@ -106,69 +87,6 @@ guestfs_int_cleanup_unlink_free (char **ptr)
 }
 
 void
-guestfs_int_cleanup_xmlFree (void *ptr)
-{
-  xmlChar *buf = * (xmlChar **) ptr;
-
-  if (buf)
-    xmlFree (buf);
-}
-
-void
-guestfs_int_cleanup_xmlBufferFree (void *ptr)
-{
-  xmlBufferPtr xb = * (xmlBufferPtr *) ptr;
-
-  if (xb)
-    xmlBufferFree (xb);
-}
-
-void
-guestfs_int_cleanup_xmlFreeDoc (void *ptr)
-{
-  xmlDocPtr doc = * (xmlDocPtr *) ptr;
-
-  if (doc)
-    xmlFreeDoc (doc);
-}
-
-void
-guestfs_int_cleanup_xmlFreeURI (void *ptr)
-{
-  xmlURIPtr uri = * (xmlURIPtr *) ptr;
-
-  if (uri)
-    xmlFreeURI (uri);
-}
-
-void
-guestfs_int_cleanup_xmlFreeTextWriter (void *ptr)
-{
-  xmlTextWriterPtr xo = * (xmlTextWriterPtr *) ptr;
-
-  if (xo)
-    xmlFreeTextWriter (xo);
-}
-
-void
-guestfs_int_cleanup_xmlXPathFreeContext (void *ptr)
-{
-  xmlXPathContextPtr ctx = * (xmlXPathContextPtr *) ptr;
-
-  if (ctx)
-    xmlXPathFreeContext (ctx);
-}
-
-void
-guestfs_int_cleanup_xmlXPathFreeObject (void *ptr)
-{
-  xmlXPathObjectPtr obj = * (xmlXPathObjectPtr *) ptr;
-
-  if (obj)
-    xmlXPathFreeObject (obj);
-}
-
-void
 guestfs_int_cleanup_fclose (void *ptr)
 {
   FILE *f = * (FILE **) ptr;
@@ -185,3 +103,20 @@ guestfs_int_cleanup_pclose (void *ptr)
   if (f)
     pclose (f);
 }
+
+void
+guestfs_int_cleanup_free_string_list (char ***ptr)
+{
+  guestfs_int_free_string_list (*ptr);
+}
+
+/* Gnulib cleanups. */
+
+void
+guestfs_int_cleanup_hash_free (void *ptr)
+{
+  Hash_table *h = * (Hash_table **) ptr;
+
+  if (h)
+    hash_free (h);
+}
diff --git a/common/utils/cleanups.h b/common/utils/cleanups.h
new file mode 100644
index 000000000..df62cafd6
--- /dev/null
+++ b/common/utils/cleanups.h
@@ -0,0 +1,82 @@
+/* libguestfs
+ * Copyright (C) 2013-2017 Red Hat Inc.
+ *
+ * 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 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef GUESTFS_CLEANUPS_H_
+#define GUESTFS_CLEANUPS_H_
+
+#ifdef HAVE_ATTRIBUTE_CLEANUP
+#define CLEANUP_FREE                                    \
+  __attribute__((cleanup(guestfs_int_cleanup_free)))
+#define CLEANUP_HASH_FREE                                       \
+  __attribute__((cleanup(guestfs_int_cleanup_hash_free)))
+#define CLEANUP_UNLINK_FREE                                     \
+  __attribute__((cleanup(guestfs_int_cleanup_unlink_free)))
+#define CLEANUP_FCLOSE                                  \
+  __attribute__((cleanup(guestfs_int_cleanup_fclose)))
+#define CLEANUP_PCLOSE                                  \
+  __attribute__((cleanup(guestfs_int_cleanup_pclose)))
+#define CLEANUP_FREE_STRING_LIST                                \
+  __attribute__((cleanup(guestfs_int_cleanup_free_string_list)))
+#define CLEANUP_XMLFREE                                 \
+  __attribute__((cleanup(guestfs_int_cleanup_xmlFree)))
+#define CLEANUP_XMLBUFFERFREE                                   \
+  __attribute__((cleanup(guestfs_int_cleanup_xmlBufferFree)))
+#define CLEANUP_XMLFREEDOC                                      \
+  __attribute__((cleanup(guestfs_int_cleanup_xmlFreeDoc)))
+#define CLEANUP_XMLFREEURI                                              \
+  __attribute__((cleanup(guestfs_int_cleanup_xmlFreeURI)))
+#define CLEANUP_XMLFREETEXTWRITER                               \
+  __attribute__((cleanup(guestfs_int_cleanup_xmlFreeTextWriter)))
+#define CLEANUP_XMLXPATHFREECONTEXT                                     \
+  __attribute__((cleanup(guestfs_int_cleanup_xmlXPathFreeContext)))
+#define CLEANUP_XMLXPATHFREEOBJECT                                      \
+  __attribute__((cleanup(guestfs_int_cleanup_xmlXPathFreeObject)))
+#else
+#define CLEANUP_FREE
+#define CLEANUP_HASH_FREE
+#define CLEANUP_UNLINK_FREE
+#define CLEANUP_FCLOSE
+#define CLEANUP_PCLOSE
+#define CLEANUP_FREE_STRING_LIST
+#define CLEANUP_XMLFREE
+#define CLEANUP_XMLBUFFERFREE
+#define CLEANUP_XMLFREEDOC
+#define CLEANUP_XMLFREEURI
+#define CLEANUP_XMLFREETEXTWRITER
+#define CLEANUP_XMLXPATHFREECONTEXT
+#define CLEANUP_XMLXPATHFREEOBJECT
+#endif
+
+/* These functions are used internally by the CLEANUP_* macros.
+ * Don't call them directly.
+ */
+extern void guestfs_int_cleanup_free (void *ptr);
+extern void guestfs_int_cleanup_hash_free (void *ptr);
+extern void guestfs_int_cleanup_unlink_free (char **ptr);
+extern void guestfs_int_cleanup_fclose (void *ptr);
+extern void guestfs_int_cleanup_pclose (void *ptr);
+extern void guestfs_int_cleanup_free_string_list (char ***ptr);
+extern void guestfs_int_cleanup_xmlFree (void *ptr);
+extern void guestfs_int_cleanup_xmlBufferFree (void *ptr);
+extern void guestfs_int_cleanup_xmlFreeDoc (void *ptr);
+extern void guestfs_int_cleanup_xmlFreeURI (void *ptr);
+extern void guestfs_int_cleanup_xmlFreeTextWriter (void *ptr);
+extern void guestfs_int_cleanup_xmlXPathFreeContext (void *ptr);
+extern void guestfs_int_cleanup_xmlXPathFreeObject (void *ptr);
+
+#endif /* GUESTFS_CLEANUPS_H_ */
diff --git a/common/utils/guestfs-internal-frontend.h b/common/utils/guestfs-internal-frontend.h
index 489b54ffd..1d5a50664 100644
--- a/common/utils/guestfs-internal-frontend.h
+++ b/common/utils/guestfs-internal-frontend.h
@@ -35,50 +35,11 @@
 #include <stdbool.h>
 
 #include "guestfs-internal-all.h"
+#include "cleanups.h"
 
 #define _(str) dgettext(PACKAGE, (str))
 #define N_(str) dgettext(PACKAGE, (str))
 
-#ifdef HAVE_ATTRIBUTE_CLEANUP
-#define CLEANUP_FREE __attribute__((cleanup(guestfs_int_cleanup_free)))
-#define CLEANUP_FREE_STRING_LIST                                \
-  __attribute__((cleanup(guestfs_int_cleanup_free_string_list)))
-#define CLEANUP_HASH_FREE                               \
-  __attribute__((cleanup(guestfs_int_cleanup_hash_free)))
-#define CLEANUP_UNLINK_FREE                                     \
-  __attribute__((cleanup(guestfs_int_cleanup_unlink_free)))
-#define CLEANUP_XMLFREE                                         \
-  __attribute__((cleanup(guestfs_int_cleanup_xmlFree)))
-#define CLEANUP_XMLBUFFERFREE                                   \
-  __attribute__((cleanup(guestfs_int_cleanup_xmlBufferFree)))
-#define CLEANUP_XMLFREEDOC                                      \
-  __attribute__((cleanup(guestfs_int_cleanup_xmlFreeDoc)))
-#define CLEANUP_XMLFREEURI                                              \
-  __attribute__((cleanup(guestfs_int_cleanup_xmlFreeURI)))
-#define CLEANUP_XMLFREETEXTWRITER                               \
-  __attribute__((cleanup(guestfs_int_cleanup_xmlFreeTextWriter)))
-#define CLEANUP_XMLXPATHFREECONTEXT                                     \
-  __attribute__((cleanup(guestfs_int_cleanup_xmlXPathFreeContext)))
-#define CLEANUP_XMLXPATHFREEOBJECT                                      \
-  __attribute__((cleanup(guestfs_int_cleanup_xmlXPathFreeObject)))
-#define CLEANUP_FCLOSE __attribute__((cleanup(guestfs_int_cleanup_fclose)))
-#define CLEANUP_PCLOSE __attribute__((cleanup(guestfs_int_cleanup_pclose)))
-#else
-#define CLEANUP_FREE
-#define CLEANUP_FREE_STRING_LIST
-#define CLEANUP_HASH_FREE
-#define CLEANUP_UNLINK_FREE
-#define CLEANUP_XMLFREE
-#define CLEANUP_XMLBUFFERFREE
-#define CLEANUP_XMLFREEDOC
-#define CLEANUP_XMLFREEURI
-#define CLEANUP_XMLFREETEXTWRITER
-#define CLEANUP_XMLXPATHFREECONTEXT
-#define CLEANUP_XMLXPATHFREEOBJECT
-#define CLEANUP_FCLOSE
-#define CLEANUP_PCLOSE
-#endif
-
 /* utils.c */
 extern void guestfs_int_free_string_list (char **);
 extern size_t guestfs_int_count_strings (char *const *);
@@ -102,23 +63,6 @@ extern void guestfs_int_fadvise_noreuse (int fd);
 //extern void guestfs_int_fadvise_willneed (int fd);
 extern char *guestfs_int_shell_unquote (const char *str);
 
-/* These functions are used internally by the CLEANUP_* macros.
- * Don't call them directly.
- */
-extern void guestfs_int_cleanup_free (void *ptr);
-extern void guestfs_int_cleanup_free_string_list (char ***ptr);
-extern void guestfs_int_cleanup_hash_free (void *ptr);
-extern void guestfs_int_cleanup_unlink_free (char **ptr);
-extern void guestfs_int_cleanup_xmlFree (void *ptr);
-extern void guestfs_int_cleanup_xmlBufferFree (void *ptr);
-extern void guestfs_int_cleanup_xmlFreeDoc (void *ptr);
-extern void guestfs_int_cleanup_xmlFreeURI (void *ptr);
-extern void guestfs_int_cleanup_xmlFreeTextWriter (void *ptr);
-extern void guestfs_int_cleanup_xmlXPathFreeContext (void *ptr);
-extern void guestfs_int_cleanup_xmlXPathFreeObject (void *ptr);
-extern void guestfs_int_cleanup_fclose (void *ptr);
-extern void guestfs_int_cleanup_pclose (void *ptr);
-
 /* These are in a separate header so the header can be generated.
  * Don't include the following file directly:
  */
diff --git a/common/utils/libxml2-cleanups.c b/common/utils/libxml2-cleanups.c
new file mode 100644
index 000000000..066573fef
--- /dev/null
+++ b/common/utils/libxml2-cleanups.c
@@ -0,0 +1,94 @@
+/* libguestfs
+ * Copyright (C) 2013-2017 Red Hat Inc.
+ *
+ * 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 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <libxml/uri.h>
+#include <libxml/tree.h>
+#include <libxml/xpath.h>
+#include <libxml/xmlwriter.h>
+
+#include "cleanups.h"
+
+void
+guestfs_int_cleanup_xmlFree (void *ptr)
+{
+  xmlChar *buf = * (xmlChar **) ptr;
+
+  if (buf)
+    xmlFree (buf);
+}
+
+void
+guestfs_int_cleanup_xmlBufferFree (void *ptr)
+{
+  xmlBufferPtr xb = * (xmlBufferPtr *) ptr;
+
+  if (xb)
+    xmlBufferFree (xb);
+}
+
+void
+guestfs_int_cleanup_xmlFreeDoc (void *ptr)
+{
+  xmlDocPtr doc = * (xmlDocPtr *) ptr;
+
+  if (doc)
+    xmlFreeDoc (doc);
+}
+
+void
+guestfs_int_cleanup_xmlFreeURI (void *ptr)
+{
+  xmlURIPtr uri = * (xmlURIPtr *) ptr;
+
+  if (uri)
+    xmlFreeURI (uri);
+}
+
+void
+guestfs_int_cleanup_xmlFreeTextWriter (void *ptr)
+{
+  xmlTextWriterPtr xo = * (xmlTextWriterPtr *) ptr;
+
+  if (xo)
+    xmlFreeTextWriter (xo);
+}
+
+void
+guestfs_int_cleanup_xmlXPathFreeContext (void *ptr)
+{
+  xmlXPathContextPtr ctx = * (xmlXPathContextPtr *) ptr;
+
+  if (ctx)
+    xmlXPathFreeContext (ctx);
+}
+
+void
+guestfs_int_cleanup_xmlXPathFreeObject (void *ptr)
+{
+  xmlXPathObjectPtr obj = * (xmlXPathObjectPtr *) ptr;
+
+  if (obj)
+    xmlXPathFreeObject (obj);
+}
diff --git a/docs/C_SOURCE_FILES b/docs/C_SOURCE_FILES
index f9ffb0a7d..e63eb3d7d 100644
--- a/docs/C_SOURCE_FILES
+++ b/docs/C_SOURCE_FILES
@@ -42,9 +42,11 @@ common/progress/progress.h
 common/qemuopts/qemuopts-tests.c
 common/qemuopts/qemuopts.c
 common/qemuopts/qemuopts.h
-common/utils/cleanup.c
+common/utils/cleanups.c
+common/utils/cleanups.h
 common/utils/guestfs-internal-frontend-cleanups.h
 common/utils/guestfs-internal-frontend.h
+common/utils/libxml2-cleanups.c
 common/utils/structs-cleanup.c
 common/utils/structs-print.c
 common/utils/structs-print.h
diff --git a/ocaml/Makefile.am b/ocaml/Makefile.am
index 57ecd608b..91be080bf 100644
--- a/ocaml/Makefile.am
+++ b/ocaml/Makefile.am
@@ -94,6 +94,7 @@ libguestfsocaml_a_SOURCES = \
 	guestfs-c.c \
 	guestfs-c-actions.c \
 	guestfs-c-errnos.c \
+	../common/utils/cleanups.c \
 	../common/utils/utils.c
 
 if HAVE_OCAMLDOC
diff --git a/python/Makefile.am b/python/Makefile.am
index ae90aa01d..7e91bbb7e 100644
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -97,9 +97,10 @@ setup-install: setup.py stamp-extra-files
 # to hard-link any extra files we need into the local directory.
 stamp-extra-files: \
 	  c-ctype.h \
+	  cleanups.c \
+	  cleanups.h \
 	  config.h \
 	  guestfs-internal-all.h \
-	  guestfs-internal-frontend-cleanups.h \
 	  guestfs-internal-frontend.h \
 	  ignore-value.h \
 	  utils.c
@@ -111,15 +112,18 @@ config.h:
 c-ctype.h:
 	ln $(top_srcdir)/gnulib/lib/c-ctype.h $@
 
+cleanups.c:
+	ln $(top_srcdir)/common/utils/cleanups.c $@
+
+cleanups.h:
+	ln $(top_srcdir)/common/utils/cleanups.h $@
+
 ignore-value.h:
 	ln $(top_srcdir)/gnulib/lib/ignore-value.h $@
 
 guestfs-internal-all.h:
 	ln $(top_srcdir)/lib/guestfs-internal-all.h $@
 
-guestfs-internal-frontend-cleanups.h:
-	ln $(top_srcdir)/common/utils/guestfs-internal-frontend-cleanups.h $@
-
 guestfs-internal-frontend.h:
 	ln $(top_srcdir)/common/utils/guestfs-internal-frontend.h $@
 
@@ -145,8 +149,9 @@ CLEANFILES += \
 	t/*~ t/*.pyc \
 	c-ctype.h \
 	config.h \
+	cleanups.c \
+	cleanups.h \
 	guestfs-internal-all.h \
-	guestfs-internal-frontend-cleanups.h \
 	guestfs-internal-frontend.h \
 	ignore-value.h \
 	stamp-extra-files \
-- 
2.13.0




More information about the Libguestfs mailing list