From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTP id B1E1D5A031D for ; Fri, 21 Jun 2024 16:56:51 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718981810; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yzIj3aci93CSAkcsXUySY+CtK7zl5vSejjID/u6w+XU=; b=Ilho5l57NPcmSQKr4npO4RqPEMZwTOhlfaCkbCO3dw7vl8WFNa8bKxaJXjbjRKbKair98e DUGIzt9aHYOSF1N01naLybUZ8qJowQncGMh8Uf8VhqAKphq+PErSAv4zssx+NCcK4aB3Tg Qx1O/EIPc2ZEroF7c9jdP4JKY4Jbs7c= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-18-KLWaoIaFNXmm2oa9peB_Bg-1; Fri, 21 Jun 2024 10:56:48 -0400 X-MC-Unique: KLWaoIaFNXmm2oa9peB_Bg-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 234D619560AE for ; Fri, 21 Jun 2024 14:56:48 +0000 (UTC) Received: from lenovo-t14s.redhat.com (unknown [10.39.192.221]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3A7683000218; Fri, 21 Jun 2024 14:56:46 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH 4/5] iov: add iov_count() Date: Fri, 21 Jun 2024 16:56:39 +0200 Message-ID: <20240621145640.1914287-5-lvivier@redhat.com> In-Reply-To: <20240621145640.1914287-1-lvivier@redhat.com> References: <20240621145640.1914287-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: CG4HC42TBJOAD2GPX6WF3CBZVWZO7OZ6 X-Message-ID-Hash: CG4HC42TBJOAD2GPX6WF3CBZVWZO7OZ6 X-MailFrom: lvivier@redhat.com 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: Laurent Vivier 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: 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 --- 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 */ -- 2.45.2