What is the point of invbool in C kernel programming?

Viewed 129

I'm beginning to do a bit more kernel programming, and in reading, found the existence of invbool. However, I can't seem to find anything on the Internet about it, except for the documented definition, copied nearly verbatim in a few places:

The invbool type inverts the value, so that true values become false and vice versa.

Thinking about this, I can't think of an instance where this would make code less confusing. Is there a good example of when you would use this? Why does it only seem to come up in kernel programming documentation?

1 Answers

The invbool functions are used in parsing kernel command lines and module options. The bool versions parse an option which the user can set as 0/1 or N/Y or off/on, and set a bool flag accordingly. The invbool versions apply a ! to the result, so Y maps to false and N maps to true.

As far as I can tell, their only purpose is for convenience or "clarity" when the programmer wants to have an internal flag with the opposite sense from the user's configuration parameter. For instance, maybe it makes more sense from the user's perspective to be able to set a flag to disable the foo option (because it should be enabled by default), but from the kernel programmer's perspective it is more convenient to have a flag that simply says whether foo is enabled. You could then have a user-set parameter called disable_foo and an internal boolean flag called foo_is_enabled. You want foo_is_enabled = false when the user specifies disable_foo=Y, so you could use invbool here.

You can see something like this happening in aty128fb.c: the user can set the nomtrr parameter to disable the use of the memory type range registers, but the programmer preferred to have a boolean variable mtrr that is true if MTRRs are enabled.

You might think these are redundant because you could just do mtrr = !parameter_bool("nomtrr"), but in fact the param_set_* functions return the parameter values by reference instead of by value, so you can't so easily just plop a ! into the expression.

I suspect the thought was that defining these functions made such code more "self-documenting". This is of course debatable.

Related