天天看點

linux kernel coding styles

linux 核心編碼規範(linux kernel coding styles)

http:// w w w.rosoo.net/a/linux/201003/8881.html 

這篇簡短的文章描述了Linux核心首選的編碼風格。編碼風格是很個人化的東西,我不會把自己的觀點強加給任何人。但是,Linux核心的代碼畢竟是我必須有能力維護的,是以我甯願它的編碼風格是我喜歡的。請至少考慮一下這一點。

首先,我建議列印一份《GNU編碼标準》,不要閱讀它。燒掉它,它不過是象征性的姿态。

然後,請看:

第 1 章: 縮進

Tabs(制表符)是8個字元的大小,是以縮進也應該是8個字元的大小。有些叛逆主張試圖把縮進變成4個(甚至是2個!)字元的長度,這就好象試圖把PI(案,圓周率)定義成3是一樣的。

依據:縮進背後的思想是:清楚地定義一個控制塊從哪裡開始,到哪裡結束。尤其是在你連續不斷的盯了20個小時的螢幕後,如果你有大尺寸的縮進。你将更容易發現縮進的好處。

現在,有些人說8個字元大小的縮進導緻代碼太偏右了,并且在一個80字元寬的終端螢幕上看着很不舒服。對這個問題的回答是:如果你有超過3個級别的縮進,你就有點犯糊塗了,應當修改你的程式。

簡而言之,8個字元的縮進使程式更易讀,而且當你把功能隐藏的太深時,多層次的縮進還會對此很直覺的給出警告。要留心這種警告資訊。

第 2 章: 放置花括号

C程式中另一個要主意的就是花括号的放置。與縮進尺寸不同的是,關于如何放置花括号沒有技術上的理由。但是,首選的方法是象先知Brain Kernighan和Dennis Ritchie展現的那樣:把左括号放在行尾,右括号放在行首。也就是:

if (x is true) {

we do y

}

然而,還有另外一種情況,就是函數:函數應當把左右括号都放在行首。也就是:

int function(int x)

{

body of function

}

叛逆的人們所在皆有。他們說,這樣會導緻…嗯,不一緻性(案,指函數的花括号使用與其他情況不統一)。但是所有正确思考的人都知道:(1) K&R是正确的;(2) K&R還是正确的。而且,函數與别任何東西都不一樣(在C語言中你沒法隐藏它)。

注意,右括号所在的行不應當有其它東西,除非跟随着一個條件判斷。也就是do- while語句中的“ while”和if-else語句中的“else”。象這樣:

do {

body of do-loop

} while (condition);

和:

if (x == y) {

..

} else if (x > y) {

...

} else {

....

}

依據: K&R。

而且,注意這種花括号的放置減少了空行的數目,并沒損害可讀性。是以,當螢幕上不可以有很多空行時(試想25行的終端螢幕),你就有更多的空行來安插注釋。

第 3 章: 命名

C是一門樸素的語言,你使用的命名也應該這樣。與Modula-2和Pascal程式員不同,C程式員不使用諸如 “ThisVariableIsATemporaryCounter”這樣“聰明”的名字。C程式員應該叫它“tmp”,這寫起來更簡單,也不會更難懂。

然而,當面對複雜情況時就有些棘手,給全局變量取一個描述性的名字是必要的。把一個全局函數叫做“foo”是一種目光短淺的行為。

全局變量(隻當你确實需要時才用)應該有描述性的名字,全局函數也一樣。如果你有一個統計目前使用者個數的函數,應當把它命名為“count_active_user()”或者簡單點些的類似名稱,不應該命名為“cntusr()”。

把函數類型寫進函數名(即所謂的“匈牙利命名法”)簡直就是大腦有問題──編譯器總是知道函數的類型并且能加以檢查,這種命名法隻會弄糊塗程式員自己。怪不得 微軟總是制造充滿bug的程式。

局部變量的名字應該盡量短,而且說到點子上。如果你有個普通的整型循環計數變量,應當命名為“i”。命名為“loop_counter”并不能帶來任何成效,如果它不被誤解的話(案,這裡的言外之意是說,如果被誤解就更慘了)。與此類似,“tmp”可以作為一個用來存儲任何類型臨時值的變量的名字。

如果你害怕弄混淆局部變量(s)的名字,你就面臨着另一個問題,也叫作“函數增長荷爾蒙失調綜合症”。請參考下一章。

第 4 章: 函數

函數應當短而精美,而且隻做一件事。它們應當占滿1或2個螢幕(就象我們知道的那樣,ISO/ANSI的螢幕大小是80X24),隻做一件事并且把它做好。

