Linux kernel programming style (ZT)

xiaoxiao2021-03-06  39

Original text / usr / src / linux / documentation / codingstyle.

Linux kernel programming style

This short document is used to describe the programming style recommended in Linux kernel programming. Programming style is very personal

Things, I don't want to put my point of view _ strong _ give anyone, but this is the code I have to maintain.

I also suggest that other parts of the code can also follow it. Please give at least some views here.

First of all, I suggest you print a GNU code style, not to read it, but burn it, this is a very

Nice attitude.

Unfair, the following is the Linux kernel programming style:

Chapter 1: Indentation

Tabs (Tabs) accounts for 8 characters, so moisture is also 8 characters. Some heroic movement wants to use 4 characters

(Or even 2 characters) indentation, this and the PI (circumferential rate) is not 3 no two.

Cause: The fundamental purpose of the indentation is to clearly identify the start of a control block. Especially when you stare continuously

After watching the screen for 20 hours, you will experience the benefits of longer indentation.

Now, some people present 8 character indentation will make the code too polarize to the right while using 80 characters

It's hard to read. The answer is if you need three levels of indenction, then you have finished, you should change

Your program.

In short, 8 characters indentation make reading code easier, and presented by your indenge level

caveat. You should pay attention to such a warning.

Chapter II: Location of Braces

The problem of parentheses is often proposed in the C programming style. Unlike indentation, the choice of parentheses

There is no more technical reasons, and more is a personal preference. Such as Kernighan and Ritchie

The disciples put the left brackets in the end of a line, put the right brackets in a row, like this:

IF (x is true) {

WE Do Y

}

However, the function is a special case, the left brackets of the function are placed in the next line, like this:

Int function (int X)

{

Body of function

}

Headly of the world points out this inconsistency ... um ... not consistent, but all thinking is

The person knows (a) K & R is _-pair _ (b) K & R is right. Moreover, the function is indeed special (you are in c

Unable to nescery the function).

It is noted that the right brackets have a single line, _ unless _ when it is still unfinished, such as DO

"While" or "Else" in the IF statement in the statement, you want to:

Do {

Body of do-loop

WHILE (condition);

with

IF (x == y) {

.

} else if (x> y) {

...

} else {

....

}

Cause: k & r.

Also, note that the layout method of this parentheses also reduces the number of blank lines (or is almost empty),

And there is no readability. Because the blank line on your screen is not recyclable (you think about 25 lines)

Terminal screen) so that you will have more blank lines for adding.

Chapter 3: Name

C is a Spartad (advocating simple style) language, so your naming method should be the case. versus

Module-2 and Pascal programmers, C programmer is not used

ThisvariableisatemporaryCounter so cute name. A C programmer will put a variable

Called "TMP", such variable names are easier to write, and it is not too difficult to understand.

_ But _, although everyone will write a mixed name to the upper and lowercase, the global variable name must be the case. tube

A global function called "foo" is deliberately found.

_ Global _ variable (only when _ real _ needs) needs to have a descriptive name, this sum

The game function is the same. If you have a function to count active users, your baby is called it

"Count_Active_Users (), not" cntusr () ".

Add the type of function to the name (so-called Hungarian nomenclature) is the performance of brain damage - compiler

Know the type, you can check it, this naming method will only faint programmers. No wonder the Microsoft

There are so many BUG-filled procedures.

_Titious _ variables should be short. If you have a random integer loop variable, it may be best called "I".

Taking it "loop_counter" is low efficiency, without confusion. Type,

"TMP" can be used for variables of any type of storage temporary value.

If you are worried about confusing your local variables, then you will have another problem, the so-called function expands

Please look down on the next chapter.

Chapter 4: Function

The function should be short and sweet, and only one thing can be done. They should only use one or two screens (we all know,

ISO / ANSI standard screen size is 80x24) to install, only do one, do one thing.

The maximum length of the function should be inversely proportional to the complexity of the function. So if you have one

A function of a very long (but very simple) CASE statement, do some of the many cases, then

This function has no relationship.

However, if you have a complex function, you are worried that a medium-minded high school student may not understand,

