iovec-util: rename iovec_increment() -> iovec_inc_many()

Also,
- use iovec_inc() in the loop,
- do not return true when input is NULL or all iovec are empty.
This commit is contained in:
Yu Watanabe
2026-04-19 16:17:47 +09:00
parent fa021e5751
commit 5034852c6c
7 changed files with 67 additions and 12 deletions
+8 -7
View File
@@ -30,29 +30,30 @@ size_t iovec_total_size(const struct iovec *iovec, size_t n) {
return sum;
}
bool iovec_increment(struct iovec *iovec, size_t n, size_t k) {
bool iovec_inc_many(struct iovec *iovec, size_t n, size_t k) {
assert(iovec || n == 0);
/* Returns true if there is nothing else to send (bytes written cover all of the iovec),
* false if there's still work to do. */
bool have = false;
FOREACH_ARRAY(j, iovec, n) {
size_t sub;
if (j->iov_len == 0)
continue;
if (k == 0)
return false;
sub = MIN(j->iov_len, k);
j->iov_len -= sub;
j->iov_base = (uint8_t*) j->iov_base + sub;
size_t sub = MIN(j->iov_len, k);
iovec_inc(j, sub);
k -= sub;
have = have || iovec_is_set(j);
}
assert(k == 0); /* Anything else would mean that we wrote more bytes than available,
* or the kernel reported writing more bytes than sent. */
return true;
return !have;
}
struct iovec* iovec_make_string(struct iovec *iovec, const char *s) {
+1 -1
View File
@@ -11,7 +11,7 @@ extern const struct iovec iovec_empty; /* Points to an empty, but valid (i.e.
size_t iovec_total_size(const struct iovec *iovec, size_t n) _nonnull_if_nonzero_(1, 2);
bool iovec_increment(struct iovec *iovec, size_t n, size_t k) _nonnull_if_nonzero_(1, 2);
bool iovec_inc_many(struct iovec *iovec, size_t n, size_t k) _nonnull_if_nonzero_(1, 2);
struct iovec* iovec_make_string(struct iovec *iovec, const char *s);
+1 -1
View File
@@ -557,7 +557,7 @@ static int write_to_syslog(
if (!syslog_is_stream)
break;
if (iovec_increment(iovec, ELEMENTSOF(iovec), n))
if (iovec_inc_many(iovec, ELEMENTSOF(iovec), n))
break;
}
+1 -1
View File
@@ -41,7 +41,7 @@ static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userd
else
assert_se(errno == EAGAIN);
} else
iovec_increment(iov, 1, n);
iovec_inc(iov, n);
}
if (revents & EPOLLIN) {
+1 -1
View File
@@ -621,7 +621,7 @@ static int pid_notify_with_fds_internal(
msghdr.msg_control = NULL;
msghdr.msg_controllen = 0;
}
} while (!iovec_increment(msghdr.msg_iov, msghdr.msg_iovlen, n));
} while (!iovec_inc_many(msghdr.msg_iov, msghdr.msg_iovlen, n));
if (address.sockaddr.sa.sa_family == AF_VSOCK && IN_SET(type, SOCK_STREAM, SOCK_SEQPACKET)) {
/* For AF_VSOCK, we need to close the socket to signal the end of the message. */
+1 -1
View File
@@ -347,7 +347,7 @@ static int on_stream_io(sd_event_source *es, int fd, uint32_t revents, void *use
IOVEC_MAKE(DNS_PACKET_DATA(s->write_packet), s->write_packet->size),
};
iovec_increment(iov, ELEMENTSOF(iov), s->n_written);
iovec_inc_many(iov, ELEMENTSOF(iov), s->n_written);
ssize_t ss = dns_stream_writev(s, iov, ELEMENTSOF(iov), 0);
if (ss < 0) {
+54
View File
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "iovec-util.h"
#include "iovec-wrapper.h"
#include "memory-util.h"
#include "tests.h"
@@ -38,6 +39,59 @@ TEST(iovec_inc) {
ASSERT_FALSE(iovec_is_set(iovec_inc(&empty, 1)));
}
TEST(iovec_inc_many) {
ASSERT_TRUE(iovec_inc_many(NULL, 0, 0));
ASSERT_TRUE(iovec_inc_many(&(struct iovec) {}, 0, 0));
ASSERT_TRUE(iovec_inc_many(&(struct iovec) {}, 1, 0));
_cleanup_(iovw_done) struct iovec_wrapper iovw = {};
ASSERT_OK(iovw_put_iov(&iovw, &IOVEC_MAKE_STRING("aaa")));
ASSERT_OK(iovw_put_iov(&iovw, &IOVEC_MAKE_STRING("bbb")));
ASSERT_OK(iovw_put_iov(&iovw, &IOVEC_MAKE_STRING("ccc")));
ASSERT_FALSE(iovec_inc_many(iovw.iovec, iovw.count, 0));
ASSERT_TRUE(iovec_equal(&iovw.iovec[0], &IOVEC_MAKE_STRING("aaa")));
ASSERT_TRUE(iovec_equal(&iovw.iovec[1], &IOVEC_MAKE_STRING("bbb")));
ASSERT_TRUE(iovec_equal(&iovw.iovec[2], &IOVEC_MAKE_STRING("ccc")));
ASSERT_FALSE(iovec_inc_many(iovw.iovec, iovw.count, 1));
ASSERT_TRUE(iovec_equal(&iovw.iovec[0], &IOVEC_MAKE_STRING("aa")));
ASSERT_TRUE(iovec_equal(&iovw.iovec[1], &IOVEC_MAKE_STRING("bbb")));
ASSERT_TRUE(iovec_equal(&iovw.iovec[2], &IOVEC_MAKE_STRING("ccc")));
ASSERT_FALSE(iovec_inc_many(iovw.iovec, iovw.count, 3));
ASSERT_FALSE(iovec_is_set(&iovw.iovec[0]));
ASSERT_NULL(iovw.iovec[0].iov_base);
ASSERT_EQ(iovw.iovec[0].iov_len, 0u);
ASSERT_TRUE(iovec_equal(&iovw.iovec[1], &IOVEC_MAKE_STRING("bb")));
ASSERT_TRUE(iovec_equal(&iovw.iovec[2], &IOVEC_MAKE_STRING("ccc")));
ASSERT_FALSE(iovec_inc_many(iovw.iovec, iovw.count, 4));
ASSERT_NULL(iovw.iovec[0].iov_base);
ASSERT_EQ(iovw.iovec[0].iov_len, 0u);
ASSERT_NULL(iovw.iovec[1].iov_base);
ASSERT_EQ(iovw.iovec[1].iov_len, 0u);
ASSERT_TRUE(iovec_equal(&iovw.iovec[2], &IOVEC_MAKE_STRING("c")));
ASSERT_TRUE(iovec_inc_many(iovw.iovec, iovw.count, 1));
ASSERT_NULL(iovw.iovec[0].iov_base);
ASSERT_EQ(iovw.iovec[0].iov_len, 0u);
ASSERT_NULL(iovw.iovec[1].iov_base);
ASSERT_EQ(iovw.iovec[1].iov_len, 0u);
ASSERT_NULL(iovw.iovec[2].iov_base);
ASSERT_EQ(iovw.iovec[2].iov_len, 0u);
ASSERT_TRUE(iovec_inc_many(iovw.iovec, iovw.count, 0));
ASSERT_NULL(iovw.iovec[0].iov_base);
ASSERT_EQ(iovw.iovec[0].iov_len, 0u);
ASSERT_NULL(iovw.iovec[1].iov_base);
ASSERT_EQ(iovw.iovec[1].iov_len, 0u);
ASSERT_NULL(iovw.iovec[2].iov_base);
ASSERT_EQ(iovw.iovec[2].iov_len, 0u);
ASSERT_SIGNAL(iovec_inc_many(iovw.iovec, iovw.count, 1), SIGABRT);
}
TEST(iovec_memcmp) {
struct iovec iov1 = CONST_IOVEC_MAKE_STRING("abcdef"), iov2 = IOVEC_MAKE_STRING("bcdefg"), empty = {};