2012-01-12 105 views

回答

52

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) 
40

這些只是宏,用於將linux代碼的某些部分定位到最終執行二進制文件中特殊的 區域中。例如 __init(或更好的是__attribute__ ((__section__ (".init.text")))這個宏擴展爲)指示編譯器以特殊的方式標記這個 函數。最後,鏈接器在二進制文件的末尾(或開頭)收集帶有該標記的所有功能 。

內核啓動時,此代碼只運行一次(初始化)。它運行後, 內核可以釋放此內存重用它,你會看到內核 消息:

解放出來未使用的內核內存:108K釋放

要使用此功能,你需要一個特殊鏈接描述文件,它告訴鏈接器在哪裏找到所有標記的函數。

+8

聰明!所以這就是「釋放未使用的內核內存:釋放108k」的意思。 :-)這些年來我一直在想。我認爲這是某種緩衝區或某種東西,而不是代碼。 – 2012-01-12 09:09:17

2

linux/ini.h中閱讀評論(和docs在同一時間)。

你也應該知道gcc有一些特別爲linux內核代碼製作的擴展,它看起來像這個宏使用其中的一個。

3

__init是在./include/linux/init.h其擴展到__attribute__ ((__section__(".init.text")))中定義的宏。

它指示編譯器以特殊方式標記此功能。最後,鏈接器在二進制文件的結尾(或開始)收集帶有該標記的所有函數。內核啓動時,此代碼只運行一次(初始化)。運行後,內核可以釋放此內存以重用它,並且您將看到內核

3

這演示了內核2.2及更高版本的功能。注意initcleanup函數定義的變化。 __init宏導致init函數被丟棄,並且一旦init函數完成內置驅動程序而不是可加載模塊,其功能將被釋放。如果您考慮何時調用init函數,這是非常有意義的。

source

相關問題