diff options
Diffstat (limited to 'Documentation/CodingStyle')
| -rw-r--r-- | Documentation/CodingStyle | 127 | 
1 files changed, 87 insertions, 40 deletions
diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle index 8bb37237ebd..6b6bef31e95 100644 --- a/Documentation/CodingStyle +++ b/Documentation/CodingStyle @@ -80,22 +80,13 @@ available tools.  The limit on the length of lines is 80 columns and this is a strongly  preferred limit. -Statements longer than 80 columns will be broken into sensible chunks. -Descendants are always substantially shorter than the parent and are placed -substantially to the right. The same applies to function headers with a long -argument list. Long strings are as well broken into shorter strings. The -only exception to this is where exceeding 80 columns significantly increases -readability and does not hide information. - -void fun(int a, int b, int c) -{ -	if (condition) -		printk(KERN_WARNING "Warning this is a long printk with " -						"3 parameters a: %u b: %u " -						"c: %u \n", a, b, c); -	else -		next_statement; -} +Statements longer than 80 columns will be broken into sensible chunks, unless +exceeding 80 columns significantly increases readability and does not hide +information. Descendants are always substantially shorter than the parent and +are placed substantially to the right. The same applies to function headers +with a long argument list. However, never break user-visible strings such as +printk messages, because that breaks the ability to grep for them. +  		Chapter 3: Placing Braces and Spaces @@ -168,8 +159,15 @@ Do not unnecessarily use braces where a single statement will do.  if (condition)  	action(); -This does not apply if one branch of a conditional statement is a single -statement. Use braces in both branches. +and + +if (condition) +	do_this(); +else +	do_that(); + +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:  if (condition) {  	do_this(); @@ -391,7 +389,8 @@ Albeit deprecated by some people, the equivalent of the goto statement is  used frequently by compilers in form of the unconditional jump instruction.  The goto statement comes in handy when a function exits from multiple -locations and some common work such as cleanup has to be done. +locations and some common work such as cleanup has to be done.  If there is no +cleanup needed then just return directly.  The rationale is: @@ -456,6 +455,16 @@ The preferred style for long (multi-line) comments is:  	 * with beginning and ending almost-blank lines.  	 */ +For files in net/ and drivers/net/ the preferred style for long (multi-line) +comments is a little different. + +	/* The preferred comment style for files in net/ and drivers/net +	 * looks like this. +	 * +	 * It is nearly the same as the generally preferred comment style, +	 * but there is no initial almost-blank line. +	 */ +  It's also important to comment data, whether they are basic types or derived  types.  To this end, use just one data declaration per line (no commas for  multiple data declarations).  This leaves you room for a small comment on each @@ -538,15 +547,7 @@ config AUDIT  	  logging of avc messages output).  Does not do system-call  	  auditing without CONFIG_AUDITSYSCALL. -Features that might still be considered unstable should be defined as -dependent on "EXPERIMENTAL": - -config SLUB -	depends on EXPERIMENTAL && !ARCH_USES_SLAB_PAGE_STRUCT -	bool "SLUB (Unqueued Allocator)" -	... - -while seriously dangerous features (such as write support for certain +Seriously dangerous features (such as write support for certain  filesystems) should advertise this prominently in their prompt string:  config ADFS_FS_RW @@ -659,22 +660,31 @@ There are a number of driver model diagnostic macros in <linux/device.h>  which you should use to make sure messages are matched to the right device  and driver, and are tagged with the right level:  dev_err(), dev_warn(),  dev_info(), and so forth.  For messages that aren't associated with a -particular device, <linux/kernel.h> defines pr_debug() and pr_info(). +particular device, <linux/printk.h> defines pr_notice(), pr_info(), +pr_warn(), pr_err(), etc.  Coming up with good debugging messages can be quite a challenge; and once -you have them, they can be a huge help for remote troubleshooting.  Such -messages should be compiled out when the DEBUG symbol is not defined (that -is, by default they are not included).  When you use dev_dbg() or pr_debug(), -that's automatic.  Many subsystems have Kconfig options to turn on -DDEBUG. -A related convention uses VERBOSE_DEBUG to add dev_vdbg() messages to the -ones already enabled by DEBUG. +you have them, they can be a huge help for remote troubleshooting.  However +debug message printing is handled differently than printing other non-debug +messages.  While the other pr_XXX() functions print unconditionally, +pr_debug() does not; it is compiled out by default, unless either DEBUG is +defined or CONFIG_DYNAMIC_DEBUG is set.  That is true for dev_dbg() also, +and a related convention uses VERBOSE_DEBUG to add dev_vdbg() messages to +the ones already enabled by DEBUG. + +Many subsystems have Kconfig debug options to turn on -DDEBUG in the +corresponding Makefile; in other cases specific files #define DEBUG.  And +when a debug message should be unconditionally printed, such as if it is +already inside a debug-related #ifdef secton, printk(KERN_DEBUG ...) can be +used.  		Chapter 14: Allocating memory  The kernel provides the following general purpose memory allocators: -kmalloc(), kzalloc(), kcalloc(), and vmalloc().  Please refer to the API -documentation for further information about them. +kmalloc(), kzalloc(), kmalloc_array(), kcalloc(), vmalloc(), and +vzalloc().  Please refer to the API documentation for further information +about them.  The preferred form for passing a size of a struct is the following: @@ -688,6 +698,17 @@ Casting the return value which is a void pointer is redundant. The conversion  from void pointer to any other pointer type is guaranteed by the C programming  language. +The preferred form for allocating an array is the following: + +	p = kmalloc_array(n, sizeof(...), ...); + +The preferred form for allocating a zeroed array is the following: + +	p = kcalloc(n, sizeof(...), ...); + +Both forms check for overflow on the allocation size n * sizeof(...), +and return NULL if that occurred. +  		Chapter 15: The inline disease @@ -795,6 +816,35 @@ own custom mode, or may have some other magic method for making indentation  work correctly. +		Chapter 19:  Inline assembly + +In architecture-specific code, you may need to use inline assembly to interface +with CPU or platform functionality.  Don't hesitate to do so when necessary. +However, don't use inline assembly gratuitously when C can do the job.  You can +and should poke hardware from C when possible. + +Consider writing simple helper functions that wrap common bits of inline +assembly, rather than repeatedly writing them with slight variations.  Remember +that inline assembly can use C parameters. + +Large, non-trivial assembly functions should go in .S files, with corresponding +C prototypes defined in C header files.  The C prototypes for assembly +functions should use "asmlinkage". + +You may need to mark your asm statement as volatile, to prevent GCC from +removing it if GCC doesn't notice any side effects.  You don't always need to +do so, though, and doing so unnecessarily can limit optimization. + +When writing a single inline assembly statement containing multiple +instructions, put each instruction on a separate line in a separate quoted +string, and end each string except the last with \n\t to properly indent the +next instruction in the assembly output: + +	asm ("magic %reg1, #42\n\t" +	     "more_magic %reg2, %reg3" +	     : /* outputs */ : /* inputs */ : /* clobbers */); + +  		Appendix I: References @@ -819,6 +869,3 @@ language C, URL: http://www.open-std.org/JTC1/SC22/WG14/  Kernel CodingStyle, by greg@kroah.com at OLS 2002:  http://www.kroah.com/linux/talks/ols_2002_kernel_codingstyle_talk/html/ --- -Last updated on 2007-July-13. -  | 
