在学习linux内核代码的时候,经常会遇见以上的符号,不知道是什么意思,貌似在C语言中不曾有规则定义过,下面是它的用法。
在linux中“/include/linux/init.h”中定义以上宏。
定义如下:
#define __init __section(.init.text) __cold notrace
#define __initdata __section(.init.data)
#define __initconst __section(.init.rodata)
#define __exitdata __section(.exit.data)
#define __exit_call __used __section(.exitcall.exit)
__init 、 __initdata ......的使用方法在原文件中是这样注释的:
- /* These macros are used to mark some functions or <span style="white-space: pre;"> </span> ||这些宏用于标记一些函数和初始化的数据(不能用于非初始化的数据),作为“初始化”的功能。
- * initialized data (doesn't apply to uninitialized data)
- * as `initialization' functions. The kernel can take this||内核将会根据这些线索在初始化阶段找到它(被标记的函数和数据),然后释放被占用的内存资
- * as hint that the function is used only during the initialization||<span style="background-color: rgb(255, 255, 255);">源。</span>
- * phase and free up used memory resources after
- *
- * <strong><span style="color: rgb(255, 0, 0); font-size: 18px;">Usage</span></strong>:||用法
- * <span style="font-size: 18px;"><strong>For functions:</strong></span>||用于函数
- *
- * You should add __init immediately before the function name, like:||你应该直接添加__init在函数名字的前面
- *
- <span style="color: rgb(0, 102, 0);"> * static void __init initme(int x, int y)
- * {
- * extern int z; z = x * y;
- * }</span>
- *
- * If the function has a prototype somewhere, you can also add//如果函数已有一个原型,你也可以添加__init在靠近函数原型的括号和分号之间
- * __init between closing brace of the prototype and semicolon:
- *
- <span style="color: rgb(0, 102, 0);"> * extern int initialize_foobar_device(int, int, int) __init;</span>
- *
- * <span style="font-size: 18px;">For initialized data:</span><span style="font-size: 14px;">//用于初始化数据</span>
- * You should insert __initdata between the variable name and equal||你应该把__initdata放在变量名和等号中间,后面跟的是具体的值
- * sign followed by value, e.g.:
- *
- <span style="color: rgb(0, 102, 0);"> * static int init_variable __initdata = 0;
- * static const char linux_logo[] __initconst = { 0x32, 0x36, ... };</span>
- *
- * Don't forget to initialize data not at file scope, i.e. within a function,
- * as gcc otherwise puts the data into the bss section and not into the init
- * section.
- * <span style="color: rgb(68, 68, 68); line-height: 13px; font-family: Verdana, sans-serif; font-size: 9pt;">不要忘了初始化数据在文件范围内,即在一个函数中,如gcc,否则将数据到的bss段,而不是到init部分。</span><span style="color: rgb(68, 68, 68); line-height: 13px; font-family: Verdana, sans-serif; font-size: 9pt; background-color: rgb(230, 236, 249);">另外请注意,这个数据不能是“const”。</span>
- * Also note, that this data cannot be "const".
- */
/* These macros are used to mark some functions or ||这些宏用于标记一些函数和初始化的数据(不能用于非初始化的数据),作为“初始化”的功能。
* initialized data (doesn't apply to uninitialized data)
* as `initialization' functions. The kernel can take this||内核将会根据这些线索在初始化阶段找到它(被标记的函数和数据),然后释放被占用的内存资
* as hint that the function is used only during the initialization||源。
* phase and free up used memory resources after
*
* Usage:||用法
* For functions:||用于函数
*
* You should add __init immediately before the function name, like:||你应该直接添加__init在函数名字的前面
*
* static void __init initme(int x, int y)
* {
* extern int z; z = x * y;
* }
*
* If the function has a prototype somewhere, you can also add//如果函数已有一个原型,你也可以添加__init在靠近函数原型的括号和分号之间
* __init between closing brace of the prototype and semicolon:
*
* extern int initialize_foobar_device(int, int, int) __init;
*
* For initialized data://用于初始化数据
* You should insert __initdata between the variable name and equal||你应该把__initdata放在变量名和等号中间,后面跟的是具体的值
* sign followed by value, e.g.:
*
* static int init_variable __initdata = 0;
* static const char linux_logo[] __initconst = { 0x32, 0x36, ... };
*
* Don't forget to initialize data not at file scope, i.e. within a function,
* as gcc otherwise puts the data into the bss section and not into the init
* section.
* 不要忘了初始化数据在文件范围内,即在一个函数中,如gcc,否则将数据到的bss段,而不是到init部分。另外请注意,这个数据不能是“const”。
* Also note, that this data cannot be "const".
*/
后面中文是我给出的简单翻译。
内核中采用这种方式去标记一些初始化的函数和数据,其中一个原因是标记为初始化的函数和数据,表明该函数和数据仅在初始化期间使用。在模块装载之后,模块装载就会将初始化函数扔掉。这样可以将该函数占用的内存释放出来。
在学习这些宏的作用时可参考博文:http://blog.chinaunix.net/uid-26694208-id-3078013.html