__init and __exit
The __init and __exit declarations are special kernel macros designed to tell the kernel to flag these
functions for special handling in cases where they are compiled in statically rather than included as
parts of modules.
 The __init declaration allows the kernel to reclaim the space used by initialization
functions, while the __exit declaration tells the kernel to just plain ignore the function altogether. If
you’re only going to write your device driver as a module, with no possibility that it will be included
statically, it is perfectly safe to leave these special declarations out.

1 __init:

/* 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 char linux_logo[] __initdata = { 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".


 * module_init() - driver initialization entry point
 * @x: function to be run at kernel boot time or module insertion
 * module_init() will add the driver initialization routine in
 * the "__initcall.int" code segment if the driver is checked as
 * "y" or static, or else it will wrap the driver initialization
 * routine with init_module() which is used by insmod and
 * modprobe when the driver is used as a module.
 * module_exit() - driver exit entry point
 * @x: function to be run when driver is removed
 * module_exit() will wrap the driver clean-up code
 * with cleanup_module() when used with rmmod when
 * the driver is a module.  If the driver is statically
 * compiled into the kernel, module_exit() has no effect.




这些宏都定义在linux/init.h中,它们会释放内核的内存。你启动内核的时候会看到一些诸如Freeing unused kernel memory:236k freed,之类的信息,这多半就是它们干的。





个人分类: linux
想对作者说点什么? 我来说一句


2012年03月26日 271KB 下载



__init and __exit