]>
Commit | Line | Data |
---|---|---|
bd1ae178 LP |
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | ||
3 | #include "iovec-util.h" | |
4 | #include "string-util.h" | |
5 | ||
1ca0b482 | 6 | size_t iovec_total_size(const struct iovec *iovec, size_t n) { |
c24e0dbe LP |
7 | size_t sum = 0; |
8 | ||
1ca0b482 | 9 | assert(iovec || n == 0); |
c24e0dbe | 10 | |
1ca0b482 | 11 | FOREACH_ARRAY(j, iovec, n) |
c24e0dbe LP |
12 | sum += j->iov_len; |
13 | ||
14 | return sum; | |
15 | } | |
bd1ae178 | 16 | |
1ca0b482 ZJS |
17 | bool iovec_increment(struct iovec *iovec, size_t n, size_t k) { |
18 | assert(iovec || n == 0); | |
986235a9 LP |
19 | |
20 | /* Returns true if there is nothing else to send (bytes written cover all of the iovec), | |
21 | * false if there's still work to do. */ | |
22 | ||
1ca0b482 | 23 | FOREACH_ARRAY(j, iovec, n) { |
986235a9 LP |
24 | size_t sub; |
25 | ||
26 | if (j->iov_len == 0) | |
27 | continue; | |
28 | if (k == 0) | |
29 | return false; | |
30 | ||
31 | sub = MIN(j->iov_len, k); | |
32 | j->iov_len -= sub; | |
33 | j->iov_base = (uint8_t*) j->iov_base + sub; | |
34 | k -= sub; | |
35 | } | |
36 | ||
37 | assert(k == 0); /* Anything else would mean that we wrote more bytes than available, | |
38 | * or the kernel reported writing more bytes than sent. */ | |
39 | return true; | |
40 | } | |
41 | ||
bd1ae178 LP |
42 | char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) { |
43 | char *x; | |
44 | ||
45 | assert(iovec); | |
46 | assert(n_iovec); | |
47 | ||
48 | x = strjoin(field, value); | |
49 | if (x) | |
50 | iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x); | |
51 | return x; | |
52 | } | |
53 | ||
54 | char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) { | |
55 | char *x; | |
56 | ||
57 | assert(iovec); | |
58 | assert(n_iovec); | |
59 | ||
60 | x = set_iovec_string_field(iovec, n_iovec, field, value); | |
61 | free(value); | |
62 | return x; | |
63 | } | |
64 | ||
ba354c16 MY |
65 | void iovec_array_free(struct iovec *iovec, size_t n_iovec) { |
66 | assert(iovec || n_iovec == 0); | |
67 | ||
68 | FOREACH_ARRAY(i, iovec, n_iovec) | |
1dd33bf3 | 69 | free(i->iov_base); |
bd1ae178 | 70 | |
1ca0b482 | 71 | free(iovec); |
bd1ae178 | 72 | } |