Linux Kernel GNUC extension-general Linux technology-Linux programming and kernel information. For more information, see the following. Gnc cc is a powerful cross-platform C compiler that provides many extensions to the C language,
These extensions provide strong support for optimization, target code layout, and more secure checks. In this article
The C Language Supporting GNU extension is called gnu c.
The Linux kernel code uses a large number of gnu c extensions, so that it can compile the unique compiler of the Linux kernel.
The interpreter is gnu cc. In the past, it even occurred that a special gnu cc version was used to compile the Linux kernel.
Status. This article is a summary of the gnu c extension used by the Linux kernel.
When you do not understand the syntax and semantics, you can find a preliminary answer from this article. For more information, see
Gcc.info. The example in this article is taken from Linux 2.4.18.
Statement expression ============
Gnu c regards compound statements contained in parentheses as an expression called a statement expression.
Currently, where expressions are allowed, you can use loops, local variables, and so on in statement expressions.
It can only be used in compound statements. For example:
+++ Include/linux/kernel. h
159: # define min_t (type, x, y )\
160: ({type _ x = (x); type _ y = (y); _ x <_ y? _ X: _ y ;})
+++ Net/ipv4/tcp_output.c
654: int full_space = min_t (int, tp-> window_clamp, tcp_full_space (sk ));
The last statement of a compound statement should be an expression, and its value will be the value of this statement expression.
A secure macro for minimum value is defined here. In Standard C, it is usually defined:
# Define min (x, y) (x) <(y )? (X): (y ))
This definition calculates x and y twice. When the parameter has side effects, an incorrect result is generated.
The statement expression calculates parameters only once, avoiding possible errors. Statement expressions are usually used for macro definition.
Typeof ======
To use the macro defined in the previous section, you need to know the parameter type. You can use typeof to define a more general macro.
The parameter type must be known in advance, for example:
+++ Include/linux/kernel. h
141: # define min (x, y )({\
142: const typeof (x) _ x = (x );\
143: const typeof (y) _ y = (y );\
144: (void) (& _ x = & _ y );\
145: _ x <_ y? _ X: _ y ;})
In this example, typeof (x) indicates the value type of x. Row 142nd defines a local variable of the same type as x.
The value _ x is converted to x. Note that the function of row 144th is to check whether the types of parameters x and y are the same.
Typeof can be used in any type and is usually used for macro definition.
Zero-length Array ============
Gnu c allows zero-length arrays. This feature is useful when defining the header structure of a variable-length object. Example
For example:
The last element of the structure is defined as a zero-length array, which does not occupy the space of the structure. In standard C
The length of the defined array is 1, and the size of the calculated object is complicated during allocation.
Variable Parameter macro ============
In gnu c, macros can accept variable parameters, just like functions, such:
Here arg indicates the remaining parameters, which can be zero or multiple.
To replace arg in macro extension. For example:
Pr_debug ("% s: % d", filename, line)
Extended
Printk ("<7>" "% s: % d", filename, line)
The reason for using ## is that when processing arg does not match any parameter, the arg value is blank
C Preprocessor in this special case, discard # the previous comma, so that
Pr_debug ("success! \ N ")
Extended
Printk ("<7>" success! \ N ")
Note that there is no comma at the end.
Label element ==========
Standard C requires that the initial values of arrays or structure variables must appear in a fixed order. In gnu c
The specified index or structure domain name allows the initialization value to appear in any order. The method for specifying an array index is
Before the initialization value, write '[INDEX] ='. to specify a range, use the format of '[FIRST... LAST] =,
For example:
++ Arch/i386/kernel/irq. c
1079: static unsigned long irq_affinity [NR_IRQS] = {[0... NR_IRQS-1] = ~ 0UL };
Convert all elements of the array ~ 0UL, which can be seen as a shorthand form.
To specify a structure element, write 'fieldname: 'before the element value. For example:
Initialize the element llseek of the structure ext2_file_operations to generic_file_llseek,
The element read is initialized to genenric_file_read, and so on. I think this is the GNU C extension.
One of the best features is that when the definition of the structure changes and the offset of the element changes, this initialization method is still
Ensure the correctness of known elements. For elements that are not in initialization, the initial value is 0.
Case range ==========
Gnu c allows you to specify a continuous range value in a case label. For example:
+++ Arch/i386/kernel/irq. c
1062: case '0'... '9': c-= '0'; break;
1063: case 'A'... 'F': c-= 'a'-10; break;
1064: case 'A'... 'F': c-= 'a'-10; break;
Case '0'... '9 ':
Equivalent
Case '0': case '1': case '2': case '3': case '4 ':
Case '5': case '6': case '7': case '8': case '9 ':
Declared special attributes ====================
Gnu c allows you to declare special attributes of functions, variables, and types for manual code optimization and more careful generation.
Code check. To specify a declared attribute, write it after the declaration
_ Attribute _ (ATTRIBUTE ))
ATTRIBUTE is an ATTRIBUTE description. Multiple Attributes are separated by commas. Gnu c supports more than a dozen attributes.
To introduce the most commonly used:
* Noreturn
The noreturn attribute is used for a function, indicating that the function is never returned. This allows the compiler to generate slightly optimized
Code, the most important thing is to eliminate unnecessary warning information, such as the variable that is not initially made. For example:
* Format (ARCHETYPE, STRING-INDEX, FIRST-TO-CHECK)
The format attribute is used by the function, indicating that the function uses parameters in the printf, scanf, or strftime style.
Number. The most common error with this type of function is that the format string does not match the parameter. You can specify the format attribute
Let the compiler check the parameter type based on the format string. For example:
Indicates that the first parameter is a format string, and the parameters are checked based on the format string from the second parameter.
* Unused
The unused attribute is used for functions and variables, indicating that the function or variable may not be used. This attribute can avoid
The compiler generates warning information.
* Section ("section-name ")
Attribute section is used for functions and variables. Generally, the compiler places the function in the. text section and the variable in
In the. data or. bss section, the section attribute allows the compiler to place functions or variables in the specified
Section. For example:
The connector can arrange code or data in the same section together. This technology is very popular in Linux kernel,
For example, the system initialization code is arranged in a separate section and can be released after initialization.
Memory.
* Aligned (ALIGNMENT)
Attribute aligned is used for variables, structures, or union types, specifying variables, structure fields, structures, or union
Qi, in bytes, for example:
++ Include/asm-i386/processor. h
294: struct i387_fxsave_struct {
295: unsigned short cwd;
296: unsigned short swd;
297: unsigned short twd;
298: unsigned short fop;
299: long fip;
300: long fcs;
301: long foo;
......
308: }__ attribute _ (aligned (16 )));
It indicates that the variable of this structure type is 16 bytes aligned. Generally, the compiler selects an appropriate alignment, indicating
Fixed alignment is usually caused by System Restrictions, optimization, and other reasons.
* Packed
Attribute packed is used for variables and types. When used for variables or structure fields, it indicates the minimum possible alignment.
During enumeration, structure, or union type, it indicates that this type uses the smallest memory. For example:
++ Include/asm-i386/desc. h
51: struct Xgt_desc_struct {
52: unsigned short size;
53: unsigned long address _ attribute _ (packed ));
54 :};
The domain address will be allocated immediately after the size. The purpose of attribute packed is mostly to define hardware-related knots.
Structure, so that there is no holes between elements due to alignment.
Current function name ============
Gnu cc predefines two specifiers to save the name of the current FUNCTION, __function _ Save the FUNCTION in the source code
In, __pretty_function _ saves names with language characteristics. In the C function, the two
The name is the same. In the C ++ function, __pretty_function _ includes additional functions such as function return types.
Information, Linux kernel only uses _ FUNCTION __.
+++ Fs/ext2/super. c
98: void ext2_update_dynamic_rev (struct super_block * sb)
99 :{
100: struct ext2_super_block * es = EXT2_SB (sb)-> s_es;
101:
102: if (le32_to_cpu (es-> s_rev_level)> EXT2_GOOD_OLD_REV)
103: return;
104:
105: ext2_warning (sb, _ FUNCTION __,
106: "updating to rev % d because of new feature flag ,"
107: "running e2fsck is recommended ",
108: EXT2_DYNAMIC_REV );
Here _ FUNCTION _ will be replaced with the string "ext2_update_dynamic_rev ". Although
_ FUNCTION _ looks similar to _ FILE __in Standard C, but actually _ FUNCTION __
Is replaced by the compiler, unlike _ FILE.
Built-in functions ==========
Gnu c provides a large number of built-in functions, many of which are the built-in versions of Standard C library functions, such
Memcpy, which has the same functions as the corresponding C-database functions. This article does not discuss such functions. Other built-in functions
The name usually starts with _ builtin.
* _ Builtin_return_address (LEVEL)
The built-in function _ builtin_return_address returns the return address of the current function or its caller, parameter
LEVEL indicates the number of frames searched on the stack. 0 indicates the return address of the current function, and 1 indicates the current function.
The return address of the caller, and so on. For example:
++ Kernel/sched. c
437: printk (KERN_ERR "schedule_timeout: wrong timeout"
438: "value % lx from % p \ n", timeout,
439: _ builtin_return_address (0 ));
* _ Builtin_constant_p (EXP)
The built-in function _ builtin_constant_p is used to determine whether a value is a compilation constant. If the Parameter
The EXP value is a constant, and the function returns 1; otherwise, 0 is returned. For example:
Many computation or operations are more optimized when the parameter is constant. The above method can be used in gnu c.
Compile only the constant version or a very number version based on whether the parameter is a constant.
Compile the optimal code when the parameter is a constant.
* _ Builtin_ct (EXP, C)
Built-in function _ builtin_expect is used to provide branch prediction information for the compiler. Its return value is an integer table.
The value of maxcompute EXP. The value of C must be the compile time. For example:
+++ Include/linux/compiler. h
13: # define likely (x) _ builtin_exact CT (x), 1)
14: # define unlikely (x) _ builtin_exact CT (x), 0)
++ Kernel/sched. c
564: if (unlikely (in_interrupt ())){
565: printk ("Scheduling in interrupt \ n ");
566: BUG ();
567 :}
The semantics of this built-in function is that the expected value of EXP is C, and the compiler can sort it according to this information.
The sequence of statement blocks enables programs to run more efficiently as expected. The preceding example indicates that the instance is in
The disconnection context rarely occurs. The target code of lines 565th-566 may be placed in a distant location to ensure
The target code that is frequently executed is more compact.
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.