€• Œsphinx.addnodes”Œdocument”“”)�”}”(Œ rawsource”Œ ”Œchildren”]”(Œtranslations”Œ
LanguagesNode”“”)�”}”(hhh]”(h Œpending_xref”“”)�”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…”�”}”Œparent”hsbaŒ
attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ(/translations/zh_CN/process/coding-style”Œmodname”NŒ classname”NŒrefexplicit”ˆuŒtagname”hhhubh)�”}”(hhh]”hŒChinese (Traditional)”…”�”}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ(/translations/zh_TW/process/coding-style”Œmodname”NŒ classname”NŒrefexplicit”ˆuh1hhhubh)�”}”(hhh]”hŒItalian”…”�”}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ(/translations/it_IT/process/coding-style”Œmodname”NŒ classname”NŒrefexplicit”ˆuh1hhhubh)�”}”(hhh]”hŒJapanese”…”�”}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ(/translations/ja_JP/process/coding-style”Œmodname”NŒ classname”NŒrefexplicit”ˆuh1hhhubh)�”}”(hhh]”hŒKorean”…”�”}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ(/translations/ko_KR/process/coding-style”Œmodname”NŒ classname”NŒrefexplicit”ˆuh1hhhubh)�”}”(hhh]”hŒPortuguese (Brazilian)”…”�”}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ(/translations/pt_BR/process/coding-style”Œmodname”NŒ classname”NŒrefexplicit”ˆuh1hhhubh)�”}”(hhh]”hŒSpanish”…”�”}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ(/translations/sp_SP/process/coding-style”Œmodname”NŒ classname”NŒrefexplicit”ˆuh1hhhubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h
hhŒ _document”hŒsource”NŒline”NubhŒtarget”“”)�”}”(hŒ.. _codingstyle:”h]”h}”(h]”h ]”h"]”h$]”h&]”Œrefid”Œcodingstyle”uh1hµh´Khhh²hh³ŒB/var/lib/git/docbuild/linux/Documentation/process/coding-style.rst”ubhŒsection”“”)�”}”(hhh]”(hŒtitle”“”)�”}”(hŒLinux kernel coding style”h]”hŒLinux kernel coding style”…”�”}”(hhËh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhhÆh²hh³hÃh´KubhŒ paragraph”“”)�”}”(hXC 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.”h]”(hŒƒThis is a short document describing the preferred coding style for the
linux kernel. Coding style is very personal, and I won’t ”…”�”}”(hhÛh²hh³Nh´NubhŒstrong”“”)�”}”(hŒ **force**”h]”hŒforce”…”�”}”(hhåh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhhÛubhŒ» 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.”…”�”}”(hhÛh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KhhÆh²hubhÚ)�”}”(hŒƒ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.”h]”hŒ‡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.”…”�”}”(hhýh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KhhÆh²hubhÚ)�”}”(hŒAnyway, here goes:”h]”hŒAnyway, here goes:”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KhhÆh²hubhÅ)�”}”(hhh]”(hÊ)�”}”(hŒ1) Indentation”h]”hŒ1) Indentation”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhj h²hh³hÃh´KubhÚ)�”}”(hŒÛ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.”h]”hŒÛ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.”…”�”}”(hj* h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Khj h²hubhÚ)�”}”(hX 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.”h]”hX 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.”…”�”}”(hj8 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Khj h²hubhÚ)�”}”(hX& 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.”h]”hX( 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.”…”�”}”(hjF h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Khj h²hubhÚ)�”}”(hŒž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.”h]”hŒ 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.”…”�”}”(hjT h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K%hj h²hubhÚ)�”}”(hŒÙThe preferred way to ease multiple indentation levels in a switch statement is
to align the ``switch`` and its subordinate ``case`` labels in the same column
instead of ``double-indenting`` the ``case`` labels. E.g.:”h]”(hŒ\The preferred way to ease multiple indentation levels in a switch statement is
to align the ”…”�”}”(hjb h²hh³Nh´NubhŒliteral”“”)�”}”(hŒ
``switch``”h]”hŒswitch”…”�”}”(hjl h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjb ubhŒ and its subordinate ”…”�”}”(hjb h²hh³Nh´Nubjk )�”}”(hŒ``case``”h]”hŒcase”…”�”}”(hj~ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjb ubhŒ& labels in the same column
instead of ”…”�”}”(hjb h²hh³Nh´Nubjk )�”}”(hŒ``double-indenting``”h]”hŒdouble-indenting”…”�”}”(hj� h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjb ubhŒ the ”…”�”}”(hjb h²hh³Nh´Nubjk )�”}”(hŒ``case``”h]”hŒcase”…”�”}”(hj¢ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjb ubhŒ labels. E.g.:”…”�”}”(hjb h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K)hj h²hubhŒ
literal_block”“”)�”}”(hŒÖswitch (suffix) {
case 'G':
case 'g':
mem <<= 30;
break;
case 'M':
case 'm':
mem <<= 20;
break;
case 'K':
case 'k':
mem <<= 10;
fallthrough;
default:
break;
}”h]”hŒÖswitch (suffix) {
case 'G':
case 'g':
mem <<= 30;
break;
case 'M':
case 'm':
mem <<= 20;
break;
case 'K':
case 'k':
mem <<= 10;
fallthrough;
default:
break;
}”…”�”}”hj¼ sbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”Œforce”‰Œlanguage”Œc”Œhighlight_args”}”uh1jº h³hÃh´K-hj h²hubhÚ)�”}”(hŒQDon't put multiple statements on a single line unless you have
something to hide:”h]”hŒSDon’t put multiple statements on a single line unless you have
something to hide:”…”�”}”(hjÑ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K@hj h²hubj» )�”}”(hŒ1if (condition) do_this;
do_something_everytime;”h]”hŒ1if (condition) do_this;
do_something_everytime;”…”�”}”hjß sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´KChj h²hubhÚ)�”}”(hŒ'Don't use commas to avoid using braces:”h]”hŒ)Don’t use commas to avoid using braces:”…”�”}”(hjî h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KHhj h²hubj» )�”}”(hŒ,if (condition)
do_this(), do_that();”h]”hŒ,if (condition)
do_this(), do_that();”…”�”}”hjü sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´KJhj h²hubhÚ)�”}”(hŒ*Always use braces for multiple statements:”h]”hŒ*Always use braces for multiple statements:”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KOhj h²hubj» )�”}”(hŒ8if (condition) {
do_this();
do_that();
}”h]”hŒ8if (condition) {
do_this();
do_that();
}”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´KQhj h²hubhÚ)�”}”(hŒxDon't put multiple assignments on a single line either. Kernel coding style
is super simple. Avoid tricky expressions.”h]”hŒzDon’t put multiple assignments on a single line either. Kernel coding style
is super simple. Avoid tricky expressions.”…”�”}”(hj( h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KXhj h²hubhÚ)�”}”(hŒŽOutside of comments, documentation and except in Kconfig, spaces are never
used for indentation, and the above example is deliberately broken.”h]”hŒŽOutside of comments, documentation and except in Kconfig, spaces are never
used for indentation, and the above example is deliberately broken.”…”�”}”(hj6 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K\hj h²hubhÚ)�”}”(hŒCGet a decent editor and don't leave whitespace at the end of lines.”h]”hŒEGet a decent editor and don’t leave whitespace at the end of lines.”…”�”}”(hjD h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K_hj h²hubeh}”(h]”Œindentation”ah ]”h"]”Œ1) indentation”ah$]”h&]”uh1hÄhhÆh²hh³hÃh´KubhÅ)�”}”(hhh]”(hÊ)�”}”(hŒ"2) Breaking long lines and strings”h]”hŒ"2) Breaking long lines and strings”…”�”}”(hj] h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhjZ h²hh³hÃh´KcubhÚ)�”}”(hŒYCoding style is all about readability and maintainability using commonly
available tools.”h]”hŒYCoding style is all about readability and maintainability using commonly
available tools.”…”�”}”(hjk h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KehjZ h²hubhÚ)�”}”(hŒAThe preferred limit on the length of a single line is 80 columns.”h]”hŒAThe preferred limit on the length of a single line is 80 columns.”…”�”}”(hjy h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KhhjZ h²hubhÚ)�”}”(hŒ§Statements longer than 80 columns should be broken into sensible chunks,
unless exceeding 80 columns significantly increases readability and does
not hide information.”h]”hŒ§Statements longer than 80 columns should be broken into sensible chunks,
unless exceeding 80 columns significantly increases readability and does
not hide information.”…”�”}”(hj‡ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KjhjZ h²hubhÚ)�”}”(hŒ»Descendants are always substantially shorter than the parent and
are placed substantially to the right. A very commonly used style
is to align descendants to a function open parenthesis.”h]”hŒ»Descendants are always substantially shorter than the parent and
are placed substantially to the right. A very commonly used style
is to align descendants to a function open parenthesis.”…”�”}”(hj• h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KnhjZ h²hubhÚ)�”}”(hŒKThese same rules are applied to function headers with a long argument list.”h]”hŒKThese same rules are applied to function headers with a long argument list.”…”�”}”(hj£ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KrhjZ h²hubhÚ)�”}”(hŒsHowever, never break user-visible strings such as printk messages because
that breaks the ability to grep for them.”h]”hŒsHowever, never break user-visible strings such as printk messages because
that breaks the ability to grep for them.”…”�”}”(hj± h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KthjZ h²hubeh}”(h]”Œbreaking-long-lines-and-strings”ah ]”h"]”Œ"2) breaking long lines and strings”ah$]”h&]”uh1hÄhhÆh²hh³hÃh´KcubhÅ)�”}”(hhh]”(hÊ)�”}”(hŒ3) Placing Braces and Spaces”h]”hŒ3) Placing Braces and Spaces”…”�”}”(hjÊ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhjÇ h²hh³hÃh´KyubhÚ)�”}”(hX[ 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:”h]”hX[ 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:”…”�”}”(hjØ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K{hjÇ h²hubj» )�”}”(hŒ"if (x is true) {
we do y
}”h]”hŒ"if (x is true) {
we do y
}”…”�”}”hjæ sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´K�hjÇ h²hubhÚ)�”}”(hŒVThis applies to all non-function statement blocks (if, switch, for,
while, do). E.g.:”h]”hŒVThis applies to all non-function statement blocks (if, switch, for,
while, do). E.g.:”…”�”}”(hjõ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K‡hjÇ h²hubj» )�”}”(hŒ¬switch (action) {
case KOBJ_ADD:
return "add";
case KOBJ_REMOVE:
return "remove";
case KOBJ_CHANGE:
return "change";
default:
return NULL;
}”h]”hŒ¬switch (action) {
case KOBJ_ADD:
return "add";
case KOBJ_REMOVE:
return "remove";
case KOBJ_CHANGE:
return "change";
default:
return NULL;
}”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´KŠhjÇ h²hubhÚ)�”}”(hŒzHowever, there is one special case, namely functions: they have the
opening brace at the beginning of the next line, thus:”h]”hŒzHowever, there is one special case, namely functions: they have the
opening brace at the beginning of the next line, thus:”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K—hjÇ h²hubj» )�”}”(hŒ0int function(int x)
{
body of function
}”h]”hŒ0int function(int x)
{
body of function
}”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´KšhjÇ h²hubhÚ)�”}”(hŒþ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).”h]”(hŒ›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 ”…”�”}”(hj/ h²hh³Nh´Nubhä)�”}”(hŒ **right**”h]”hŒright”…”�”}”(hj7 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhj/ ubhŒ\ and (b) K&R are right. Besides, functions are
special anyway (you can’t nest them in C).”…”�”}”(hj/ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K¡hjÇ h²hubhÚ)�”}”(hŒß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:”h]”(hŒ;Note that the closing brace is empty on a line of its own, ”…”�”}”(hjO h²hh³Nh´Nubhä)�”}”(hŒ
**except**”h]”hŒexcept”…”�”}”(hjW h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhjO ubhŒQ in
the cases where it is followed by a continuation of the same statement,
ie a ”…”�”}”(hjO h²hh³Nh´Nubjk )�”}”(hŒ ``while``”h]”hŒwhile”…”�”}”(hji h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjO ubhŒ in a do-statement or an ”…”�”}”(hjO h²hh³Nh´Nubjk )�”}”(hŒ``else``”h]”hŒelse”…”�”}”(hj{ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjO ubhŒ in an if-statement, like
this:”…”�”}”(hjO h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K¦hjÇ h²hubj» )�”}”(hŒ1do {
body of do-loop
} while (condition);”h]”hŒ1do {
body of do-loop
} while (condition);”…”�”}”hj“ sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´K«hjÇ h²hubhÚ)�”}”(hŒand”h]”hŒand”…”�”}”(hj¢ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K±hjÇ h²hubj» )�”}”(hŒPif (x == y) {
..
} else if (x > y) {
...
} else {
....
}”h]”hŒPif (x == y) {
..
} else if (x > y) {
...
} else {
....
}”…”�”}”hj° sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´K³hjÇ h²hubhÚ)�”}”(hŒRationale: K&R.”h]”hŒRationale: K&R.”…”�”}”(hj¿ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K½hjÇ h²hubhÚ)�”}”(hX# 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.”h]”hX# 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.”…”�”}”(hjÍ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´K¿hjÇ h²hubhÚ)�”}”(hŒADo not unnecessarily use braces where a single statement will do.”h]”hŒADo not unnecessarily use braces where a single statement will do.”…”�”}”(hjÛ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KÅhjÇ h²hubj» )�”}”(hŒ if (condition)
action();”h]”hŒ if (condition)
action();”…”�”}”hjé sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´KÇhjÇ h²hubhÚ)�”}”(hŒand”h]”hŒand”…”�”}”(hjø h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KÌhjÇ h²hubj» )�”}”(hŒ9if (condition)
do_this();
else
do_that();”h]”hŒ9if (condition)
do_this();
else
do_that();”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´KÎhjÇ h²hubhÚ)�”}”(hŒˆThis does not apply if only one branch of a conditional statement is a single
statement; in the latter case use braces in both branches:”h]”hŒˆThis does not apply if only one branch of a conditional statement is a single
statement; in the latter case use braces in both branches:”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KÕhjÇ h²hubj» )�”}”(hŒVif (condition) {
do_this();
do_that();
} else {
otherwise();
}”h]”hŒVif (condition) {
do_this();
do_that();
} else {
otherwise();
}”…”�”}”hj# sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´KØhjÇ h²hubhÚ)�”}”(hŒJAlso, use braces when a loop contains more than a single simple statement:”h]”hŒJAlso, use braces when a loop contains more than a single simple statement:”…”�”}”(hj2 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KáhjÇ h²hubj» )�”}”(hŒGwhile (condition) {
if (test)
do_something();
}”h]”hŒGwhile (condition) {
if (test)
do_something();
}”…”�”}”hj@ sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´KãhjÇ h²hubhÅ)�”}”(hhh]”(hÊ)�”}”(hŒ3.1) Spaces”h]”hŒ3.1) Spaces”…”�”}”(hjR h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhjO h²hh³hÃh´KëubhÚ)�”}”(hXŒ Linux kernel style for use of spaces depends (mostly) on
function-versus-keyword usage. Use a space after (most) keywords. The
notable exceptions are sizeof, typeof, alignof, and __attribute__, which look
somewhat like functions (and are usually used with parentheses in Linux,
although they are not required in the language, as in: ``sizeof info`` after
``struct fileinfo info;`` is declared).”h]”(hXO Linux kernel style for use of spaces depends (mostly) on
function-versus-keyword usage. Use a space after (most) keywords. The
notable exceptions are sizeof, typeof, alignof, and __attribute__, which look
somewhat like functions (and are usually used with parentheses in Linux,
although they are not required in the language, as in: ”…”�”}”(hj` h²hh³Nh´Nubjk )�”}”(hŒ``sizeof info``”h]”hŒsizeof info”…”�”}”(hjh h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj` ubhŒ after
”…”�”}”(hj` h²hh³Nh´Nubjk )�”}”(hŒ``struct fileinfo info;``”h]”hŒstruct fileinfo info;”…”�”}”(hjz h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj` ubhŒ is declared).”…”�”}”(hj` h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KíhjO h²hubhÚ)�”}”(hŒ%So use a space after these keywords::”h]”hŒ$So use a space after these keywords:”…”�”}”(hj’ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KôhjO h²hubj» )�”}”(hŒ if, switch, case, for, do, while”h]”hŒ if, switch, case, for, do, while”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË uh1jº h³hÃh´KöhjO h²hubhÚ)�”}”(hŒ>but not with sizeof, typeof, alignof, or __attribute__. E.g.,”h]”hŒ>but not with sizeof, typeof, alignof, or __attribute__. E.g.,”…”�”}”(hj® h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KøhjO h²hubj» )�”}”(hŒs = sizeof(struct file);”h]”hŒs = sizeof(struct file);”…”�”}”hj¼ sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´KúhjO h²hubhÚ)�”}”(hŒVDo not add spaces around (inside) parenthesized expressions. This example is
**bad**:”h]”(hŒNDo not add spaces around (inside) parenthesized expressions. This example is
”…”�”}”(hjË h²hh³Nh´Nubhä)�”}”(hŒ**bad**”h]”hŒbad”…”�”}”(hjÓ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhjË ubhŒ:”…”�”}”(hjË h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´KÿhjO h²hubj» )�”}”(hŒs = sizeof( struct file );”h]”hŒs = sizeof( struct file );”…”�”}”hjë sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´MhjO h²hubhÚ)�”}”(hŒ½When declaring pointer data or a function that returns a pointer type, the
preferred use of ``*`` is adjacent to the data name or function name and not
adjacent to the type name. Examples:”h]”(hŒ\When declaring pointer data or a function that returns a pointer type, the
preferred use of ”…”�”}”(hjú h²hh³Nh´Nubjk )�”}”(hŒ``*``”h]”hŒ*”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjú ubhŒ\ is adjacent to the data name or function name and not
adjacent to the type name. Examples:”…”�”}”(hjú h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MhjO h²hubj» )�”}”(hŒnchar *linux_banner;
unsigned long long memparse(char *ptr, char **retptr);
char *match_strdup(substring_t *s);”h]”hŒnchar *linux_banner;
unsigned long long memparse(char *ptr, char **retptr);
char *match_strdup(substring_t *s);”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´MhjO h²hubhÚ)�”}”(hŒ`Use one space around (on each side of) most binary and ternary operators,
such as any of these::”h]”hŒ_Use one space around (on each side of) most binary and ternary operators,
such as any of these:”…”�”}”(hj) h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MhjO h²hubj» )�”}”(hŒ5= + - < > * / % | & ^ <= >= == != ? :”h]”hŒ5= + - < > * / % | & ^ <= >= == != ? :”…”�”}”hj7 sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË uh1jº h³hÃh´MhjO h²hubhÚ)�”}”(hŒ$but no space after unary operators::”h]”hŒ#but no space after unary operators:”…”�”}”(hjE h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MhjO h²hubj» )�”}”(hŒA& * + - ~ ! sizeof typeof alignof __attribute__ defined”h]”hŒA& * + - ~ ! sizeof typeof alignof __attribute__ defined”…”�”}”hjS sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË uh1jº h³hÃh´MhjO h²hubhÚ)�”}”(hŒCno space before the postfix increment & decrement unary operators::”h]”hŒBno space before the postfix increment & decrement unary operators:”…”�”}”(hja h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MhjO h²hubj» )�”}”(hŒ++ --”h]”hŒ++ --”…”�”}”hjo sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË uh1jº h³hÃh´MhjO h²hubhÚ)�”}”(hŒAno space after the prefix increment & decrement unary operators::”h]”hŒ@no space after the prefix increment & decrement unary operators:”…”�”}”(hj} h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MhjO h²hubj» )�”}”(hŒ++ --”h]”hŒ++ --”…”�”}”hj‹ sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË uh1jº h³hÃh´M!hjO h²hubhÚ)�”}”(hŒDand no space around the ``.`` and ``->`` structure member operators.”h]”(hŒand no space around the ”…”�”}”(hj™ h²hh³Nh´Nubjk )�”}”(hŒ``.``”h]”hŒ.”…”�”}”(hj¡ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj™ ubhŒ and ”…”�”}”(hj™ h²hh³Nh´Nubjk )�”}”(hŒ``->``”h]”hŒ->”…”�”}”(hj³ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj™ ubhŒ structure member operators.”…”�”}”(hj™ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M#hjO h²hubhÚ)�”}”(hX Do not leave trailing whitespace at the ends of lines. Some editors with
``smart`` indentation will insert whitespace at the beginning of new lines as
appropriate, so you can start typing the next line of code right away.
However, some such editors do not remove the whitespace if you end up not
putting a line of code there, such as if you leave a blank line. As a result,
you end up with lines containing trailing whitespace.”h]”(hŒJDo not leave trailing whitespace at the ends of lines. Some editors with
”…”�”}”(hjË h²hh³Nh´Nubjk )�”}”(hŒ ``smart``”h]”hŒsmart”…”�”}”(hjÓ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjË ubhXZ indentation will insert whitespace at the beginning of new lines as
appropriate, so you can start typing the next line of code right away.
However, some such editors do not remove the whitespace if you end up not
putting a line of code there, such as if you leave a blank line. As a result,
you end up with lines containing trailing whitespace.”…”�”}”(hjË h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M%hjO h²hubhÚ)�”}”(hŒ÷Git will warn you about patches that introduce trailing whitespace, and can
optionally strip the trailing whitespace for you; however, if applying a series
of patches, this may make later patches in the series fail by changing their
context lines.”h]”hŒ÷Git will warn you about patches that introduce trailing whitespace, and can
optionally strip the trailing whitespace for you; however, if applying a series
of patches, this may make later patches in the series fail by changing their
context lines.”…”�”}”(hjë h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M,hjO h²hubeh}”(h]”Œspaces”ah ]”h"]”Œ3.1) spaces”ah$]”h&]”uh1hÄhjÇ h²hh³hÃh´Këubeh}”(h]”Œplacing-braces-and-spaces”ah ]”h"]”Œ3) placing braces and spaces”ah$]”h&]”uh1hÄhhÆh²hh³hÃh´KyubhÅ)�”}”(hhh]”(hÊ)�”}”(hŒ 4) Naming”h]”hŒ 4) Naming”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhj h²hh³hÃh´M3ubhÚ)�”}”(hX: C is a Spartan language, and your naming conventions should follow suit.
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.”h]”(hŒãC is a Spartan language, and your naming conventions should follow suit.
Unlike Modula-2 and Pascal programmers, C programmers do not use cute
names like ThisVariableIsATemporaryCounter. A C programmer would call that
variable ”…”�”}”(hj h²hh³Nh´Nubjk )�”}”(hŒ``tmp``”h]”hŒtmp”…”�”}”(hj" h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj ubhŒP, which is much easier to write, and not the least more
difficult to understand.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M5hj h²hubhÚ)�”}”(hŒž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.”h]”(hŒ€HOWEVER, while mixed-case names are frowned upon, descriptive names for
global variables are a must. To call a global function ”…”�”}”(hj: h²hh³Nh´Nubjk )�”}”(hŒ``foo``”h]”hŒfoo”…”�”}”(hjB h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj: ubhŒ is a
shooting offense.”…”�”}”(hj: h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M;hj h²hubhÚ)�”}”(hX 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()``.”h]”(hŒ)GLOBAL variables (to be used only if you ”…”�”}”(hjZ h²hh³Nh´Nubhä)�”}”(hŒ
**really**”h]”hŒreally”…”�”}”(hjb h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhjZ ubhŒ™ 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
”…”�”}”(hjZ h²hh³Nh´Nubjk )�”}”(hŒ``count_active_users()``”h]”hŒcount_active_users()”…”�”}”(hjt h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjZ ubhŒ or similar, you should ”…”�”}”(hjZ h²hh³Nh´Nubhä)�”}”(hŒ**not**”h]”hŒnot”…”�”}”(hj† h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhjZ ubhŒ call it ”…”�”}”(hjZ h²hh³Nh´Nubjk )�”}”(hŒ``cntusr()``”h]”hŒcntusr()”…”�”}”(hj˜ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjZ ubhŒ.”…”�”}”(hjZ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M?hj h²hubhÚ)�”}”(hŒ·Encoding the type of a function into the name (so-called Hungarian
notation) is asinine - the compiler knows the types anyway and can check
those, and it only confuses the programmer.”h]”hŒ·Encoding the type of a function into the name (so-called Hungarian
notation) is asinine - the compiler knows the types anyway and can check
those, and it only confuses the programmer.”…”�”}”(hj° h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MDhj h²hubhÚ)�”}”(hXN 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.”h]”(hŒ„LOCAL variable names should be short, and to the point. If you have
some random integer loop counter, it should probably be called ”…”�”}”(hj¾ h²hh³Nh´Nubjk )�”}”(hŒ``i``”h]”hŒi”…”�”}”(hjÆ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj¾ ubhŒ
.
Calling it ”…”�”}”(hj¾ h²hh³Nh´Nubjk )�”}”(hŒ``loop_counter``”h]”hŒloop_counter”…”�”}”(hjØ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj¾ ubhŒR is non-productive, if there is no chance of it
being mis-understood. Similarly, ”…”�”}”(hj¾ h²hh³Nh´Nubjk )�”}”(hŒ``tmp``”h]”hŒtmp”…”�”}”(hjê h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj¾ ubhŒO can be just about any type of
variable that is used to hold a temporary value.”…”�”}”(hj¾ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MHhj h²hubhÚ)�”}”(hŒ«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 chapter 6 (Functions).”h]”hŒ«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 chapter 6 (Functions).”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MNhj h²hubhÚ)�”}”(hŒ•For symbol names and documentation, avoid introducing new usage of
'master / slave' (or 'slave' independent of 'master') and 'blacklist /
whitelist'.”h]”hŒ¥For symbol names and documentation, avoid introducing new usage of
‘master / slave’ (or ‘slave’ independent of ‘master’) and ‘blacklist /
whitelist’.”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MRhj h²hubhŒdefinition_list”“”)�”}”(hhh]”(hŒdefinition_list_item”“”)�”}”(hŒêRecommended replacements for 'master / slave' are:
'{primary,main} / {secondary,replica,subordinate}'
'{initiator,requester} / {target,responder}'
'{controller,host} / {device,worker,proxy}'
'leader / follower'
'director / performer'
”h]”(hŒterm”“”)�”}”(hŒ2Recommended replacements for 'master / slave' are:”h]”hŒ6Recommended replacements for ‘master / slave’ are:”…”�”}”(hj+ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j) h³hÃh´M[hj% ubhŒ
definition”“”)�”}”(hhh]”hÚ)�”}”(hŒ¶'{primary,main} / {secondary,replica,subordinate}'
'{initiator,requester} / {target,responder}'
'{controller,host} / {device,worker,proxy}'
'leader / follower'
'director / performer'”h]”hŒÊ‘{primary,main} / {secondary,replica,subordinate}’
‘{initiator,requester} / {target,responder}’
‘{controller,host} / {device,worker,proxy}’
‘leader / follower’
‘director / performer’”…”�”}”(hj> h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MWhj; ubah}”(h]”h ]”h"]”h$]”h&]”uh1j9 hj% ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j# h³hÃh´M[hj ubj$ )�”}”(hŒfRecommended replacements for 'blacklist/whitelist' are:
'denylist / allowlist'
'blocklist / passlist'
”h]”(j* )�”}”(hŒ7Recommended replacements for 'blacklist/whitelist' are:”h]”hŒ;Recommended replacements for ‘blacklist/whitelist’ are:”…”�”}”(hj\ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j) h³hÃh´M_hjX ubj: )�”}”(hhh]”hÚ)�”}”(hŒ-'denylist / allowlist'
'blocklist / passlist'”h]”hŒ5‘denylist / allowlist’
‘blocklist / passlist’”…”�”}”(hjm h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M^hjj ubah}”(h]”h ]”h"]”h$]”h&]”uh1j9 hjX ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j# h³hÃh´M_hj h²hubeh}”(h]”h ]”h"]”h$]”h&]”uh1j hj h²hh³hÃh´NubhÚ)�”}”(hX/ Exceptions for introducing new usage is to maintain a userspace ABI/API,
or when updating code for an existing (as of 2020) hardware or protocol
specification that mandates those terms. For new specifications
translate specification usage of the terminology to the kernel coding
standard where possible.”h]”hX/ Exceptions for introducing new usage is to maintain a userspace ABI/API,
or when updating code for an existing (as of 2020) hardware or protocol
specification that mandates those terms. For new specifications
translate specification usage of the terminology to the kernel coding
standard where possible.”…”�”}”(hj� h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Mahj h²hubeh}”(h]”Œnaming”ah ]”h"]”Œ 4) naming”ah$]”h&]”uh1hÄhhÆh²hh³hÃh´M3ubhÅ)�”}”(hhh]”(hÊ)�”}”(hŒ5) Typedefs”h]”hŒ5) Typedefs”…”�”}”(hj¦ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhj£ h²hh³hÃh´MhubhÚ)�”}”(hŒuPlease don't use things like ``vps_t``.
It's a **mistake** to use typedef for structures and pointers. When you see a”h]”(hŒPlease don’t use things like ”…”�”}”(hj´ h²hh³Nh´Nubjk )�”}”(hŒ ``vps_t``”h]”hŒvps_t”…”�”}”(hj¼ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj´ ubhŒ.
It’s a ”…”�”}”(hj´ h²hh³Nh´Nubhä)�”}”(hŒ**mistake**”h]”hŒmistake”…”�”}”(hjÎ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhj´ ubhŒ; to use typedef for structures and pointers. When you see a”…”�”}”(hj´ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Mjhj£ h²hubj» )�”}”(hŒvps_t a;”h]”hŒvps_t a;”…”�”}”hjæ sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´Mmhj£ h²hubhÚ)�”}”(hŒ9in the source, what does it mean?
In contrast, if it says”h]”hŒ9in the source, what does it mean?
In contrast, if it says”…”�”}”(hjõ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Mrhj£ h²hubj» )�”}”(hŒstruct virtual_container *a;”h]”hŒstruct virtual_container *a;”…”�”}”hj sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´Muhj£ h²hubhÚ)�”}”(hŒ$you can actually tell what ``a`` is.”h]”(hŒyou can actually tell what ”…”�”}”(hj h²hh³Nh´Nubjk )�”}”(hŒ``a``”h]”hŒa”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj ubhŒ is.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Myhj£ h²hubhÚ)�”}”(hŒZLots of people think that typedefs ``help readability``. Not so. They are
useful only for:”h]”(hŒ#Lots of people think that typedefs ”…”�”}”(hj2 h²hh³Nh´Nubjk )�”}”(hŒ``help readability``”h]”hŒhelp readability”…”�”}”(hj: h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj2 ubhŒ#. Not so. They are
useful only for:”…”�”}”(hj2 h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M{hj£ h²hubhŒblock_quote”“”)�”}”(hX (a) totally opaque objects (where the typedef is actively used to **hide**
what the object is).
Example: ``pte_t`` etc. opaque objects that you can only access using
the proper accessor functions.
.. note::
Opaqueness and ``accessor functions`` are not good in themselves.
The reason we have them for things like pte_t etc. is that there
really is absolutely **zero** portably accessible information there.
(b) Clear integer types, where the abstraction **helps** avoid confusion
whether it is ``int`` or ``long``.
u8/u16/u32 are perfectly fine typedefs, although they fit into
category (d) better than here.
.. note::
Again - there needs to be a **reason** for this. If something is
``unsigned long``, then there's no reason to do
typedef unsigned long myflags_t;
but if there is a clear reason for why it under certain circumstances
might be an ``unsigned int`` and under other configurations might be
``unsigned long``, then by all means go ahead and use a typedef.
(c) when you use sparse to literally create a **new** type for
type-checking.
(d) New types which are identical to standard C99 types, in certain
exceptional circumstances.
Although it would only take a short amount of time for the eyes and
brain to become accustomed to the standard types like ``uint32_t``,
some people object to their use anyway.
Therefore, the Linux-specific ``u8/u16/u32/u64`` types and their
signed equivalents which are identical to standard types are
permitted -- although they are not mandatory in new code of your
own.
When editing existing code which already uses one or the other set
of types, you should conform to the existing choices in that code.
(e) Types safe for use in userspace.
In certain structures which are visible to userspace, we cannot
require C99 types and cannot use the ``u32`` form above. Thus, we
use __u32 and similar types in all structures which are shared
with userspace.
”h]”hŒenumerated_list”“”)�”}”(hhh]”(hŒ list_item”“”)�”}”(hXœ totally opaque objects (where the typedef is actively used to **hide**
what the object is).
Example: ``pte_t`` etc. opaque objects that you can only access using
the proper accessor functions.
.. note::
Opaqueness and ``accessor functions`` are not good in themselves.
The reason we have them for things like pte_t etc. is that there
really is absolutely **zero** portably accessible information there.
”h]”(hÚ)�”}”(hŒ[totally opaque objects (where the typedef is actively used to **hide**
what the object is).”h]”(hŒ>totally opaque objects (where the typedef is actively used to ”…”�”}”(hjc h²hh³Nh´Nubhä)�”}”(hŒ**hide**”h]”hŒhide”…”�”}”(hjk h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhjc ubhŒ
what the object is).”…”�”}”(hjc h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M~hj_ ubhÚ)�”}”(hŒdExample: ``pte_t`` etc. opaque objects that you can only access using
the proper accessor functions.”h]”(hŒ Example: ”…”�”}”(hjƒ h²hh³Nh´Nubjk )�”}”(hŒ ``pte_t``”h]”hŒpte_t”…”�”}”(hj‹ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjƒ ubhŒR etc. opaque objects that you can only access using
the proper accessor functions.”…”�”}”(hjƒ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M�hj_ ubhŒnote”“”)�”}”(hŒÇOpaqueness and ``accessor functions`` are not good in themselves.
The reason we have them for things like pte_t etc. is that there
really is absolutely **zero** portably accessible information there.”h]”hÚ)�”}”(hŒÇOpaqueness and ``accessor functions`` are not good in themselves.
The reason we have them for things like pte_t etc. is that there
really is absolutely **zero** portably accessible information there.”h]”(hŒOpaqueness and ”…”�”}”(hj© h²hh³Nh´Nubjk )�”}”(hŒ``accessor functions``”h]”hŒaccessor functions”…”�”}”(hj± h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj© ubhŒs are not good in themselves.
The reason we have them for things like pte_t etc. is that there
really is absolutely ”…”�”}”(hj© h²hh³Nh´Nubhä)�”}”(hŒ**zero**”h]”hŒzero”…”�”}”(hjà h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhj© ubhŒ' portably accessible information there.”…”�”}”(hj© h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M†hj¥ ubah}”(h]”h ]”h"]”h$]”h&]”uh1j£ hj_ ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j] hjZ ubj^ )�”}”(hX: Clear integer types, where the abstraction **helps** avoid confusion
whether it is ``int`` or ``long``.
u8/u16/u32 are perfectly fine typedefs, although they fit into
category (d) better than here.
.. note::
Again - there needs to be a **reason** for this. If something is
``unsigned long``, then there's no reason to do
typedef unsigned long myflags_t;
but if there is a clear reason for why it under certain circumstances
might be an ``unsigned int`` and under other configurations might be
``unsigned long``, then by all means go ahead and use a typedef.
”h]”(hÚ)�”}”(hŒgClear integer types, where the abstraction **helps** avoid confusion
whether it is ``int`` or ``long``.”h]”(hŒ+Clear integer types, where the abstraction ”…”�”}”(hjë h²hh³Nh´Nubhä)�”}”(hŒ **helps**”h]”hŒhelps”…”�”}”(hjó h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhjë ubhŒ avoid confusion
whether it is ”…”�”}”(hjë h²hh³Nh´Nubjk )�”}”(hŒ``int``”h]”hŒint”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjë ubhŒ or ”…”�”}”(hjë h²hh³Nh´Nubjk )�”}”(hŒ``long``”h]”hŒlong”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjë ubhŒ.”…”�”}”(hjë h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MŠhjç ubhÚ)�”}”(hŒ]u8/u16/u32 are perfectly fine typedefs, although they fit into
category (d) better than here.”h]”hŒ]u8/u16/u32 are perfectly fine typedefs, although they fit into
category (d) better than here.”…”�”}”(hj/ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M�hjç ubj¤ )�”}”(hŒ“Again - there needs to be a **reason** for this. If something is
``unsigned long``, then there's no reason to do
typedef unsigned long myflags_t;”h]”(hÚ)�”}”(hŒpAgain - there needs to be a **reason** for this. If something is
``unsigned long``, then there's no reason to do”h]”(hŒAgain - there needs to be a ”…”�”}”(hjA h²hh³Nh´Nubhä)�”}”(hŒ
**reason**”h]”hŒreason”…”�”}”(hjI h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhjA ubhŒ for this. If something is
”…”�”}”(hjA h²hh³Nh´Nubjk )�”}”(hŒ``unsigned long``”h]”hŒ
unsigned long”…”�”}”(hj[ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjA ubhŒ , then there’s no reason to do”…”�”}”(hjA h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M’hj= ubjS )�”}”(hŒ typedef unsigned long myflags_t;”h]”hÚ)�”}”(hju h]”hŒ typedef unsigned long myflags_t;”…”�”}”(hjw h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M•hjs ubah}”(h]”h ]”h"]”h$]”h&]”uh1jR h³hÃh´M•hj= ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j£ hjç ubhÚ)�”}”(hŒËbut if there is a clear reason for why it under certain circumstances
might be an ``unsigned int`` and under other configurations might be
``unsigned long``, then by all means go ahead and use a typedef.”h]”(hŒRbut if there is a clear reason for why it under certain circumstances
might be an ”…”�”}”(hj� h²hh³Nh´Nubjk )�”}”(hŒ``unsigned int``”h]”hŒunsigned int”…”�”}”(hj˜ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj� ubhŒ) and under other configurations might be
”…”�”}”(hj� h²hh³Nh´Nubjk )�”}”(hŒ``unsigned long``”h]”hŒ
unsigned long”…”�”}”(hjª h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj� ubhŒ/, then by all means go ahead and use a typedef.”…”�”}”(hj� h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M—hjç ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j] hjZ ubj^ )�”}”(hŒJwhen you use sparse to literally create a **new** type for
type-checking.
”h]”hÚ)�”}”(hŒIwhen you use sparse to literally create a **new** type for
type-checking.”h]”(hŒ*when you use sparse to literally create a ”…”�”}”(hjÌ h²hh³Nh´Nubhä)�”}”(hŒ**new**”h]”hŒnew”…”�”}”(hjÔ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhjÌ ubhŒ type for
type-checking.”…”�”}”(hjÌ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M›hjÈ ubah}”(h]”h ]”h"]”h$]”h&]”uh1j] hjZ ubj^ )�”}”(hXX New types which are identical to standard C99 types, in certain
exceptional circumstances.
Although it would only take a short amount of time for the eyes and
brain to become accustomed to the standard types like ``uint32_t``,
some people object to their use anyway.
Therefore, the Linux-specific ``u8/u16/u32/u64`` types and their
signed equivalents which are identical to standard types are
permitted -- although they are not mandatory in new code of your
own.
When editing existing code which already uses one or the other set
of types, you should conform to the existing choices in that code.
”h]”(hÚ)�”}”(hŒZNew types which are identical to standard C99 types, in certain
exceptional circumstances.”h]”hŒZNew types which are identical to standard C99 types, in certain
exceptional circumstances.”…”�”}”(hjö h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Mžhjò ubhÚ)�”}”(hŒ¯Although it would only take a short amount of time for the eyes and
brain to become accustomed to the standard types like ``uint32_t``,
some people object to their use anyway.”h]”(hŒzAlthough it would only take a short amount of time for the eyes and
brain to become accustomed to the standard types like ”…”�”}”(hj
h²hh³Nh´Nubjk )�”}”(hŒ``uint32_t``”h]”hŒuint32_t”…”�”}”(hj
h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj
ubhŒ),
some people object to their use anyway.”…”�”}”(hj
h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M¡hjò ubhÚ)�”}”(hŒÃTherefore, the Linux-specific ``u8/u16/u32/u64`` types and their
signed equivalents which are identical to standard types are
permitted -- although they are not mandatory in new code of your
own.”h]”(hŒTherefore, the Linux-specific ”…”�”}”(hj$
h²hh³Nh´Nubjk )�”}”(hŒ``u8/u16/u32/u64``”h]”hŒu8/u16/u32/u64”…”�”}”(hj,
h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hj$
ubhŒ“ types and their
signed equivalents which are identical to standard types are
permitted -- although they are not mandatory in new code of your
own.”…”�”}”(hj$
h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M¥hjò ubhÚ)�”}”(hŒ…When editing existing code which already uses one or the other set
of types, you should conform to the existing choices in that code.”h]”hŒ…When editing existing code which already uses one or the other set
of types, you should conform to the existing choices in that code.”…”�”}”(hjD
h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´Mªhjò ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j] hjZ ubj^ )�”}”(hŒóTypes safe for use in userspace.
In certain structures which are visible to userspace, we cannot
require C99 types and cannot use the ``u32`` form above. Thus, we
use __u32 and similar types in all structures which are shared
with userspace.
”h]”(hÚ)�”}”(hŒ Types safe for use in userspace.”h]”hŒ Types safe for use in userspace.”…”�”}”(hj\
h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MhjX
ubhÚ)�”}”(hŒÐIn certain structures which are visible to userspace, we cannot
require C99 types and cannot use the ``u32`` form above. Thus, we
use __u32 and similar types in all structures which are shared
with userspace.”h]”(hŒeIn certain structures which are visible to userspace, we cannot
require C99 types and cannot use the ”…”�”}”(hjj
h²hh³Nh´Nubjk )�”}”(hŒ``u32``”h]”hŒu32”…”�”}”(hjr
h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjj
ubhŒd form above. Thus, we
use __u32 and similar types in all structures which are shared
with userspace.”…”�”}”(hjj
h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M¯hjX
ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j] hjZ ubeh}”(h]”h ]”h"]”h$]”h&]”Œenumtype”Œ
loweralpha”Œprefix”Œ(”Œsuffix”Œ)”uh1jX hjT ubah}”(h]”h ]”h"]”h$]”h&]”uh1jR h³hÃh´M~hj£ h²hubhÚ)�”}”(hŒŽMaybe there are other cases too, but the rule should basically be to NEVER
EVER use a typedef unless you can clearly match one of those rules.”h]”hŒŽMaybe there are other cases too, but the rule should basically be to NEVER
EVER use a typedef unless you can clearly match one of those rules.”…”�”}”(hj¢
h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M´hj£ h²hubhÚ)�”}”(hŒ|In general, a pointer, or a struct that has elements that can reasonably
be directly accessed should **never** be a typedef.”h]”(hŒeIn general, a pointer, or a struct that has elements that can reasonably
be directly accessed should ”…”�”}”(hj°
h²hh³Nh´Nubhä)�”}”(hŒ **never**”h]”hŒnever”…”�”}”(hj¸
h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhj°
ubhŒ be a typedef.”…”�”}”(hj°
h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M·hj£ h²hubeh}”(h]”Œtypedefs”ah ]”h"]”Œ5) typedefs”ah$]”h&]”uh1hÄhhÆh²hh³hÃh´MhubhÅ)�”}”(hhh]”(hÊ)�”}”(hŒ6) Functions”h]”hŒ6) Functions”…”�”}”(hjÛ
h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhjØ
h²hh³hÃh´M¼ubhÚ)�”}”(hŒÅ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.”h]”hŒÅ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.”…”�”}”(hjé
h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M¾hjØ
h²hubhÚ)�”}”(hXB 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.”h]”hXD 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.”…”�”}”(hj÷
h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MÂhjØ
h²hubhÚ)�”}”(hX¬ 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).”h]”hX® 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).”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MÈhjØ
h²hubhÚ)�”}”(hX‡ 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.”h]”hX� 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.”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MÐhjØ
h²hubhÚ)�”}”(hŒºIn source files, separate functions with one blank line. If the function is
exported, the **EXPORT** macro for it should follow immediately after the
closing function brace line. E.g.:”h]”(hŒ[In source files, separate functions with one blank line. If the function is
exported, the ”…”�”}”(hj! h²hh³Nh´Nubhä)�”}”(hŒ
**EXPORT**”h]”hŒEXPORT”…”�”}”(hj) h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hãhj! ubhŒU macro for it should follow immediately after the
closing function brace line. E.g.:”…”�”}”(hj! h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´M×hjØ
h²hubj» )�”}”(hŒfint system_is_up(void)
{
return system_state == SYSTEM_RUNNING;
}
EXPORT_SYMBOL(system_is_up);”h]”hŒfint system_is_up(void)
{
return system_state == SYSTEM_RUNNING;
}
EXPORT_SYMBOL(system_is_up);”…”�”}”hjA sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË jÌ ‰jÍ jÎ jÏ }”uh1jº h³hÃh´MÛhjØ
h²hubhÅ)�”}”(hhh]”(hÊ)�”}”(hŒ6.1) Function prototypes”h]”hŒ6.1) Function prototypes”…”�”}”(hjS h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÉhjP h²hh³hÃh´MäubhÚ)�”}”(hŒ×In function prototypes, include parameter names with their data types.
Although this is not required by the C language, it is preferred in Linux
because it is a simple way to add valuable information for the reader.”h]”hŒ×In function prototypes, include parameter names with their data types.
Although this is not required by the C language, it is preferred in Linux
because it is a simple way to add valuable information for the reader.”…”�”}”(hja h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MæhjP h²hubhÚ)�”}”(hŒuDo not use the ``extern`` keyword with function declarations as this makes
lines longer and isn't strictly necessary.”h]”(hŒDo not use the ”…”�”}”(hjo h²hh³Nh´Nubjk )�”}”(hŒ
``extern``”h]”hŒextern”…”�”}”(hjw h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jj hjo ubhŒ^ keyword with function declarations as this makes
lines longer and isn’t strictly necessary.”…”�”}”(hjo h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MêhjP h²hubhÚ)�”}”(hŒïWhen writing function prototypes, please keep the `order of elements regular
`_.
For example, using this function declaration example::”h]”(hŒ2When writing function prototypes, please keep the ”…”�”}”(hj� h²hh³Nh´NubhŒ reference”“”)�”}”(hŒ…`order of elements regular
`_”h]”hŒorder of elements regular”…”�”}”(hj™ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œorder of elements regular”Œrefuri”Œfhttps://lore.kernel.org/mm-commits/CAHk-=wiOCLRny5aifWNhr621kYrJwhfURsa0vFPeUEm8mF0ufg@mail.gmail.com/”uh1j— hj� ubh¶)�”}”(hŒi
”h]”h}”(h]”Œorder-of-elements-regular”ah ]”h"]”Œorder of elements regular”ah$]”h&]”Œrefuri”jª uh1hµŒ
referenced”Khj� ubhŒ7.
For example, using this function declaration example:”…”�”}”(hj� h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÙh³hÃh´MíhjP h²hubj» )�”}”(hŒ•__init void * __must_check action(enum magic value, size_t size, u8 count,
char *fmt, ...) __printf(4, 5) __malloc;”h]”hŒ•__init void * __must_check action(enum magic value, size_t size, u8 count,
char *fmt, ...) __printf(4, 5) __malloc;”…”�”}”hjà sbah}”(h]”h ]”h"]”h$]”h&]”jÊ jË uh1jº h³hÃh´MñhjP h²hubhÚ)�”}”(hŒ