zerons's Blog

For The Dream

linux 内核代码格式 Documentation/CodingStyle

zerons posted @ 2014年12月08日 11:33 in chanllenge , 1672 阅读

tabs为8空格, 这样对结构块的开始结尾比较清晰, 特别当你对着电脑看了很多小时, 大缩进让看代码更容易. 有人会说8缩进让代码太靠右了, 一行里面很难写完, 如果你有了至少四层的缩进, 那只能说你的代码写太差了.

不要在一行里面写多条语句, 除非你想做一些特别的事.

避免奇怪的表达式.

不要用空格代表缩进.

每行最多有80个字符

大括号的用法, 一般控制结构前一个大括号在行尾, 后一个大括号占一行的首非空字符. 函数的时候, 两半大括号都占一行. 在一般只有一条语句的控制结构里面最好不使用{}

        比如:

        if (condition)

                do_this;

        或者:

        if (condition)

                do_this;

        else

                do_else;

但是如果有一个分支只有一条语句, 每个里面都使用{}

 

多数关键字后用一个空格, 除了(sizeof, typeof, alignof, __attribute__). 在(后面和)前面不要加多余的空格. 指针的*紧贴着指针名, 像char *pointer; char *match_strdup(substring_t *s); 在下列运算符左右都加一个空格= + - < > * / % | & ^ <= >= == != ? :   但是&(取地址) *(指针) + - ~(取反) !(非) sizeof typeof alignof __attribute__ 后面不加空格. ++ -- 前面不要或后面不要空格.

 

变量名函数名, 不要使用混合大小写的名字(一些全局的除外), 例如, 全局函数想计算当前活动的用户数, 函数名应该是count_active_users()或者更简单的, 而不是cntusr(). 不要把类型加进变量名或者函数名中.  本地(局部)变量或函数的命名应该简洁明了, 像循环计数i或者一个临时tmp; 一个函数里面不要太多变量以致于自己都容易弄混...

 

不要给结构体或者指针用typedef. typedef只用于(totally opaque objects, clear integer types, use sparse to literally create a new type for type_checking, new types identical to C99 types, types safe for use in userspace, ==), 一个标准就是永远不要用typedef,

 

函数应该尽量短, 只做一件事就好. 长短一两页即可(80x24), do one thing and do it well.

当然, 如果你的函数里面有一个很长的switch-case结构. 或者, 你有一个比较复杂的函数(大一的学生都看不懂)you should adhere to the maximum limits all the more closely, 使用恰当名字的函数.

函数的局部变量应该在5-10, 否则你就走太远了.

源码中函数前后用空白行分开. 如果函数需要被导出, EXPORT*宏应该在}的下一行

函数声明中, 参数应该也包括参数名,

 

适当的使用goto, 当一个函数有多个退出点的时候,

 

注释, 适当的使用注释, 不要解释你的代码是怎么工作的, 解释你的代码是做什么的, 而不是怎么做的.

尽量不要在一个函数体内部写注释.

内核函数注释有特定的格式, Documentation/kernel-doc-nano-HOWTO.txt and scripts/kernel-doc

linux使用C89格式注释/*...*/, 不要使用C99// ... 注释

多行注释如下

        /*

         * something

         * something

         */

 

emacs用户

 

Kconfig文件格式   Documentation/kbuild/kconfig-language.txt

 

各种数据结构都应该有一个引用计数, 内核中是没有垃圾收集机制的. 如果另外一个线程能使用你的数据结构但是却没有一个引用计数, 很可能就是一个bug了.

 

宏, 枚举和RTL

避免使用宏的情况: 里面包含控制流的, 如exit return; 宏里面包含了一个局部本地变量的; 宏被当作左值用了; 宏定义时的封装问题

 

输出内核信息

输出信息要准确明了

 

函数返回值问题, if the name of a function is an action or an imperative command, the function should return an error-code integer. if the name is a predicate, the function should return a "succeeded" boolean.

 

不要重复做轮子, include/linux/kernel.h里面包含一些常用的宏变量

 

不要带有一些编辑器插入的语句

 

 

