From patchwork Tue Feb 7 13:27:24 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 139921 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1F20D1007D3 for ; Wed, 8 Feb 2012 00:28:01 +1100 (EST) Received: from localhost ([::1]:34501 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rul5R-0007Cf-G0 for incoming@patchwork.ozlabs.org; Tue, 07 Feb 2012 08:27:57 -0500 Received: from eggs.gnu.org ([140.186.70.92]:47338) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rul5M-0007CO-0b for qemu-devel@nongnu.org; Tue, 07 Feb 2012 08:27:53 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Rul5E-0005E5-3Q for qemu-devel@nongnu.org; Tue, 07 Feb 2012 08:27:51 -0500 Received: from e06smtp16.uk.ibm.com ([195.75.94.112]:33967) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rul5D-0005DF-Qv for qemu-devel@nongnu.org; Tue, 07 Feb 2012 08:27:44 -0500 Received: from /spool/local by e06smtp16.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 7 Feb 2012 13:27:40 -0000 Received: from d06nrmr1507.portsmouth.uk.ibm.com (9.149.38.233) by e06smtp16.uk.ibm.com (192.168.101.146) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 7 Feb 2012 13:27:39 -0000 Received: from d06av06.portsmouth.uk.ibm.com (d06av06.portsmouth.uk.ibm.com [9.149.37.217]) by d06nrmr1507.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q17DRcO12584716 for ; Tue, 7 Feb 2012 13:27:38 GMT Received: from d06av06.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av06.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q17DRbTB023350 for ; Tue, 7 Feb 2012 06:27:38 -0700 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av06.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q17DRbZu023345; Tue, 7 Feb 2012 06:27:37 -0700 From: Stefan Hajnoczi To: Date: Tue, 7 Feb 2012 13:27:24 +0000 Message-Id: <1328621250-8130-2-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.8.3 In-Reply-To: <1328621250-8130-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1328621250-8130-1-git-send-email-stefanha@linux.vnet.ibm.com> x-cbid: 12020713-3548-0000-0000-000000F852E9 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.112 Cc: Kevin Wolf , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v5 1/6] cutils: extract buffer_is_zero() from qemu-img.c X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- cutils.c | 35 +++++++++++++++++++++++++++++++++++ qemu-common.h | 2 ++ qemu-img.c | 46 +++++++--------------------------------------- 3 files changed, 44 insertions(+), 39 deletions(-) diff --git a/cutils.c b/cutils.c index a6ffd46..af308cd 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) diff --git a/qemu-common.h b/qemu-common.h index 8b69a9e..2cd3bf4 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -334,6 +334,8 @@ void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count); void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count, size_t skip); +bool buffer_is_zero(const void *buf, size_t len); + void qemu_progress_init(int enabled, float min_skip); void qemu_progress_end(void); void qemu_progress_print(float delta, int max); diff --git a/qemu-img.c b/qemu-img.c index 01cc0d3..c4bcf41 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) {