public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Laurent Vivier <lvivier@redhat.com>
To: passt-dev@passt.top
Cc: Laurent Vivier <lvivier@redhat.com>
Subject: [PATCH 4/5] iov: add iov_count()
Date: Fri, 21 Jun 2024 16:56:39 +0200	[thread overview]
Message-ID: <20240621145640.1914287-5-lvivier@redhat.com> (raw)
In-Reply-To: <20240621145640.1914287-1-lvivier@redhat.com>

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.

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
 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 */
-- 
@@ -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 */
-- 
2.45.2


  parent reply	other threads:[~2024-06-21 14:56 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-21 14:56 [PATCH 0/5] Add vhost-user support to passt. (part 3) Laurent Vivier
2024-06-21 14:56 ` [PATCH 1/5] packet: replace struct desc by struct iovec Laurent Vivier
2024-06-24  2:48   ` David Gibson
2024-07-04 15:52     ` Laurent Vivier
2024-07-05  1:28       ` David Gibson
2024-06-21 14:56 ` [PATCH 2/5] vhost-user: introduce virtio API Laurent Vivier
2024-06-24  2:56   ` David Gibson
2024-07-05 15:06     ` Laurent Vivier
2024-07-05 23:53       ` David Gibson
2024-06-21 14:56 ` [PATCH 3/5] vhost-user: introduce vhost-user API Laurent Vivier
2024-06-24  3:02   ` David Gibson
2024-07-11 12:07     ` Laurent Vivier
2024-06-21 14:56 ` Laurent Vivier [this message]
2024-06-24  3:03   ` [PATCH 4/5] iov: add iov_count() David Gibson
2024-06-24  6:59     ` Laurent Vivier
2024-06-21 14:56 ` [PATCH 5/5] vhost-user: add vhost-user Laurent Vivier
2024-06-24  5:05   ` David Gibson
2024-07-12 14:49     ` Laurent Vivier
2024-07-15  0:37       ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240621145640.1914287-5-lvivier@redhat.com \
    --to=lvivier@redhat.com \
    --cc=passt-dev@passt.top \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).