一個函數的最大長度與它的複雜度和縮進級别成反比。是以,如果如果你有一個概念上簡單(案,“簡單”是simple而不是easy)的函數,它恰恰包含着一個很長的case語句,這樣你不得不為不同的情況準備不同的處理,那麼這樣的長函數是沒問題的。

然而,如果你有一個複雜的函數,你猜想一個并非天才的高一學生可能看不懂得這個函數,你就應當努力把它減縮得更接近前面提到的最大函數長度限制。可以使用一些輔助函數,給它們取描述性的名字(如果你認為這些輔助函數的調用是性能關鍵的,可以讓編譯器把它們内聯進來,這比在單個函數内完成所有的事情通常要好些)。

對函數還存在另一個測量标準:局部變量的數目。這不該超過5到10個,否則你可能會弄錯。應當重新考慮這個函數,把它分解成小片。人類的大腦一般能同時記住7個不同的東西,超過這個數目就會犯糊塗。或許你認為自己很聰明,那麼請你了解一下從現在開始的2周時間你都做什麼了。

第 5 章:注釋

注釋是有用的,但過量的注釋則是有害的。不要試圖在注釋中解釋你的代碼是如何工作的:把代碼是如何工作的視為一件顯然的事情會更好些,而且,給糟糕的代碼作注釋則是在浪費時間。

通常,你願意自己的注釋說出代碼是做什麼的,而不是如何做。還有,盡量避免在函數體内作注釋:如果函數很複雜,你很可能需要分開來注釋,回頭到第4章去看看吧。你可以給一段代碼──漂亮的或醜陋的──作注釋以引起注意或警告,但是不要過量。取而代之,應當把注釋放在函數首部,告訴人們該函數作什麼,而不是為什麼這樣做。

第 6 章:你把事情弄亂了

好吧,我們來看看。很可能有長期使用UNIX的人告訴過你,“GNU emacs”能自動為你格式化C程式源代碼,你注意到這是真的,它确實能做到,但是預設情況下它的用處遠遠小于期望值──鍵入無數的monkeys到GNU emacs中絕不可能造出好的程式。

是以,你可以或者删除GNU emacs,或者對它進行理智的配置。對于後者,可以把下面的行粘貼到你的.emacs檔案中:

(defun linux-c-mode ()

"C mode with adjusted defaults for use with the Linux kernel."

(interactive)

(c-mode)

(c-set-style "K&R")

(setq c-basic-offset 8))

這将會定義一個把C代碼弄成linux風格的指令。當hacking一個子產品時,如果你把“-*- linux-c -*-”放到了最初的兩行,這個子產品将被自動調用。而且,如果你打算每當在/usr/src/linux下編輯源檔案時就自動調用它,也許你會把下面的指令:

(setq auto-mode-alist (cons '("/usr/src/linux.*/.*\\.[ch]$" . linux-c-mode)

auto-mode-alist))

添加進你的.emacs檔案。

但是,即使你沒能讓emacs正确做到格式化,也并非将就此一無所有:還有“indent”程式呢。

嗯,再提醒一下,GNU indent跟GNU emacs有同樣的毛病,這就需要你給它一些指令行選項。然而,這不是很糟糕的事,因為即使是GNU indent也承認K&R的權威性(GNU的人不是魔鬼,他們隻是在這裡太過嚴格了,以緻于誤導人),是以你可以隻需給indent這樣的選項: “-kr -i8”(表示“K&R風格,8個字元的縮進”)。

“indent”程式有很多選項,特别是當為重排過的程式作注釋的時候,你需要看一下它的手冊。記住:“indent”可不是修正糟糕程式的萬能鑰匙。

第 7 章: 配置檔案(configuration-files)

對配置選項來說(arch/xxx/config.in和所有的Config.in檔案),使用不同的縮進風格。

若代碼中的縮進級别為3,配置選項就應該為2,這樣可以暗示出依賴關系。後者隻是用于bool/tristate(即二态/三态)的選項。對其它情況用常識就行了。舉例來說:

if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then

tristate 'Apply nitroglycerine inside the keyboard (DANGEROUS)' CONFIG_BOOM

if [ "$CONFIG_BOOM" != "n" ]; then

bool '  Output nice messages when you explode' CONFIG_CHEER

fi

fi

通常CONFIG_EXPERIMENTAL應當在所有不穩定的選項的周圍出現。所有已知會破壞資料的選項(如檔案系統的實驗性的寫支援功能)應當被标記為(DANGEROUS),其他實驗性的選項應當被标記為(EXPERIMENTAL)。

第 8 章: 資料結構

假如資料結構在其被建立/銷毀的線程環境(案:這裡說的線程是一個執行實體,可能是程序、核心線程或其它)之外還具有可見性,那麼他們都該有引用計數。在核心中沒有垃圾收集機制(而且核心之外的垃圾收集也是緩慢而低效的),這意味着你絕對應該為每一次使用進行引用計數。

引用計數意味着你可以避開鎖,還能允許多個線程并行通路該資料結構──而且不用擔心僅僅因為通路資料結構的線程睡眠了一會兒或者幹别的去了,它們就會消失。

注意,鎖不是引用計數的替代品。鎖是用來保持資料結構的一緻性的,而引用計數是一種記憶體管理技術。通常二者都需要,而且不會彼此混淆。

确實有許多資料結構可以有兩個級别的引用計數,當使用者具有不同的“等級”(classes)時就是這樣。子等級(subclass)記錄了處于該等級的使用者個數,而且當它減到零的時候就把總體計數(global count)減一。

這種“多級引用計數”(multi-reference-counting)的一個執行個體可以在記憶體管理子系統("struct mm_struct":mm_users和mm_count)中找到,也可以在檔案系統的代碼中("struct super_block":s_count和s_active)找到。

記住:如果另一個線程能找到你的資料結構,而你有沒對它做引用計數,那幾乎可以肯定:這是一個bug。

英文的,MS要新一些

Linux kernel coding style

Torvalds Linus.

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

vie ws 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.

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.

Any way, here goes:

Chapter 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.

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

ho w the indentation works if you have large indentations.

No w, 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 ans wer to that is that if you need

more than 3 levels of indentation, you're scre wed any way, and should fix

your program.

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.

Don't put multiple statements on a single line unless you have

something to hide:

if (condition) do_this;

do_something_everytime;

Outside of comments, documentation and except in Kconfig, spaces are never

used for indentation, and the above example is deliberately broken.

Get a decent editor and don't leave whitespace at the end of lines.

Chapter 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 hard limit.

Statements longer than 80 columns will be broken into sensible chunks.

Descendants are al ways substantially shorter than the parent and are placed

substantially to the right. The same applies to function headers with a long

argument list. Long strings are as well broken into shorter strings.

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;

}

Chapter 3: Placing Braces

The other issue that al ways comes up in C styling is the placement of

braces.  Unlike the indent size, there are fe w technical reasons to

choose one placement strategy over the other, but the preferred way, as

sho wn 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:

if (x is true) {

we do y

}

Ho wever, 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 kno w that

(a) K&R are _right_ and (b) K&R are right.  Besides, functions are

special any way (you can't nest them in C).

Note that the closing brace is empty on a line of its o wn, _except_ in

the cases where it is follo wed by a continuation of the same statement,

ie a " while" in a do-statement or an "else" in an if-statement, like

this:

do {

body of do-loop

} while (condition);

and

if (x == y) {

..

} else if (x > y) {

...

} else {

....

}

Rationale: 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 ne w-lines on your screen is not a rene wable resource (think

25-line terminal screens here), you have more empty lines to put

comments on.

Chapter 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.

HO WEVER, while mixed-case names are fro wned upon, descriptive names for

global variables are a must.  To call a global function "foo" is a

shooting offense.

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()".

Encoding the type of a function into the name (so-called Hungarian

notation) is brain damaged - the compiler kno ws the types any way and can

check those, and it only confuses the programmer.  No wonder MicroSoft

makes buggy programs.

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.

If you are afraid to mix up your local variable names, you have another

problem, which is called the function-gro wth-hormone-imbalance syndrome.

See next chapter.

Chapter 5: Functions

Functions should be short and s weet, and do just one thing.  They should

fit on one or t wo screenfuls of text (the ISO/ANSI screen size is 80x24,

as we all kno w), and do one thing and do that well.

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.

Ho wever, 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 kno w you're brilliant, but maybe you'd like

to understand what you did 2 weeks from no w.

Chapter 6: 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.

The goto statement comes in handy when a function exits from multiple

locations and some common work such as cleanup has to be done.

The rationale is:

- unconditional statements are easier to understand and follo w

- nesting is reduced

- errors by not updating individual exit points when making

modifications are prevented

- saves the compiler work to optimize redundant code a way ;)

int fun(int )

{

int result = 0;

char *buffer = kmalloc(SIZE);

if (buffer == NULL)

return -ENOMEM;

if (condition1) {

while (loop1) {

...

}

result = 1;

goto out;

}

...

out:

kfree(buffer);

return result;

}

Chapter 7: Commenting

Comments are good, but there is also a danger of over-commenting.  NEVER

try to explain HO W 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 HO W.

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 5 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.

When commenting the kernel API functions, please use the kerneldoc format.

