Article Title: Learn more about the Linux kernel programming style. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
This short document describes the recommended programming style in Linux kernel programming. Programming style is very personal
I don't want to impose my opinion on anyone, but this is what is observed in the code that I must maintain
I also recommend that other parts of the code also comply with it. Please give at least some considerations here.
First, I suggest you print a GNU code style, instead of reading it, but burning it.
A good attitude.
The Linux kernel programming style is as follows:
Chapter 1: Indent The tab character (tabs) occupies 8 characters, so the indentation is also 8 characters. Some adopts a four-character version for the cult movement.
(Or even 2 characters) indentation, which is no different from setting PI (circumference rate) to 3.
Cause: indent is used to clearly identify the starting point of a control block. Especially when you keep an eye on
After watching the screen for 20 hours, you will experience the benefits of longer indentation.
At present, some people propose that 8-character indentation will make the code too biased towards the right side. when using a terminal with 80 characters
It is difficult to read. The answer is that if you need more than three layers of indentation, you are finished and you should change it.
Your program is running.
In short, 8-character indentation makes it easier to read the code, and when your indentation level is too deep
Warning. Pay attention to such warnings.
I cannot agree with this. The four-character indentation does have its advantages. the indentation is too much, but it looks tired.
And sometimes only two layers of nesting will make the code very long.
Chapter 2: Brackets The issue of brackets is often raised in the C programming style. Different from the indentation size, the selection of the brackets
There are not many technical reasons, but more of them are personal preferences. For example
The disciples put the left brackets at the end of a row and the right brackets at the beginning of a row, as shown in the following figure:
If (x is true ){
We do y
}
However, a function is a special case where the left braces of the function are placed at the beginning of the next row, as shown in the following code:
Int function (int x)
{
Body of function
}
Those who are outside the world point out this inconsistent approach... well... not quite consistent, but all thinking is
Indeed, people know that (a) K & R is _ pair _ (B) K & R is correct. Furthermore, functions are special (
).
Note that the parentheses fully occupy a single line, _ unless _ when it is followed by unfinished statements, such as do
The "while" or "else" in the if statement can be as follows:
Do {
Body of do-loop
} While (condition );
And
If (x = y ){
..
} Else if (x> y ){
...
} Else {
....
}
Cause: K & R.
Also, it is noted that the layout of parentheses also reduces the number of empty rows (or almost empty rows,
It does not reduce readability. Because the blank lines on your screen cannot recycle resources (think about 25 lines here
Terminal screen), so you will have more blank lines for adding comments.
Chapter 3: naming C is a Sparta (concise style) language, so your naming method should also be like this. And
Modula-2 and Pascal programmers are different, C programmers do not use
ThisVariableIsATemporaryCounter. A c programmer will put a variable
It is called "tmp". such a variable name is easier to write and not too difficult to understand.
_ But _, even though everyone is frowning on mixed-case names, this is required for global variable names. Management
A global function is called "foo.
_ Global _ variable (used only when _ true _ is required) requires a descriptive name, which is full
Same as local functions. If you have a function to count active users, you should call it
"Count_active_users ()" instead of "cntusr ()".
Adding the function type to the name (the so-called Hungarian naming method) is a manifestation of brain injury-compiler
If you know the type, you can check it. This naming method will only confuse the programmer. No wonder Microsoft does
So many bugs. Haha ^ _ ^. it makes sense. Don't worry about yourself.
_ Local _ variables should be short and concise. If you have a random integer cyclic variable, you 'd better call it "I ".
It is inefficient to call it "loop_counter" without confusion. Type location,
"Tmp" can be used for any type of variables that store temporary values.
If you are worried about obfuscation of your local variables, you will have another problem, called function expansion.
Mongolia imbalance syndrome, please refer to the next chapter.
Chapter 4: functions 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,
The ISO/ANSI standard screen size is 80x24.
The maximum length of a function is inversely proportional to the complexity and indentation level of the function. So, if you have only
A very long (but very simple) case statement function does a few operations on many cases.
This function does not matter for a long time.
However, if you have a complex function, you are worried that a medium-level high school student may not be able to understand it,
Then you should strictly abide by the maximum length limit. Use helper functions with descriptive names (you can
Let the compiler in-line help functions, if you think the performance is very important, and the compiler is afraid
It will be better than what you do ).
Another indicator of the function is the number of local variables. the number of local variables should not exceed 5-10. otherwise
It must be a problem. Design this function and break it down into smaller ones. Human brain
You can track 7 different things at the same time. if you have more than 7 things, you will be dizzy. Although you are smart, maybe you
Sometimes I want to understand the code I wrote two weeks ago.
Chapter 5: Notes Comments are good, but there is a danger of over-comments. _ Never _ do not explain in comments that your code is
How to work: a better way is to write the code that is obvious in the way of work, and a poor explanation of the code is a waste.
Time-consuming.
In general, comments should explain what the code is doing, rather than how it is done. Also, do not add comments
In the function body: If the function is so complex that you must annotate each part, you may need
Read Chapter 4. You can add some short comments to remind or warn some smart (or ugly)
Practice, but not too much. A better choice is to place comments in the function header to describe what the function is doing,
It may also include why it is done.
Chapter 6: messy code Nothing. we have all met. You may have heard from old Unix users that "GNU emacs" will automatically
Alignment C source code, but the default settings are not very good (in fact, the default settings are worse than random hits-
A group of monkeys using GNU emacs will never make beautiful programs ).
Depend! I use emacs! However, automatic alignment is fine now.
Therefore, you can either completely drop GNU emacs or adopt more rational settings. If you select the latter
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 defines the M-x linux-c-mode command. When writing a Linux module,
Linux-c-*-"is placed in the first two lines of the file. this mode is automatically activated. Also, if you
Linux-c-mode is automatically activated when you want to edit the source file in the/usr/src/linux directory.
Emacs file needs to be added
But even if you cannot use emacs, it is not the end of the world: You can also use "indent ".
Once again, GNU indent uses the same brain death settings as GNU emacs, so you need to give it
Command line options. However, this is not too bad, because even the authors of GNU indent realized that
K & R authority (GNU people are not the devil, they are misled in this case), so you can
Run indent with the option "-kr-i8" ("K & R, 8-character indent.
"Indent" has many options, especially the comment layout section. you may want to see its man manual. However
Remember: "indent" cannot modify bad programs.
I personally think that emacs has a good deal of indentation alignment. The problem is that you use different Linux
The release will face different. emacs files. sometimes I have to carry my. emacs files with me,
Or put it in the storage box of my email.
Chapter 7: configuration file The configuration options (arch/xxx/config. in, and all the Config. in files) use some different
Indent mode.
The code uses three characters of indentation, and the config-option should use two characters of indentation to identify the dependency.
The latter applies only to the bool/tristat option. For other options, use the indent method that you think is the most appropriate
You can. For example:
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
In general, all unstable options should be marked as CONFIG_EXPERIMENTAL. Number of all possible damages
Data options should be labeled as (DANGEROUS), and other test options should be labeled as (EXPERIMENTAL ).
Chapter 8: Data structure Reference counts should be used for the multi-threaded data structure ). In the kernel,
Garbage collection does not exist (the efficiency of garbage collection outside the kernel is not high ),
This means that you _ must _ use reference count.
The use of reference counting can avoid the use of locks, so that different users can use the data structure in parallel-no need
Worry that the structure will suddenly disappear due to sleep.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.