Linux kernel coding style (Linux內核編碼風格)

Linux內核編碼風格

前陣子做驅動的時候看得文章,Torvalds Linus所作,值得借鑑一下( 當然,現在也沒有能力作出什麼反駁:) )。中文版本是Google而來,我也不知道是哪位仁兄翻譯的,若有冒犯,還請指出。另外,英文版的內容MS要新一些。

關鍵字:Linux, kernel, style


   
這篇簡短的文章描述了Linux內核首選的編碼風格。編碼風格是很個人化的東西,我不會把自己的觀點強加給任何人。但是,Linux內核的代碼畢竟是我必須有能力維護的,因此我寧願它的編碼風格是我喜歡的。請至少考慮一下這一點。
   
首先,我建議打印一份《GNU編碼標準》,不要閱讀它。燒掉它,它不過是象徵性的姿態。
   
然後,請看:


   
1 章: 縮進

   
Tabs(製表符)8個字符的大小,因此縮進也應該是8個字符的大小。有些叛逆主張試圖把縮進變成4(甚至是2個!)字符的長度,這就好象試圖把PI(案,圓周率)定義成3是一樣的。
   
依據:縮進背後的思想是:清楚地定義一個控制塊從哪裏開始,到哪裏結束。尤其是在你連續不斷的盯了20個小時的屏幕後,如果你有大尺寸的縮進。你將更容易發現縮進的好處。
   
現在,有些人說8個字符大小的縮進導致代碼太偏右了,並且在一個80字符寬的終端屏幕上看着很不舒服。對這個問題的回答是:如果你有超過3個級別的縮進,你就有點犯糊塗了,應當修改你的程序。
   
簡而言之,8個字符的縮進使程序更易讀,而且當你把功能隱藏的太深時,多層次的縮進還會對此很直觀的給出警告。要留心這種警告信息。
   

    2 章: 放置花括號

 
  C程序中另一個要主意的就是花括號的放置。與縮進尺寸不同的是,關於如何放置花括號沒有技術上的理由。但是,首選的方法是象先知Brain KernighanDennis 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-2Pascal程序員不同,C程序員不使用諸如 “ThisVariableIsATemporaryCounter”這樣聰明的名字。C程序員應該叫它“tmp”,這寫起來更簡單,也不會更難懂。
   
然而,當面對複雜情況時就有些棘手,給全局變量取一個描述性的名字是必要的。把一個全局函數叫做“foo”是一種目光短淺的行爲。
   
全局變量(只當你確實需要時才用)應該有描述性的名字,全局函數也一樣。如果你有一個統計當前用戶個數的函數,應當把它命名爲“count_active_user()”或者簡單點些的類似名稱,不應該命名爲“cntusr()”
   
把函數類型寫進函數名(即所謂的匈牙利命名法”)簡直就是大腦有問題──編譯器總是知道函數的類型並且能加以檢查,這種命名法只會弄糊塗程序員自己。怪不得微軟總是製造充滿bug的程序。
   
局部變量的名字應該儘量短,而且說到點子上。如果你有個普通的整型循環計數變量,應當命名爲“i”。命名爲“loop_counter”並不能帶來任何 成效,如果它不被誤解的話(案,這裏的言外之意是說,如果被誤解就更慘了)。與此類似,“tmp”可以作爲一個用來存儲任何類型臨時值的變量的名字。
   
如果你害怕弄混淆局部變量(s)的名字,你就面臨着另一個問題,也叫作函數增長荷爾蒙失調綜合症。請參考下一章。
   

   
4 章: 函數

   
函數應當短而精美,而且只做一件事。它們應當佔滿12個屏幕(就象我們知道的那樣,ISO/ANSI的屏幕大小是80X24),只做一件事並且把它做好。
   
一個函數的最大長度與它的複雜度和縮進級別成反比。所以,如果如果你有一個概念上簡單(案,簡單simple而不是easy)的函數,它恰恰包含着一個很長的case語句,這樣你不得不爲不同的情況準備不同的處理,那麼這樣的長函數是沒問題的。
   
然而,如果你有一個複雜的函數,你猜想一個並非天才的高一學生可能看不懂得這個函數,你就應當努力把它減縮得更接近前面提到的最大函數長度限制。可以使 用一些輔助函數,給它們取描述性的名字(如果你認爲這些輔助函數的調用是性能關鍵的,可以讓編譯器把它們內聯進來,這比在單個函數內完成所有的事情通常要 好些)
   
對函數還存在另一個測量標準:局部變量的數目。這不該超過510個,否則你可能會弄錯。應當重新考慮這個函數,把它分解成小片。人類的大腦一般能同時 記住7個不同的東西,超過這個數目就會犯糊塗。或許你認爲自己很聰明,那麼請你理解一下從現在開始的2周時間你都做什麼了。

   
   
5 章:註釋

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

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


   
6 章:你把事情弄亂了

   
好吧,我們來看看。很可能有長期使用UNIX的人告訴過你,“GNU emacs”能自動爲你格式化C程序源代碼,你注意到這是真的,它確實能做到,但是缺省情況下它的用處遠遠小於期望值──鍵入無數的monkeysGNU 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 indentGNU 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_usersmm_count)中找到,也可以在文件系統的代碼中("struct super_block":s_counts_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
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.

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.

Anyway, 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
how the indentation works if you have large indentations.

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.

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 always 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 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:

    if (x is true) {
        we do y
    }

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

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


        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.

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.

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 knows the types anyway 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-growth-hormone-imbalance syndrome.
See next chapter.


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

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.

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.


        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 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 )
{
    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 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 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-HOWTO.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 following 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 -*- somewhere on the first
two 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 switched 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".

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.

"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),
somewhat 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 known 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 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).

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

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)

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 following 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 following:

    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://www.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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章