diff options
Diffstat (limited to 'cutils.c')
-rw-r--r-- | cutils.c | 47 |
1 files changed, 47 insertions, 0 deletions
@@ -101,3 +101,50 @@ int qemu_fls(int i) { return 32 - clz32(i); } + +/* io vectors */ + +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; +} + +void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len) +{ + if (qiov->niov == qiov->nalloc) { + qiov->nalloc = 2 * qiov->nalloc + 1; + qiov->iov = qemu_realloc(qiov->iov, qiov->nalloc * sizeof(struct iovec)); + } + qiov->iov[qiov->niov].iov_base = base; + qiov->iov[qiov->niov].iov_len = len; + ++qiov->niov; +} + +void qemu_iovec_destroy(QEMUIOVector *qiov) +{ + qemu_free(qiov->iov); +} + +void qemu_iovec_to_buffer(QEMUIOVector *qiov, void *buf) +{ + uint8_t *p = (uint8_t *)buf; + int i; + + for (i = 0; i < qiov->niov; ++i) { + memcpy(p, qiov->iov[i].iov_base, qiov->iov[i].iov_len); + p += qiov->iov[i].iov_len; + } +} + +void qemu_iovec_from_buffer(QEMUIOVector *qiov, const void *buf) +{ + const uint8_t *p = (const uint8_t *)buf; + 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; + } +} |