1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

iovec-util: add iovec_append() for appending to an existing iovec

This commit is contained in:
Lennart Poettering 2024-08-29 11:46:07 +02:00
parent 9a78f9e10c
commit 664570f531
3 changed files with 27 additions and 0 deletions

View File

@ -82,3 +82,15 @@ void iovec_array_free(struct iovec *iovec, size_t n_iovec) {
free(iovec);
}
struct iovec* iovec_append(struct iovec *iovec, const struct iovec *append) {
assert(iovec_is_valid(iovec));
if (!iovec_is_set(append))
return iovec;
if (!greedy_realloc_append(&iovec->iov_base, &iovec->iov_len, append->iov_base, append->iov_len, 1))
return NULL;
return iovec;
}

View File

@ -70,3 +70,5 @@ static inline struct iovec *iovec_memdup(const struct iovec *source, struct iove
return ret;
}
struct iovec* iovec_append(struct iovec *iovec, const struct iovec *append);

View File

@ -54,4 +54,17 @@ TEST(iovec_set_and_valid) {
assert_se(!iovec_is_valid(&invalid));
}
TEST(iovec_append) {
_cleanup_(iovec_done) struct iovec iov = {};
assert_se(iovec_append(&iov, &IOVEC_MAKE_STRING("")) == &iov);
assert_se(iovec_append(&iov, &IOVEC_MAKE_STRING("waldo")) == &iov);
assert_se(iovec_append(&iov, &IOVEC_MAKE_STRING("quux")) == &iov);
assert_se(iovec_append(&iov, &IOVEC_MAKE_STRING("")) == &iov);
assert_se(iovec_append(&iov, &IOVEC_MAKE_STRING("p")) == &iov);
assert_se(iovec_append(&iov, &IOVEC_MAKE_STRING("")) == &iov);
assert_se(iovec_memcmp(&iov, &IOVEC_MAKE_STRING("waldoquuxp")) == 0);
}
DEFINE_TEST_MAIN(LOG_INFO);