From: Yu Watanabe Date: Tue, 23 Jun 2026 13:37:37 +0000 (+0900) Subject: iovec-util: introduce several helper functions X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=82ff3f71f53b2be33e4086ca799e40331c52e741;p=thirdparty%2Fsystemd.git iovec-util: introduce several helper functions --- diff --git a/src/basic/iovec-util.c b/src/basic/iovec-util.c index 429f08dea9a..0be326a1836 100644 --- a/src/basic/iovec-util.c +++ b/src/basic/iovec-util.c @@ -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); diff --git a/src/basic/iovec-util.h b/src/basic/iovec-util.h index 35cdce6a76e..ade2e6be344 100644 --- a/src/basic/iovec-util.h +++ b/src/basic/iovec-util.h @@ -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);