2014-12-17 update

1. 头文件

        例子: asm/delay.h => linux/delay.h (https://groups.google.com/forum/#!topic/fa.linux.kernel/yccGyGoT3pQ)

 

2. printk VS pr_*

        (http://stackoverflow.com/questions/22077540/order-of-preference-printk-vs-dev-dbg-vs-netdev-dbg, https://lkml.org/lkml/2014/7/14/174)

        Depending on what you are coding you should use a different print style:

                printk(): never

                pr_debug(): always good

                dev_dbg(): prefered when you have a struct device object

                netdev_dbg(): prefered when you have a struct netdevice object

                [something]_dbg(): prefered when you have a that something object

 

3. Documentation/zh_CN/CodingStyle

Avatar_small
Bong88 说:
2021年6月26日 19:05

Valuable information! Looking forward to seeing your notes posted. Thank you for sharing the nice article. Good to see your article.

Avatar_small
instant hack 说:
2021年7月08日 22:45

Wow, this is really great. I was wondering if some one know how to crack instagram account until i found out about instant hack

Avatar_small
Cialis tablets 说:
2021年7月09日 08:25

Fabulous post, you have denoted out some fantastic points, I likewise think this s a very wonderful website. I will visit again for more quality contents and also, recommend this site to all. Thanks.

Avatar_small
visit website 说:
2021年7月15日 21:31

Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks

Avatar_small
gwaa.net 说:
2021年7月16日 20:42

hello!! Very interesting discussion glad that I came across such informative post. Keep up the good work friend. Glad to be part of your net community

Avatar_small
subcene 说:
2021年7月20日 03:48

Hello! I just wish to give an enormous thumbs up for the nice info you've got right here on this post. I will probably be coming back to your weblog for more soon!

Avatar_small
สล็อตฟรีเครดิต 说:
2021年7月20日 19:36

Very great post. I simply stumbled upon your weblog and wished to say that I have truly loved surfing around your blog posts. After all I will be subscribing in your feed and I'm hoping you write again soon!

Avatar_small
fire safety 说:
2021年7月20日 23:26

Thank you, for your willingness to share information with us. We always thank you for everything you do here because we know you care about us.

Avatar_small
moviesflix 说:
2021年7月23日 06:22

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.

Avatar_small
Akku Luftpumpe 说:
2021年7月24日 06:30

Hello! I just wish to give an enormous thumbs up for the nice info you've got right here on this post. I will probably be coming back to your weblog for more soon!

Avatar_small
fat burners 说:
2021年7月26日 01:00

Thanks for making the effort to go over this particular, Personally i think highly about this as well as adore understanding more on this topic. If at all possible, as you gain knowledge, would you mind updating your blog with more info? It is very helpful for me personally.

Avatar_small
ambcrypto epaper 说:
2021年7月26日 06:03

Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work.

Avatar_small
how fast does pine g 说:
2021年7月26日 22:39

What’s Going down i’m new to this, I stumbled upon this I have discovered It positively useful and it has aided me out loads. I’m hoping to give a contribution & help different users like its aided me. Good job

Avatar_small
extratorrent 说:
2021年7月27日 08:18

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!Thanks

Avatar_small
τουριστική βίζα του 说:
2021年7月28日 09:38

A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.

Avatar_small
nextbet 说:
2021年7月29日 05:55

It is an excellent blog, I have ever seen. I found all the material on this blog utmost unique and well written. And, I have decided to visit it again and again.

Avatar_small
amazon sale of phenq 说:
2021年8月01日 04:51

Hey, this day is too much good for me, since this time I am reading this enormous informative article here at my home. Thanks a lot for massive hard work.

Avatar_small
hotmail login 说:
2021年8月02日 07:56

What’s Going down i’m new to this, I stumbled upon this I have discovered It positively useful and it has aided me out loads. I’m hoping to give a contribution & help different users like its aided me. Good job

Avatar_small
khatrimaza 说:
2021年8月02日 11:13

Attractive, post. I just stumbled upon your weblog and wanted to say that I have liked browsing your blog posts. After all, I will surely subscribe to your feed, and I hope you will write again soon!

Avatar_small
relx煙彈 说:
2021年8月04日 10:46

Thanks for making the honest attempt to speak about this. I believe very robust approximately it and want to read more. If it’s OK, as you gain more in depth wisdom, would you thoughts adding extra articles similar to this one with additional information? It might be extremely useful and useful for me and my friends.

Avatar_small
download free 3d mod 说:
2021年8月06日 09:39

Your content is nothing short of brilliant in many ways. I think this is engaging and eye-opening material. Thank you so much for caring about your content and your readers

Avatar_small
Slotenmaker 说:
2021年8月08日 18:36

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much.

Avatar_small
check it 说:
2021年8月09日 14:40

This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you.

Avatar_small
Khatrimaza 说:
2021年8月09日 22:03

Great post. I used to be checking continuously this weblog and I’m inspired! Very useful information specially the final phase I care for such information a lot. I used to be seeking this particular info for a lengthy time. Thank you and best of luck.

Avatar_small
instagram takipçi sa 说:
2021年8月11日 00:47

Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

Avatar_small
significant figures 说:
2021年8月11日 11:41

Thanks for picking out the time to discuss this, I feel great about it and love studying more on this topic. It is extremely helpful for me. Thanks for such a valuable help again

Avatar_small
amherstnational 说:
2021年8月13日 23:15

i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future

Avatar_small
sloty online 说:
2021年8月16日 20:06

This is actually the kind of information I have been trying to find. Thank you for writing this information.

Avatar_small
best home goods 说:
2021年8月18日 04:09

I would like to say that this blog really convinced me to do it! Thanks, very good post.

Avatar_small
เครื่องบันทึก NVR 说:
2021年8月25日 15:13

I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit

Avatar_small
188bet 说:
2021年8月26日 09:38

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.

Avatar_small
mega888 official doa 说:
2021年8月29日 02:52

I should say only that its awesome! The blog is informational and always produce amazing things.

Avatar_small
عروض هايبر بنده هذا 说:
2021年8月29日 03:21

i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future

Avatar_small
soap2da 说:
2021年8月30日 22:22

You have a real ability for writing unique content. I like how you think and the way you represent your views in this article. Thank you for sharing.

Avatar_small
visit here 说:
2021年8月31日 19:21

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.

Avatar_small
merchant sales rep 说:
2021年9月01日 01:13

Thank you that is very helpful for me, as a new site has been inundated with comments that seem OK at first glance but then get repeated with a slight change of wording. I have something concrete to go on now and will delete quite a lot of them.

Avatar_small
Satta king 说:
2021年9月13日 01:09

Cool you inscribe, the info is really salubrious further fascinating, I'll give you a connect to my scene.

Avatar_small
ท่อHDPE 说:
2021年9月15日 21:50

This is such a great resource that you are providing and you give it away for free.

Avatar_small
elizabeth rabbit 说:
2021年9月19日 09:15

Exceptionally useful post ! There is a considerable measure of data here that can enable any business to begin with a fruitful long range informal communication campaign !

Avatar_small
دانلود فیلم سریال 说:
2021年10月26日 23:32

The best article I ran over various years, compose something about it on this page.

Avatar_small
Best Social Plan 说:
2021年10月31日 08:48

I just thought it may be an idea to post incase anyone else was having problems researching but I am a little unsure if I am allowed to put names and addresses on here

Avatar_small
seoprovider 说:
2021年11月10日 19:21

thanks for the tips and information..i really appreciate it..

Avatar_small
finance 说:
2021年11月10日 20:51

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
JUNK PICK UP HOUSTON 说:
2021年11月12日 18:46

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

Avatar_small
auto 说:
2021年11月13日 21:43

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that.

Avatar_small
fit 说:
2021年11月18日 23:18

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that.

Avatar_small
YOLO 说:
2021年12月07日 20:00

slotxo Football price online Online betting odds

Avatar_small
รีวิวเว็บufabet 说:
2022年1月22日 21:09

Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work.

Avatar_small
Asphalt Paving 说:
2022年2月17日 19:17

Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter