On Tue, Apr 23, 2019 at 02:06:33PM -0500, Eric Blake wrote:
The CLEANUP_FREE macro and friends can be useful to filters and
in-tree plugins; as such, move them to common/ so more than just the
server/ code can take advantage of our compiler magic.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
common/utils/cleanup.h | 48 ++++++++++++++++++++++++++++++
server/internal.h | 12 +-------
{server => common/utils}/cleanup.c | 5 ++--
common/utils/Makefile.am | 4 ++-
server/Makefile.am | 7 +++--
5 files changed, 58 insertions(+), 18 deletions(-)
create mode 100644 common/utils/cleanup.h
rename {server => common/utils}/cleanup.c (96%)
diff --git a/common/utils/cleanup.h b/common/utils/cleanup.h
new file mode 100644
index 0000000..e6e6140
--- /dev/null
+++ b/common/utils/cleanup.h
@@ -0,0 +1,48 @@
+/* nbdkit
+ * Copyright (C) 2013-2019 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_CLEANUP_H
+#define NBDKIT_CLEANUP_H
+
+#include <pthread.h>
+
+extern void cleanup_free (void *ptr);
+#define CLEANUP_FREE __attribute__((cleanup (cleanup_free)))
+extern void cleanup_extents_free (void *ptr);
+#define CLEANUP_EXTENTS_FREE __attribute__((cleanup (cleanup_extents_free)))
+extern void cleanup_unlock (pthread_mutex_t **ptr);
+#define CLEANUP_UNLOCK __attribute__((cleanup (cleanup_unlock)))
+#define ACQUIRE_LOCK_FOR_CURRENT_SCOPE(mutex) \
+ CLEANUP_UNLOCK pthread_mutex_t *_lock = mutex; \
+ pthread_mutex_lock (_lock)
+
+#endif /* NBDKIT_CLEANUP_H */
diff --git a/server/internal.h b/server/internal.h
index 817f022..67fccfc 100644
--- a/server/internal.h
+++ b/server/internal.h
@@ -42,6 +42,7 @@
#define NBDKIT_API_VERSION 2
#include "nbdkit-plugin.h"
#include "nbdkit-filter.h"
+#include "cleanup.h"
#ifdef __APPLE__
#define UNIX_PATH_MAX 104
@@ -135,17 +136,6 @@ extern unsigned int get_socket_activation (void);
/* usergroup.c */
extern void change_user (void);
-/* cleanup.c */
-extern void cleanup_free (void *ptr);
-#define CLEANUP_FREE __attribute__((cleanup (cleanup_free)))
-extern void cleanup_extents_free (void *ptr);
-#define CLEANUP_EXTENTS_FREE __attribute__((cleanup (cleanup_extents_free)))
-extern void cleanup_unlock (pthread_mutex_t **ptr);
-#define CLEANUP_UNLOCK __attribute__((cleanup (cleanup_unlock)))
-#define ACQUIRE_LOCK_FOR_CURRENT_SCOPE(mutex) \
- CLEANUP_UNLOCK pthread_mutex_t *_lock = mutex; \
- pthread_mutex_lock (_lock)
-
/* connections.c */
struct connection;
diff --git a/server/cleanup.c b/common/utils/cleanup.c
similarity index 96%
rename from server/cleanup.c
rename to common/utils/cleanup.c
index 292f1ce..196d910 100644
--- a/server/cleanup.c
+++ b/common/utils/cleanup.c
@@ -34,10 +34,9 @@
#include <stdio.h>
#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#include "internal.h"
+#include "cleanup.h"
+#include "nbdkit-filter.h"
void
cleanup_free (void *ptr)
diff --git a/common/utils/Makefile.am b/common/utils/Makefile.am
index 1773ece..d40d6cf 100644
--- a/common/utils/Makefile.am
+++ b/common/utils/Makefile.am
@@ -34,7 +34,9 @@ include $(top_srcdir)/common-rules.mk
noinst_LTLIBRARIES = libutils.la
libutils_la_SOURCES = \
- utils.c \
+ cleanup.c \
+ cleanup.h \
+ utils.c \
utils.h
libutils_la_CPPFLAGS = \
-I$(top_srcdir)/include
diff --git a/server/Makefile.am b/server/Makefile.am
index 9e13c03..8aa8d3a 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -38,7 +38,6 @@ sbin_PROGRAMS = nbdkit
nbdkit_SOURCES = \
background.c \
captive.c \
- cleanup.c \
connections.c \
crypto.c \
debug.c \
@@ -124,9 +123,11 @@ check_PROGRAMS = test-utils
test_utils_SOURCES = \
test-utils.c \
utils.c \
- cleanup.c \
extents.c
test_utils_CPPFLAGS = \
-I$(top_srcdir)/include \
- -I$(top_srcdir)/common/include
+ -I$(top_srcdir)/common/include \
+ -I$(top_srcdir)/common/utils
test_utils_CFLAGS = $(WARNINGS_CFLAGS) $(VALGRIND_CFLAGS)
+test_utils_LDADD = \
+ $(top_builddir)/common/utils/libutils.la
This is a simple code motion, looks fine so:
ACK
Rich.
--
Richard Jones, Virtualization Group, Red Hat
http://people.redhat.com/~rjones
Read my programming and virtualization blog:
http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html