Patchwork [RFC,6/9] allow switching a qiov between internal and external storage

login
register
mail settings
Submitter Paolo Bonzini
Date June 6, 2011, 4:26 p.m.
Message ID <1307377620-7538-7-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/98977/
State New
Headers show

Comments

Paolo Bonzini - June 6, 2011, 4:26 p.m.
qemu_iovec_reset and qemu_iovec_destroy will switch from external to
internal storage (it was previously forbidden to call it with external
storage).  So, qemu_iovec_destroy followed by qemu_iovec_init_external
will not leak memory when called on a qiov that already had internal
storage allocated.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 cutils.c |   14 +++++++++++---
 1 files changed, 11 insertions(+), 3 deletions(-)

Patch

diff --git a/cutils.c b/cutils.c
index f9a7e36..0fef64a 100644
--- a/cutils.c
+++ b/cutils.c
@@ -215,14 +215,22 @@  void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size)
 
 void qemu_iovec_destroy(QEMUIOVector *qiov)
 {
-    assert(qiov->nalloc != -1);
+    if (qiov->nalloc != -1) {
+        qemu_free(qiov->iov);
+        qiov->nalloc = 0;
+        qiov->iov = NULL;
+    }
 
-    qemu_free(qiov->iov);
+    qiov->niov = 0;
+    qiov->size = 0;
 }
 
 void qemu_iovec_reset(QEMUIOVector *qiov)
 {
-    assert(qiov->nalloc != -1);
+    if (qiov->nalloc == -1) {
+        qiov->nalloc = 0;
+        qiov->iov = NULL;
+    }
 
     qiov->niov = 0;
     qiov->size = 0;