From patchwork Wed Mar 27 16:36:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 231747 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 C98672C007E for ; Thu, 28 Mar 2013 03:37:34 +1100 (EST) Received: from localhost ([::1]:49765 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKtLu-0006it-Nj for incoming@patchwork.ozlabs.org; Wed, 27 Mar 2013 12:37:30 -0400 Received: from eggs.gnu.org ([208.118.235.92]:45635) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKtLE-0006K7-1Y for qemu-devel@nongnu.org; Wed, 27 Mar 2013 12:36:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UKtLA-0002s5-Ba for qemu-devel@nongnu.org; Wed, 27 Mar 2013 12:36:47 -0400 Received: from mail-ee0-f54.google.com ([74.125.83.54]:43648) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UKtLA-0002rr-47 for qemu-devel@nongnu.org; Wed, 27 Mar 2013 12:36:44 -0400 Received: by mail-ee0-f54.google.com with SMTP id e51so738174eek.27 for ; Wed, 27 Mar 2013 09:36:43 -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=lPDcIzR6+/O3nQj4RMoCpBavUJteM8vlazFhSZMT4j8=; b=yEyWRbfj/YTa08YFcDQNzaqKHekHw/n4RVZdFgWMjy0qhqJa7cM4NTgCV0MwFfI5Y7 lX2pQZp1pcXJtDP6GZv4vYOfpjkMfwIb/1iEi9R7GL4CG9e3X7M2k4CdGWhve3wyyvp7 WtFZocZhpVUxjz0CqUgmo6nNxzoQHeTss8ZcZ2RN3StMWy7ZT4d0c8RGgGKk+Nqjh4sH GzpL4DjsB64DZ5XuIZufO6vLkDtsWOspyPfcj5aujh2ZlAW8LwSDUoqMUWRNcrBnwLe7 gTBMIrhQdHz2fMm3QJqR7zoNAdfFIi+PULW5EgS4gcytBqLfkIaYIRID+FRdlZDn+sih R/RQ== X-Received: by 10.14.179.201 with SMTP id h49mr9134886eem.26.1364402203362; Wed, 27 Mar 2013 09:36:43 -0700 (PDT) Received: from playground.lan (93-34-176-20.ip50.fastwebnet.it. [93.34.176.20]) by mx.google.com with ESMTPS id d47sm32366397eem.9.2013.03.27.09.36.41 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 27 Mar 2013 09:36:42 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 27 Mar 2013 17:36:29 +0100 Message-Id: <1364402192-18169-4-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1364402192-18169-1-git-send-email-pbonzini@redhat.com> References: <1364402192-18169-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 74.125.83.54 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. Signed-off-by: Paolo Bonzini Reviewed-by: Juan Quintela --- 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; }