iov: Introduce a new file for helpers around iovs, add iov_from_buf()
[qemu.git] / hw / iov.c
blob07bd499b0d0928edc3b519a7364ecebffe6b4cab
1 /*
2 * Helpers for getting linearized buffers from iov / filling buffers into iovs
4 * Copyright IBM, Corp. 2007, 2008
5 * Copyright (C) 2010 Red Hat, Inc.
7 * Author(s):
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Amit Shah <amit.shah@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
15 #include "iov.h"
17 size_t iov_from_buf(struct iovec *iov, unsigned int iovcnt,
18 const void *buf, size_t size)
20 size_t offset;
21 unsigned int i;
23 offset = 0;
24 for (i = 0; offset < size && i < iovcnt; i++) {
25 size_t len;
27 len = MIN(iov[i].iov_len, size - offset);
29 memcpy(iov[i].iov_base, buf + offset, len);
30 offset += len;
32 return offset;