Linux kernel programming style (ZT) (Yinyong)

xiaoxiao2021-03-06  37

Linux kernel programming style (ZT)

Original text / usr / src / linux / documentation / codingstyle. Linux kernel programming styles This short document is used to describe the recommended programming style in Linux kernel programming. The programming style is very personal, I don't want to put my point of view _ strong _ give anyone, but this is the code I have to maintain, I also recommend that other parts of the code can also obey 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 good posture. Unfair, the following is the Linux kernel programming style: Chapter 1: The indent tab (Tabs) accounts for 8 characters, so the indentation is also 8 characters. Some of the heterogeneous movement wants to use 4 characters (even 2 characters) to indent, 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 look at the screen for 20 hours, you will experience the benefits of longer indentation. Now, some people put eight character indentation will make the code too biased toward the right, and it is difficult to read when using the 80-character terminal. The answer is if you need to indent the three floors, then you have finished, you should change your program. In short, 8 characters indent make read code easier and give a warning at your indenge level. You should pay attention to such a warning. Chapter II: The problem of parentheses in parentheses is often proposed in the C programming style. Unlike indentation, the choice of parentheses is not too many technologies, and more is a personal preference. For example, Kernighan and Ritchie disciples put the left brackets in a row, put the right brackets in a row, like this: if (x is true) {we do y}, but the function is a special case, function The left brackets are placed in the beginning of the next line, like this: int function (int x) {body of function} The absenteeism of the world pointed out this inconsistent approach ... um ... not consistent, but all thinking is correct People know (a) K & R is _ pair _ (b) K & R is right. Moreover, the function is indeed special (you can't nescery the function in C). Note that the right brackets completely occupy a separate line, _ unless it is only the unfinished statement, such as "while" in the DO statement or "Else" in the IF statement, you want to do this: do {body of do-loop WHILE (condition); and IF (x == y) {..} else = {....} reason: k & r. Also, it is noted that the layout method of this parentheses also reduces the number of blank lines (or is almost empty), and does not reduce readability. Because you are not recyclable resources on your screen (you want a 25-row terminal screen) so you will have more space lines for adding. Chapter III: Name C is a Spadad (advocating a simple style) language, so your naming method should be the case. Unlike Modula-2 and Pascal programmers, C programmers do not use the lovely names like thisvariableisatemporaryCounter. A C programmer is called "TMP" to call a variable, so that the variable name is easier to write, and it is not too difficult to understand it. _ But _, although everyone will write a mixed name to the upper and lowercase, the global variable name must be the case.

A global function is called "foo" is deliberate to find. _ Global _ variable (only when _ real _ needs) requires a descriptive name, which is the same as the full-class function. 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 - the compiler knows the type, which can check it. This naming method will only make the programmer himself. It is no wonder that Microsoft has done so much BUG. _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 any type of storage temporary value variable. If you are worried about confusing your local variables, then you will have another problem. If you call the so-called function, you will see the next chapter. Chapter 4: The function 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) can be installed, only do and do one thing. The maximum length of the function should be inversely proportional to the complexity of the function. So, if you have a function of only a long (but very simple) CASE statement, do some of the do little operations for many cases, then this function has no relationship. However, if you have a complex function, you are worried that a medium-intelligence of high school students may not understand, then you should comply with the maximum length restrictions more strict. Use a help function with a descriptive name (you can make the compiler in-line these help functions, if you think the performance is important, and the compiler will be better than you do). Another indicator of a function is the number of local variables, and the number of local variables should not exceed 5-10, otherwise, where is it. 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, you may sometimes want to understand the code written in two weeks. Chapter 5: Comment Notes are good things, but there is a risk of excessive comments. _ Forever _ Don't explain how your code is working in the comment: Better approach is to write the code that is easy to see, explaining the bad code is a waste time. In general, the comment should indicate what the code is doing, not how to do it. Also, do not add the comment in the function body: If the function is too complex, you must annotate each part, then you may have to read the fourth chapter. You can add some short comments to remind or warn some intelligence (or ugly) practice, but don't be too excessive. Better choice is to release the note in the function head, indicating what the function is doing, and may also include it. Chapter 6: Your code is in a mess, we have encountered. You may have heard of "GNU Emacs" from the old UNIX users, "GNU Emacs" will automatically align the C source code, but the default setting is not very good (in fact, the default setting is better than hooking - 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 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- (SetQ C-Basic-Offset) This defines the MX Linux-c-mode command. When writing Linux modules, if you put strings - * - linux-c - * - "Placing in the first two lines of the file, this mode will be automatically activated. Also, if you want to activate the Linux-c-mode when you want to edit / usr / src / linux directory, you In your. Emacs files need to be added (setq auto-mode-alist ("/usr/src/linux.*/.*/1) Auto-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". Yes, GNU Indent uses the same brain death setting as GNU Emacs, so you need to give it some command line options. But This is not too bad, because even the author of GNU Indent realizes the authority of K & R (GNU is not a devil, they are only misleaded on this matter), so you can use the option "-kr -i8 "(indicating" K & R, 8 character indent ") runs indent." Indent "has many options, especially the annotation layout section, you might want to see its MAN manual. But remember:" indent "cannot be modified Worse procedures. Chapter 7: Profile Configuration Options (Arch / XXX / Config.in, and all config.in files) use some different indentation methods. Use 3 characters indentation, CONFIG-Options 2 characters should be used to indent the logo dependencies. The latter is only applied to the BOOL / TRISTAT option. For other options, you can use the most appropriate indentation mode. For example: IF ["$ config_experimental" = "y" ]; The Tristate 'Apply Nitroglycerine Inside The Keyboard (Dangerous)' config_boomiff ["$ config_boom"! = "n"]; THEN BOOL 'OUTPUT NICE Messages When You Explode' config_cheer FI FI In general, all unstable options should be labeled as config_experimental. All options that may damage the data should be marked as (Dangerous), and other test options should be marked (Experimental). Chapter 8: The data structure for multi-threaded data structures should be referenced (Reference Count). In the kernel, garbage collection is not existed (the garbage collection efficiency outside the kernel is not high), which 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.

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

New Post(0)