From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Chris Gregory Newsgroups: gmane.emacs.bugs Subject: bug#25332: [PATCH] Change while loops to for loops. Date: Sun, 8 Jan 2017 13:24:04 -0800 Message-ID: References: <87h95hdg0w.fsf@gmail.com> <83wpe7b7cb.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=94eb2c189cee436a7e05459be15e X-Trace: blaine.gmane.org 1483910721 3824 195.159.176.226 (8 Jan 2017 21:25:21 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 8 Jan 2017 21:25:21 +0000 (UTC) Cc: 25332@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Jan 08 22:25:12 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQKxi-00087A-69 for geb-bug-gnu-emacs@m.gmane.org; Sun, 08 Jan 2017 22:25:10 +0100 Original-Received: from localhost ([::1]:35121 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cQKxm-0006Fd-GH for geb-bug-gnu-emacs@m.gmane.org; Sun, 08 Jan 2017 16:25:14 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:43017) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cQKxf-0006D0-Jt for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2017 16:25:09 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cQKxa-0003cj-QO for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2017 16:25:07 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:60006) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cQKxa-0003cf-Mg for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2017 16:25:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cQKxa-0004xC-GT for bug-gnu-emacs@gnu.org; Sun, 08 Jan 2017 16:25:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Chris Gregory Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 08 Jan 2017 21:25:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 25332 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch notabug Original-Received: via spool by 25332-submit@debbugs.gnu.org id=B25332.148391069219023 (code B ref 25332); Sun, 08 Jan 2017 21:25:02 +0000 Original-Received: (at 25332) by debbugs.gnu.org; 8 Jan 2017 21:24:52 +0000 Original-Received: from localhost ([127.0.0.1]:47172 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQKxP-0004wl-Ns for submit@debbugs.gnu.org; Sun, 08 Jan 2017 16:24:52 -0500 Original-Received: from mail-io0-f178.google.com ([209.85.223.178]:35578) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cQKxO-0004wZ-NL for 25332@debbugs.gnu.org; Sun, 08 Jan 2017 16:24:51 -0500 Original-Received: by mail-io0-f178.google.com with SMTP id j18so21865825ioe.2 for <25332@debbugs.gnu.org>; Sun, 08 Jan 2017 13:24:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=aE0H/ibT/YODJ8Ars3HTjIaF1GwUKN4B7D59Np1cJ1c=; b=PZV2pi3WjLhOAbeRjaJUUBivRXda9JlnNiFf+Kv8Kktf3cdE/1vDqgi7IT1w7VsL3+ n1vov+irZN5gHsaLcCTjlTey8Yki7kTN59jTlqoUpAuDC9YKXaqxk/P1GPLk188On9/U FsOljceic9vJSrea9lTUnaZFpf/gBNpc6Eipk8Rai/5NLmic58eaYpTaE95URKlWCi0m RfVish1iaeTqG4S6FOvTlBPmY7bYmdRgQbafkfdOXIkPOxzMW+bwfvGKwwbf3KUEc2DJ Yfb7xlJAptVloBZBJUDFrBt9/iXMpJDLecSzOMgmVNzFI54niQrmsDwjMIyIeBI9K6f6 TDbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=aE0H/ibT/YODJ8Ars3HTjIaF1GwUKN4B7D59Np1cJ1c=; b=paTcbYoduMrmc7j340HwTCkx/guxKBFu/1mpcxLdoxJRAubmnYX3c8ec+/bI8ewdhl GdnxsKPOUdXAaoTLpDLrQwPUqQe2lwAQp2gnWzHq4aUdsC9xWki1nMZqmXg5oCmsRNOv /fRJxFryDAPYrtAv+GUYidtnIesdY9hpDMs0lDqroAhMbwpw465O+ODLCLCBozC/pOyr SOvBaziSiTOgEPQMQAjAiob45aHM7FgCJNA21xO1TEYByrHCUohYzOAIsz0T8yWi+BRe k/+vZCtaWN1JT04zEAN2j4PULB3tusKnq2wGGD76Krv68zaJetKQROXJX2vs3WZSRnHL E7XQ== X-Gm-Message-State: AIkVDXLpqk/Eaf0JXZVHfQ40PakyyhQXn2VRBYNw7OQy5ajqtYN0UdT/jpaKC5a2tLCccw1+PQiSfCaqcIhuVQ== X-Received: by 10.107.195.204 with SMTP id t195mr65348938iof.46.1483910685090; Sun, 08 Jan 2017 13:24:45 -0800 (PST) Original-Received: by 10.50.191.197 with HTTP; Sun, 8 Jan 2017 13:24:04 -0800 (PST) In-Reply-To: <83wpe7b7cb.fsf@gnu.org> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:127917 Archived-At: --94eb2c189cee436a7e05459be15e Content-Type: text/plain; charset=UTF-8 There are places in ralloc.c where a for loop is used to express a loop, and places where a while loop is used with the increment operation at the end of it. This would just simplify all code so that it was consistently using a for loop for this purpose. Without this patch here is a good usage of for: heap_ptr heap; for (heap = last_heap; heap; heap = heap->prev) { if (heap->start <= address && address <= heap->end) return heap; } Here is a usage of while that should be a for loop like the previous example: bloc_ptr p = first_bloc; while (p != NIL_BLOC) { /* Consistency check. Don't return inconsistent blocs. Don't abort here, as callers might be expecting this, but callers that always expect a bloc to be returned should abort if one isn't to avoid a memory corruption bug that is difficult to track down. */ if (p->variable == ptr && p->data == *ptr) return p; p = p->next; } Recommended new code: bloc_ptr p; for (p = first_bloc; p != NIL_BLOC; p = p->next) { /* Consistency check. Don't return inconsistent blocs. Don't abort here, as callers might be expecting this, but callers that always expect a bloc to be returned should abort if one isn't to avoid a memory corruption bug that is difficult to track down. */ if (p->variable == ptr && p->data == *ptr) return p; } This style exact loop is even used later on (line 509 in update_heap_bloc_correspondence()): /* Advance through blocs one by one. */ for (b = bloc; b != NIL_BLOC; b = b->next) -- Chris Gregory On Sat, Jan 7, 2017 at 12:30 AM, Eli Zaretskii wrote: > tags 25332 notabug > close 25332 > thanks > > > From: Chris Gregory > > Date: Mon, 02 Jan 2017 00:26:07 -0800 > > > > This patch changes while loops to for loops to make the code more > > consistent. > > I don't think I understand the rationale. Why is using 'while' > inconsistent? > > In any case, please don't bother making style changes in ralloc.c, as > that file should almost never be used in Emacs, except in some > marginal configurations, and we actually would like to get rid of it > altogether. It's therefore a waste of effort to try to make its style > better. > --94eb2c189cee436a7e05459be15e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
There are places in ralloc.c where a for loop is used to e= xpress a loop, and places where a while loop is used with the increment ope= ration at the end of it.=C2=A0 This would just simplify all code so that it= was consistently using a for loop for this purpose.

