qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [patch] qemu iovec: keep track of total size, allow partial copies.
Date: Fri, 23 Jan 2009 13:14:02 +0100	[thread overview]
Message-ID: <4979B48A.2070003@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 267 bytes --]

  Hi,

Keeping track of the total size is trivial for the helpers, and it
allows buffer allocation without looping over the iovec once.

qemu_iovec_from_buffer got a length parameter (number of bytes to copy)
so we can handle partial reads correctly.

cheers,
  Gerd

[-- Attachment #2: 0002-qemu-iovec-keep-track-of-total-size-allow-partial.patch --]
[-- Type: text/plain, Size: 2877 bytes --]

>From 520c69da6d2e52b7f8b1e72d86f7b6ee8e8acad0 Mon Sep 17 00:00:00 2001
From: Gerd Hoffmann <kraxel@redhat.com>
Date: Fri, 23 Jan 2009 12:32:56 +0100
Subject: [PATCH 2/3] qemu iovec: keep track of total size, allow partial copies.


Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 block.c       |    2 +-
 cutils.c      |   15 +++++++++++----
 qemu-common.h |    3 ++-
 3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/block.c b/block.c
index 50ec589..b3d2f12 100644
--- a/block.c
+++ b/block.c
@@ -1265,7 +1265,7 @@ static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
     VectorTranslationState *s = opaque;
 
     if (!s->is_write) {
-        qemu_iovec_from_buffer(s->iov, s->bounce);
+        qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
     }
     qemu_free(s->bounce);
     s->this_aiocb->cb(s->this_aiocb->opaque, ret);
diff --git a/cutils.c b/cutils.c
index 80a7a1d..1090aa4 100644
--- a/cutils.c
+++ b/cutils.c
@@ -109,6 +109,7 @@ void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint)
     qiov->iov = qemu_malloc(alloc_hint * sizeof(struct iovec));
     qiov->niov = 0;
     qiov->nalloc = alloc_hint;
+    qiov->size = 0;
 }
 
 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
@@ -119,6 +120,7 @@ void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len)
     }
     qiov->iov[qiov->niov].iov_base = base;
     qiov->iov[qiov->niov].iov_len = len;
+    qiov->size += len;
     ++qiov->niov;
 }
 
@@ -138,13 +140,18 @@ void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf)
     }
 }
 
-void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf)
+void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count)
 {
     const uint8_t *p = (const uint8_t *)buf;
+    size_t copy;
     int i;
 
-    for (i = 0; i < qiov->niov; ++i) {
-        memcpy(qiov->iov[i].iov_base, p, qiov->iov[i].iov_len);
-        p += qiov->iov[i].iov_len;
+    for (i = 0; i < qiov->niov && count; ++i) {
+        copy = count;
+        if (copy > qiov->iov[i].iov_len)
+            copy = qiov->iov[i].iov_len;
+        memcpy(qiov->iov[i].iov_base, p, copy);
+        p     += copy;
+        count -= copy;
     }
 }
diff --git a/qemu-common.h b/qemu-common.h
index ae773e0..42d5e49 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -195,13 +195,14 @@ typedef struct QEMUIOVector {
     struct iovec *iov;
     int niov;
     int nalloc;
+    size_t size;
 } QEMUIOVector;
 
 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
 void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
 void qemu_iovec_destroy(QEMUIOVector *qiov);
 void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf);
-void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf);
+void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf, size_t count);
 
 #endif /* dyngen-exec.h hack */
 
-- 
1.6.1


             reply	other threads:[~2009-01-23 12:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-23 12:14 Gerd Hoffmann [this message]
2009-01-26 17:18 ` [Qemu-devel] [patch] qemu iovec: keep track of total size, allow partial copies Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4979B48A.2070003@redhat.com \
    --to=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).