iovec-wrapper: introduce several more helper functions

This commit is contained in:
Yu Watanabe
2026-04-19 06:15:54 +09:00
parent 6d14614f4d
commit f71dcac4dc
3 changed files with 143 additions and 0 deletions
+49
View File
@@ -63,6 +63,36 @@ int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) {
return 1;
}
int iovw_put_iov(struct iovec_wrapper *iovw, const struct iovec *iov) {
assert(iovw);
if (!iov)
return 0;
return iovw_put(iovw, iov->iov_base, iov->iov_len);
}
int iovw_put_iovw(struct iovec_wrapper *iovw, const struct iovec_wrapper *source) {
assert(iovw);
if (iovw_isempty(source))
return 0;
/* We will reallocate iovw->iovec, hence the source cannot point to the same object. */
if (iovw == source)
return -EINVAL;
if (iovw->count > SIZE_MAX - source->count)
return -E2BIG;
if (iovw->count + source->count > IOV_MAX)
return -E2BIG;
if (!GREEDY_REALLOC_APPEND(iovw->iovec, iovw->count, source->iovec, source->count))
return -ENOMEM;
return 0;
}
int iovw_consume(struct iovec_wrapper *iovw, void *data, size_t len) {
/* Move data into iovw or free on error */
int r;
@@ -74,6 +104,25 @@ int iovw_consume(struct iovec_wrapper *iovw, void *data, size_t len) {
return r;
}
int iovw_consume_iov(struct iovec_wrapper *iovw, struct iovec *iov) {
int r;
assert(iovw);
if (!iov)
return 0;
r = iovw_put_iov(iovw, iov);
if (r <= 0)
iovec_done(iov);
else
/* On success, iov->iov_base is now owned by iovw. Let's emptify iov, but do not call
* iovec_done(), of course. */
*iov = (struct iovec) {};
return r;
}
int iovw_extend(struct iovec_wrapper *iovw, const void *data, size_t len) {
if (len == 0)
return 0;
+3
View File
@@ -17,7 +17,10 @@ static inline bool iovw_equal(const struct iovec_wrapper *a, const struct iovec_
}
int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len);
int iovw_put_iov(struct iovec_wrapper *iovw, const struct iovec *iov);
int iovw_put_iovw(struct iovec_wrapper *iovw, const struct iovec_wrapper *source);
int iovw_consume(struct iovec_wrapper *iovw, void *data, size_t len);
int iovw_consume_iov(struct iovec_wrapper *iovw, struct iovec *iov);
int iovw_extend(struct iovec_wrapper *iovw, const void *data, size_t len);
int iovw_extend_iov(struct iovec_wrapper *iovw, const struct iovec *iov);
int iovw_extend_iovw(struct iovec_wrapper *iovw, const struct iovec_wrapper *source);