iovec-util: introduce several helper functions

This commit is contained in:
Yu Watanabe
2026-06-27 14:04:48 +09:00
parent ee6522d036
commit 82ff3f71f5
2 changed files with 29 additions and 0 deletions
+20
View File
@@ -16,6 +16,17 @@ const struct iovec iovec_empty = {
.iov_len = 0,
};
int iovec_alloc(size_t n, struct iovec *ret) {
assert(ret);
void *buf = malloc(n ?: 1);
if (!buf)
return -ENOMEM;
*ret = IOVEC_MAKE(buf, n);
return 0;
}
size_t iovec_total_size(const struct iovec *iovec, size_t n) {
size_t sum = 0;
@@ -63,6 +74,15 @@ struct iovec* iovec_make_string(struct iovec *iovec, const char *s) {
return iovec;
}
void iovec_erase(struct iovec *iovec) {
assert(iovec);
/* Unlike iovec_done_erase(), which derives the buffer size with MALLOC_SIZEOF_SAFE(), this uses
* iov_len as the buffer size. Hence, it can be used with iovec referring to a static array or a
* buffer allocated on the stack. */
explicit_bzero_safe(iovec->iov_base, iovec->iov_len);
}
void iovec_done_erase(struct iovec *iovec) {
assert(iovec);
+9
View File
@@ -10,6 +10,8 @@
extern const struct iovec iovec_nul_byte; /* Points to a single NUL byte */
extern const struct iovec iovec_empty; /* Points to an empty, but valid (i.e. non-NULL) pointer */
int iovec_alloc(size_t n, struct iovec *ret);
size_t iovec_total_size(const struct iovec *iovec, size_t n) _nonnull_if_nonzero_(1, 2);
bool iovec_inc_many(struct iovec *iovec, size_t n, size_t k) _nonnull_if_nonzero_(1, 2);
@@ -31,6 +33,13 @@ struct iovec* iovec_make_string(struct iovec *iovec, const char *s);
.iov_len = 1, \
}
#define IOVEC_ALLOCA(n) \
({ \
size_t _n_ = (n); \
IOVEC_MAKE(alloca_safe(_n_), _n_); \
})
void iovec_erase(struct iovec *iovec);
void iovec_done_erase(struct iovec *iovec);
char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value);