在Linux内核源代码,我发现这个功能:__init在Linux内核代码中的含义是什么?
static int __init clk_disable_unused(void)
{
// some code
}
在这里,我不明白什么呢__init
手段。
在Linux内核源代码,我发现这个功能:__init在Linux内核代码中的含义是什么?
static int __init clk_disable_unused(void)
{
// some code
}
在这里,我不明白什么呢__init
手段。
include/linux/init.h
/* 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:
*
* 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 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
* 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.
*
* Also note, that this data cannot be "const".
*/
/* These are for everybody (although not all archs will actually
discard it in modules) */
#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)
这些只是宏,用于将linux代码的某些部分定位到最终执行二进制文件中特殊的 区域中。例如 __init
(或更好的是__attribute__ ((__section__ (".init.text")))
这个宏扩展为)指示编译器以特殊的方式标记这个 函数。最后,链接器在二进制文件的末尾(或开头)收集带有该标记的所有功能 。
内核启动时,此代码只运行一次(初始化)。它运行后, 内核可以释放此内存重用它,你会看到内核 消息:
解放出来未使用的内核内存:108K释放
要使用此功能,你需要一个特殊链接描述文件,它告诉链接器在哪里找到所有标记的函数。
在linux/ini.h中阅读评论(和docs在同一时间)。
你也应该知道gcc有一些特别为linux内核代码制作的扩展,它看起来像这个宏使用其中的一个。
__init是在./include/linux/init.h其扩展到__attribute__ ((__section__(".init.text")))
中定义的宏。
它指示编译器以特殊方式标记此功能。最后,链接器在二进制文件的结尾(或开始)收集带有该标记的所有函数。内核启动时,此代码只运行一次(初始化)。运行后,内核可以释放此内存以重用它,并且您将看到内核
这演示了内核2.2及更高版本的功能。注意init
和cleanup
函数定义的变化。 __init
宏导致init
函数被丢弃,并且一旦init
函数完成内置驱动程序而不是可加载模块,其功能将被释放。如果您考虑何时调用init
函数,这是非常有意义的。
聪明!所以这就是“释放未使用的内核内存:释放108k”的意思。 :-)这些年来我一直在想。我认为这是某种缓冲区或某种东西,而不是代码。 – 2012-01-12 09:09:17