[中英对照]Linux kernel coding style | Linux内核编码风格

Posted veli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[中英对照]Linux kernel coding style | Linux内核编码风格相关的知识,希望对你有一定的参考价值。

Linux kernel coding style | Linux内核编码风格

This is a short document describing the preferred coding style for the linux kernel. Coding style is very personal, and I won\'t force my views on anybody, but this is what goes for anything that I have to be able to maintain, and I\'d prefer it for most other things too. Please at least consider the points made here.
本文是一个简短的文档,描述了Linux内核的首选的编码风格。编码风格是非常个人化的东东,不强迫任何人接受我的观点。但是,这是我必须能够坚持的东西,也希望它对其他大多数事情也有帮助。请至少考虑一下本文谈及的要点。

First off, I\'d suggest printing out a copy of the GNU coding standards, and NOT read it. Burn them, it\'s a great symbolic gesture.
首先,建议把GNU的编码标准打印一份出来,然后不要阅读,直接烧掉。这是一个了不起的象征姿态。

Anyway, here goes:
言归正转:

1) Indentation | 缩进

Tabs are 8 characters, and thus indentations are also 8 characters. There are heretic movements that try to make indentations 4 (or even 2!) characters deep, and that is akin to trying to define the value of PI to be 3.
制表符(Tab)占8个字符的位置,因此缩进也是占8个字符的位置。将缩进宽度设置为4(甚至为2!)被认为是一种异教徒发起的运动,类似于将PI的值定义为3。

Rationale: The whole idea behind indentation is to clearly define where a block of control starts and ends. Especially when you\'ve been looking at your screen for 20 straight hours, you\'ll find it a lot easier to see how the indentation works if you have large indentations.
原理: 隐藏在缩进背后的思想是清楚地定义一个控制块的开始和结束的位置。尤其是当你盯着屏幕看了20个小时之后,你会发现,看到的缩进如果有较大的缺口的话,看起来就更容易。

Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you\'re screwed anyway, and should fix your program.
那么,有些人会抱怨8个字符的缩进使得代码向右边移动得太远了,对一个宽度为80个字符的终端屏幕来说,让人很难阅读下去。答案是,如果你需要超过3个级别的缩进,无论如何,你都要想办法了,而且应该修正你的程序。

In short, 8-char indents make things easier to read, and have the added benefit of warning you when you\'re nesting your functions too deep. Heed that warning.
简而言之,宽度为8个字符的缩进更适合阅读,而且带来额外的好处,那就是当你的函数嵌套太深时给出警告。请认真对待这种警告。

The preferred way to ease multiple indentation levels in a switch statement is to align the switch and its subordinate case labels in the same column instead of double-indenting the case labels. E.g.:
在switch语句中消除多级缩进的首选方法是,使case标签与switch对齐,用单缩进代替双缩进。例如:

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;
}

Don’t put multiple statements on a single line unless you have something to hide:
不要把多个语句放在一行中,除非你有什么需要隐藏起来:

if (condition) do_this;
  do_something_everytime;

Don’t put multiple assignments on a single line either. Kernel coding style is super simple. Avoid tricky expressions.
不要把多个变量分配放在同一行中。内核编码风格可谓超级简单。而且需要避免花里胡哨的表达。

Outside of comments, documentation and except in Kconfig, spaces are never used for indentation, and the above example is deliberately broken.
除注释、文档和Kconfig除外,不要使用空格来缩进。上面的例子之所以破了例,因为是故意的。

Get a decent editor and don’t leave whitespace at the end of lines.
使用一个好的编辑器,请不要在行尾留空白(空格或Tab键)。

2) Breaking long lines and strings | 打破过长的代码行和字符串

Coding style is all about readability and maintainability using commonly available tools.
编码风格是关于使用常用工具时的可读性可维护性

The limit on the length of lines is 80 columns and this is a strongly preferred limit.
单行代码的宽度限制为80列,这是一个强有力且为首选的限制。

Statements longer than 80 columns will be broken into sensible chunks, unless exceeding 80 columns significantly increases readability and does not hide information. Descendants are always substantially shorter than the parent and are placed substantially to the right. The same applies to function headers with a long argument list. However, never break user-visible strings such as printk messages, because that breaks the ability to grep for them.
超过80列的语句将被分解为更明智的代码块,除非超过80列显著地增加可读性并且没有隐藏信息。后代语句总是比父语句短得多,大体上都被放置在右边。这同样适用于带有长参数列表的函数头。然而,请永远不要中断用户可见的字符串,例如printk的消息,因为一旦中断了,就打破了他们被grep到的能力。

3) Placing Braces and Spaces | 括号和空格的位置

The other issue that always comes up in C styling is the placement of braces. Unlike the indent size, there are few technical reasons to choose one placement strategy over the other, but the preferred way, as shown to us by the prophets Kernighan and Ritchie, is to put the opening brace last on the line, and put the closing brace first, thusly:
经常出现在C编码风格中的另一个问题就是大括号({})的位置问题。与缩进大小不同的是,在选择放置大括号的时候,鲜有技术上的原因,而是基于某种偏好。例如,我们的先知(C语言教父)K&R,就是把‘{’放在一行的结尾,而把‘}’放在一行的开始。

if (x is true) {
        we do y
}

This applies to all non-function statement blocks (if, switch, for, while, do). E.g.:
这适用于所有非函数语句块(if, switch, for, while, do)。例如:

switch (action) {
case KOBJ_ADD:
        return "add";
case KOBJ_REMOVE:
        return "remove";
case KOBJ_CHANGE:
        return "change";
default:
        return NULL;
}

However, there is one special case, namely functions: they have the opening brace at the beginning of the next line, thus:
但是,有一个例外,那就是函数:开括号\'{\'放置在函数的下一行的开头,因此:

int function(int x)
{
        body of function
}

Heretic people all over the world have claimed that this inconsistency is ... well ... inconsistent, but all right-thinking people know that (a) K&R are right and (b) K&R are right. Besides, functions are special anyway (you can\'t nest them in C).
分布在世界各地的异教徒们都认为这种不一致不好。但是,所有坚持正确思考的人们都知道(a)K&R是正确的,(b)K&R是正确的。此外,无论如何函数都是特殊的(你不能在C语言中嵌套它们)。

Note that the closing brace is empty on a line of its own, except in the cases where it is followed by a continuation of the same statement, ie a while in a do-statement or an else in an if-statement, like this:
注意右大括号\'}\'本身是空的,除非后面的语句是连续的,也就是说,在do...while...的while中或if...else...的else中,例如:

do {
        body of do-loop
} while (condition);

and

if (x == y) {
        ..
} else if (x > y) {
        ...
} else {
        ....
}

Rationale: K&R.
理论依据: K&R。

Also, note that this brace-placement also minimizes the number of empty (or almost empty) lines, without any loss of readability. Thus, as the supply of new-lines on your screen is not a renewable resource (think 25-line terminal screens here), you have more empty lines to put comments on.
另外,注意这种大括号的放置方法也减少了空行数(或几乎为空的行数),没有损失任何可读性。因此,由于屏幕上新行的供应不是可再生资源(此处考虑25-行终端屏幕),所以您有更多的空行用于存放注释。

Do not unnecessarily use braces where a single statement will do.
只有一行的语句块不用大括号。

if (condition)
        action();

and

if (condition)
        do_this();
else
        do_that();

This does not apply if only one branch of a conditional statement is a single statement; in the latter case use braces in both branches:
如果if用了大括号, 那么else即使只有一行也要用大括号。

if (condition) {
        do_this();
        do_that();
} else {
        otherwise();
}

3.1) Spaces | 空格

Linux kernel style for use of spaces depends (mostly) on function-versus-keyword usage. Use a space after (most) keywords. The notable exceptions are sizeof, typeof, alignof, and __attribute__, which look somewhat like functions (and are usually used with parentheses in Linux, although they are not required in the language, as in: sizeof info after struct fileinfo info; is declared).
Linux内核编码风格在使用空白的问题上主要取决于函数v.s.关键字。在(大部分)关键字的后面要使用空格。明显的例外是sizeof、typeof、alignof和__attribute__,这看起来有点像函数(在Linux中,通常在这些关键字后添加括号,虽然他们不是C语言所必须的。例如:在C语言中,声明struct fileinfo info之后,可以使用sizeof info取得结构体变量info的长度。当然,我们的编码风格则是使用sizeof(info)去取得结构体变量info的长度)。

So use a space after these keywords:
在这些关键字后面要添加一个空格:

if, switch, case, for, do, while

but not with sizeof, typeof, alignof, or __attribute__. E.g.,
但是不要在sizeof, typeof, alignof,  或__attribute__后边添加空格。例如:

s = sizeof(struct file);

Do not add spaces around (inside) parenthesized expressions. This example is bad:
在用括号\'()\'括起来的表达式的前后不要添加空格。下面这个例子就很糟糕

s = sizeof( struct file );

When declaring pointer data or a function that returns a pointer type, the preferred use of * is adjacent to the data name or function name and not adjacent to the type name. Examples:
在声明一个指针数据或一个返回值为指针类型的函数时,让*号与数据名或函数名相邻,而不是与类型名相邻。例如:

char *linux_banner;
unsigned long long memparse(char *ptr, char **retptr);
char *match_strdup(substring_t *s);

Use one space around (on each side of) most binary and ternary operators, such as any of these:
在大多数二目和三目运算符的每个边上加一个空格,例如下面的运算符中的其中任何一个:

=  +  -  <  >  *  /  %  |  &  ^  <=  >=  ==  !=  ?  :

but no space after unary operators:
但是,在单目运算符之后不加空格:

&  *  +  -  ~  !  sizeof  typeof  alignof  __attribute__  defined

no space before the postfix increment & decrement unary operators:
在作为后缀的++和--单目运算符前面不加空格:

++  --

no space after the prefix increment & decrement unary operators:
在作为前缀的++和--单目运算符后面不加空格:

++  --

and no space around the . and -> structure member operators.
在结构体成员操作符.和->的前后都不加空格。

Do not leave trailing whitespace at the ends of lines. Some editors with smart indentation will insert whitespace at the beginning of new lines as appropriate, so you can start typing the next line of code right away. However, some such editors do not remove the whitespace if you end up not putting a line of code there, such as if you leave a blank line. As a result, you end up with lines containing trailing whitespace.
每行代码之后不要有多余的空格。一些支持智能缩进的编辑器将在新起一行的开始插入空格以方便输入下一行代码。然而,一些这样的编辑器并不删除行尾的空格,比如你留下了一个空白行。其结果就是,你的代码中包含了空白行。

Git will warn you about patches that introduce trailing whitespace, and can optionally strip the trailing whitespace for you; however, if applying a series of patches, this may make later patches in the series fail by changing their context lines.
Git会警告你的补丁包含有行尾空格,你可以选择性地删除掉行尾空格;然而,如果打一系列补丁的话,这可能会使在此系列中的后续的补丁打失败,因为删除空白行改变了他们的上下文代码行。

4) Naming | 命名

C is a Spartan language, and so should your naming be. Unlike Modula-2 and Pascal programmers, C programmers do not use cute names like ThisVariableIsATemporaryCounter. A C programmer would call that variable tmp, which is much easier to write, and not the least more difficult to understand.
C语言是一种斯巴达式的(严于自律的)语言,那么命名也应如此。C程序员跟Modula-2和Pascal的程序员不一样,不使用诸如ThisVariableIsATemporaryCounter一样可爱的变量名。C程序员会给这个变量命名为tmp,该名字更容易书写,而且也不难理解。

HOWEVER, while mixed-case names are frowned upon, descriptive names for global variables are a must. To call a global function foo is a shooting offense.
然而,虽然混合的命名方式不尽人意,但是给全局变量命名的时候给出一个描述性名称是必须的。给一个全局的函数命名为foo是相当要不得的(容易挨枪子)。

GLOBAL variables (to be used only if you really need them) need to have descriptive names, as do global functions. If you have a function that counts the number of active users, you should call that count_active_users() or similar, you should not call it cntusr().
全局变量(只在真正需要时才使用)需要有描述性的名字,全局函数也一样。如果你有一个函数用来计算活跃用户的数量,你应该叫它为类似count_active_users()的名字,不应该命名为cntusr()。

Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged - the compiler knows the types anyway and can check those, and it only confuses the programmer. No wonder MicroSoft makes buggy programs.
将函数的类型包含到函数名中(所谓的匈牙利命名法)简直就是脑残的做法,只会让程序员感到困惑,因为编译器知道类型而且可以检查类型好不啦?!好吧,难怪微软的程序总是bug接连不断。

LOCAL variable names should be short, and to the point. If you have some random integer loop counter, it should probably be called i. Calling it loop_counter is non-productive, if there is no chance of it being mis-understood. Similarly, tmp can be just about any type of variable that is used to hold a temporary value.
局部变量名应当简短而且直抵要点。如果你有一些随机整数循环计数器,应该命名它们为i。将它们命名为loop_counter,无助于提高生产力,如果没有机会被人误解的话。类似地,tmp可以用于任意类型的临时变量命名。

If you are afraid to mix up your local variable names, you have another problem, which is called the function-growth-hormone-imbalance syndrome. See chapter 6 (Functions).
如果您担心局部变量名会混淆,那么您存在另一个问题,那就是函数增长激素不平衡综合症。请参加见第6章(函数)。

5) Typedefs

Please don\'t use things like vps_t. It\'s a mistake to use typedef for structures and pointers. When you see a
请不要使用诸如vps_t之类的类型定义。对结构体和指针使用typedef是一个错误当你看到一个

vps_t a;

in the source, what does it mean? In contrast, if it says
在源代码中,知道a是啥意思吗? 相反,如果是

struct virtual_container *a;

you can actually tell what a is.
你就可以知道a是什么了。

Lots of people think that typedefs help readability. Not so. They are useful only for:
很多人认为typedef有助于提高可读性。其实不然。 typede只对如下情形有用:

  • a. totally opaque objects (where the typedef is actively used to hide what the object is).

完全不透明的对象(typedef被用来隐藏对象)。

Example: pte_t etc. opaque objects that you can only access using the proper accessor functions.
例如: pte_t等。 对于不透明的对象,你只能使用适当的函数去访问它。

!NOTE

Opaqueness and accessor functions are not good in themselves. The reason we have them for things like pte_t etc. is that there really is absolutely zero portably accessible information there.
  • b. Clear integer types, where the abstraction helps avoid confusion whether it is int or long.

