svn commit: r236917 - head/sys/kern

Bruce Evans brde at optusnet.com.au
Wed Jun 13 02:38:10 UTC 2012


On Tue, 12 Jun 2012, Pawel Jakub Dawidek wrote:

> On Tue, Jun 12, 2012 at 12:53:47PM +1000, Bruce Evans wrote:
>> On Mon, 11 Jun 2012, Pawel Jakub Dawidek wrote:
>>> -        KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
>>> +        KASSERT((unsigned int)fd < fdp->fd_nfiles,
>>>             ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
>>> 	return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
>>> }
>>
>> This is backwards.  Apart from using the worst possible (most verbose)
>> spelling of `unsigned', it uses a type hack manually optimize away the
>> test for fd being < 0.  The compiler will do this "optimization"
>> automatically if it is any good (or undo it if it is not), so all the
>> hack does is obfuscate the test.  With the verbose spelling of u_int,
>> it even takes more space.
>
> Well, to be honest I presonally would prefer explicit check for fd being
> less than 0, but my impression was that using cast is the most popular
> way and I wanted this check to be consistent across our source tree.
>
> Feel free to change it.

I'm only free to ask you to back out it out.

> BTW. I really dislike using 'unsigned' with omitted 'int'. u_int is fine.

I really dislike 'unsigned int'.  The int in it is just noise, as in
'long int'.

>From K&R 1: p34: "The declarations for the qualifiers look like [...]
unsigned int x;  The word int can be omitted in such situations, and
typically is."  p45: example that uses plain unsigned.  p183: "Unisgned
integers, declared unsigned, ...".  p193: [semi-formal grammar]:
"type-specifier: char, short, int, long, unsigned, float, double,
struct-or-union-specifier, typedef-name.  [short int, long int,
unsigned int, long float are also acceptable]".

>From K&R 2: p36: same as above p34.  p49: similar to above p45 (now
missing in index; no longer "implicit int" for the function return
type; excessive use of unsigned fixed).  p196: similar to above p183
(now says "...declared using the unsigned keyword).  p211: similzr to
above p193 (add void, signed, enum-specifier; remove long float, and
tighten up the description of which combinations are allowed in other
ways).

Other interesting points from K&R:
- according to the grammar, both `unsigned' and 'signed' are full-fledged
   types, not qualifiers for integer types.  You can even write plain
   'signed' meaning 'signed int', but no one does that.
- K&R very rarely uses `unsigned'.  This shows that it should rarely be
   used.  I could only find the above examples in it (these are all that
   are in the index for K&R 1), plus 1 more a page later than the p45/49
   one (the index is broken in a different way for this -- in K&R 1 the
   one on p46 is not indexed, while in K&R 2 the one on p49 is not
   indexed).

Bruce


More information about the svn-src-all mailing list