net: asynchronous send/receive infrastructure for net/socket.c
[qemu.git] / iov.h
blob381f37a5463f189230ff382b53c4bcf0d7b96df9
1 /*
2 * Helpers for using (partial) iovecs.
4 * Copyright (C) 2010 Red Hat, Inc.
6 * Author(s):
7 * Amit Shah <amit.shah@redhat.com>
8 * Michael Tokarev <mjt@tls.msk.ru>
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
14 #include "qemu-common.h"
16 /**
17 * count and return data size, in bytes, of an iovec
18 * starting at `iov' of `iov_cnt' number of elements.
20 size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt);
22 /**
23 * Copy from single continuous buffer to scatter-gather vector of buffers
24 * (iovec) and back like memcpy() between two continuous memory regions.
25 * Data in single continuous buffer starting at address `buf' and
26 * `bytes' bytes long will be copied to/from an iovec `iov' with
27 * `iov_cnt' number of elements, starting at byte position `offset'
28 * within the iovec. If the iovec does not contain enough space,
29 * only part of data will be copied, up to the end of the iovec.
30 * Number of bytes actually copied will be returned, which is
31 * min(bytes, iov_size(iov)-offset)
32 * `Offset' must point to the inside of iovec.
33 * It is okay to use very large value for `bytes' since we're
34 * limited by the size of the iovec anyway, provided that the
35 * buffer pointed to by buf has enough space. One possible
36 * such "large" value is -1 (sinice size_t is unsigned),
37 * so specifying `-1' as `bytes' means 'up to the end of iovec'.
39 size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt,
40 size_t offset, const void *buf, size_t bytes);
41 size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
42 size_t offset, void *buf, size_t bytes);
44 /**
45 * Set data bytes pointed out by iovec `iov' of size `iov_cnt' elements,
46 * starting at byte offset `start', to value `fillc', repeating it
47 * `bytes' number of times. `Offset' must point to the inside of iovec.
48 * If `bytes' is large enough, only last bytes portion of iovec,
49 * up to the end of it, will be filled with the specified value.
50 * Function return actual number of bytes processed, which is
51 * min(size, iov_size(iov) - offset).
52 * Again, it is okay to use large value for `bytes' to mean "up to the end".
54 size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
55 size_t offset, int fillc, size_t bytes);
58 * Send/recv data from/to iovec buffers directly
60 * `offset' bytes in the beginning of iovec buffer are skipped and
61 * next `bytes' bytes are used, which must be within data of iovec.
63 * r = iov_send_recv(sockfd, iov, iovcnt, offset, bytes, true);
65 * is logically equivalent to
67 * char *buf = malloc(bytes);
68 * iov_to_buf(iov, iovcnt, offset, buf, bytes);
69 * r = send(sockfd, buf, bytes, 0);
70 * free(buf);
72 * For iov_send_recv() _whole_ area being sent or received
73 * should be within the iovec, not only beginning of it.
75 ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
76 size_t offset, size_t bytes, bool do_send);
77 #define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \
78 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false)
79 #define iov_send(sockfd, iov, iov_cnt, offset, bytes) \
80 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true)
82 /**
83 * Produce a text hexdump of iovec `iov' with `iov_cnt' number of elements
84 * in file `fp', prefixing each line with `prefix' and processing not more
85 * than `limit' data bytes.
87 void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
88 FILE *fp, const char *prefix, size_t limit);