清晰的整数类型,这一抽象有助于避免混淆整数是int或是long。

u8/u16/u32 are perfectly fine typedefs, although they fit into category (d) better than here.
u8/u16/u32是完美的typedefs, 虽然把他们归类到(d)比在这里(b)要合适。

!NOTE

Again - there needs to be a reason for this. If something is unsigned long, then there’s no reason to do

    typedef unsigned long myflags_t;

but if there is a clear reason for why it under certain circumstances might be an unsigned int and under other configurations might be unsigned long, then by all means go ahead and use a typedef.
但是,如果有一个明确的理由说明为什么它在某些情况下可能是一个unsigned int,而在其他配置中可能是一个unsigned long的话,那就毫不犹豫地通过各种手段去使用typedef。

  • c. when you use sparse to literally create a new type for type-checking.

当使用kernel的sparse工具做变量类型检查时, 可以用typedef定义一个类型。

  • d. New types which are identical to standard C99 types, in certain exceptional circumstances.

在某些特殊情况下,可以用typedef定义等价于C99标准中的新类型。
Although it would only take a short amount of time for the eyes and brain to become accustomed to the standard types like uint32_t, some people object to their use anyway.
对于像uint32_t之类的标准类型,虽然只需要花费少量的时间让眼睛和大脑去习惯和适应,但是一些人反正就是反对使用它们。

Therefore, the Linux-specific u8/u16/u32/u64 types and their signed equivalents which are identical to standard types are permitted – although they are not mandatory in new code of your own.
因此,Linux特定的u8/u16/u32/u64类型与标准类型相同,虽然不强制性在新代码中使用它们。

When editing existing code which already uses one or the other set of types, you should conform to the existing choices in that code.
在编辑已经使用一个或一组其他类型的已有的代码时,应当遵守该代码中业已存在的选择。

Maybe there are other cases too, but the rule should basically be to NEVER EVER use a typedef unless you can clearly match one of those rules.
也许还有其他的情况,但是规则应该基本上是绝不使用typedef,除非你可以很清楚地匹配到一个规则。

In general, a pointer, or a struct that has elements that can reasonably be directly accessed should never be a typedef.
一般情况下,对于指针可以直接访问其元素的结构体,永远不要使用typedef去定义。

6) Functions | 函数

Functions should be short and sweet, and do just one thing. They should fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24, as we all know), and do one thing and do that well.
函数应当设计得简洁,一个函数只做一件事儿。一个函数的文本至多占两屏(我们知道,ISO/ANSI屏幕尺寸是80*24),而且只做一件事儿并把事情做好。

The maximum length of a function is inversely proportional to the complexity and indentation level of that function. So, if you have a conceptually simple function that is just one long (but simple) case-statement, where you have to do lots of small things for a lot of different cases, it\'s OK to have a longer function.
函数的最大长度与该函数的复杂度和缩进级别成反比。所以,如果你有一个概念上简单的函数,它只是一个很长(但很简单)的case语句,你必须在很多不同的情况下做很多小的事情,那么函数较长的话也可以接受。

However, if you have a complex function, and you suspect that a less-than-gifted first-year high-school student might not even understand what the function is all about, you should adhere to the maximum limits all the more closely. Use helper functions with descriptive names (you can ask the compiler to in-line them if you think it\'s performance-critical, and it will probably do a better job of it than you would have done).
然而,如果你有一个复杂的函数,并且你怀疑一个不太有天赋的高中一年级的学生甚至可能理解不了,那么你应该更严格地遵守最大的函数长度。使用具有描述性名称的帮助函数(如果你认为性能很关键,可以让编译器把它们内联起来,这样做可能比你做得更好)。

Another measure of the function is the number of local variables. They shouldn\'t exceed 5-10, or you\'re doing something wrong. Re-think the function, and split it into smaller pieces. A human brain can generally easily keep track of about 7 different things, anything more and it gets confused. You know you\'re brilliant, but maybe you\'d like to understand what you did 2 weeks from now.
函数的另一个度量就是使用的局部变量的个数。局部变量的个数不能超过5~10个,否则你就是没做对的事情。重新思考一下函数实现,并将其分割成多个小块实现。通常人脑能较容易地跟踪大约7种不同的东西,更多的话脑子都会变得混乱起来。尽管你不怀疑自己的聪明程度,但也许你也想知道你两周前做了些什么。

In source files, separate functions with one blank line. If the function is exported, the EXPORT macro for it should follow immediately after the closing function brace line. E.g.:
在源文件中,函数之间使用一个空白行进行分隔。如果函数是可以被外部调用的,那么EXPORT宏紧随函数的\'}\'所在的行。 例如:

int system_is_up(void)
{
        return system_state == SYSTEM_RUNNING;
}
EXPORT_SYMBOL(system_is_up);

In function prototypes, include parameter names with their data types. Although this is not required by the C language, it is preferred in Linux because it is a simple way to add valuable information for the reader.
在函数原型中,需要包含参数名称及其数据类型。虽然这不是C语言所要求的,但它在Linux中更受欢迎,因为它是为读者添加有价值的信息的一种简单方式。

7) Centralized exiting of functions | 函数的集中退出

