[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: static functions are your friends (Was GPIO revisited)
On Jul 25, 2009, at 3:53 AM, Marc Balmer wrote:
One more comment...
+int gpio_pinbyname(struct gpio_softc *, char *gp_name);
+/* Old API version */
+int gpio_ioctl_oapi(struct gpio_softc *, u_long cmd, void
*data, int flag,
+ int pinset);
KNF: no variables names in the declaration.
Also, any reason why these are not static?
While it certainly can make sense to declare functions static in
some userland programs for the sole reason to include a command in
a crunched binary, it does not make sense in kernel code; much to
the contrary, it makes debugging harder.
static functions make great sense in kernel code. In fact, we have
too many public functions as is and even more of the kernel should
be using static.
When a component only has a relatively small on entry points, it
allows the compiler to treat the component as one single unit to
compiled. This allows it to inline, follow the scope of variables,
and lots of other thing to generate better code.
The other reason to use static it prevents code from calling your
function that should never have never called it in the first place.
I want to quickly come back to this and explain my reasoning for not
making all functions static in the kernel, although I am totally aware
of all the positive effects of static in general.
I do mostly low-level driver development, something where you see the
ddb prompt more often than you like... Now with the functions not
being declared static, the trace command is my friend, I see
immediately in which function my driver crashed, this is an immense
I took the time to write a function that crahses the kernel on
purpose. Compiled as non static function, ddb showed me the function
name and function call trace with proper function names. Quite easy
to locate the faulty spot. To complete my test, I compiled the very
same code with all functions declared static. Now the ddb trace does
not show me the function names, making it a lot harder to locate the
To try to avoid name clashes I prefix all functions with the driver/
subsystem/whatever name to avoid someone accidentally using the same
name. And of course function names should not something that someelse
would likely use as well, like "error()" or whatever stupid choice.
Why do I tell you this? Just to make clear that making not every
function static is a deliberate choice with good reasoning for my
work. I think there is good reason why style mentions that functions
not used anywhere else "should" be static, and not "must" be static.
- Marc Balmer
Main Index |
Thread Index |