aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorLaurent Vivier <lvivier@redhat.com>2026-05-13 13:52:10 +0200
committerStefano Brivio <sbrivio@redhat.com>2026-05-20 01:21:09 +0200
commitd7259ea24c8aa4908f60d54f4df40bfcb3da6407 (patch)
tree8984d347ec07b64fbc3cc64b0bd6d436a683f7a0
parenta804574bc42c73915944b38ef638f667067bbf8c (diff)
downloadpasst-d7259ea24c8aa4908f60d54f4df40bfcb3da6407.tar
passt-d7259ea24c8aa4908f60d54f4df40bfcb3da6407.tar.gz
passt-d7259ea24c8aa4908f60d54f4df40bfcb3da6407.tar.bz2
passt-d7259ea24c8aa4908f60d54f4df40bfcb3da6407.tar.lz
passt-d7259ea24c8aa4908f60d54f4df40bfcb3da6407.tar.xz
passt-d7259ea24c8aa4908f60d54f4df40bfcb3da6407.tar.zst
passt-d7259ea24c8aa4908f60d54f4df40bfcb3da6407.zip
iov: Add iov_memcpy() to copy data between iovec arrays
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 <lvivier@redhat.com> Reviewed-by: David Gibson <david@gibson.dropbear.id.au> Reviewed-by: Jon Maloy <jmaloy@redhat.com> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--iov.c51
-rw-r--r--iov.h3
2 files changed, 54 insertions, 0 deletions
diff --git a/iov.c b/iov.c
index 0188acd..d2b0609 100644
--- a/iov.c
+++ b/iov.c
@@ -198,6 +198,57 @@ 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)
+{
+ size_t i, j, 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 d295d05..3c63308 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