From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DTp9+n7W; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id A0C0A5A061C for ; Thu, 16 Apr 2026 17:57:31 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1776355050; 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=YpddPC6mKI/p9WuhClKPQKNCxoBUqkUNvUC4kWb0oM0=; b=DTp9+n7WaU5tpq21ZLax+NFHzcxMB7/yKAMpoWitWdf4T+Am+T9WCbe2jkBhh691DaTm6F w/7wdNwU/jzI4Ab5un338jWZEpjtmEVoXGwKq1p3V8klQU+N7hqal8/6C9LFzCT8OCteAq sHaAeLG3UeDwtAb9RnecN6zBaxST/Tc= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-177-EJ-5Ohe7Ncy5Zq30JShXyg-1; Thu, 16 Apr 2026 11:57:29 -0400 X-MC-Unique: EJ-5Ohe7Ncy5Zq30JShXyg-1 X-Mimecast-MFC-AGG-ID: EJ-5Ohe7Ncy5Zq30JShXyg_1776355048 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BF1B718005AB; Thu, 16 Apr 2026 15:57:27 +0000 (UTC) Received: from lenovo-t14s.redhat.corp (headnet01.pony-001.prod.iad2.dc.redhat.com [10.2.32.101]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5FA6C1955D88; Thu, 16 Apr 2026 15:57:26 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v3 02/10] iov: Add iov_memcpy() to copy data between iovec arrays Date: Thu, 16 Apr 2026 17:57:13 +0200 Message-ID: <20260416155721.3807225-3-lvivier@redhat.com> In-Reply-To: <20260416155721.3807225-1-lvivier@redhat.com> References: <20260416155721.3807225-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 4YG5cjUrQj-kmnSveXVZnj5uc3V7bgzj0jjZyyZ67cU_1776355048 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: IFODVQZMKYLUQCV3GYFHSUUZ2QQ4WGRR X-Message-ID-Hash: IFODVQZMKYLUQCV3GYFHSUUZ2QQ4WGRR 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 , David Gibson 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 helper to copy data from a source iovec array to a destination iovec array, each starting at an arbitrary byte offset, iterating through both arrays simultaneously and copying in chunks matching the smaller of the two current segments. Signed-off-by: Laurent Vivier Reviewed-by: David Gibson --- iov.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ iov.h | 3 +++ 2 files changed, 55 insertions(+) diff --git a/iov.c b/iov.c index 0188acdf5eba..dabc4f1ceea3 100644 --- a/iov.c +++ b/iov.c @@ -197,6 +197,58 @@ void iov_memset(const struct iovec *iov, size_t iov_cnt, size_t offset, int c, } } +/** + * iov_memcpy() - Copy data between two iovec arrays + * @dst_iov: Destination iovec array + * @dst_iov_cnt: Number of elements in destination iovec array + * @dst_offset: Destination offset + * @src_iov: Source iovec array + * @src_iov_cnt: Number of elements in source iovec array + * @offs: Source offset + * @length: Number of bytes to copy + * + * Return: total number of bytes copied + */ +/* cppcheck-suppress unusedFunction */ +size_t iov_memcpy(struct iovec *dst_iov, size_t dst_iov_cnt, size_t dst_offset, + const struct iovec *src_iov, size_t src_iov_cnt, + size_t src_offset, size_t length) +{ + unsigned int i, j; + size_t total = 0; + + i = iov_skip_bytes(src_iov, src_iov_cnt, src_offset, &src_offset); + j = iov_skip_bytes(dst_iov, dst_iov_cnt, dst_offset, &dst_offset); + + /* copying data */ + while (length && i < src_iov_cnt && j < dst_iov_cnt) { + size_t n = MIN(dst_iov[j].iov_len - dst_offset, + src_iov[i].iov_len - src_offset); + + if (n > length) + n = length; + + memcpy((char *)dst_iov[j].iov_base + dst_offset, + (const char *)src_iov[i].iov_base + src_offset, n); + + dst_offset += n; + src_offset += n; + total += n; + length -= n; + + if (dst_offset == dst_iov[j].iov_len) { + dst_offset = 0; + j++; + } + if (src_offset == src_iov[i].iov_len) { + src_offset = 0; + i++; + } + } + + return total; +} + /** * iov_tail_prune() - Remove any unneeded buffers from an IOV tail * @tail: IO vector tail (modified) diff --git a/iov.h b/iov.h index d295d05b3bab..3c63308e554f 100644 --- a/iov.h +++ b/iov.h @@ -32,6 +32,9 @@ size_t iov_size(const struct iovec *iov, size_t iov_cnt); size_t iov_truncate(struct iovec *iov, size_t iov_cnt, size_t size); void iov_memset(const struct iovec *iov, size_t iov_cnt, size_t offset, int c, size_t length); +size_t iov_memcpy(struct iovec *dst_iov, size_t dst_iov_cnt, size_t dst_offset, + const struct iovec *src_iov, size_t src_iov_cnt, + size_t src_offset, size_t length); /* * DOC: Theory of Operation, struct iov_tail -- 2.53.0