GNUC Extension: Linux operating system kernel usage-general Linux technology-Linux programming and kernel information. For details, refer to the following section. 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 ;})
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:
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:
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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service