summaryrefslogtreecommitdiff
path: root/qemu-img.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 /qemu-img.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 'qemu-img.c')
-rw-r--r--qemu-img.c46
1 files changed, 7 insertions, 39 deletions
diff --git a/qemu-img.c b/qemu-img.c
index 01cc0d35ad..c4bcf41e15 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -515,40 +515,6 @@ static int img_commit(int argc, char **argv)
}
/*
- * Checks whether the sector is not a zero sector.
- *
- * Attention! The len must be a multiple of 4 * sizeof(long) due to
- * restriction of optimizations in this function.
- */
-static int is_not_zero(const uint8_t *sector, int 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.
- */
-
- int i;
- long d0, d1, d2, d3;
- const long * const data = (const long *) sector;
-
- 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 1;
- }
- }
-
- return 0;
-}
-
-/*
* Returns true iff the first sector pointed to by 'buf' contains at least
* a non-NUL byte.
*
@@ -557,20 +523,22 @@ static int is_not_zero(const uint8_t *sector, int len)
*/
static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
{
- int v, i;
+ bool is_zero;
+ int i;
if (n <= 0) {
*pnum = 0;
return 0;
}
- v = is_not_zero(buf, 512);
+ is_zero = buffer_is_zero(buf, 512);
for(i = 1; i < n; i++) {
buf += 512;
- if (v != is_not_zero(buf, 512))
+ if (is_zero != buffer_is_zero(buf, 512)) {
break;
+ }
}
*pnum = i;
- return v;
+ return !is_zero;
}
/*
@@ -955,7 +923,7 @@ static int img_convert(int argc, char **argv)
if (n < cluster_sectors) {
memset(buf + n * 512, 0, cluster_size - n * 512);
}
- if (is_not_zero(buf, cluster_size)) {
+ if (!buffer_is_zero(buf, cluster_size)) {
ret = bdrv_write_compressed(out_bs, sector_num, buf,
cluster_sectors);
if (ret != 0) {