Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 4 Dec 2011 08:52:18 -0800
From:      mdf@FreeBSD.org
To:        John Baldwin <jhb@freebsd.org>, freebsd-arch@freebsd.org,  Zack Kirsch <zack@freebsd.org>, mdf@freebsd.org
Subject:   Re: Use of bool / stdbool.h in kernel
Message-ID:  <CAMBSHm-opYiyPqz0YVSYdOKHA7fbN=ZxOmMYC2Q_U4R7iFHOew@mail.gmail.com>
In-Reply-To: <20111201014944.GA78010@zim.MIT.EDU>
References:  <CAMBSHm_Be0hCimgg0KpCFs24MHOW=LBczJbFZ3F1cOaCgrS8LA@mail.gmail.com> <20111130154604.B949@besplex.bde.org> <201111301032.04102.jhb@freebsd.org> <20111201014944.GA78010@zim.MIT.EDU>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Nov 30, 2011 at 5:49 PM, David Schultz <das@freebsd.org> wrote:
> On Wed, Nov 30, 2011, John Baldwin wrote:
>> On Wednesday, November 30, 2011 12:13:53 am Bruce Evans wrote:
>> > On Tue, 29 Nov 2011 mdf@freebsd.org wrote:
>> >
>> > > At $WORK we have a hack in one of the *.mk files to allow including
>> > > stdbool.h in the kernel and we use it extensively. =A0This is not
>> > > allowed by style(9), as far as I can tell, because the file is in
>> > > include/stdbool.h and those files are not allowed to be included in
>> > > kernel sources.
>> >
>> > Including stdbool.h in the kernel is not a style bug, but unsupported.
>> >
>> > > What I want to check on is, would it be acceptable to move stdbool.h
>> > > from include/stdbool.h to sys/sys/stdbool.h (i.e. like errno.h) and
>> > > then include it in the kernel as <sys/stdbool.h>? =A0That is, is the
>> >
>> > Would be a larger style bug, especially if it were actually used.
>> > Even its spellings of TRUE and FALSE are strange. =A0Even in userland
>> > stdbool.h is considered so useful that it is never used in src/bin
>> > and is only used a few times on other src/*bin. =A0src/bin never uses
>> > TRUE of FALSE either.
>>
>> I suspect there is some bias here though due to the fact that there wasn=
't
>> a standard bool type when most of this code was written. :) =A0I don't t=
hink
>> that means we have to forgo use of the new type now that it is in fact
>> standardized in C99. =A0I would be happy to have 'bool' available and th=
e
>> lowercase 'true' and 'false' are fine with me.
>
> The lowercase 'true' and 'false' are intended to mimic C++, where
> they are keywords. =A0Regardless of how you prefer to capitalize
> them, using them instead of 0 and 1 makes the intent much clearer.
> This is especially true in the kernel, where non-zero could mean
> true, or it could be an error code.
>
> Unfortunately, the "new type" is mostly useless, aside from
> improving readability. =A0Unlike modern languages, C doesn't
> consider it a compile-time error to mix up bools and ints.

Partly true.  In addition to changing the semantics of the ++ and --
operators, _Bool is usually implemented as an 8-bit type and so
pointers-to-bool and pointers-to-int are not compatible.  Also, C (or
at least gcc? I don't know my C99 standard the way I know C89) will
apply default promotion and casting rules, so that e.g. an assignment
like:

int a;
bool b;

a =3D 2;
b =3D a;
printf("%d", b);

will print 1.

Thanks,
matthew



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAMBSHm-opYiyPqz0YVSYdOKHA7fbN=ZxOmMYC2Q_U4R7iFHOew>