]> git.ipfire.org Git - thirdparty/qemu.git/blame - include/qemu/iov.h
misc: move include files to include/qemu/
[thirdparty/qemu.git] / include / qemu / iov.h
CommitLineData
e4d5639d 1/*
2278a69e 2 * Helpers for using (partial) iovecs.
e4d5639d
AS
3 *
4 * Copyright (C) 2010 Red Hat, Inc.
5 *
6 * Author(s):
7 * Amit Shah <amit.shah@redhat.com>
2278a69e 8 * Michael Tokarev <mjt@tls.msk.ru>
e4d5639d
AS
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
12 */
13
cb9c377f
PB
14#ifndef IOV_H
15#define IOV_H
16
e4d5639d
AS
17#include "qemu-common.h"
18
dcf6f5e1
MT
19/**
20 * count and return data size, in bytes, of an iovec
21 * starting at `iov' of `iov_cnt' number of elements.
22 */
23size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt);
24
25/**
26 * Copy from single continuous buffer to scatter-gather vector of buffers
27 * (iovec) and back like memcpy() between two continuous memory regions.
28 * Data in single continuous buffer starting at address `buf' and
29 * `bytes' bytes long will be copied to/from an iovec `iov' with
30 * `iov_cnt' number of elements, starting at byte position `offset'
31 * within the iovec. If the iovec does not contain enough space,
32 * only part of data will be copied, up to the end of the iovec.
33 * Number of bytes actually copied will be returned, which is
34 * min(bytes, iov_size(iov)-offset)
2278a69e
MT
35 * `Offset' must point to the inside of iovec.
36 * It is okay to use very large value for `bytes' since we're
37 * limited by the size of the iovec anyway, provided that the
38 * buffer pointed to by buf has enough space. One possible
39 * such "large" value is -1 (sinice size_t is unsigned),
40 * so specifying `-1' as `bytes' means 'up to the end of iovec'.
dcf6f5e1 41 */
844b5cea 42size_t iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
dcf6f5e1 43 size_t offset, const void *buf, size_t bytes);
348e7b8d 44size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
dcf6f5e1
MT
45 size_t offset, void *buf, size_t bytes);
46
47/**
48 * Set data bytes pointed out by iovec `iov' of size `iov_cnt' elements,
49 * starting at byte offset `start', to value `fillc', repeating it
2278a69e 50 * `bytes' number of times. `Offset' must point to the inside of iovec.
dcf6f5e1
MT
51 * If `bytes' is large enough, only last bytes portion of iovec,
52 * up to the end of it, will be filled with the specified value.
53 * Function return actual number of bytes processed, which is
54 * min(size, iov_size(iov) - offset).
2278a69e 55 * Again, it is okay to use large value for `bytes' to mean "up to the end".
dcf6f5e1
MT
56 */
57size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
58 size_t offset, int fillc, size_t bytes);
59
3e80bf93
MT
60/*
61 * Send/recv data from/to iovec buffers directly
62 *
63 * `offset' bytes in the beginning of iovec buffer are skipped and
64 * next `bytes' bytes are used, which must be within data of iovec.
65 *
25e5e4c7 66 * r = iov_send_recv(sockfd, iov, iovcnt, offset, bytes, true);
3e80bf93
MT
67 *
68 * is logically equivalent to
69 *
70 * char *buf = malloc(bytes);
71 * iov_to_buf(iov, iovcnt, offset, buf, bytes);
72 * r = send(sockfd, buf, bytes, 0);
73 * free(buf);
25e5e4c7
MT
74 *
75 * For iov_send_recv() _whole_ area being sent or received
76 * should be within the iovec, not only beginning of it.
3e80bf93 77 */
25e5e4c7 78ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
e3e87df4 79 size_t offset, size_t bytes, bool do_send);
25e5e4c7
MT
80#define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \
81 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false)
82#define iov_send(sockfd, iov, iov_cnt, offset, bytes) \
83 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true)
3e80bf93 84
dcf6f5e1
MT
85/**
86 * Produce a text hexdump of iovec `iov' with `iov_cnt' number of elements
87 * in file `fp', prefixing each line with `prefix' and processing not more
88 * than `limit' data bytes.
89 */
3a1dca94
GH
90void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
91 FILE *fp, const char *prefix, size_t limit);
d336336c
MT
92
93/*
94 * Partial copy of vector from iov to dst_iov (data is not copied).
95 * dst_iov overlaps iov at a specified offset.
96 * size of dst_iov is at most bytes. dst vector count is returned.
97 */
98unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
99 const struct iovec *iov, unsigned int iov_cnt,
100 size_t offset, size_t bytes);
cb9c377f
PB
101
102#endif