Commit 522584a57bccbb78ea14799263f50730d4dc8583
1 parent
cef3017c
add qemu_iovec_init_external (Christoph Hellwig)
Allow to initialize a QEMUIOVector from an externally allocated iovec. qiov->nalloc is initialized to -1 to indicate external storage for qiov->iov and all functions dealing with memory management assert on the iovec beeing an internally managed first. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com> git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6902 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
2 changed files
with
20 additions
and
0 deletions
cutils.c
@@ -23,6 +23,7 @@ | @@ -23,6 +23,7 @@ | ||
23 | */ | 23 | */ |
24 | #include "qemu-common.h" | 24 | #include "qemu-common.h" |
25 | #include "host-utils.h" | 25 | #include "host-utils.h" |
26 | +#include <assert.h> | ||
26 | 27 | ||
27 | void pstrcpy(char *buf, int buf_size, const char *str) | 28 | void pstrcpy(char *buf, int buf_size, const char *str) |
28 | { | 29 | { |
@@ -112,8 +113,22 @@ void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint) | @@ -112,8 +113,22 @@ void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint) | ||
112 | qiov->size = 0; | 113 | qiov->size = 0; |
113 | } | 114 | } |
114 | 115 | ||
116 | +void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov) | ||
117 | +{ | ||
118 | + int i; | ||
119 | + | ||
120 | + qiov->iov = iov; | ||
121 | + qiov->niov = niov; | ||
122 | + qiov->nalloc = -1; | ||
123 | + qiov->size = 0; | ||
124 | + for (i = 0; i < niov; i++) | ||
125 | + qiov->size += iov[i].iov_len; | ||
126 | +} | ||
127 | + | ||
115 | void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) | 128 | void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) |
116 | { | 129 | { |
130 | + assert(qiov->nalloc != -1); | ||
131 | + | ||
117 | if (qiov->niov == qiov->nalloc) { | 132 | if (qiov->niov == qiov->nalloc) { |
118 | qiov->nalloc = 2 * qiov->nalloc + 1; | 133 | qiov->nalloc = 2 * qiov->nalloc + 1; |
119 | qiov->iov = qemu_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec)); | 134 | qiov->iov = qemu_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec)); |
@@ -126,11 +141,15 @@ void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) | @@ -126,11 +141,15 @@ void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) | ||
126 | 141 | ||
127 | void qemu_iovec_destroy(QEMUIOVector *qiov) | 142 | void qemu_iovec_destroy(QEMUIOVector *qiov) |
128 | { | 143 | { |
144 | + assert(qiov->nalloc != -1); | ||
145 | + | ||
129 | qemu_free(qiov->iov); | 146 | qemu_free(qiov->iov); |
130 | } | 147 | } |
131 | 148 | ||
132 | void qemu_iovec_reset(QEMUIOVector *qiov) | 149 | void qemu_iovec_reset(QEMUIOVector *qiov) |
133 | { | 150 | { |
151 | + assert(qiov->nalloc != -1); | ||
152 | + | ||
134 | qiov->niov = 0; | 153 | qiov->niov = 0; |
135 | qiov->size = 0; | 154 | qiov->size = 0; |
136 | } | 155 | } |
qemu-common.h
@@ -194,6 +194,7 @@ typedef struct QEMUIOVector { | @@ -194,6 +194,7 @@ typedef struct QEMUIOVector { | ||
194 | } QEMUIOVector; | 194 | } QEMUIOVector; |
195 | 195 | ||
196 | void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint); | 196 | void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint); |
197 | +void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov); | ||
197 | void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len); | 198 | void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len); |
198 | void qemu_iovec_destroy(QEMUIOVector *qiov); | 199 | void qemu_iovec_destroy(QEMUIOVector *qiov); |
199 | void qemu_iovec_reset(QEMUIOVector *qiov); | 200 | void qemu_iovec_reset(QEMUIOVector *qiov); |