[Libguestfs] [nbdkit PATCH 2/4] filters: Utilize CLEANUP_EXTENTS_FREE

Eric Blake eblake at redhat.com
Tue Apr 23 19:06:34 UTC 2019


Now that cleanup.h is in common code, we can use it in our
filters. The first round focuses just on places that called
nbdkit_extents_free(), as all three callers had multiple exit paths
that definitely benefit from the macro.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 filters/offset/offset.c       | 13 +++++--------
 filters/partition/partition.c | 12 ++++--------
 filters/truncate/truncate.c   | 12 ++++--------
 filters/offset/Makefile.am    |  5 ++++-
 filters/partition/Makefile.am |  5 ++++-
 filters/truncate/Makefile.am  |  5 ++++-
 6 files changed, 25 insertions(+), 27 deletions(-)

diff --git a/filters/offset/offset.c b/filters/offset/offset.c
index ebd590b..24ccb4c 100644
--- a/filters/offset/offset.c
+++ b/filters/offset/offset.c
@@ -39,6 +39,8 @@

 #include <nbdkit-filter.h>

+#include "cleanup.h"
+
 #define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL

 static int64_t offset = 0, range = -1;
@@ -138,7 +140,7 @@ offset_extents (struct nbdkit_next_ops *next_ops, void *nxdata,
                 struct nbdkit_extents *extents, int *err)
 {
   size_t i;
-  struct nbdkit_extents *extents2;
+  CLEANUP_EXTENTS_FREE struct nbdkit_extents *extents2 = NULL;
   struct nbdkit_extent e;
   int64_t real_size = next_ops->get_size (nxdata);

@@ -149,20 +151,15 @@ offset_extents (struct nbdkit_next_ops *next_ops, void *nxdata,
   }
   if (next_ops->extents (nxdata, count, offs + offset,
                          flags, extents2, err) == -1)
-    goto error;
+    return -1;

   for (i = 0; i < nbdkit_extents_count (extents2); ++i) {
     e = nbdkit_get_extent (extents2, i);
     e.offset -= offset;
     if (nbdkit_add_extent (extents, e.offset, e.length, e.type) == -1)
-      goto error;
+      return -1;
   }
-  nbdkit_extents_free (extents2);
   return 0;
-
- error:
-  nbdkit_extents_free (extents2);
-  return -1;
 }

 static struct nbdkit_filter filter = {
diff --git a/filters/partition/partition.c b/filters/partition/partition.c
index ab692ba..a89dbec 100644
--- a/filters/partition/partition.c
+++ b/filters/partition/partition.c
@@ -41,6 +41,7 @@
 #include <nbdkit-filter.h>

 #include "byte-swapping.h"
+#include "cleanup.h"

 #include "partition.h"

@@ -229,7 +230,7 @@ partition_extents (struct nbdkit_next_ops *next_ops, void *nxdata,
 {
   struct handle *h = handle;
   size_t i;
-  struct nbdkit_extents *extents2;
+  CLEANUP_EXTENTS_FREE struct nbdkit_extents *extents2 = NULL;
   struct nbdkit_extent e;
   int64_t real_size = next_ops->get_size (nxdata);

@@ -240,20 +241,15 @@ partition_extents (struct nbdkit_next_ops *next_ops, void *nxdata,
   }
   if (next_ops->extents (nxdata, count, offs + h->offset,
                          flags, extents2, err) == -1)
-    goto error;
+    return -1;

   for (i = 0; i < nbdkit_extents_count (extents2); ++i) {
     e = nbdkit_get_extent (extents2, i);
     e.offset -= h->offset;
     if (nbdkit_add_extent (extents, e.offset, e.length, e.type) == -1)
-      goto error;
+      return -1;
   }
-  nbdkit_extents_free (extents2);
   return 0;
-
- error:
-  nbdkit_extents_free (extents2);
-  return -1;
 }

 static struct nbdkit_filter filter = {
diff --git a/filters/truncate/truncate.c b/filters/truncate/truncate.c
index dfc6873..076ae22 100644
--- a/filters/truncate/truncate.c
+++ b/filters/truncate/truncate.c
@@ -43,6 +43,7 @@

 #include <nbdkit-filter.h>

+#include "cleanup.h"
 #include "ispowerof2.h"
 #include "iszero.h"
 #include "rounding.h"
@@ -292,7 +293,7 @@ truncate_extents (struct nbdkit_next_ops *next_ops, void *nxdata,
 {
   uint32_t n;
   uint64_t real_size_copy;
-  struct nbdkit_extents *extents2;
+  CLEANUP_EXTENTS_FREE struct nbdkit_extents *extents2 = NULL;
   size_t i;

   pthread_mutex_lock (&lock);
@@ -322,20 +323,15 @@ truncate_extents (struct nbdkit_next_ops *next_ops, void *nxdata,
     n = count;
   else
     n = real_size_copy - offset;
-  if (next_ops->extents (nxdata, n, offset, flags, extents2, err) == -1) {
-    nbdkit_extents_free (extents2);
+  if (next_ops->extents (nxdata, n, offset, flags, extents2, err) == -1)
     return -1;
-  }

   for (i = 0; i < nbdkit_extents_count (extents2); ++i) {
     struct nbdkit_extent e = nbdkit_get_extent (extents2, i);

-    if (nbdkit_add_extent (extents, e.offset, e.length, e.type) == -1) {
-      nbdkit_extents_free (extents2);
+    if (nbdkit_add_extent (extents, e.offset, e.length, e.type) == -1)
       return -1;
-    }
   }
-  nbdkit_extents_free (extents2);

   return 0;
 }
diff --git a/filters/offset/Makefile.am b/filters/offset/Makefile.am
index 14591bb..525d9b6 100644
--- a/filters/offset/Makefile.am
+++ b/filters/offset/Makefile.am
@@ -40,12 +40,15 @@ nbdkit_offset_filter_la_SOURCES = \
 	$(top_srcdir)/include/nbdkit-filter.h

 nbdkit_offset_filter_la_CPPFLAGS = \
-	-I$(top_srcdir)/include
+	-I$(top_srcdir)/include \
+	-I$(top_srcdir)/common/utils
 nbdkit_offset_filter_la_CFLAGS = \
 	$(WARNINGS_CFLAGS)
 nbdkit_offset_filter_la_LDFLAGS = \
 	-module -avoid-version -shared \
 	-Wl,--version-script=$(top_srcdir)/filters/filters.syms
+nbdkit_offset_filter_la_LIBADD = \
+	$(top_builddir)/common/utils/libutils.la

 if HAVE_POD

diff --git a/filters/partition/Makefile.am b/filters/partition/Makefile.am
index 6fbbe17..f335bdc 100644
--- a/filters/partition/Makefile.am
+++ b/filters/partition/Makefile.am
@@ -45,12 +45,15 @@ nbdkit_partition_filter_la_SOURCES = \
 nbdkit_partition_filter_la_CPPFLAGS = \
 	-I$(top_srcdir)/include \
 	-I$(top_srcdir)/common/gpt \
-	-I$(top_srcdir)/common/include
+	-I$(top_srcdir)/common/include \
+	-I$(top_srcdir)/common/utils
 nbdkit_partition_filter_la_CFLAGS = \
 	$(WARNINGS_CFLAGS)
 nbdkit_partition_filter_la_LDFLAGS = \
 	-module -avoid-version -shared \
 	-Wl,--version-script=$(top_srcdir)/filters/filters.syms
+nbdkit_partition_filter_la_LIBADD = \
+	$(top_builddir)/common/utils/libutils.la

 if HAVE_POD

diff --git a/filters/truncate/Makefile.am b/filters/truncate/Makefile.am
index 86709d4..c591703 100644
--- a/filters/truncate/Makefile.am
+++ b/filters/truncate/Makefile.am
@@ -41,12 +41,15 @@ nbdkit_truncate_filter_la_SOURCES = \

 nbdkit_truncate_filter_la_CPPFLAGS = \
 	-I$(top_srcdir)/include \
-	-I$(top_srcdir)/common/include
+	-I$(top_srcdir)/common/include \
+	-I$(top_srcdir)/common/utils
 nbdkit_truncate_filter_la_CFLAGS = \
 	$(WARNINGS_CFLAGS)
 nbdkit_truncate_filter_la_LDFLAGS = \
 	-module -avoid-version -shared \
 	-Wl,--version-script=$(top_srcdir)/filters/filters.syms
+nbdkit_truncate_filter_la_LIBADD = \
+	$(top_builddir)/common/utils/libutils.la

 if HAVE_POD

-- 
2.20.1




More information about the Libguestfs mailing list