Use of bool / stdbool.h in kernel

Bruce Evans brde at optusnet.com.au
Wed Nov 30 05:13:59 UTC 2011


On Tue, 29 Nov 2011 mdf at 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.  This 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>?  That is, is the

Would be a larger style bug, especially if it were actually used.
Even its spellings of TRUE and FALSE are strange.  Even 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.  src/bin never uses
TRUE of FALSE either.

> objection / non-use because of where the file is located in the
> repository, or is there some other reason?  Note that the pre-C99
> boolean_t and TRUE/FALSE are spread over the kernel, mostly in sys/vm
> where I assume they come from old AT&T sources.

Thes came from mach vm.  Pure BSD code never uses them.  For example,
in 4.4BSD-Lite2 /sys/kern, FALSE is used 3 times, all to call vm functions
whose API uses it, while TRUE is used 3 times, once for a vm call and
twice in shell scripts where it is an unquoted string.  Pure BSD code also
never uses booleans.  It uses explicit comparisons with 0, which is
equivalent to always spelling FALSE as 0 and both !FALSE and TRUE as
either 1 or another literal more-magic number  or "!= 0" (never !0 or
!(expr) since those are booleans !!).

In 1995, I made the mistake of moving the mach definitions of FALSE and
TRUE and the declaration of boolean_t from a vm header to <sys/types.h>,
because I wanted to use them more globally and I didn't know BSD style
very well at the time.  Especially that it never uses booleans.

4.4Lite2 could not have used C99's <stdbool.h> since it is older than
C99, and it didn't use its own <mumble-bool.h> since it preferred
explicit 0's and 1's.  You may wish to change this style, but then all
old BSD code would not conform to the new style.

Bruce


More information about the freebsd-arch mailing list