<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, Aug 1, 2018 at 2:14 PM Richard W.M. Jones <<a href="mailto:rjones@redhat.com">rjones@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Currently this adds two useful header files containing functions which<br>
will be consumed by filters in later commits.<br>
---<br>
Makefile.am | 5 +++-<br>
common/include/Makefile.am | 39 ++++++++++++++++++++++++<br>
common/include/ispowerof2.h | 50 +++++++++++++++++++++++++++++++<br>
common/include/iszero.h | 60 +++++++++++++++++++++++++++++++++++++<br>
<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> | 1 +<br>
5 files changed, 154 insertions(+), 1 deletion(-)<br>
<br>
diff --git a/Makefile.am b/Makefile.am<br>
index 6a3a377..09dbdb9 100644<br>
--- a/Makefile.am<br>
+++ b/Makefile.am<br>
@@ -53,7 +53,10 @@ SUBDIRS = \<br>
src<br>
<br>
if HAVE_PLUGINS<br>
-SUBDIRS += plugins filters<br>
+SUBDIRS += \<br>
+ common/include \<br>
+ plugins \<br>
+ filters<br>
endif<br>
<br>
SUBDIRS += tests<br>
diff --git a/common/include/Makefile.am b/common/include/Makefile.am<br>
new file mode 100644<br>
index 0000000..4ce1723<br>
--- /dev/null<br>
+++ b/common/include/Makefile.am<br>
@@ -0,0 +1,39 @@<br>
+# nbdkit<br>
+# Copyright (C) 2018 Red Hat Inc.<br>
+# All rights reserved.<br>
+#<br>
+# Redistribution and use in source and binary forms, with or without<br>
+# modification, are permitted provided that the following conditions are<br>
+# met:<br>
+#<br>
+# * Redistributions of source code must retain the above copyright<br>
+# notice, this list of conditions and the following disclaimer.<br>
+#<br>
+# * Redistributions in binary form must reproduce the above copyright<br>
+# notice, this list of conditions and the following disclaimer in the<br>
+# documentation and/or other materials provided with the distribution.<br>
+#<br>
+# * Neither the name of Red Hat nor the names of its contributors may be<br>
+# used to endorse or promote products derived from this software without<br>
+# specific prior written permission.<br>
+#<br>
+# THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND<br>
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,<br>
+# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A<br>
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR<br>
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,<br>
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT<br>
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF<br>
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND<br>
+# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,<br>
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT<br>
+# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF<br>
+# SUCH DAMAGE.<br>
+<br>
+include $(top_srcdir)/<a href="http://common-rules.mk" rel="noreferrer" target="_blank">common-rules.mk</a><br>
+<br>
+# These headers contain only common code shared by plugins and<br>
+# filters. They are not installed.<br>
+EXTRA_DIST = \<br>
+ ispowerof2.h \<br>
+ iszero.h<br>
diff --git a/common/include/ispowerof2.h b/common/include/ispowerof2.h<br>
new file mode 100644<br>
index 0000000..41844b3<br>
--- /dev/null<br>
+++ b/common/include/ispowerof2.h<br>
@@ -0,0 +1,50 @@<br>
+/* nbdkit<br>
+ * Copyright (C) 2018 Red Hat Inc.<br>
+ * All rights reserved.<br>
+ *<br>
+ * Redistribution and use in source and binary forms, with or without<br>
+ * modification, are permitted provided that the following conditions are<br>
+ * met:<br>
+ *<br>
+ * * Redistributions of source code must retain the above copyright<br>
+ * notice, this list of conditions and the following disclaimer.<br>
+ *<br>
+ * * Redistributions in binary form must reproduce the above copyright<br>
+ * notice, this list of conditions and the following disclaimer in the<br>
+ * documentation and/or other materials provided with the distribution.<br>
+ *<br>
+ * * Neither the name of Red Hat nor the names of its contributors may be<br>
+ * used to endorse or promote products derived from this software without<br>
+ * specific prior written permission.<br>
+ *<br>
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND<br>
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,<br>
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A<br>
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR<br>
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,<br>
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT<br>
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF<br>
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND<br>
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,<br>
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT<br>
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF<br>
+ * SUCH DAMAGE.<br>
+ */<br>
+<br>
+#ifndef NBDKIT_ISPOWEROF2_H<br>
+#define NBDKIT_ISPOWEROF2_H<br>
+<br>
+#include <stdbool.h><br>
+<br>
+/* Returns true if v is a power of 2.<br>
+ *<br>
+ * Uses the algorithm described at<br>
+ * <a href="http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2" rel="noreferrer" target="_blank">http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2</a><br>
+ */<br>
+static inline bool<br>
+is_power_of_2 (unsigned long v)<br>
+{<br>
+ return v && ((v & (v - 1)) == 0);<br>
+}<br>
+<br>
+#endif /* NBDKIT_ISPOWEROF2_H */<br>
diff --git a/common/include/iszero.h b/common/include/iszero.h<br>
new file mode 100644<br>
index 0000000..331614c<br>
--- /dev/null<br>
+++ b/common/include/iszero.h<br>
@@ -0,0 +1,60 @@<br>
+/* nbdkit<br>
+ * Copyright (C) 2018 Red Hat Inc.<br>
+ * All rights reserved.<br>
+ *<br>
+ * Redistribution and use in source and binary forms, with or without<br>
+ * modification, are permitted provided that the following conditions are<br>
+ * met:<br>
+ *<br>
+ * * Redistributions of source code must retain the above copyright<br>
+ * notice, this list of conditions and the following disclaimer.<br>
+ *<br>
+ * * Redistributions in binary form must reproduce the above copyright<br>
+ * notice, this list of conditions and the following disclaimer in the<br>
+ * documentation and/or other materials provided with the distribution.<br>
+ *<br>
+ * * Neither the name of Red Hat nor the names of its contributors may be<br>
+ * used to endorse or promote products derived from this software without<br>
+ * specific prior written permission.<br>
+ *<br>
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND<br>
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,<br>
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A<br>
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR<br>
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,<br>
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT<br>
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF<br>
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND<br>
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,<br>
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT<br>
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF<br>
+ * SUCH DAMAGE.<br>
+ */<br>
+<br>
+#ifndef NBDKIT_ISZERO_H<br>
+#define NBDKIT_ISZERO_H<br>
+<br>
+#include <string.h><br>
+#include <stdbool.h><br>
+<br>
+/* Return true iff the buffer is all zero bytes.<br>
+ *<br>
+ * The clever approach here was suggested by Eric Blake. See:<br>
+ * <a href="https://www.redhat.com/archives/libguestfs/2017-April/msg00171.html" rel="noreferrer" target="_blank">https://www.redhat.com/archives/libguestfs/2017-April/msg00171.html</a></blockquote><div><br></div><div>It would be nice to mention the original author:</div><div><a href="http://rusty.ozlabs.org/?p=560">http://rusty.ozlabs.org/?p=560</a><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
+ */<br>
+static inline bool<br>
+is_zero (const char *buffer, size_t size)<br>
+{<br>
+ size_t i;<br>
+ const size_t limit = size < 16 ? size : 16;<br>
+<br>
+ for (i = 0; i < limit; ++i)<br>
+ if (buffer[i])<br>
+ return false;<br>
+ if (size != limit)<br>
+ return ! memcmp (buffer, buffer + 16, size - 16);<br>
+<br>
+ return true;<br>
+}<br>
+<br>
+#endif /* NBDKIT_ISZERO_H */<br>
diff --git a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
index b87efb9..774f290 100644<br>
--- a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
+++ b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
@@ -538,6 +538,7 @@ AC_CONFIG_FILES([<a href="http://podwrapper.pl" rel="noreferrer" target="_blank">podwrapper.pl</a>],<br>
[chmod +x,-w <a href="http://podwrapper.pl" rel="noreferrer" target="_blank">podwrapper.pl</a>])<br>
AC_CONFIG_FILES([Makefile<br>
bash/Makefile<br>
+ common/include/Makefile<br>
docs/Makefile<br>
include/Makefile<br>
plugins/Makefile<br>
-- <br>
2.18.0<br>
<br>
_______________________________________________<br>
Libguestfs mailing list<br>
<a href="mailto:Libguestfs@redhat.com" target="_blank">Libguestfs@redhat.com</a><br>
<a href="https://www.redhat.com/mailman/listinfo/libguestfs" rel="noreferrer" target="_blank">https://www.redhat.com/mailman/listinfo/libguestfs</a><br>
</blockquote></div></div>