[libvirt] proposal: allow use of "bool": HACKING: discuss C types
Daniel P. Berrange
berrange at redhat.com
Wed Dec 17 12:16:46 UTC 2008
On Fri, Dec 12, 2008 at 09:58:32AM +0100, Jim Meyering wrote:
> Hello,
>
> I don't want to make waves, but I do care about certain aspects
> of code quality, so...
>
> I propose to allow (encourage, even) the use of the standard C99 type,
> bool, in libvirt, but not in public interfaces[1]. There are already
> uses in cgroup.c and xmlrpc.c, as well as those in the gnulib "c-ctype.h"
> header which is included from many of libvirt's .c files.
>
> The motivation is to make the code as readable as possible.
> When you see the declaration of an "int" variable, member, or function,
> that type gives you no clue whether it is used as a boolean. If you
> see a few uses that treat it as boolean, that's still no guarantee,
> and it may be non-trivial to ensure that there isn't some non-boolean
> value with a special meaning.
>
> However, if you see a "bool" variable, you do have that guarantee.
I don't particularly like the idea of using the bool type
- No system header files use it
- Library header files which use a boolean type have nearly all defined
their own custom bool types, not using stdbool.h and there's no guarentee
that stdbool's idea of 'true' matches the other apps'
- We don't use it in the public API, or on the wire for the remote
protocol, since it has undefined size.
- The GNULIB bool emulation is unable to provide equivlance between
C's idea of true (any non-zero value) and the defined 'true'
constant (whose value is 1)
- Bitfields are more size efficient than bools.
In a language like python where there's a fundamental builtin type in
the language for 'bool' this it makes sense. In the C world with many
many[1] different definitions of bool, true and false it just feels
like a world of hurt. The only thing you can ultimately rely on is that
a true value is non-zero.
Daniel
[1] Picking a *tiny* selection of the bool's in my dev box headers
/usr/include/xulrunner-sdk-1.9/stable/jritypes.h:typedef enum JRIBoolean {
/usr/include/xulrunner-sdk-1.9/stable/nptypes.h: typedef int bool;
/usr/include/xulrunner-sdk-1.9/stable/npapi.h:typedef unsigned char NPBool;
/usr/include/xulrunner-sdk-1.9/stable/jri_md.h:typedef unsigned char jbool;
/usr/include/wine/windows/wtypes.idl:typedef long BOOL;
/usr/include/cups/raster.h:typedef enum cups_bool_e /**** Boolean type ****/
/usr/include/lcms.h:typedef int LCMSBOOL;
/usr/include/mp4.h:typedef int bool;
/usr/include/tss/tpm.h:typedef BYTE TPM_BOOL;
/usr/include/tss/platform.h: typedef int8_t TSS_BOOL;
/usr/include/tss/platform.h: typedef signed char TSS_BOOL;
--
|: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :|
|: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
More information about the libvir-list
mailing list