From 5231d3d165135f7aae8716069c67788555332136 Mon Sep 17 00:00:00 2001 From: Yaniv Kaul Date: Fri, 16 Nov 2018 12:32:22 +0200 Subject: 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 Change-Id: I1cac54067eb44801b216d5620fc5ee2c89befdd0 --- libglusterfs/src/common-utils.h | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'libglusterfs') diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h index 51e05515401..c08c16ccedb 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 -- cgit