From owner-svn-src-head@freebsd.org Wed Apr 20 18:45:02 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id E69A5B16440; Wed, 20 Apr 2016 18:45:02 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from bigwig.baldwin.cx (bigwig.baldwin.cx [IPv6:2001:470:1f11:75::1]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id C1E54132D; Wed, 20 Apr 2016 18:45:02 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from ralph.baldwin.cx (c-73-231-226-104.hsd1.ca.comcast.net [73.231.226.104]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id 81E9CB98D; Wed, 20 Apr 2016 14:45:01 -0400 (EDT) From: John Baldwin To: Bruce Evans Cc: araujo@freebsd.org, "src-committers@freebsd.org" , "svn-src-all@freebsd.org" , "svn-src-head@freebsd.org" Subject: Re: svn commit: r298247 - head/sbin/fdisk_pc98 Date: Wed, 20 Apr 2016 10:38:06 -0700 Message-ID: <17068472.zUDTmEYeVg@ralph.baldwin.cx> User-Agent: KMail/4.14.3 (FreeBSD/10.2-STABLE; KDE/4.14.3; amd64; ; ) In-Reply-To: <20160420115844.Y967@besplex.bde.org> References: <201604190446.u3J4kD9G050780@repo.freebsd.org> <20160420115844.Y967@besplex.bde.org> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (bigwig.baldwin.cx); Wed, 20 Apr 2016 14:45:01 -0400 (EDT) X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Apr 2016 18:45:03 -0000 On Wednesday, April 20, 2016 01:06:38 PM Bruce Evans wrote: > On Wed, 20 Apr 2016, Marcelo Araujo wrote: > > > 2016-04-20 0:16 GMT+08:00 John Baldwin : > > > >> On Tuesday, April 19, 2016 04:46:13 AM Marcelo Araujo wrote: > >>> Author: araujo > >>> Date: Tue Apr 19 04:46:13 2016 > >>> New Revision: 298247 > >>> URL: https://svnweb.freebsd.org/changeset/base/298247 > >>> > >>> Log: > >>> Remove redundant parenthesis. > >>> > >>> Submitted by: pfg > >>> MFC after: 2 weeks. > > I don't realling like churnging to the nonstandard nitems(). Use > of the nonstandard is bad enough. I think it's not that bad from a readability standpoint. Other languages have fairly concise syntax for 'for-each' loops and this provides a closer variant of that for statically sized arrays. TAILQ_FOREACH() is still nicer of course. One could imagine doing some sort of ARRAY_FOREACH() that was: #define ARRAY_FOREACH(p, array) \ for (size_t __i = 0, (p) = &(array)[0]; __i < nitems((array)); __i++, (p)++) (This requires C99 to handle __i) Perhaps better is this: #define ARRAY_FOREACH(p, array) \ for ((p) = &(array)[0]; (p) < &(array)[nitems((array))]; (p)++) (No need for __i) > >> For this case, it might be better to remove numentries and use > >> nitems() directly in the one place it is used. I would probably > >> even do this as a for-loop: > >> > >> struct part_type *ptr; > >> int counter; > >> > >> for (counter = 0, ptr = part_types; counter < nitems(part_types); > >> counter++, ptr++) { > >> if (ptr->type == (type & 0x7f)) > >> return (ptr->name); > >> } > >> return ("unknown"); > >> > >> If you renamed 'counter' to 'i' you could probably fit it all on one line. > > 'ptr' is also not a usefully verbose name. If its name is longer than that > of 'p', then it could more usefully give a hint of the pointer type (pp or > ptp). > > nitimems() is not even easy to use. It is of course undocumented, but > if we look at its internals we can see that its type is the binary > promotion of size_t. This type is normally size_t again, thus normally > unsigned. Broken compilers might warn about this. Only broken ones > would, since it is clear that 'counter' always has a small non-negative > value. Such warnings are often "fixed" by unimproving the code using > casts. Here the old code uses a temporary variable of type int. > Consistently broken compilers might warn about assigning the unsigned > expression to this signed variable. Yes, I end up using 'unsigned' with it often due to compile warnings for sign mismatches on comparison. > Churnging too much (also remove excessive parentheses and braces) gives: > > size_t i; /* XXX: I don't like unsigned types, but... */ > > for (i = 0; i < nitems(part_types); i++) > if (part_types[i].type == type & 0x7f) > return (part_types[i].name); > return ("unknown"); This would work for me (unless folks actually like the ARRAY_FOREACH() idea). -- John Baldwin