From patchwork Wed Nov 21 18:32:58 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 200830 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 900682C0093 for ; Thu, 22 Nov 2012 06:03:27 +1100 (EST) Received: from localhost ([::1]:43282 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbF8I-0003XY-EX for incoming@patchwork.ozlabs.org; Wed, 21 Nov 2012 13:34:46 -0500 Received: from eggs.gnu.org ([208.118.235.92]:55503) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbF7M-0001yN-8x for qemu-devel@nongnu.org; Wed, 21 Nov 2012 13:33:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TbF7K-0006Ub-Pz for qemu-devel@nongnu.org; Wed, 21 Nov 2012 13:33:48 -0500 Received: from mx1.redhat.com ([209.132.183.28]:27830) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbF7K-0006UO-Gr for qemu-devel@nongnu.org; Wed, 21 Nov 2012 13:33:46 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id qALIXUbQ014057 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 21 Nov 2012 13:33:30 -0500 Received: from localhost (ovpn-112-38.ams2.redhat.com [10.36.112.38]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id qALIXTIW018885; Wed, 21 Nov 2012 13:33:29 -0500 From: Stefan Hajnoczi To: Date: Wed, 21 Nov 2012 19:32:58 +0100 Message-Id: <1353522781-12721-10-git-send-email-stefanha@redhat.com> In-Reply-To: <1353522781-12721-1-git-send-email-stefanha@redhat.com> References: <1353522781-12721-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Anthony Liguori , dkoch@cloudswitch.com, "Michael S. Tsirkin" , Michael Roth , Blue Swirl , khoa@us.ibm.com, Stefan Hajnoczi , Paolo Bonzini , Asias He Subject: [Qemu-devel] [PATCH v3 09/12] iov: add iov_get_ptr() to reference vector data X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The iov_get_ptr() data returns a pointer to contiguous data within a vector. This allows the caller to manipulate data inside the vector without copying in/out using iov_from_buf()/iov_to_buf() when we know that data is contiguous within an iovec element. Signed-off-by: Stefan Hajnoczi --- iov.c | 25 +++++++++++++++++++++++++ iov.h | 11 +++++++++++ 2 files changed, 36 insertions(+) diff --git a/iov.c b/iov.c index 6eed089..bc78c34 100644 --- a/iov.c +++ b/iov.c @@ -395,3 +395,28 @@ size_t iov_discard(struct iovec **iov, unsigned int *iov_cnt, ssize_t bytes) } return total; } + +void *iov_get_ptr(struct iovec *iov, unsigned int iov_cnt, + ssize_t offset, size_t bytes) +{ + if (offset < 0) { + offset += iov_size(iov, iov_cnt); + if (offset < 0) { + return NULL; /* offset before beginning of vector */ + } + } + + while (iov_cnt > 0) { + if (offset < iov->iov_len) { + if (bytes > iov->iov_len - offset) { + return NULL; /* would span iovec elements */ + } + return iov->iov_base + offset; + } + + offset -= iov->iov_len; + iov_cnt--; + iov++; + } + return NULL; /* offset beyond end of vector */ +} diff --git a/iov.h b/iov.h index d6d1fa6..674dd51 100644 --- a/iov.h +++ b/iov.h @@ -108,3 +108,14 @@ unsigned iov_copy(struct iovec *dst_iov, unsigned int dst_iov_cnt, * smaller than requested if the vector is too small. */ size_t iov_discard(struct iovec **iov, unsigned int *iov_cnt, ssize_t bytes); + +/* + * Get a pointer into a vector at offset if the given number of bytes is + * contiguous and not split across iovec elements. NULL is returned if + * memory would span iovec elements or exceed the length of the vector. + * + * The offset is ssize_t so that an offset from the end of the vector can + * be specified with a negative number. + */ +void *iov_get_ptr(struct iovec *iov, unsigned int iov_cnt, ssize_t offset, + size_t bytes);