On Fri, Jun 21, 2024 at 04:56:39PM +0200, Laurent Vivier wrote: > Add a function that count how many buffers from a given > iovec list we need to contain a given number of bytes. > It also provides how many bytes are used in the last > buffer if it is not fully filled. Isn't this equivalent to the iov_skip_bytes() function we already have? > > Signed-off-by: Laurent Vivier > --- > iov.c | 35 +++++++++++++++++++++++++++++++++++ > iov.h | 2 ++ > 2 files changed, 37 insertions(+) > > diff --git a/iov.c b/iov.c > index 3741db21790f..793788b5d2bc 100644 > --- a/iov.c > +++ b/iov.c > @@ -155,3 +155,38 @@ size_t iov_size(const struct iovec *iov, size_t iov_cnt) > > return len; > } > + > +/** > + * iov_count - Calculate the number of I/O vectors and the size of > + * the last one to store a given number of bytes. > + * > + * @iov: Pointer to the array of struct iovec describing the > + * scatter/gather I/O vector. > + * @iov_cnt: Number of elements in the iov array. > + * @size: number of bytes we need to store in iovec > + * @last_iov_length: output parameter, length used in the last iovec > + * if return value is 0, this output parameter is > + * undefined. > + * > + * Returns: The number of iovec needed to store @size bytes. > + */ > +/* cppcheck-suppress unusedFunction */ > +size_t iov_count(const struct iovec *iov, size_t iov_cnt, > + size_t size, size_t *last_iov_length) > +{ > + size_t n = 0; > + > + while (size && n < iov_cnt) { > + if (size <= iov[n].iov_len) { > + *last_iov_length = size; > + return n + 1; > + } > + size -= iov[n].iov_len; > + n++; > + } > + > + if (n > 0) > + *last_iov_length = iov[n - 1].iov_len; > + > + return n; > +} > diff --git a/iov.h b/iov.h > index a9e1722713b3..0fa456d7051b 100644 > --- a/iov.h > +++ b/iov.h > @@ -28,4 +28,6 @@ size_t iov_from_buf(const struct iovec *iov, size_t iov_cnt, > size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt, > size_t offset, void *buf, size_t bytes); > size_t iov_size(const struct iovec *iov, size_t iov_cnt); > +size_t iov_count(const struct iovec *iov, size_t iov_cnt, > + size_t size, size_t *last_iov_length); > #endif /* IOVEC_H */ -- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson