Linux Kernel Coding Style
1. 缩进
缩进用 Tab, 并且Tab的宽度为8个字符
swich 和 case对齐, 不用缩进
switch (suffix) {
case 'G':
case 'g':
mem <<= 30;
break;
case 'M':
case 'm':
mem <<= 20;
break;
case 'K':
case 'k':
mem <<= 10;
/* fall through */
default:
break;
}
一行只有一个表达式
if (condition) do_this; /* bad example */
不要用空格来缩进 (除了注释或文档)
2. 代码行长度控制在80个字符以内
长度过长的行截断时, 注意保持易读性
void fun(int a, int b, int c)
{
if (condition)
printk(KERN_WARNING "Warning this is a long printk with "
"3 parameters a: %u b: %u "
"c: %u \n", a, b, c);
else
next_statement;
}
3. 括号和空格的位置
函数的大括号另起一行
int function(int x)
{ /* 这个大括号 { 另起了一行 */
body of function
}
非函数的语句块(if, switch, for, while, do)不用另起一行
if (x is