From a804574bc42c73915944b38ef638f667067bbf8c Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Wed, 13 May 2026 13:52:09 +0200 Subject: iov: Introduce iov_memset() Add a helper to set a range of bytes across an IO vector to a given value, similar to memset() but operating over scatter-gather buffers. It skips to the given offset and fills across iovec entries up to the requested length. Signed-off-by: Laurent Vivier Reviewed-by: David Gibson Reviewed-by: Jon Maloy Signed-off-by: Stefano Brivio --- iov.c | 27 +++++++++++++++++++++++++++ iov.h | 2 ++ 2 files changed, 29 insertions(+) diff --git a/iov.c b/iov.c index ae07439..0188acd 100644 --- a/iov.c +++ b/iov.c @@ -170,6 +170,33 @@ size_t iov_truncate(struct iovec *iov, size_t iov_cnt, size_t size) return i; } +/** + * iov_memset() - Set bytes of an IO vector to a given value + * @iov: IO vector + * @iov_cnt: Number of elements in @iov + * @offset: Byte offset in the iovec at which to start + * @c: Byte value to fill with + * @length: Number of bytes to set + * Will write less than @length bytes if it runs out of space in + * the iov + */ +/* cppcheck-suppress unusedFunction */ +void iov_memset(const struct iovec *iov, size_t iov_cnt, size_t offset, int c, + size_t length) +{ + size_t i; + + i = iov_skip_bytes(iov, iov_cnt, offset, &offset); + + for ( ; i < iov_cnt && length; i++) { + size_t n = MIN(iov[i].iov_len - offset, length); + + memset((char *)iov[i].iov_base + offset, c, n); + offset = 0; + length -= n; + } +} + /** * 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 b4e50b0..d295d05 100644 --- a/iov.h +++ b/iov.h @@ -30,6 +30,8 @@ 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_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); /* * DOC: Theory of Operation, struct iov_tail -- cgit v1.2.3