From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 751EC5A0319 for ; Mon, 24 Jun 2024 07:05:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1719205528; bh=UFqgkdmJ8u/kTsFML6aK2gQYCprDjUYoC39m1xUV5F4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=QNAlyjbNnRZVRpmp3add8YloqLnio/5vlF66d01xPhJh/vKxhcVM2l+/KRnBQWtBV ifF0jdHXwqfl5xjZ0jkP7smBRHDx4+Axy8l/Z3jWF4/NLg/aP76++LfentatREz08D SzEs+pMiFDgSeMBmb8g+S/Jjr9pxqtbx+NLHUpsMnhJ+A2MgG85/d9LhtXBsiYdgKs jXWXSEgOQ/ZlaPczCSuwvhwmYZYyaX4omuqYr8C8dHoYoxfeZUIIkDCSvIswoDKQZV pmTeVKl15+q3MRp8HIn9kWxpFBJ0DIw+LeZdtkWTuraMt9RF5W09T2l47Ah1YwUmwZ zKvsdHkpYxW0w== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4W6wrm26Qzz4wny; Mon, 24 Jun 2024 15:05:28 +1000 (AEST) Date: Mon, 24 Jun 2024 13:03:40 +1000 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH 4/5] iov: add iov_count() Message-ID: References: <20240621145640.1914287-1-lvivier@redhat.com> <20240621145640.1914287-5-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="DUz61Gr0MxGpdm60" Content-Disposition: inline In-Reply-To: <20240621145640.1914287-5-lvivier@redhat.com> Message-ID-Hash: NFJBUQS7JDHJHBNWQE3VSLGU7UHNUN25 X-Message-ID-Hash: NFJBUQS7JDHJHBNWQE3VSLGU7UHNUN25 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --DUz61Gr0MxGpdm60 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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? >=20 > Signed-off-by: Laurent Vivier > --- > iov.c | 35 +++++++++++++++++++++++++++++++++++ > iov.h | 2 ++ > 2 files changed, 37 insertions(+) >=20 > 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) > =20 > 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 =3D 0; > + > + while (size && n < iov_cnt) { > + if (size <=3D iov[n].iov_len) { > + *last_iov_length =3D size; > + return n + 1; > + } > + size -=3D iov[n].iov_len; > + n++; > + } > + > + if (n > 0) > + *last_iov_length =3D 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 */ --=20 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 --DUz61Gr0MxGpdm60 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmZ44gsACgkQzQJF27ox 2GfyRw//UQK0HvXRx2fGIcpJSxxzTQggLK2cLDK0j6eB3yIFLyJoriO4guYWqYK6 FBYv0YaVWlvddISfOT2mw2tzn14/IwZRAsBQ3Keal5LY0SZNVcRcCUXxRTBQQpVs ccByX+gJc5JHhOjzf9kyRNlmdkohI7aVXYfFKxaWmqM0Ah/C0fpvuw3W3pZB88wb aaizgb7MRgNeURT3fLeqXkB2wKub4Oh6WUePx6ondC2crWCicqd1l4cE4c8mCho4 ThruGI5azKKcvFM3Njhxn0xIQZFcc0PJGluI3t3vscaLD0pujOS3z3yqCmv/EAAI oSMgJAVbBJvdrEQEOtxnrA3gw5ACU7LYPWHWsYv1YiTARazaxofvAxfWc5fV6JGT bZFXh+E1q2iDc+h3NlCrNg3ZAwPkgUqdEJBJGiNUUBVJI0UYcemFYfrySPioy7GE QoXsf65bowNHVovqBU6+SkEOZnkmJjhzBDymuMLFIB6UHxH5Od6Tyoxxv7lvMjKP ZwdGDdzwG6T4kbnFyb4IOjsPi4dzf28SN1qCO79GM1Ex3qef0+sRdyCerHHcZMQ9 MIk+bXfvwIIwj5CzncqfKP08dKXQmDy7ivXNNW/u/W7Yzn2oFgKYdG9RzorCskwm deHxRIRxujtKe280aKbRfUIwOx1shQIrkMiWpZkoqfRj0YLmzVQ= =9uE5 -----END PGP SIGNATURE----- --DUz61Gr0MxGpdm60--