Wit= hout this patch here is a good usage of for:

= =C2=A0 heap_ptr heap;

=C2=A0 for (heap = =3D last_heap; heap; heap =3D heap->prev)
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 if (heap->start <=3D address && ad= dress <=3D heap->end)
return heap;
=C2=A0 =C2=A0 }

Here is a usage of while that should be a for = loop like the previous example:

=C2=A0 bloc_p= tr p =3D first_bloc;

=C2=A0 while (p !=3D NIL_BLOC= )
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 /* Consistency c= heck. Don't return inconsistent blocs.
Don't abort here, as = callers might be expecting this, but
callers that always expect a bl= oc to be returned should abort
if one isn't to avoid a memory co= rruption bug that is
difficult to track down. =C2=A0*/
= =C2=A0 =C2=A0 =C2=A0 if (p->variable =3D=3D ptr && p->data = =3D=3D *ptr)
return p;

=C2=A0 =C2=A0 =C2=A0 = p =3D p->next;
=C2=A0 =C2=A0 }

= Recommended new code:

=C2=A0 bloc_ptr p;

=C2=A0 for (p =3D first_bloc; p !=3D NIL_BLOC; p =3D p= ->next)
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 /* Cons= istency check. Don't return inconsistent blocs.
Don't abor= t here, as callers might be expecting this, but
callers that always = expect a bloc to be returned should abort
if one isn't to avoid = a memory corruption bug that is
difficult to track down. =C2=A0*/
=C2=A0 =C2=A0 =C2=A0 if (p->variable =3D=3D ptr && p->= ;data =3D=3D *ptr)
return p;
=C2=A0 =C2=A0 }

This style exact loop is even used later on (line 509 in u= pdate_heap_bloc_correspondence()):

=C2=A0 /* = Advance through blocs one by one. =C2=A0*/
=C2=A0 for (b =3D bloc= ; b !=3D NIL_BLOC; b =3D b->next)

--=C2=A0
Chris Gregory
=

On Sat, Jan 7, 2017 at 12:30 AM, Eli Zaretsk= ii <= eliz@gnu.org> wrote:
tags 2= 5332 notabug
close 25332
thanks

> From: Chris Gregory <czipperz= @gmail.com>
> Date: Mon, 02 Jan 2017 00:26:07 -0800
>
> This patch changes while loops to for loops to make the code more
> consistent.

I don't think I understand the rationale.=C2=A0 Why is using 'while= '
inconsistent?

In any case, please don't bother making style changes in ralloc.c, as that file should almost never be used in Emacs, except in some
marginal configurations, and we actually would like to get rid of it
altogether.=C2=A0 It's therefore a waste of effort to try to make its s= tyle
better.

--94eb2c189cee436a7e05459be15e--