The preferred implementation uses __auto_type, a GCC extension also
now supported by Clang.
Unfortunately OpenBSD ships with GCC 4.2.1 (from 2007!) which predates
this extension by quite a few years, so we have to be able to fall
back to a plain macro.
---
configure.ac | 20 ++++++++++-
common/include/minmax.h | 63 +++++++++++++++++++++++++++++++++++
filters/blocksize/blocksize.c | 3 +-
filters/cache/lru.c | 3 +-
filters/nozero/nozero.c | 3 +-
plugins/pattern/pattern.c | 3 +-
common/include/Makefile.am | 1 +
filters/blocksize/Makefile.am | 3 +-
filters/nozero/Makefile.am | 3 +-
9 files changed, 93 insertions(+), 9 deletions(-)
diff --git a/configure.ac b/configure.ac
index 606e632..ee1ff0d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -156,9 +156,27 @@ you may be using a C compiler which does not support this attribute,
or the configure test may be wrong.
This code requires the attribute to work for proper locking between threads.])])
-dnl restore CFLAGS
CFLAGS="${acx_nbdkit_save_CFLAGS}"
+dnl Check for __auto_type (GCC extension).
+AC_MSG_CHECKING([if __auto_type is available in this compiler])
+AC_COMPILE_IFELSE([
+AC_LANG_SOURCE([[
+static int
+test (int a)
+{
+ __auto_type at = a;
+ return at;
+}
+]])
+ ],[
+ AC_MSG_RESULT([yes])
+ AC_DEFINE([HAVE_AUTO_TYPE],[1],[__auto_type is available])
+ ],[
+ AC_MSG_RESULT([no])
+ ]
+)
+
dnl Check for other headers, all optional.
AC_CHECK_HEADERS([\
alloca.h \
diff --git a/common/include/minmax.h b/common/include/minmax.h
new file mode 100644
index 0000000..dc01679
--- /dev/null
+++ b/common/include/minmax.h
@@ -0,0 +1,63 @@
+/* nbdkit
+ * Copyright (C) 2019 Red Hat Inc.
+ * All rights reserved.
+ *
+ * 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_MINMAX_H
+#define NBDKIT_MINMAX_H
+
+#include <config.h>
+
+#ifdef HAVE_AUTO_TYPE
+
+/* __auto_type is a GCC extension, added in GCC 4.9 in 2014, and to
+ * Clang in 2015.
+ */
+
+#define MIN(x, y) ({ \
+ __auto_type _x = (x); \
+ __auto_type _y = (y); \
+ _x < _y ? _x : _y; \
+ })
+#define MAX(x, y) ({ \
+ __auto_type _x = (x); \
+ __auto_type _y = (y); \
+ _x > _y ? _x : _y; \
+ })
+
+#else
+
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+
+#endif
+
+#endif /* NBDKIT_MINMAX_H */
diff --git a/filters/blocksize/blocksize.c b/filters/blocksize/blocksize.c
index 6acd9b6..34f58c9 100644
--- a/filters/blocksize/blocksize.c
+++ b/filters/blocksize/blocksize.c
@@ -44,11 +44,12 @@
#include <nbdkit-filter.h>
+#include "minmax.h"
+
/* XXX See design comment in filters/cow/cow.c. */
#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
#define BLOCKSIZE_MIN_LIMIT (64U * 1024)
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
/* As long as we don't have parallel requests, we can reuse a common
* buffer for alignment purposes; size it to the maximum we allow for
diff --git a/filters/cache/lru.c b/filters/cache/lru.c
index c828968..9e20408 100644
--- a/filters/cache/lru.c
+++ b/filters/cache/lru.c
@@ -46,13 +46,12 @@
#include <nbdkit-filter.h>
#include "bitmap.h"
+#include "minmax.h"
#include "cache.h"
#include "blk.h"
#include "lru.h"
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-
/* LRU bitmaps. These bitmaps implement a simple, fast LRU structure.
*
* bm[0] bm[1] blocks not in either bitmap
diff --git a/filters/nozero/nozero.c b/filters/nozero/nozero.c
index dac47ad..6a22f9b 100644
--- a/filters/nozero/nozero.c
+++ b/filters/nozero/nozero.c
@@ -42,9 +42,10 @@
#include <nbdkit-filter.h>
+#include "minmax.h"
+
#define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX_WRITE (64 * 1024 * 1024)
static char buffer[MAX_WRITE];
diff --git a/plugins/pattern/pattern.c b/plugins/pattern/pattern.c
index 1d1b234..32db381 100644
--- a/plugins/pattern/pattern.c
+++ b/plugins/pattern/pattern.c
@@ -45,6 +45,7 @@
#include <nbdkit-plugin.h>
#include "byte-swapping.h"
+#include "minmax.h"
/* The size of disk in bytes (initialized by size=<SIZE> parameter). */
static int64_t size = 0;
@@ -87,8 +88,6 @@ pattern_get_size (void *handle)
return size;
}
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-
/* Read data. */
static int
pattern_pread (void *handle, void *buf, uint32_t count, uint64_t offset,
diff --git a/common/include/Makefile.am b/common/include/Makefile.am
index 3e7f056..7df4855 100644
--- a/common/include/Makefile.am
+++ b/common/include/Makefile.am
@@ -41,6 +41,7 @@ EXTRA_DIST = \
isaligned.h \
ispowerof2.h \
iszero.h \
+ minmax.h \
nextnonzero.h \
random.h \
rounding.h
diff --git a/filters/blocksize/Makefile.am b/filters/blocksize/Makefile.am
index b49fc3f..231c9b5 100644
--- a/filters/blocksize/Makefile.am
+++ b/filters/blocksize/Makefile.am
@@ -41,7 +41,8 @@ nbdkit_blocksize_filter_la_SOURCES = \
$(top_srcdir)/include/nbdkit-filter.h
nbdkit_blocksize_filter_la_CPPFLAGS = \
- -I$(top_srcdir)/include
+ -I$(top_srcdir)/include \
+ -I$(top_srcdir)/common/include
nbdkit_blocksize_filter_la_CFLAGS = \
$(WARNINGS_CFLAGS)
nbdkit_blocksize_filter_la_LDFLAGS = \
diff --git a/filters/nozero/Makefile.am b/filters/nozero/Makefile.am
index b1fd4a8..fe88233 100644
--- a/filters/nozero/Makefile.am
+++ b/filters/nozero/Makefile.am
@@ -41,7 +41,8 @@ nbdkit_nozero_filter_la_SOURCES = \
$(top_srcdir)/include/nbdkit-filter.h
nbdkit_nozero_filter_la_CPPFLAGS = \
- -I$(top_srcdir)/include
+ -I$(top_srcdir)/include \
+ -I$(top_srcdir)/common/include
nbdkit_nozero_filter_la_CFLAGS = \
$(WARNINGS_CFLAGS)
nbdkit_nozero_filter_la_LDFLAGS = \
--
2.19.2