On Fri, Apr 03, 2026 at 06:38:03PM +0200, Laurent Vivier wrote: > Add a helper to copy data from a source iovec array to a destination > iovec array, each starting at an arbitrary byte offset, iterating > through both arrays simultaneously and copying in chunks matching the > smaller of the two current segments. > > Signed-off-by: Laurent Vivier Reviewed-by: David Gibson > --- > iov.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ > iov.h | 3 +++ > 2 files changed, 55 insertions(+) > > diff --git a/iov.c b/iov.c > index 0188acdf5eba..dabc4f1ceea3 100644 > --- a/iov.c > +++ b/iov.c > @@ -197,6 +197,58 @@ void iov_memset(const struct iovec *iov, size_t iov_cnt, size_t offset, int c, > } > } > > +/** > + * iov_memcpy() - Copy data between two iovec arrays > + * @dst_iov: Destination iovec array > + * @dst_iov_cnt: Number of elements in destination iovec array > + * @dst_offset: Destination offset > + * @src_iov: Source iovec array > + * @src_iov_cnt: Number of elements in source iovec array > + * @offs: Source offset > + * @length: Number of bytes to copy > + * > + * Return: total number of bytes copied > + */ > +/* cppcheck-suppress unusedFunction */ > +size_t iov_memcpy(struct iovec *dst_iov, size_t dst_iov_cnt, size_t dst_offset, > + const struct iovec *src_iov, size_t src_iov_cnt, > + size_t src_offset, size_t length) > +{ > + unsigned int i, j; > + size_t total = 0; > + > + i = iov_skip_bytes(src_iov, src_iov_cnt, src_offset, &src_offset); > + j = iov_skip_bytes(dst_iov, dst_iov_cnt, dst_offset, &dst_offset); > + > + /* copying data */ > + while (length && i < src_iov_cnt && j < dst_iov_cnt) { > + size_t n = MIN(dst_iov[j].iov_len - dst_offset, > + src_iov[i].iov_len - src_offset); > + > + if (n > length) > + n = length; > + > + memcpy((char *)dst_iov[j].iov_base + dst_offset, > + (const char *)src_iov[i].iov_base + src_offset, n); > + > + dst_offset += n; > + src_offset += n; > + total += n; > + length -= n; > + > + if (dst_offset == dst_iov[j].iov_len) { > + dst_offset = 0; > + j++; > + } > + if (src_offset == src_iov[i].iov_len) { > + src_offset = 0; > + i++; > + } > + } > + > + return total; > +} > + > /** > * iov_tail_prune() - Remove any unneeded buffers from an IOV tail > * @tail: IO vector tail (modified) > diff --git a/iov.h b/iov.h > index d295d05b3bab..3c63308e554f 100644 > --- a/iov.h > +++ b/iov.h > @@ -32,6 +32,9 @@ size_t iov_size(const struct iovec *iov, size_t iov_cnt); > size_t iov_truncate(struct iovec *iov, size_t iov_cnt, size_t size); > void iov_memset(const struct iovec *iov, size_t iov_cnt, size_t offset, int c, > size_t length); > +size_t iov_memcpy(struct iovec *dst_iov, size_t dst_iov_cnt, size_t dst_offset, > + const struct iovec *src_iov, size_t src_iov_cnt, > + size_t src_offset, size_t length); > > /* > * DOC: Theory of Operation, struct iov_tail > -- > 2.53.0 > -- 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