Albeit deprecated by some people, the equivalent of the goto statement is used frequently by compilers in form of the unconditional jump instruction.
尽管有人反对使用goto语句,但是编译器却是在以无条件跳转指令的方式频繁地使用goto语句的等价形式。

The goto statement comes in handy when a function exits from multiple locations and some common work such as cleanup has to be done. If there is no cleanup needed then just return directly.
当一个函数从多个位置退出时,使用goto语句做一些常见的工作就很方便,比如cleanup。如果不需要清理的话,直接返回即可。

Choose label names which say what the goto does or why the goto exists. An example of a good name could be out_free_buffer: if the goto frees buffer. Avoid using GW-BASIC names like err1: and err2:, as you would have to renumber them if you ever add or remove exit paths, and they make correctness difficult to verify anyway.
选择合适的标签名称,告诉goto语句做什么或为什么要使用goto语句。如果是释放缓冲区的话,一个好的标签名字的例子可能是out_free_buffer。我们要避免使用GW-BASIC的标签名,诸如err1和err2之类的。因为你可能需要对它们进行重新编号,如果增加或减少一条路径的话。无论如何,使用err1之类的标签名使得正确性难以核实。

The rationale for using gotos is:
使用goto语句的基本原理:

  • unconditional statements are easier to understand and follow 无条件语句更容易被理解和遵循
  • nesting is reduced 减少了嵌套
  • errors by not updating individual exit points when making modifications are prevented 防止在做修改时出现不更新单个退出点的错误
  • saves the compiler work to optimize redundant code away ;) 节省编译器做优化冗余代码的工作
int fun(int a)
{
        int result = 0;
        char *buffer;

        buffer = kmalloc(SIZE, GFP_KERNEL);
        if (!buffer)
                return -ENOMEM;

        if (condition1) {
                while (loop1) {
                        ...
                }
                result = 1;
                goto out_free_buffer;
        }
        ...
out_free_buffer:
        kfree(buffer);
        return result;
}

A common type of bug to be aware of is one err bugs which look like this:
一个常见的错误处理bug看起来是这样的:

err:
        kfree(foo->bar);
        kfree(foo);
        return ret;

The bug in this code is that on some exit paths foo is NULL. Normally the fix for this is to split it up into two error labels err_free_bar: and err_free_foo::
在上面的代码中存在的bug是在某些退出路径中foo为空指针。 通常的fix就是将错误便签分割成两个,err_free_bar和err_free_foo:

err_free_bar:
       kfree(foo->bar);
err_free_foo:
       kfree(foo);
       return ret;

Ideally you should simulate errors to test all exit paths.
理想情况下,你应该模拟错误来测试所有的退出路径。 (P.S. 这似乎是不可能地:-))

8) Commenting | 注释

Comments are good, but there is also a danger of over-commenting. NEVER try to explain HOW your code works in a comment: it\'s much better to write the code so that the working is obvious, and it\'s a waste of time to explain badly written code.
写注释是好的,但是注释过多就不好了。永远不要在注释中解释你的代码是如何工作的:写出一目了然的代码最好不过,试图解释写得不好的代码简直就是浪费时间。

Generally, you want your comments to tell WHAT your code does, not HOW. Also, try to avoid putting comments inside a function body: if the function is so complex that you need to separately comment parts of it, you should probably go back to chapter 6 for a while. You can make small comments to note or warn about something particularly clever (or ugly), but try to avoid excess. Instead, put the comments at the head of the function, telling people what it does, and possibly WHY it does it.
通常地,通过注释告诉别人你的代码是做什么的,而不是如何做的。此外,尽量避免在函数体内插入注释:如果你的函数太复杂以至于需要单独写一打断注释的话,那么你可能需要返回到第6章。可以对一些特别聪明(或丑陋)的东西做些小的注释或警告,但是尽量避免过度地注释。相反,把注释放在函数头,告诉人们该函数做了什么,以及为什么要这么做。

When commenting the kernel API functions, please use the kernel-doc format. See the files at Documentation/doc-guide/ and scripts/kernel-doc for details.
当给kernel API函数写注释的时候,请使用kernel-doc格式。详情请参见Documentation/doc-guide/和scripts/kernel-doc。

The preferred style for long (multi-line) comments is:
多行注释的首选风格是这样的:

/*
 * This is the preferred style for multi-line
 * comments in the Linux kernel source code.
 * Please use it consistently.
 *
 * Description:  A column of asterisks on the left side,
 * with beginning and ending almost-blank lines.
 */

