Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > muc.lists.netbsd.source-changes > #156200

Re: CVS commit: src/sys/uvm

From Taylor R Campbell <riastradh@NetBSD.org>
Newsgroups muc.lists.netbsd.source-changes
Subject Re: CVS commit: src/sys/uvm
Date 2026-07-07 14:07 +0000
Organization Newsgate at muc.de e.V.
Message-ID <20260707140751.7ECDD84DB1@mail.netbsd.org> (permalink)
References <CAKDaYtB7EH5nxYer3Ch5+oukiq6B20xgxSPBBZ=EDpvy2=M=Ag@mail.gmail.com>

Show all headers | View raw


> Date: Mon, 6 Jul 2026 00:14:59 +0900
> From: Takashi YAMAMOTO <yamt9999@gmail.com>
> 
> @@ -900,7 +897,7 @@ uvm_swap_stats(char *ptr, int misc,
>         error = 0;
>         count = 0;
>         sp = sdps;
> -       while (misc-- > 0) {
> +       while (misc --> 0) {
> 
> this style change is quite surprising to me. it it intentional?

This is the best operator in C!  --> is the `goes down to' operator.

If you're iterating _forward_ over an array of length N, the usual
idiom is:

	for (i = 0; i < N; i++) {
		... i ...
	}

This loop runs for exactly N iterations; inside the loop body, i is
always a valid index into the array; and it works with both signed and
unsigned arithmetic, and with arbitrary [start,end) intervals instead
of [0,N).

What if you want to iterate _backward_ over an array of length N?

You could try something like

	for (i = N; i > 0; i--) {
		... i - 1 ...
	}

but then your code is full of -1 or +1 that are hard to follow in the
body and make it look like there are fencepost errors everywhere.  You
could maybe fix that with

	for (i = N - 1; i >= 0; i--) {
		... i ...
	}

but now the loop head looks like it has fencepost errors, and it only
works if i is signed.  You could use

	for (i = N; --i >= 0;)
or
	for (i = N; i-- > 0;)

but who can remember whether post-increment or pre-increment is
correct, and whether to use >= or > with them, and which way makes
fencepost errors, and which one works in unsigned arithmetic?

Better approach, much more memorable and easy to read: use the `goes
down to' operator!

	for (i = N; i --> 0;) {
		... i ...
	}

As with forward iteration, this loop runs for exactly N iterations;
inside the loop body, i is always a valid index into the array; and it
works with both signed and unsigned arithmetic, and with arbitrary
[start,end) intervals instead of [0,N).

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de

Back to muc.lists.netbsd.source-changes | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: CVS commit: src/sys/uvm Takashi YAMAMOTO <yamt9999@gmail.com> - 2026-07-06 00:14 +0900
  Re: CVS commit: src/sys/uvm Taylor R Campbell <riastradh@NetBSD.org> - 2026-07-07 14:07 +0000

csiph-web