1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef IOV_H
15#define IOV_H
16
17
18
19
20
21size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt);
22
23
24
25
26
27
28
29
30
31
32
33
34
35size_t iov_from_buf_full(const struct iovec *iov, unsigned int iov_cnt,
36 size_t offset, const void *buf, size_t bytes);
37size_t iov_to_buf_full(const struct iovec *iov, const unsigned int iov_cnt,
38 size_t offset, void *buf, size_t bytes);
39
40static inline size_t
41iov_from_buf(const struct iovec *iov, unsigned int iov_cnt,
42 size_t offset, const void *buf, size_t bytes)
43{
44 if (__builtin_constant_p(bytes) && iov_cnt &&
45 offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) {
46 memcpy(iov[0].iov_base + offset, buf, bytes);
47 return bytes;
48 } else {
49 return iov_from_buf_full(iov, iov_cnt, offset, buf, bytes);
50 }
51}
52
53static inline size_t
54iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
55 size_t offset, void *buf, size_t bytes)
56{
57 if (__builtin_constant_p(bytes) && iov_cnt &&
58 offset <= iov[0].iov_len && bytes <= iov[0].iov_len - offset) {
59 memcpy(buf, iov[0].iov_base + offset, bytes);
60 return bytes;
61 } else {
62 return iov_to_buf_full(iov, iov_cnt, offset, buf, bytes);
63 }
64}
65
66
67
68
69
70
71
72
73
74
75size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
76 size_t offset, int fillc, size_t bytes);
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96ssize_t iov_send_recv(int sockfd, const struct iovec *iov, unsigned iov_cnt,
97 size_t offset, size_t bytes, bool do_send);
98#define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \
99 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false)
100#define iov_send(sockfd, iov, iov_cnt, offset, bytes) \
101 iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true)
102
103
104
105
106
107
108void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
109 FILE *fp, const char *prefix, size_t limit);
110
111
112
113
114
115
116unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt,
117 const struct iovec *iov, unsigned int iov_cnt,
118 size_t offset, size_t bytes);
119
120
121
122
123
124
125
126
127
128size_t iov_discard_front(struct iovec **iov, unsigned int *iov_cnt,
129 size_t bytes);
130size_t iov_discard_back(struct iovec *iov, unsigned int *iov_cnt,
131 size_t bytes);
132
133typedef struct QEMUIOVector {
134 struct iovec *iov;
135 int niov;
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 union {
152 struct {
153 int nalloc;
154 struct iovec local_iov;
155 };
156 struct {
157 char __pad[sizeof(int) + offsetof(struct iovec, iov_len)];
158 size_t size;
159 };
160 };
161} QEMUIOVector;
162
163QEMU_BUILD_BUG_ON(offsetof(QEMUIOVector, size) !=
164 offsetof(QEMUIOVector, local_iov.iov_len));
165
166#define QEMU_IOVEC_INIT_BUF(self, buf, len) \
167{ \
168 .iov = &(self).local_iov, \
169 .niov = 1, \
170 .nalloc = -1, \
171 .local_iov = { \
172 .iov_base = (void *)(buf), \
173 .iov_len = (len), \
174 }, \
175}
176
177
178
179
180
181
182
183
184
185
186static inline void qemu_iovec_init_buf(QEMUIOVector *qiov,
187 const void *buf, size_t len)
188{
189 *qiov = (QEMUIOVector) QEMU_IOVEC_INIT_BUF(*qiov, buf, len);
190}
191
192static inline void *qemu_iovec_buf(QEMUIOVector *qiov)
193{
194
195 assert(qiov->nalloc == -1 && qiov->iov == &qiov->local_iov);
196
197 return qiov->local_iov.iov_base;
198}
199
200void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint);
201void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int niov);
202void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len);
203void qemu_iovec_concat(QEMUIOVector *dst,
204 QEMUIOVector *src, size_t soffset, size_t sbytes);
205size_t qemu_iovec_concat_iov(QEMUIOVector *dst,
206 struct iovec *src_iov, unsigned int src_cnt,
207 size_t soffset, size_t sbytes);
208bool qemu_iovec_is_zero(QEMUIOVector *qiov);
209void qemu_iovec_destroy(QEMUIOVector *qiov);
210void qemu_iovec_reset(QEMUIOVector *qiov);
211size_t qemu_iovec_to_buf(QEMUIOVector *qiov, size_t offset,
212 void *buf, size_t bytes);
213size_t qemu_iovec_from_buf(QEMUIOVector *qiov, size_t offset,
214 const void *buf, size_t bytes);
215size_t qemu_iovec_memset(QEMUIOVector *qiov, size_t offset,
216 int fillc, size_t bytes);
217ssize_t qemu_iovec_compare(QEMUIOVector *a, QEMUIOVector *b);
218void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf);
219void qemu_iovec_discard_back(QEMUIOVector *qiov, size_t bytes);
220
221#endif
222