[Libguestfs] [PATCH v2 2/2] lib: Reimplement is_zero.

Richard W.M. Jones rjones at redhat.com
Wed Apr 19 16:46:39 UTC 2017


As suggested by Eric Blake.
---
 lib/guestfs-internal-all.h | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/lib/guestfs-internal-all.h b/lib/guestfs-internal-all.h
index 4f7433332..7da93828e 100644
--- a/lib/guestfs-internal-all.h
+++ b/lib/guestfs-internal-all.h
@@ -32,6 +32,8 @@
 #ifndef GUESTFS_INTERNAL_ALL_H_
 #define GUESTFS_INTERNAL_ALL_H_
 
+#include <string.h>
+
 /* This is also defined in <guestfs.h>, so don't redefine it. */
 #if defined(__GNUC__) && !defined(GUESTFS_GCC_VERSION)
 # define GUESTFS_GCC_VERSION \
@@ -89,20 +91,18 @@
 #define xdr_uint32_t xdr_u_int32_t
 #endif
 
-/* Return true iff the buffer is all zero bytes.
- *
- * Note that gcc is smart enough to optimize this properly:
- * http://stackoverflow.com/questions/1493936/faster-means-of-checking-for-an-empty-buffer-in-c/1493989#1493989
- */
+/* Return true iff the buffer is all zero bytes. */
 static inline int
 is_zero (const char *buffer, size_t size)
 {
   size_t i;
+  const size_t limit = MIN (size, 16);
 
-  for (i = 0; i < size; ++i) {
-    if (buffer[i] != 0)
+  for (i = 0; i < limit; ++i)
+    if (buffer[i])
       return 0;
-  }
+  if (size != limit)
+    return !memcmp (buffer, buffer + 16, size - 16);
 
   return 1;
 }
-- 
2.12.0




More information about the Libguestfs mailing list