What coding style should I use?

If you are working on a new piece of code, you should follow the coding style from the file Documentation/CodingStyle in your kernel source directory.

When working on an existing piece of code, follow the coding style from that code. After you edit a file, a reader should not be able to tell just from coding style which parts you worked on.

What do I do if my patch got rejected due to the coding style?

(answer lifted from a Plan9 wiki)

The patch may well come back in the "sorry" category rather than the "applied" category, with suggestions on what should be changed to make it better and a request to resubmit it. This doesn't mean you should give up. It means you did a good job and the maintainers think it's worth trying to get you to make the job even better. Remember: you can't polish a turd.

Should I initialize variables?

(anwer graciously provided by Jesper Juhl)

Performance is certainly one reason [for not immediately initializing a variable value]. An example is kmalloc() vs kzalloc() - kmalloc() doesn't initialize the allocated memory but kzalloc() does. If you are allocating memory for a buffer and you know that whatever you pass the buffer to will overwrite all of it anyway (or at least some of it and you'll know how much) then having kmalloc() initialize it all to zero first would just be a waste of time. On the other hand, when you are allocating some memory and you possibly only write to part of it and the rest absolutely need to be zeroed, then it's probably a good idea to just use kzalloc() instead so you don't have to wory about adding logic to zero the remaining part of the buffer.

So, you have different API's for different needs.

In the case of this patch, we are talking about a automatic variable.

In C automatic variables are not initialized unless you do it explicitly. And if you have code like

int a;
if (some_condition)
   a = 1;
   a = 2;

then it would be completely pointless to have the compiler initialize the variable at the declaration since it would always be overwritten. Variables with explicit initialization also take up a bit of space in the kernel image file and there's no reason to waste memory if it's not needed.

So, to save on .text size, to save on CPU cycles doing pointless initializations etc variables and other storage is only initialized when the following code won't always do it.


KernelNewbies: FAQ/CodingStyle (last edited 2017-12-30 01:30:09 by localhost)