For files in net/ and drivers/net/ the preferred style for long (multi-line) comments is a little different.
对net/和drivers/net/中的文件,多行注释的首选风格稍微有点儿不同。

/* The preferred comment style for files in net/ and drivers/net
 * looks like this.
 *
 * It is nearly the same as the generally preferred comment style,
 * but there is no initial almost-blank line.
 */

It\'s also important to comment data, whether they are basic types or derived types. To this end, use just one data declaration per line (no commas for multiple data declarations). This leaves you room for a small comment on each item, explaining its use.
给数据写注释也是很重要的,无论这些数据是基本类型还是派生类型。为此,每一行只声明一个数据(对于多个数据声明来说,不使用逗号)。这就为每一项提供了一个用来解释其用途而写短注释的空间。

9) You\'ve made a mess of it | 搞乱了咋整?(控制缩进的方法)

That\'s OK, we all do. You\'ve probably been told by your long-time Unix user helper that GNU emacs automatically formats the C sources for you, and you\'ve noticed that yes, it does do that, but the defaults it uses are less than desirable (in fact, they are worse than random typing - an infinite number of monkeys typing into GNU emacs would never make a good program).
不要紧,我们都搞乱过。你长期使用的Unix用户帮手可能已经告诉过你,GNU Emacs能自动格式化你的C源代码,而且你已经注意到它确实也是这样做了,但是它的默认做法都不理想(事实上,它们比随机打字更糟。在GNU emacs里打字的无数只猴子永远不会写出一个好的程序)。

So, you can either get rid of GNU emacs, or change it to use saner values. To do the latter, you can stick the following in your .emacs file:
因此,要么摆脱GNU emacs, 要么让GNU emacs使用更加心智健全的值。如果选择后者,那么就坚持下面的.emacs文件就好了:

(defun c-lineup-arglist-tabs-only (ignored)
  "Line up argument lists by tabs, not spaces"
  (let* ((anchor (c-langelem-pos c-syntactic-element))
         (column (c-langelem-2nd-pos c-syntactic-element))
         (offset (- (1+ column) anchor))
         (steps (floor offset c-basic-offset)))
    (* (max steps 1)
       c-basic-offset)))

(add-hook \'c-mode-common-hook
          (lambda ()
            ;; Add kernel style
            (c-add-style
             "linux-tabs-only"
             \'("linux" (c-offsets-alist
                        (arglist-cont-nonempty
                         c-lineup-gcc-asm-reg
                         c-lineup-arglist-tabs-only))))))

(add-hook \'c-mode-hook
          (lambda ()
            (let ((filename (buffer-file-name)))
              ;; Enable kernel mode for the appropriate files
              (when (and filename
                         (string-match (expand-file-name "~/src/linux-trees")
                                       filename))
                (setq indent-tabs-mode t)
                (setq show-trailing-whitespace t)
                (c-set-style "linux-tabs-only")))))

This will make emacs go better with the kernel coding style for C files below ~/src/linux-trees.
这将使emacs更好地适应内核编码风格,在编辑~/src/linux-trees下面的C文件的时候。

But even if you fail in getting emacs to do sane formatting, not everything is lost: use indent.
但是,即使你不能使emacs做心智健全的排版,也不是所有的一切都丢失了:使用缩进。

Now, again, GNU indent has the same brain-dead settings that GNU emacs has, which is why you need to give it a few command line options. However, that\'s not too bad, because even the makers of GNU indent recognize the authority of K&R (the GNU people aren\'t evil, they are just severely misguided in this matter), so you just give indent the options -kr -i8 (stands for K&R, 8 character indents), or use scripts/Lindent, which indents in the latest style.
现在不得不重提一下,GNU缩进具有与GNU emacs一样脑残的设置,这就是为什么需要给它一些命令行选项。然而,这并不是太坏,因为GNU缩进制造者承认K&R的权威(GNU的人并不邪恶,在这个问题上他们只是被严重误导了),所以你只要给出缩进选项
-kr -i8(代表K&R,8字符缩进)就好,或使用采用最新缩进风格的脚本/Lindent。

indent has a lot of options, and especially when it comes to comment re-formatting you may want to take a look at the man page. But remember: indent is not a fix for bad programming.
缩进有很多选项,尤其是在重新格式化注释的时候,你可能需要看一下手册。但是请记住:缩进并不是对不良编程的修复

 

10) Kconfig configuration files | Kconfig配置文件

For all of the Kconfig* configuration files throughout the source tree, the indentation is somewhat different. Lines under a config definition are indented with one tab, while help text is indented an additional two spaces. Example:
在源代码树中,所有的Kconfig配置文件,缩进有一些不同。配置定义行用一个tab缩进,而帮助文本在增加额外的两个空格进行缩进。例如:

config AUDIT
      bool "Auditing support"
      depends on NET
      help
        Enable auditing infrastructure that can be used with another
        kernel subsystem, such as SELinux (which requires this for
        logging of avc messages output).  Does not do system-call
        auditing without CONFIG_AUDITSYSCALL.

