From nobody Mon Feb 9 01:19:47 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1559665124; cv=none; d=zoho.com; s=zohoarc; b=kA83Wfnomr2Rsrl90d9S6i2M+VrOa9VHLw5NEDAQI4q27Xr4FYVYRgnPM86ZJzG79dYPeHp/90z9lwjNTAbGf1Vxm20YxXFfTjtHqlx4cXUxsw9LoPwvcO0hd/ATfZClrEVwdFzST7nUcBMCeg84MgF3UIWAks5aMct7fyUNeCA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1559665124; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=NQKSEN86rfkexqO6lNEm47iIp8z9/aFsu78qickc+OE=; b=P3xPFURciR5cwVBQ0DH942MFkFIOE4X5utXOVpg2Ued8c5zAh3Y8rgd+UHgSC5rdHJXeQ1sdnmKeXcIb5jWPKv8cWcbCRsRu1p1GgtT+TyKLa53dxmo1364zYQln06YMuWNjV6bYzok3fxjhvoVb3FvRH5FvGq4rjBwzq25Jo2w= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1559665124969410.12775103775857; Tue, 4 Jun 2019 09:18:44 -0700 (PDT) Received: from localhost ([127.0.0.1]:54860 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYC93-0002os-03 for importer@patchew.org; Tue, 04 Jun 2019 12:18:41 -0400 Received: from eggs.gnu.org ([209.51.188.92]:35893) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hYC6H-00018d-R8 for qemu-devel@nongnu.org; Tue, 04 Jun 2019 12:15:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hYC6B-0005hJ-Bg for qemu-devel@nongnu.org; Tue, 04 Jun 2019 12:15:46 -0400 Received: from relay.sw.ru ([185.231.240.75]:41660) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hYC6A-0005C1-O6; Tue, 04 Jun 2019 12:15:43 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1hYC5j-0005Pq-8I; Tue, 04 Jun 2019 19:15:15 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Tue, 4 Jun 2019 19:15:03 +0300 Message-Id: <20190604161514.262241-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20190604161514.262241-1-vsementsov@virtuozzo.com> References: <20190604161514.262241-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v2 01/12] util/iov: introduce qemu_iovec_init_extended X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: fam@euphon.net, kwolf@redhat.com, vsementsov@virtuozzo.com, mreitz@redhat.com, stefanha@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Introduce new initialization API, to create requests with padding. Will be used in the following patch. New API uses qemu_iovec_init_buf if resulting io vector has only one element, to avoid extra allocations. So, we need to update qemu_iovec_destroy to support destroying such QIOVs. Signed-off-by: Vladimir Sementsov-Ogievskiy --- include/qemu/iov.h | 7 +++ util/iov.c | 112 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 114 insertions(+), 5 deletions(-) diff --git a/include/qemu/iov.h b/include/qemu/iov.h index 48b45987b7..f3787a0cf7 100644 --- a/include/qemu/iov.h +++ b/include/qemu/iov.h @@ -199,6 +199,13 @@ static inline void *qemu_iovec_buf(QEMUIOVector *qiov) =20 void qemu_iovec_init(QEMUIOVector *qiov, int alloc_hint); void qemu_iovec_init_external(QEMUIOVector *qiov, struct iovec *iov, int n= iov); +void qemu_iovec_init_extended( + QEMUIOVector *qiov, + void *head_buf, size_t head_len, + QEMUIOVector *mid_qiov, size_t mid_offset, size_t mid_len, + void *tail_buf, size_t tail_len); +void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source, + size_t offset, size_t len); void qemu_iovec_add(QEMUIOVector *qiov, void *base, size_t len); void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t soffset, size_t sbytes); diff --git a/util/iov.c b/util/iov.c index 74e6ca8ed7..39b6e31494 100644 --- a/util/iov.c +++ b/util/iov.c @@ -353,6 +353,103 @@ void qemu_iovec_concat(QEMUIOVector *dst, qemu_iovec_concat_iov(dst, src->iov, src->niov, soffset, sbytes); } =20 +/* + * qiov_find_iov + * + * Return pointer to iovec structure, where byte at @offset in original ve= ctor + * @iov exactly is. + * Set @remaining_offset to be offset inside that iovec to the same byte. + */ +static struct iovec *iov_skip_offset(struct iovec *iov, size_t offset, + size_t *remaining_offset) +{ + while (offset > 0 && offset >=3D iov->iov_len) { + offset -=3D iov->iov_len; + iov++; + } + *remaining_offset =3D offset; + + return iov; +} + +/* + * qiov_slice + * + * Find subarray of iovec's, containing requested range. @head would + * be offset in first iov (returned by the function), @tail would be + * count of extra bytes in last iovec (returned iov + @niov - 1). + */ +static struct iovec *qiov_slice(QEMUIOVector *qiov, + size_t offset, size_t len, + size_t *head, size_t *tail, int *niov) +{ + struct iovec *iov, *end_iov; + + assert(offset + len <=3D qiov->size); + + iov =3D iov_skip_offset(qiov->iov, offset, head); + end_iov =3D iov_skip_offset(iov, *head + len, tail); + + if (*tail > 0) { + assert(*tail < end_iov->iov_len); + *tail =3D end_iov->iov_len - *tail; + end_iov++; + } + + *niov =3D end_iov - iov; + + return iov; +} + +/* + * Compile new iovec, combining @head_buf buffer, sub-qiov of @mid_qiov, + * and @tail_buf buffer into new qiov. + */ +void qemu_iovec_init_extended( + QEMUIOVector *qiov, + void *head_buf, size_t head_len, + QEMUIOVector *mid_qiov, size_t mid_offset, size_t mid_len, + void *tail_buf, size_t tail_len) +{ + size_t mid_head, mid_tail; + int total_niov, mid_niov; + struct iovec *p, *mid_iov; + + if (mid_len) { + mid_iov =3D qiov_slice(mid_qiov, mid_offset, mid_len, + &mid_head, &mid_tail, &mid_niov); + } + + total_niov =3D !!head_len + mid_niov + !!tail_len; + if (total_niov =3D=3D 1) { + qemu_iovec_init_buf(qiov, NULL, 0); + p =3D &qiov->local_iov; + } else { + qiov->niov =3D qiov->nalloc =3D total_niov; + qiov->size =3D head_len + mid_len + tail_len; + p =3D qiov->iov =3D g_new(struct iovec, qiov->niov); + } + + if (head_len) { + p->iov_base =3D head_buf; + p->iov_len =3D head_len; + p++; + } + + if (mid_len) { + memcpy(p, mid_iov, mid_niov * sizeof(*p)); + p[0].iov_base =3D (uint8_t *)p[0].iov_base + mid_head; + p[0].iov_len -=3D mid_head; + p[mid_niov - 1].iov_len -=3D mid_tail; + p +=3D mid_niov; + } + + if (tail_len) { + p->iov_base =3D tail_buf; + p->iov_len =3D tail_len; + } +} + /* * Check if the contents of the iovecs are all zero */ @@ -374,14 +471,19 @@ bool qemu_iovec_is_zero(QEMUIOVector *qiov) return true; } =20 +void qemu_iovec_init_slice(QEMUIOVector *qiov, QEMUIOVector *source, + size_t offset, size_t len) +{ + qemu_iovec_init_extended(qiov, NULL, 0, source, offset, len, NULL, 0); +} + void qemu_iovec_destroy(QEMUIOVector *qiov) { - assert(qiov->nalloc !=3D -1); + if (qiov->nalloc !=3D -1) { + g_free(qiov->iov); + } =20 - qemu_iovec_reset(qiov); - g_free(qiov->iov); - qiov->nalloc =3D 0; - qiov->iov =3D NULL; + memset(qiov, 0, sizeof(*qiov)); } =20 void qemu_iovec_reset(QEMUIOVector *qiov) --=20 2.18.0