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: Mon, 02 Jan 2017 00:26:07 -0800 Message-ID: <87h95hdg0w.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1483345640 12367 195.159.176.226 (2 Jan 2017 08:27:20 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 2 Jan 2017 08:27:20 +0000 (UTC) To: 25332@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Jan 02 09:27:16 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 1cNxxW-0001p9-1L for geb-bug-gnu-emacs@m.gmane.org; Mon, 02 Jan 2017 09:27:10 +0100 Original-Received: from localhost ([::1]:55867 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cNxxZ-0000NC-Of for geb-bug-gnu-emacs@m.gmane.org; Mon, 02 Jan 2017 03:27:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33511) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cNxxT-0000Mt-1P for bug-gnu-emacs@gnu.org; Mon, 02 Jan 2017 03:27:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cNxxO-0002pj-0U for bug-gnu-emacs@gnu.org; Mon, 02 Jan 2017 03:27:07 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:51840) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cNxxN-0002pd-Sx for bug-gnu-emacs@gnu.org; Mon, 02 Jan 2017 03:27:01 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cNxxN-0004vZ-Na for bug-gnu-emacs@gnu.org; Mon, 02 Jan 2017 03:27:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Chris Gregory Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 02 Jan 2017 08:27:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 25332 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.148334558518893 (code B ref -1); Mon, 02 Jan 2017 08:27:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 2 Jan 2017 08:26:25 +0000 Original-Received: from localhost ([127.0.0.1]:39006 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cNxwn-0004ue-5I for submit@debbugs.gnu.org; Mon, 02 Jan 2017 03:26:25 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:42420) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cNxwk-0004uR-QM for submit@debbugs.gnu.org; Mon, 02 Jan 2017 03:26:23 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cNxwe-0002id-9r for submit@debbugs.gnu.org; Mon, 02 Jan 2017 03:26:17 -0500 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:60161) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cNxwe-0002iX-79 for submit@debbugs.gnu.org; Mon, 02 Jan 2017 03:26:16 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33390) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cNxwc-0000DT-Ll for bug-gnu-emacs@gnu.org; Mon, 02 Jan 2017 03:26:15 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cNxwX-0002iC-Nw for bug-gnu-emacs@gnu.org; Mon, 02 Jan 2017 03:26:14 -0500 Original-Received: from mail-pg0-x244.google.com ([2607:f8b0:400e:c05::244]:35789) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cNxwX-0002i5-Hf for bug-gnu-emacs@gnu.org; Mon, 02 Jan 2017 03:26:09 -0500 Original-Received: by mail-pg0-x244.google.com with SMTP id i5so29869356pgh.2 for ; Mon, 02 Jan 2017 00:26:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version; bh=F0xe1nt9eO/xMga/xnv/JhLq/FQiE0kf9ajVGZ5JJfE=; b=OptVC1P3AbtUh/vJ+kxK9fMPEWwt7njGzY6KcxZ3oewmuxhXnvf3dqYGKVjn2B2MwG W2PgWOkhx2RRexBUowVABwOtbGnJQWJYYHb4kJ5ohNlySpytSpic5ryGzgFiM/a3D8I6 9desJc1dw1NNeL8jtsXI2uqMOJuVgDYaukaTHLnC5wFLOuVfWW8BCehR9wgqym2QlElP 6HRUeYTIn2uBNK3CScWSAy03JX2HtFCgsMJhk0B8a5j8UkOKfsq5GI4npuTpd0mfh99K fKXSNUkNc5O007rfwJ6XYcPpx32Q4sxC4hehcQXQBUIyvWsM+JcdvpywRs1B7UITEDyU hDzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=F0xe1nt9eO/xMga/xnv/JhLq/FQiE0kf9ajVGZ5JJfE=; b=MmF7z1LqsoS0l4tJyXjUeJn707cmmLVpVMbVFGgbfqtWfQBn890plzHFGb9qK+hPef Ty2ryxVfWAEYi13d7Wl4z9ssZMdTmGNvLa7mroapIH0qz8/19bKtP4oB+VDThnEe+JyL dJJFDnAZ+s4sKG/kl5YTvqnxKaxnk0E2J5+lbnv7Nc6kfxMh4I52GQpCO2NyokSx6Wel bm8ttHbXXRjdi/LSOFt4EKboPj80QUKcPVEpkpb4NPruW3wmPy7WH9E4Rgn6XzdWFsYQ Ei0zeFxzDq6atWDQ+MKIdFSDZkwN2MwINDwDMLF3YsiOPART/rivC9zHXdoTLuhnRS2u 6hbA== X-Gm-Message-State: AIkVDXJaISMXEQJB8GhKL5UxQ3eMn4HsKTDLdeLz/E6UiNAHsZPKJAgJuccVrLD5bERCYg== X-Received: by 10.84.232.137 with SMTP id i9mr87824961plk.95.1483345568493; Mon, 02 Jan 2017 00:26:08 -0800 (PST) Original-Received: from CzipperZeroArch (c-50-139-114-145.hsd1.or.comcast.net. [50.139.114.145]) by smtp.gmail.com with ESMTPSA id y15sm130744019pgc.43.2017.01.02.00.26.07 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 02 Jan 2017 00:26:08 -0800 (PST) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x 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:127672 Archived-At: This patch changes while loops to for loops to make the code more consistent. -- Chris Gregory diff --git a/src/ralloc.c b/src/ralloc.c index 8a3d2b797f..ccc019618c 100644 --- a/src/ralloc.c +++ b/src/ralloc.c @@ -222,13 +222,10 @@ obtain (void *address, size_t size) /* If we can't fit SIZE bytes in that heap, try successive later heaps. */ - while (heap && (char *) address + size > (char *) heap->end) - { - heap = heap->next; - if (heap == NIL_HEAP) - break; - address = heap->bloc_start; - } + for (; heap && (char *) address + size > (char *) heap->end; + address = heap->bloc_start) + if ((heap = heap->next) == NIL_HEAP) + break; /* If we can't fit them within any existing heap, get more space. */ @@ -350,20 +347,16 @@ relinquish (void) static bloc_ptr find_bloc (void **ptr) { - 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; - } + 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) + break; return p; } @@ -430,13 +423,13 @@ get_bloc (size_t size) static int relocate_blocs (bloc_ptr bloc, heap_ptr heap, void *address) { - bloc_ptr b = bloc; + bloc_ptr b; /* No need to ever call this if arena is frozen, bug somewhere! */ if (r_alloc_freeze_level) emacs_abort (); - while (b) + for (b = bloc; b; b = b->next) { /* If bloc B won't fit within HEAP, move to the next heap and try again. */ @@ -452,17 +445,13 @@ relocate_blocs (bloc_ptr bloc, heap_ptr heap, void *address) get enough new space to hold BLOC and all following blocs. */ if (heap == NIL_HEAP) { - bloc_ptr tb = b; + bloc_ptr tb; size_t s = 0; /* Add up the size of all the following blocs. */ - while (tb != NIL_BLOC) - { - if (tb->variable) - s += tb->size; - - tb = tb->next; - } + for (tb = b; tb != NIL_BLOC; tb = tb->next) + if (tb->variable) + s += tb->size; /* Get that space. */ address = obtain (address, s); @@ -477,7 +466,6 @@ relocate_blocs (bloc_ptr bloc, heap_ptr heap, void *address) b->new_data = address; if (b->variable) address = (char *) address + b->size; - b = b->next; } return 1; @@ -535,13 +523,11 @@ update_heap_bloc_correspondence (bloc_ptr bloc, heap_ptr heap) /* If there are any remaining heaps and no blocs left, mark those heaps as empty. */ - heap = heap->next; - while (heap) + for (heap = heap->next; heap; heap = heap->next) { heap->first_bloc = NIL_BLOC; heap->last_bloc = NIL_BLOC; heap->free = heap->bloc_start; - heap = heap->next; } } @@ -578,12 +564,9 @@ resize_bloc (bloc_ptr bloc, size_t size) /* Note that bloc could be moved into the previous heap. */ address = (bloc->prev ? (char *) bloc->prev->data + bloc->prev->size : (char *) first_heap->bloc_start); - while (heap) - { - if (heap->bloc_start <= address && address <= heap->end) - break; - heap = heap->prev; - } + for (; heap; heap = heap->next) + if (heap->bloc_start <= address && address <= heap->end) + break; if (! relocate_blocs (bloc, heap, address)) { @@ -1084,7 +1067,7 @@ r_alloc_check (void) if (pb && pb->data + pb->size != b->data) { assert (ph && b->data == h->bloc_start); - while (ph) + for (; ph; ph = ph->prev) { if (ph->bloc_start <= pb->data && pb->data + pb->size <= ph->end) @@ -1093,10 +1076,7 @@ r_alloc_check (void) break; } else - { - assert (ph->bloc_start + b->size > ph->end); - } - ph = ph->prev; + assert (ph->bloc_start + b->size > ph->end); } } pb = b; @@ -1120,18 +1100,14 @@ r_alloc_check (void) void r_alloc_reset_variable (void **old, void **new) { - bloc_ptr bloc = first_bloc; + bloc_ptr bloc; /* Find the bloc that corresponds to the data pointed to by pointer. find_bloc cannot be used, as it has internal consistency checks which fail when the variable needs resetting. */ - while (bloc != NIL_BLOC) - { - if (bloc->data == *new) - break; - - bloc = bloc->next; - } + for (bloc = first_bloc; bloc != NIL_BLOC; bloc = bloc->next) + if (bloc->data == *new) + break; if (bloc == NIL_BLOC || bloc->variable != old) emacs_abort (); /* Already freed? OLD not originally used to allocate? */