summaryrefslogtreecommitdiff
path: root/cutils.c
diff options
context:
space:
mode:
authorStefan Hajnoczi <stefanha@linux.vnet.ibm.com>2012-02-07 13:27:24 +0000
committerKevin Wolf <kwolf@redhat.com>2012-02-09 16:17:50 +0100
commit1a6d39fd71ddf90c5b76026cac4d5ff51fbaf8d8 (patch)
tree9b15c86f85e8141f3a20cbf9f52ea21a0719e598 /cutils.c
parent7a65c8cc315c76bde6d692845c12e7ba06f44f89 (diff)
downloadqemu-1a6d39fd71ddf90c5b76026cac4d5ff51fbaf8d8.tar.gz
qemu-1a6d39fd71ddf90c5b76026cac4d5ff51fbaf8d8.tar.bz2
qemu-1a6d39fd71ddf90c5b76026cac4d5ff51fbaf8d8.zip
cutils: extract buffer_is_zero() from qemu-img.c
The qemu-img.c:is_not_zero() function checks if a buffer contains all zeroes. This function will come in handy for zero-detection in the block layer, so clean it up and move it to cutils.c. Note that the function now returns true if the buffer is all zeroes. This avoids the double-negatives (i.e. !is_not_zero()) that the old function can cause in callers. Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'cutils.c')
-rw-r--r--cutils.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/cutils.c b/cutils.c
index a6ffd46445..af308cd7b9 100644
--- a/cutils.c
+++ b/cutils.c
@@ -303,6 +303,41 @@ void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count,
}
}
+/*
+ * Checks if a buffer is all zeroes
+ *
+ * Attention! The len must be a multiple of 4 * sizeof(long) due to
+ * restriction of optimizations in this function.
+ */
+bool buffer_is_zero(const void *buf, size_t len)
+{
+ /*
+ * Use long as the biggest available internal data type that fits into the
+ * CPU register and unroll the loop to smooth out the effect of memory
+ * latency.
+ */
+
+ size_t i;
+ long d0, d1, d2, d3;
+ const long * const data = buf;
+
+ assert(len % (4 * sizeof(long)) == 0);
+ len /= sizeof(long);
+
+ for (i = 0; i < len; i += 4) {
+ d0 = data[i + 0];
+ d1 = data[i + 1];
+ d2 = data[i + 2];
+ d3 = data[i + 3];
+
+ if (d0 || d1 || d2 || d3) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
#ifndef _WIN32
/* Sets a specific flag */
int fcntl_setfl(int fd, int flag)