iovec-wrapper: introduce several more helper functions
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user