See the files Documentation/kernel-doc-nano-HO WTO.txt and scripts/kernel-doc

for details.

Chapter 8: 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).

So, you can either get rid of GNU emacs, or change it to use saner

values.  To do the latter, you can stick the follo wing in your .emacs file:

(defun linux-c-mode ()

"C mode with adjusted defaults for use with the Linux kernel."

(interactive)

(c-mode)

(c-set-style "K&R")

(setq tab- width 8)

(setq indent-tabs-mode t)

(setq c-basic-offset 8))

This will define the M-x linux-c-mode command.  When hacking on a

module, if you put the string -*- linux-c -*- some where on the first

t wo lines, this mode will be automatically invoked. Also, you may want

to add

(setq auto-mode-alist (cons '("/usr/src/linux.*/.*\\.[ch]$" . linux-c-mode)

auto-mode-alist))

to your .emacs file if you want to have linux-c-mode s witched on

automagically when you edit source files under /usr/src/linux.

But even if you fail in getting emacs to do sane formatting, not

everything is lost: use "indent".

No w, again, GNU indent has the same brain-dead settings that GNU emacs

has, which is why you need to give it a fe w command line options.

Ho wever, 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.

"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.

Chapter 9: Configuration-files

For configuration options (arch/xxx/Kconfig, and all the Kconfig files),

some what different indentation is used.

Help text is indented with 2 spaces.

if CONFIG_EXPERIMENTAL

tristate CONFIG_BOOM

default n

help

Apply nitroglycerine inside the keyboard (DANGEROUS)

bool CONFIG_CHEER

depends on CONFIG_BOOM

default y

help

Output nice messages when you explode

endif

Generally, CONFIG_EXPERIMENTAL should surround all options not considered

stable. All options that are kno wn to trash data (experimental write-

support for file-systems, for instance) should be denoted (DANGEROUS), other

experimental options should be denoted (EXPERIMENTAL).

Chapter 10: Data structures

Data structures that have visibility outside the single-threaded

environment they are created and destroyed in should al ways have

reference counts.  In the kernel, garbage collection doesn't exist (and

outside the kernel garbage collection is slo w and inefficient), which

means that you absolutely _have_ to reference count all your uses.

Reference counting means that you can avoid locking, and allo ws multiple

users to have access to the data structure in parallel - and not having

to worry about the structure suddenly going a way 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 t wo 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).

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.

Chapter 11: Macros, Enums, Inline functions and 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 lo wer case.

Generally, inline functions are preferable to macros resembling functions.

Macros with multiple statements should be enclosed in a do - while block:

#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 flo w:

#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.

4) forgetting about precedence: macros defining constants using expressions

must enclose the expression in parentheses. Be ware of similar issues with

macros using parameters.

#define CONSTANT 0x4000

#define CONSTEXP (CONSTANT | 3)

The cpp manual deals with macros exhaustively. The gcc internals manual also

covers RTL which is used frequently with assembly language in the kernel.

Chapter 12: 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" and use "do not" or "don't" instead.

Kernel messages do not have to be terminated with a period.

Printing numbers in parentheses (%d) adds no value and should be avoided.

Chapter 13: Allocating memory

The kernel provides the follo wing general purpose memory allocators:

kmalloc(), kzalloc(), kcalloc(), and vmalloc().  Please refer to the API

documentation for further information about them.

The preferred form for passing a size of a struct is the follo wing:

p = kmalloc(sizeof(*p), ...);

The alternative form where struct name is spelled out hurts readability and

introduces an opportunity for a bug when the pointer variable type is changed

but the corresponding sizeof that is passed to a memory allocator is not.

Casting the return value which is a void pointer is redundant. The conversion

from void pointer to any other pointer type is guaranteed by the C programming

language.

Chapter 14: References

The C Programming Language, Second Edition

by Brian W. Kernighan and Dennis M. Ritchie.

Prentice Hall, Inc., 1988.

ISBN 0-13-110362-8 (paperback), 0-13-110370-9 (hardback).

URL: http://cm.bell-labs.com/cm/cs/cbook/

The Practice of Programming

by Brian W. Kernighan and Rob Pike.

Addison- Wesley, Inc., 1999.

ISBN 0-201-61586-X.

URL: http://cm.bell-labs.com/cm/cs/tpop/

GNU manuals - where in compliance with K&R and this text - for cpp, gcc,

gcc internals and indent, all available from http:// w w w.gnu.org

WG14 is the international standardization working group for the programming

language C, URL: http://std.dkuug.dk/JTC1/SC22/ WG14/

--

Last updated on 16 February 2004 by a community effort on LKML.