From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NuRvs-0001sj-Dp for qemu-devel@nongnu.org; Wed, 24 Mar 2010 10:51:44 -0400 Received: from [140.186.70.92] (port=60979 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NuRvq-0001rw-Qd for qemu-devel@nongnu.org; Wed, 24 Mar 2010 10:51:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NuRvo-0006Ww-H7 for qemu-devel@nongnu.org; Wed, 24 Mar 2010 10:51:42 -0400 Received: from mx1.redhat.com ([209.132.183.28]:31072) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NuRvo-0006Wi-9e for qemu-devel@nongnu.org; Wed, 24 Mar 2010 10:51:40 -0400 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o2OEpdpk018534 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 24 Mar 2010 10:51:39 -0400 From: Amit Shah Date: Wed, 24 Mar 2010 20:19:31 +0530 Message-Id: <1269442173-18421-14-git-send-email-amit.shah@redhat.com> In-Reply-To: <1269442173-18421-13-git-send-email-amit.shah@redhat.com> References: <1269442173-18421-1-git-send-email-amit.shah@redhat.com> <1269442173-18421-2-git-send-email-amit.shah@redhat.com> <1269442173-18421-3-git-send-email-amit.shah@redhat.com> <1269442173-18421-4-git-send-email-amit.shah@redhat.com> <1269442173-18421-5-git-send-email-amit.shah@redhat.com> <1269442173-18421-6-git-send-email-amit.shah@redhat.com> <1269442173-18421-7-git-send-email-amit.shah@redhat.com> <1269442173-18421-8-git-send-email-amit.shah@redhat.com> <1269442173-18421-9-git-send-email-amit.shah@redhat.com> <1269442173-18421-10-git-send-email-amit.shah@redhat.com> <1269442173-18421-11-git-send-email-amit.shah@redhat.com> <1269442173-18421-12-git-send-email-amit.shah@redhat.com> <1269442173-18421-13-git-send-email-amit.shah@redhat.com> Subject: [Qemu-devel] [PATCH 13/15] iov: Add iov_to_buf and iov_size helpers List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu list Cc: Amit Shah , Juan Quintela , Gerd Hoffmann , "Michael S. Tsirkin" iov_to_buf() puts the buffer contents in the iov in a linearized buffer. iov_size() gets the length of the contents in the iov. The iov_to_buf() function is the memcpy_to_iovec() function that was used in virtio-ballon.c. Signed-off-by: Amit Shah --- hw/iov.c | 37 +++++++++++++++++++++++++++++++++++++ hw/iov.h | 3 +++ hw/virtio-balloon.c | 35 ++++------------------------------- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/hw/iov.c b/hw/iov.c index 07bd499..d4013cd 100644 --- a/hw/iov.c +++ b/hw/iov.c @@ -31,3 +31,40 @@ size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt, } return offset; } + +size_t iov_to_buf(struct iovec *iov, unsigned int iovcnt, + void *buf, size_t offset, size_t size) +{ + uint8_t *ptr; + size_t iov_off, buf_off; + unsigned int i; + + ptr = buf; + iov_off = 0; + buf_off = 0; + for (i = 0; i < iovcnt && size; i++) { + if (offset < (iov_off + iov[i].iov_len)) { + size_t len = MIN((iov_off + iov[i].iov_len) - offset , size); + + memcpy(ptr + buf_off, iov[i].iov_base + (offset - iov_off), len); + + buf_off += len; + offset += len; + size -= len; + } + iov_off += iov[i].iov_len; + } + return buf_off; +} + +size_t iov_size(struct iovec *iov, unsigned int iovcnt) +{ + size_t len; + unsigned int i; + + len = 0; + for (i = 0; i < iovcnt; i++) { + len += iov[i].iov_len; + } + return len; +} diff --git a/hw/iov.h b/hw/iov.h index 5e3e541..c977ff1 100644 --- a/hw/iov.h +++ b/hw/iov.h @@ -14,3 +14,6 @@ size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt, const void *buf, size_t size); +size_t iov_to_buf(struct iovec *iov, unsigned int iovcnt, + void *buf, size_t offset, size_t size); +size_t iov_size(struct iovec *iov, unsigned int iovcnt); diff --git a/hw/virtio-balloon.c b/hw/virtio-balloon.c index 6d12024..4414eae 100644 --- a/hw/virtio-balloon.c +++ b/hw/virtio-balloon.c @@ -11,6 +11,7 @@ * */ +#include "iov.h" #include "qemu-common.h" #include "virtio.h" #include "pc.h" @@ -91,33 +92,6 @@ static QObject *get_stats_qobject(VirtIOBalloon *dev) return QOBJECT(dict); } -/* FIXME: once we do a virtio refactoring, this will get subsumed into common - * code */ -static size_t memcpy_from_iovector(void *data, size_t offset, size_t size, - struct iovec *iov, int iovlen) -{ - int i; - uint8_t *ptr = data; - size_t iov_off = 0; - size_t data_off = 0; - - for (i = 0; i < iovlen && size; i++) { - if (offset < (iov_off + iov[i].iov_len)) { - size_t len = MIN((iov_off + iov[i].iov_len) - offset , size); - - memcpy(ptr + data_off, iov[i].iov_base + (offset - iov_off), len); - - data_off += len; - offset += len; - size -= len; - } - - iov_off += iov[i].iov_len; - } - - return data_off; -} - static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) { VirtIOBalloon *s = to_virtio_balloon(vdev); @@ -127,8 +101,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) size_t offset = 0; uint32_t pfn; - while (memcpy_from_iovector(&pfn, offset, 4, - elem.out_sg, elem.out_num) == 4) { + while (iov_to_buf(elem.out_sg, elem.out_num, &pfn, offset, 4) == 4) { ram_addr_t pa; ram_addr_t addr; @@ -180,8 +153,8 @@ static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) */ reset_stats(s); - while (memcpy_from_iovector(&stat, offset, sizeof(stat), elem->out_sg, - elem->out_num) == sizeof(stat)) { + while (iov_to_buf(elem->out_sg, elem->out_num, &stat, offset, sizeof(stat)) + == sizeof(stat)) { uint16_t tag = tswap16(stat.tag); uint64_t val = tswap64(stat.val); -- 1.6.2.5