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=JHN09b/A; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id 7816A5A0275 for ; Fri, 03 Apr 2026 18:38:19 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1775234298; 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=OqDFbmve2GZ0xWkc0X9/snZa2a2//gFHOLLOrQKq6e8=; b=JHN09b/AfxPdO338OY+oSMu0FkHJitARDQNZkxVgej/TSKuhm8uyKhdHcLcQANavUesJzq 3rOo3BbaVME6s0OGcESF2gMpvRJnWtdCdhlE5HqNXrD6/MqET5jLYx+UmgTFGbyq0KShYs B8LP6ZpdovYvokZBTr+uU2h2OFYPkhE= Received: from mx-prod-mc-03.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-116-4YvaBIZXPj6FV_8Kr2AHhA-1; Fri, 03 Apr 2026 12:38:17 -0400 X-MC-Unique: 4YvaBIZXPj6FV_8Kr2AHhA-1 X-Mimecast-MFC-AGG-ID: 4YvaBIZXPj6FV_8Kr2AHhA_1775234296 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 593B2195605C for ; Fri, 3 Apr 2026 16:38:16 +0000 (UTC) Received: from lenovo-t14s.redhat.corp (headnet01.pony-001.prod.iad2.dc.redhat.com [10.2.32.101]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8FE1E1800673; Fri, 3 Apr 2026 16:38:15 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v2 02/10] iov: Add iov_memcpy() to copy data between iovec arrays Date: Fri, 3 Apr 2026 18:38:03 +0200 Message-ID: <20260403163811.3209635-3-lvivier@redhat.com> In-Reply-To: <20260403163811.3209635-1-lvivier@redhat.com> References: <20260403163811.3209635-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: srSL1FPUbuAT_3bpVuol9mJgd-TOk4-Iu485bGYWBQk_1775234296 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: V3OJTNPPOAUPGUWRQZAPALCXIV7JWJQW X-Message-ID-Hash: V3OJTNPPOAUPGUWRQZAPALCXIV7JWJQW 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 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 --- 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