Then you should strictly comply with the maximum length limit. Use help functions with descriptive names (you can

Let the compiler in-line These help functions, if you think the performance is important, and the compiler is probably

Will be better than you.).

Another indicator of a function is the number of local variables, and the number of local variables should not exceed 5-10, otherwise one

Where is the problem? Decompose this function and decompose it smaller.

The human brain can usually track 7 different things at the same time, and more than 7 will haar. Although you are very smart, it may be

Sometimes I want to understand the code written in two weeks.

Chapter 5: Note

The comment is a good thing, but there is a risk of excessive comments. _ Forever _ Don't explain your code in the comment is

How to work: Better practice is to write the code of work, explaining the bad code is the wave

Wasting time.

In general, the comment should indicate what the code is doing, not how to do it. And, don't put the comment

In the main body: If the function is too complicated, you must annotate each part, then you may have to

Read the fourth chapter. You can join some short comments to remind or warn some smart (or ugly)

Practice, but not too much. Better choice is to release the note in the function head, indicating what the function is doing,

May also include why it is.

Chapter 6: Your code is chaos

Nothing, we have encountered it. You may have heard of "GNU Emacs" from the old Unix users.

Aligned C source code, but the default setting is not very good (in fact, the default setting is more bad than hitting "

A group of GNU Emacs monkeys will never make a beautiful program).

So, you or completely remove the GNU Emacs, or use more ingredient settings. If you choose the latter, you

You can add the following code to 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 C-Basic-Offset 8)))

This will define the m-x Linux-c-mode command. If you write a Linux module, if you put the string "- * -

Linux-c - * - "In the first two lines of the file, this mode will be activated automatically. Also, if you

LINUX-C-Mode is automatically activated when you want to edit / usr / src / linux directory, you are in yours.

Need to join in the Emacs file

(setq auto-mode-alist ("/usr/src/linux.*/.*//.[ch]". Linux-c-mode)

Auto-mode-alist)))))

But even if you can't use Emacs, it is not the end of the world: you can also use "indent".

Any time, GNU Indent uses the same brain death as GNU Emacs, so you need to give it one

Some command line options. However, this is not too bad, because even the authors of GNU Indent also realize

K & R's authority (people of GNU are not devil, they just missed this matter), so you can

Run Indents using option "-kr -i8" (indicating "K & R, 8 Character Indentation").

"Indent" has many options, especially the annotation layout section, you might want to see its MAN manual. but

Yes, remember: "Indent" cannot modify the bad program.

Chapter 7: Profile

Configuration options (Arch / XXX / Config.in, and all config.in files) have used some different

Indentation mode.

The code is used in the code is 3 characters indentation, and the CONFIG-option should use 2 characters to indent the identity dependencies.

The latter is only applied to the BOOL / TRISTAT option. For other options, use you think the most appropriate indentation method

Yes. E.g:

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 expende' config_cheer

Fi

Fi

In general, all unstable options should be labeled as config_experimental. All possible damage

According to the options should be marked (Dangerous), and other test options should be marked (Experimental).

Chapter 8: Data Structure

The data structure for multi-threaded use should be referenced (Reference Count). In the kernel,

Garbage Collection does not exist (garbage collection efficiency outside the kernel is not high),

This means you _ must _ use the reference count.

The use of the reference count can avoid locking, so that different users can use the data structure in parallel - do not need to worry that the structure will suddenly disappear because of sleep.

Note that the locking _ is not a replacement of the _ reference count. Plock is used to ensure the integrity of the data structure, and the reference count is a memory management technology. Usually you need, there should be no confused place. Some data structures may use two quotable references, when they are used for different "classes". child

The count of the class statistics All sub-users, only the total number is reduced when the count is zero.

This example of this "multi-layer reference count" can be in memory management code ("struct mm_struct":

MM_USERS and MM_COUT and file system code ("struct super_block": s_count and

S_Active) found.

Remember: If another thread can see your data structure, you don't have a reference to it, then you will definitely have bugs.

转载请注明原文地址:https://www.9cbs.com/read-64907.html

New Post(0)