Seriously dangerous features (such as write support for certain filesystems) should advertise this prominently in their prompt string:
对那些相当危险的特性(例如对某些文件系统进行写支持),应该在其提示字符串予以特别强调:

config ADFS_FS_RW
      bool "ADFS write support (DANGEROUS)"
      depends on ADFS_FS
      ...

For full documentation on the configuration files, see the file Documentation/kbuild/kconfig-language.txt.
有关配置文件的完整文档,请参见Documentation/kbuild/kconfig-language.txt。

 

11) Data structures | 数据结构

Data structures that have visibility outside the single-threaded environment they are created and destroyed in should always have reference counts. In the kernel, garbage collection doesn\'t exist (and outside the kernel garbage collection is slow and inefficient), which means that you absolutely have to reference count all your uses.
创建和销毁在单线程环境之外具有可见性的数据结构总是应该具有引用计数。在内核中,不存在垃圾回收(在内核外,垃圾回收是缓慢和低效的),这意味着你绝对必须使用引用计数。

Reference counting means that you can avoid locking, and allows multiple users to have access to the data structure in parallel - and not having to worry about the structure suddenly going away from under them just because they slept or did something else for a while.
引用计数意味着可以避免锁定,并允许多个用户并行地访问同一个数据结构,而不必担心用户在睡眠了或做了一件别的事情后数据结构会突然消失掉。

Note that locking is not a replacement for reference counting. Locking is used to keep data structures coherent, while reference counting is a memory management technique. Usually both are needed, and they are not to be confused with each other.
注意锁不是对引用计数的替代。锁用于保持数据结构的一致性,而引用计数是一种内存管理技术。通常两者都是需要的,不应该将它们相互混淆。

Many data structures can indeed have two levels of reference counting, when there are users of different classes. The subclass count counts the number of subclass users, and decrements the global count just once when the subclass count goes to zero.
当有不同类别的用户时,事实上许多数据结构可以有两级引用计数。子类计数器统计类的用户数,当子类计数器为灵时对全局计数器减一。

Examples of this kind of multi-level-reference-counting can be found in memory management (struct mm_struct: mm_users and mm_count), and in filesystem code (struct super_block: s_count and s_active).
使用这种多层次的引用计数的例子可以内存管理(struct mm_struct:mm_users和mm_count),和文件系统代码(struct super_block:s_count和s_active)中找到。

Remember: if another thread can find your data structure, and you don\'t have a reference count on it, you almost certainly have a bug.
记住:如果另一个线程可以找到你的数据结构,而你并没有一个引用计数,那么几乎肯定你有一个bug。

 

12) Macros, Enums and RTL | 宏, 枚举类型和RTL

Names of macros defining constants and labels in enums are capitalized.
宏定义常量或者枚举的标签时使用大写字母。

#define CONSTANT 0x12345

Enums are preferred when defining several related constants.
当定义多个相关的常量时,首选枚举类型。

CAPITALIZED macro names are appreciated but macros resembling functions may be named in lower case.
提倡使用大写的宏名称,但类似于函数的宏则可以用小写。

Generally, inline functions are preferable to macros resembling functions.
一般来说,内联函数比类似函数的宏更可取。

Macros with multiple statements should be enclosed in a do - while block:
宏定义多行语句时要放入do - while中, 此时宏的名称用小写。

#define macrofun(a, b, c)                       \\
        do {                                    \\
                if (a == 5)                     \\
                        do_this(b, c);          \\
        } while (0)

Things to avoid when using macros:
在使用宏时要避免的事项:

1. macros that affect control flow:

#define FOO(x)                                  \\
        do {                                    \\
                if (blah(x) < 0)                \\
                        return -EBUGGERED;      \\
        } while (0)

is a very bad idea. It looks like a function call but exits the calling function; don’t break the internal parsers of those who will read the code.
使用影响控制流的宏是不好的。它看起来像一个函数调用,但却退出函数;不要破坏那些读取代码的内部解析器。

2. macros that depend on having a local variable with a magic name:

#define FOO(val) bar(index, val)

might look like a good thing, but it’s confusing as hell when one reads the code and it’s prone to breakage from seemingly innocent changes.
依赖于具有神奇名称的局部变量的宏貌似不错,但是当别人读代码则会感到困惑,很容易在看似无辜的变化中受挫。

3. macros with arguments that are used as l-values: FOO(x) = y; will bite you if somebody e.g. turns FOO into an inline function.
作为左值参数的宏:foo(x)= Y; 会刺痛你,如果有人将FOO改成内联函数的话。

4. forgetting about precedence: macros defining constants using expressions must enclose the expression in parentheses. Beware of similar issues with macros using parameters.
忘掉优先级吧:使用表达式定义常量的宏必须用括号把表达式括起来。使用参数的宏也存在类似的问题。

#define CONSTANT 0x4000
#define CONSTEXP (CONSTANT | 3)

