summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYaniv Kaul <ykaul@redhat.com>2018-11-16 12:32:22 +0200
committerAmar Tumballi <amarts@redhat.com>2018-11-20 03:11:30 +0000
commit5231d3d165135f7aae8716069c67788555332136 (patch)
treec35f0831d7a100ddb4cd1d32e813abd9cc0b37c6
parentbdf8d8684fa4814cf055350db38f53618c5de6ae (diff)
downloadglusterfs-5231d3d165135f7aae8716069c67788555332136.tar.gz
glusterfs-5231d3d165135f7aae8716069c67788555332136.tar.xz
glusterfs-5231d3d165135f7aae8716069c67788555332136.zip
libglusterfs/src/common-utils.h: faster mem_0filled() function
based on the amusing discussion @ https://rusty.ozlabs.org/?p=560 Compile-tested only! updates: bz#1193929 Signed-off-by: Yaniv Kaul <ykaul@redhat.com> Change-Id: I1cac54067eb44801b216d5620fc5ee2c89befdd0
-rw-r--r--libglusterfs/src/common-utils.h32
1 files changed, 22 insertions, 10 deletions
diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h
index 51e0551540..c08c16cced 100644
--- a/libglusterfs/src/common-utils.h
+++ b/libglusterfs/src/common-utils.h
@@ -645,19 +645,31 @@ iov_copy(const struct iovec *dst, int dcnt, const struct iovec *src, int scnt)
return ret;
}
-static inline int
-mem_0filled(const char *buf, size_t size)
+/* based on the amusing discussion @ https://rusty.ozlabs.org/?p=560 */
+static bool
+memeqzero(const void *data, size_t length)
{
- int i = 0;
- int ret = 0;
-
- for (i = 0; i < size; i++) {
- ret = buf[i];
- if (ret)
- break;
+ const unsigned char *p = data;
+ size_t len;
+
+ /* Check first 16 bytes manually */
+ for (len = 0; len < 16; len++) {
+ if (!length)
+ return true;
+ if (*p)
+ return false;
+ p++;
+ length--;
}
- return ret;
+ /* Now we know that's zero, memcmp with self. */
+ return memcmp(data, p, length) == 0;
+}
+
+static inline int
+mem_0filled(const char *buf, size_t size)
+{
+ return !memeqzero(buf, size);
}
static inline int