From patchwork Wed Apr 17 09:46:49 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 237194 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 EA0562C015D for ; Wed, 17 Apr 2013 19:50:24 +1000 (EST) Received: from localhost ([::1]:41491 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USP0R-0002n9-7Z for incoming@patchwork.ozlabs.org; Wed, 17 Apr 2013 05:50:23 -0400 Received: from eggs.gnu.org ([208.118.235.92]:39338) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USOxK-0006OO-D8 for qemu-devel@nongnu.org; Wed, 17 Apr 2013 05:47:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1USOxF-0002DI-G3 for qemu-devel@nongnu.org; Wed, 17 Apr 2013 05:47:10 -0400 Received: from mail-ea0-x22d.google.com ([2a00:1450:4013:c01::22d]:34185) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1USOxF-0002D9-AS for qemu-devel@nongnu.org; Wed, 17 Apr 2013 05:47:05 -0400 Received: by mail-ea0-f173.google.com with SMTP id k11so624146eaj.4 for ; Wed, 17 Apr 2013 02:47:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=0PlyOVvyf1F3P7gam+Slx2BboB0a3O2lGEuxJSTaaHc=; b=Cvg2dHi2TxJL1zDHLhay+S9h11C+6OeQwcwDcrzoC14NkAIqr3QPuYAn6cm5NWiLFw w5oW1AI5KER0/tdiSCa8juqjiCXNXRRZl/8GFItzrZd5cR9h+wpmgGosAPtyQPCjI4o4 OxhD3sNqKVsLAUK+4muBWwF1wr3RqePJa+PEHSRMV0LYGTtBQ3X90SYsDhcYPslFKVVR yx3DDlDjSTyODEe1LkqfaNec7JzEOGMsriZ0Y4M66BtcZohpEgwzpabDSIAw35sWcn9e HNAcWEZI2DmAp3Q4Ge85B6QmTJGTeEjLIb7LyaOYVo7aOo5FJF3hfdNhBb/eAuNmizIb IiGg== X-Received: by 10.14.5.137 with SMTP id 9mr16421827eel.30.1366192024508; Wed, 17 Apr 2013 02:47:04 -0700 (PDT) Received: from playground.lan (93-34-176-20.ip50.fastwebnet.it. [93.34.176.20]) by mx.google.com with ESMTPS id w51sm7697038eev.13.2013.04.17.02.47.02 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 17 Apr 2013 02:47:03 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 17 Apr 2013 11:46:49 +0200 Message-Id: <1366192012-14872-4-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1366192012-14872-1-git-send-email-pbonzini@redhat.com> References: <1366192012-14872-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4013:c01::22d Cc: owasserm@redhat.com, quintela@redhat.com Subject: [Qemu-devel] [PATCH 3/6] iov: reorganize iov_send_recv, part 2 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 Do not touch the "bytes" argument anymore. Instead, remember the original length of the last iovec if we touch it, and restore it afterwards. This requires undoing the changes in opposite order. The previous algorithm didn't care. Reviewed-by: Juan Quintela Reviewed-by: Orit Wassermann Signed-off-by: Paolo Bonzini --- util/iov.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/util/iov.c b/util/iov.c index adb9a70..110d18e 100644 --- a/util/iov.c +++ b/util/iov.c @@ -145,7 +145,9 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, bool do_send) { ssize_t ret; + size_t orig_len, tail; unsigned si, ei; /* start and end indexes */ + if (bytes == 0) { /* Catch the do-nothing case early, as otherwise we will pass an * empty iovec to sendmsg/recvmsg(), and not all implementations @@ -174,31 +176,29 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt, } /* Find the end position skipping `bytes' bytes: */ /* first, skip all full-sized elements */ - for (ei = 0; ei < iov_cnt && iov[ei].iov_len <= bytes; ++ei) { - bytes -= iov[ei].iov_len; + tail = bytes; + for (ei = 0; ei < iov_cnt && iov[ei].iov_len <= tail; ++ei) { + tail -= iov[ei].iov_len; } - if (bytes) { - /* second, fixup the last element, and remember - * the length we've cut from the end of it in `bytes' */ - size_t tail; + if (tail) { + /* second, fixup the last element, and remember the original + * length */ assert(ei < iov_cnt); - assert(iov[ei].iov_len > bytes); - tail = iov[ei].iov_len - bytes; - iov[ei].iov_len = bytes; - bytes = tail; /* bytes is now equal to the tail size */ - ++ei; + assert(iov[ei].iov_len > tail); + orig_len = iov[ei].iov_len; + iov[ei++].iov_len = tail; } ret = do_send_recv(sockfd, iov, ei, do_send); /* Undo the changes above */ + if (tail) { + iov[ei-1].iov_len = orig_len; + } if (offset) { iov[0].iov_base -= offset; iov[0].iov_len += offset; } - if (bytes) { - iov[ei-1].iov_len += bytes; - } return ret; }