2012-01-12 14 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(または、このマクロが展開されるよりも良い)は、この を特別な方法でマークするようにコンパイラに指示します。最後に、リンカーはバイナリファイルの末尾(または先頭)にすべての機能 をこのマーク付きで収集します。

カーネルが起動すると、このコードは1回だけ実行されます(初期化)。それが実行された後、 カーネルは、それを再利用するために、このメモリを解放することができますし、カーネル メッセージが表示されます。

は、未使用のカーネルメモリの解放:108Kは、この機能を使用するには

を解放し、あなたが必要とします特別なリンカースクリプトファイルで、マークされたすべての機能をどこに配置するかは、 リンカーに伝えます。

+8

クレバー!つまり、「未使用のカーネルメモリを解放する:108k解放」は意味します。 :-)私はこれらすべての年月が不思議だと思っています。私はそれがコードではなく何らかのバッファーか何かであると仮定しました。 –

2

linux/ini.hのコメント(および同時にdocs)を読む。

また、gccにはLinuxカーネルコード用に特別に作られた拡張機能がいくつかあります。

3

__initは__attribute__ ((__section__(".init.text")))に展開./include/linux/init.hで定義されたマクロです。

この関数を特別な方法でマークするようにコンパイラに指示します。最後に、リンカーはバイナリファイルの終わり(または始まり)にこのマークを付けてすべての関数を収集します。カーネルが起動すると、このコードは1回だけ実行されます(初期化)。実行後、カーネルはこのメモリを解放して再利用し、カーネルを表示します。

3

これはカーネル2.2以降の機能を示しています。 initcleanup関数の定義の変更に注目してください。 __initマクロでは、init関数が破棄され、内蔵ドライバではinit関数が終了し、ロード可能なモジュールでは機能しなくなるとメモリは解放されます。 init関数が呼び出されたときを考えると、これは理にかなっています。

source