iov_tail_truncate() truncates a tail so it contains at most a given
number of bytes from the current position, adjusting both the last
iovec entry and the buffer count.
iov_tail_zero_end() zero-fills all backing buffer bytes beyond a given
number of leading bytes from the current tail position and can be
used to clear trailing padding in fixed-size frames.
Signed-off-by: Laurent Vivier
---
iov.c | 39 +++++++++++++++++++++++++++++++++++++++
iov.h | 2 ++
2 files changed, 41 insertions(+)
diff --git a/iov.c b/iov.c
index ad726daa4cd8..cb4d6fef5567 100644
--- a/iov.c
+++ b/iov.c
@@ -170,6 +170,45 @@ bool iov_tail_prune(struct iov_tail *tail)
return !!tail->cnt;
}
+/**
+ * iov_tail_truncate() - Truncate tail to at most @size bytes
+ * @tail: IO vector tail (modified in place, including backing iovecs)
+ * @size: Maximum number of bytes to keep, relative to current tail offset
+ */
+/* cppcheck-suppress unusedFunction */
+void iov_tail_truncate(struct iov_tail *tail, size_t size)
+{
+ size_t i, off;
+
+ i = iov_skip_bytes(tail->iov, tail->cnt, tail->off + size, &off);
+
+ if (i < tail->cnt) {
+ struct iovec *last = (struct iovec *)&tail->iov[i];
+
+ last->iov_len = off;
+ tail->cnt = i + !!off;
+ }
+}
+
+/**
+ * iov_tail_zero_end() - Zero-fill tail bytes beyond @size
+ * @tail: IO vector tail (backing buffers modified in place)
+ * @size: Number of leading bytes to preserve
+ */
+/* cppcheck-suppress unusedFunction */
+void iov_tail_zero_end(struct iov_tail *tail, size_t size)
+{
+ size_t i, off;
+
+ i = iov_skip_bytes(tail->iov, tail->cnt, tail->off + size, &off);
+
+ for (; i < tail->cnt; i++) {
+ memset((char *)tail->iov[i].iov_base + off, 0,
+ tail->iov[i].iov_len - off);
+ off = 0;
+ }
+}
+
/**
* iov_tail_size() - Calculate the total size of an IO vector tail
* @tail: IO vector tail
diff --git a/iov.h b/iov.h
index d2184bfd12bd..a7b873d58134 100644
--- a/iov.h
+++ b/iov.h
@@ -89,6 +89,8 @@ void *iov_peek_header_(struct iov_tail *tail, void *v, size_t len, size_t align)
void *iov_remove_header_(struct iov_tail *tail, void *v, size_t len, size_t align);
ssize_t iov_tail_clone(struct iovec *dst_iov, size_t dst_iov_cnt,
struct iov_tail *tail);
+void iov_tail_truncate(struct iov_tail *tail, size_t size);
+void iov_tail_zero_end(struct iov_tail *tail, size_t size);
/**
* IOV_PEEK_HEADER() - Get typed pointer to a header from an IOV tail
--
2.53.0