Skip to content

Commit

Permalink
iovec-util: add iovec_append() for appending to an existing iovec
Browse files Browse the repository at this point in the history
  • Loading branch information
poettering committed Sep 6, 2024
1 parent 9a78f9e commit 664570f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/basic/iovec-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/basic/iovec-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
13 changes: 13 additions & 0 deletions src/test/test-iovec-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 664570f

Please sign in to comment.