5. namespace collisions when defining local variables in macros resembling functions:

#define FOO(x)                          \\
({                                      \\
        typeof(x) ret;                  \\
        ret = calc_ret(x);              \\
        (ret);                          \\
})

ret is a common name for a local variable - __foo_ret is less likely to collide with an existing variable.
在定义一个类似函数的宏的时候可能存在命名空间冲突。 ret是一个给局部变量命名很常见的名字,那么__foo_ret跟存在的局部变量冲突的可能性就极小了。

The cpp manual deals with macros exhaustively. The gcc internals manual also covers RTL which is used frequently with assembly language in the kernel.
cpp手册对宏讲得很详尽。gcc内部手册还涵盖了RTL, RTL被频繁地和内核编程中使用到的汇编语言一起使用。

 

13) Printing kernel messages | 打印内核消息

Kernel developers like to be seen as literate. Do mind the spelling of kernel messages to make a good impression. Do not use crippled words like dont; use do not or don\'t instead. Make the messages concise, clear, and unambiguous.
内核开发人员喜欢被看作是有文化的人。请注意内核消息的拼写,给人留下一个好印象。不要使用残缺不全的词例如don\'t,要使用do not或don\'t。内核消息的书写要点是简洁、明了、不模棱两可。

Kernel messages do not have to be terminated with a period.
内核消息不必以句号终止。

Printing numbers in parentheses (%d) adds no value and should be avoided.
在括号内的打印数字(%d)没有意义,应当予以避免。

There are a number of driver model diagnostic macros in <linux/device.h> which you should use to make sure messages are matched to the right device and driver, and are tagged with the right level: dev_err(), dev_warn(), dev_info(), and so forth. For messages that aren’t associated with a particular device, <linux/printk.h> defines pr_notice(), pr_info(), pr_warn(), pr_err(), etc.
在<linux/device.h>中存在着一些设备驱动模型诊断宏。应当使用它们以确保内核消息被匹配到正确的设备及驱动程序上,并贴上正确的标签。这些宏是: dev_err(), dev_warn(), dev_info()等等。对于那些不关联到任何特定设备的内核消息, <linux/printk.h>中定义了一些宏供使用,这些宏是:pr_notice(), pr_info(), pr_warn()等等。

Coming up with good debugging messages can be quite a challenge; and once you have them, they can be a huge help for remote troubleshooting. However debug message printing is handled differently than printing other non-debug messages. While the other pr_XXX() functions print unconditionally, pr_debug() does not; it is compiled out by default, unless either DEBUG is defined or CONFIG_DYNAMIC_DEBUG is set. That is true for dev_dbg() also, and a related convention uses VERBOSE_DEBUG to add dev_vdbg() messages to the ones already enabled by DEBUG.
打印出好的调试消息是一个很大的挑战;一旦拥有了它们,就可以对远程故障排除提供巨大的帮助。然而,打印调试消息不同于打印其他非调试消息。pr_XXX()之类的函数将无条件打印内核消息,pr_debug()则不然。默认情况下是不编译调试信息的,除非DEBUG被定义或者设置了CONFIG_DYNAMIC_DEBUG。对dev_dbg()也是这样处理的,相应的约定是使用VERBOSE_DEBUG,将dev_vdebug()打印的消息增加到已经启用了DEBUG的调试消息中。

Many subsystems have Kconfig debug options to turn on -DDEBUG in the corresponding Makefile; in other cases specific files #define DEBUG. And when a debug message should be unconditionally printed, such as if it is already inside a debug-related #ifdef section, printk(KERN_DEBUG ...) can be used.
许多内核子系统有Kconfig调试选项,用来在相应的Makefile中打开-DDEBUG;在其他情况下,则是使用#DEBUG。当调试消息应当被无条件打印时,例如:如果它已经位于调试相关的#ifdef部分时,可以使用printk(KERN_DEBUG ...)。

 

14) Allocating memory | 内存分配

The kernel provides the following general purpose memory allocators: kmalloc(), kzalloc(), kmalloc_array(), kcalloc(), vmalloc(), and vzalloc(). Please refer to the API documentation for further information about them.
内核提供了几个通用的内存分配器:kmalloc(),kzalloc(),kmalloc_array(),kcalloc(),vmalloc(),和vzalloc()。如欲获取进一步的信息,请参阅相应的API文档。

The preferred form for passing a size of a struct is the following:
传递一个结构体大小的首选形

以上是关于[中英对照]Linux kernel coding style | Linux内核编码风格的主要内容,如果未能解决你的问题,请参考以下文章

Linux命令中英文对照

Linux命令中英文对照

HTTP返回码的中英文对照和返回码000的情况

[中英对照]Booting Process in Linux RHEL 7 | Linux RHEL 7启动过程

[中英对照]User-Space Device Drivers in Linux: A First Look

急求3Dmax中英文对照表,要最全的,