From patchwork Sat Mar 10 16:32:26 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 145864 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 270BDB6FB6 for ; Sun, 11 Mar 2012 03:33:21 +1100 (EST) Received: from localhost ([::1]:55948 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S6PEN-00005n-1P for incoming@patchwork.ozlabs.org; Sat, 10 Mar 2012 11:33:19 -0500 Received: from eggs.gnu.org ([208.118.235.92]:58781) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S6PEA-0008CK-Rr for qemu-devel@nongnu.org; Sat, 10 Mar 2012 11:33:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S6PE7-0006N1-8c for qemu-devel@nongnu.org; Sat, 10 Mar 2012 11:33:06 -0500 Received: from isrv.corpit.ru ([86.62.121.231]:49434) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S6PE6-0006Mc-TE for qemu-devel@nongnu.org; Sat, 10 Mar 2012 11:33:03 -0500 Received: from gandalf.tls.msk.ru (mjt.vpn.tls.msk.ru [192.168.177.99]) by isrv.corpit.ru (Postfix) with ESMTP id 560AEA0728; Sat, 10 Mar 2012 20:33:01 +0400 (MSK) Received: by gandalf.tls.msk.ru (Postfix, from userid 1000) id 55B812BB3; Sat, 10 Mar 2012 20:33:00 +0400 (MSK) From: Michael Tokarev To: qemu-devel@nongnu.org Date: Sat, 10 Mar 2012 20:32:26 +0400 Message-Id: <1331397150-25700-2-git-send-email-mjt@msgid.tls.msk.ru> X-Mailer: git-send-email 1.7.9.1 In-Reply-To: <1331397150-25700-1-git-send-email-mjt@tls.msk.ru> References: <1331397150-25700-1-git-send-email-mjt@tls.msk.ru> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 86.62.121.231 Cc: Michael Tokarev Subject: [Qemu-devel] [PATCH 1/5] Consolidate qemu_iovec_memset{, _skip}() into single, simplified function 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 This patch combines two functions into one, simplifies the implementation and adds some assert()s into place. Signed-off-by: Michael Tokarev --- block/qcow2.c | 4 +- block/qed.c | 4 +- cutils.c | 54 +++++++++++++++------------------------------------ linux-aio.c | 2 +- posix-aio-compat.c | 2 +- qemu-common.h | 4 +-- 6 files changed, 23 insertions(+), 47 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index eb5ea48..b39bdcc 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -406,7 +406,7 @@ int qcow2_backing_read1(BlockDriverState *bs, QEMUIOVector *qiov, else n1 = bs->total_sectors - sector_num; - qemu_iovec_memset_skip(qiov, 0, 512 * (nb_sectors - n1), 512 * n1); + qemu_iovec_memset(qiov, 0, 512 * (nb_sectors - n1), 512 * n1); return n1; } @@ -466,7 +466,7 @@ static coroutine_fn int qcow2_co_readv(BlockDriverState *bs, int64_t sector_num, } } else { /* Note: in this case, no need to wait */ - qemu_iovec_memset(&hd_qiov, 0, 512 * cur_nr_sectors); + qemu_iovec_memset(&hd_qiov, 0, 512 * cur_nr_sectors, 0); } } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) { /* add AIO support for compressed blocks ? */ diff --git a/block/qed.c b/block/qed.c index a041d31..427cf13 100644 --- a/block/qed.c +++ b/block/qed.c @@ -738,7 +738,7 @@ static void qed_read_backing_file(BDRVQEDState *s, uint64_t pos, /* Zero all sectors if reading beyond the end of the backing file */ if (pos >= backing_length || pos + qiov->size > backing_length) { - qemu_iovec_memset(qiov, 0, qiov->size); + qemu_iovec_memset(qiov, 0, qiov->size, 0); } /* Complete now if there are no backing file sectors to read */ @@ -1253,7 +1253,7 @@ static void qed_aio_read_data(void *opaque, int ret, /* Handle zero cluster and backing file reads */ if (ret == QED_CLUSTER_ZERO) { - qemu_iovec_memset(&acb->cur_qiov, 0, acb->cur_qiov.size); + qemu_iovec_memset(&acb->cur_qiov, 0, acb->cur_qiov.size, 0); qed_aio_next_io(acb, 0); return; } else if (ret != QED_CLUSTER_FOUND) { diff --git a/cutils.c b/cutils.c index af308cd..27687c8 100644 --- a/cutils.c +++ b/cutils.c @@ -260,46 +260,24 @@ void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count) } } -void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t count) +void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t bytes, size_t offset) { - size_t n; - int i; - - for (i = 0; i < qiov->niov && count; ++i) { - n = MIN(count, qiov->iov[i].iov_len); - memset(qiov->iov[i].iov_base, c, n); - count -= n; + struct iovec *iov = qiov->iov; + unsigned i; + assert(qiov->size >= offset); + assert(qiov->size - offset >= bytes); + + /* first skip initial full-sized elements */ + for(i = 0; offset >= iov[i].iov_len; ++i) { + offset -= iov[i].iov_len; } -} - -void qemu_iovec_memset_skip(QEMUIOVector *qiov, int c, size_t count, - size_t skip) -{ - int i; - size_t done; - void *iov_base; - uint64_t iov_len; - - done = 0; - for (i = 0; (i < qiov->niov) && (done != count); i++) { - if (skip >= qiov->iov[i].iov_len) { - /* Skip the whole iov */ - skip -= qiov->iov[i].iov_len; - continue; - } else { - /* Skip only part (or nothing) of the iov */ - iov_base = (uint8_t*) qiov->iov[i].iov_base + skip; - iov_len = qiov->iov[i].iov_len - skip; - skip = 0; - } - - if (done + iov_len > count) { - memset(iov_base, c, count - done); - break; - } else { - memset(iov_base, c, iov_len); - } - done += iov_len; + /* skip/memset partial element and memset the rest */ + while(bytes) { + size_t n = MIN(bytes, iov[i].iov_len - offset); + memset(iov[i].iov_base + offset, c, n); + bytes -= n; + ++i; + offset = 0; } } diff --git a/linux-aio.c b/linux-aio.c index d2fc2e7..303f410 100644 --- a/linux-aio.c +++ b/linux-aio.c @@ -64,7 +64,7 @@ static void qemu_laio_process_completion(struct qemu_laio_state *s, } else if (ret >= 0) { /* Short reads mean EOF, pad with zeros. */ if (laiocb->is_read) { - qemu_iovec_memset_skip(laiocb->qiov, 0, + qemu_iovec_memset(laiocb->qiov, 0, laiocb->qiov->size - ret, ret); } else { ret = -EINVAL; diff --git a/posix-aio-compat.c b/posix-aio-compat.c index d311d13..84aafed 100644 --- a/posix-aio-compat.c +++ b/posix-aio-compat.c @@ -355,7 +355,7 @@ static void *aio_thread(void *unused) qemu_iovec_init_external(&qiov, aiocb->aio_iov, aiocb->aio_niov); - qemu_iovec_memset_skip(&qiov, 0, aiocb->aio_nbytes - ret, ret); + qemu_iovec_memset(&qiov, 0, aiocb->aio_nbytes - ret, ret); ret = aiocb->aio_nbytes; } diff --git a/qemu-common.h b/qemu-common.h index dbfce6f..b7e426e 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -340,9 +340,7 @@ void qemu_iovec_destroy(QEMUIOVector *qiov); void qemu_iovec_reset(QEMUIOVector *qiov); void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf); void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count); -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); +void qemu_iovec_memset(QEMUIOVector *qiov, int c, size_t bytes, size_t offset); bool buffer_is_zero(const void *buf, size_t len);