From: David Gibson <david@gibson.dropbear.id.au>
To: Laurent Vivier <lvivier@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH 4/5] iov: add iov_count()
Date: Mon, 24 Jun 2024 13:03:40 +1000 [thread overview]
Message-ID: <ZnjiDO03x5HOWh51@zatzit> (raw)
In-Reply-To: <20240621145640.1914287-5-lvivier@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 2530 bytes --]
On Fri, Jun 21, 2024 at 04:56:39PM +0200, Laurent Vivier wrote:
> 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.
Isn't this equivalent to the iov_skip_bytes() function we already have?
>
> 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 */
--
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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2024-06-24 5:05 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 ` [PATCH 4/5] iov: add iov_count() Laurent Vivier
2024-06-24 3:03 ` David Gibson [this message]
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=ZnjiDO03x5HOWh51@zatzit \
--to=david@gibson.dropbear.id.au \
--cc=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).