]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
iovec-util: introduce several helper functions
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 23 Jun 2026 13:37:37 +0000 (22:37 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 27 Jun 2026 05:04:48 +0000 (14:04 +0900)
src/basic/iovec-util.c
src/basic/iovec-util.h

index 429f08dea9ad44a9f0bda6bf0490f0ddc1afa1a3..0be326a18364e75ed4e1e919a360b1f0cb9c8a5b 100644 (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);
 
index 35cdce6a76ed1877f6ea814a6bcd0a422cd86460..ade2e6be3449762b119e9ae4d8bd1c69abbb9df2 100644 (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);