Macros in the Linux kernel: __init and __exit

Source: Internet
Author: User

ZZ from:

http://blog.csdn.net/musein/article/details/742609

==================================================


The __init and __exit declarations is special kernel macros designed to tell the kernel to flag these

functions for special handling in cases where they is 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 'll be included
statically, it is perfectly safe to leave these special declarations out.

1 __init:

/* These macros is 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 function was 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 is at file scope, i.e. within a function,
* As GCC otherwise puts the data into the BSS sections and not into the Init
* section.
*
* Also Note, that this data cannot is "const".

its primary role is initialization.

2) Module_init:
/**
* Module_init ()-Driver initialization entry point
* @x:function to being run at kernel boot time or module insertion
*
* Module_init () would add the driver initialization routine in
* the "__initcall.int" code segment if the driver is checked as
* "Y" or static, or else it'll wrap the driver initialization
* Routine with Init_module () which are used by Insmod and
* Modprobe When the driver is used as a module.
*/
/**
* Module_exit ()-Driver exit entry point
* @x:function to being run when driver is removed
*
* Module_exit () would 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 () have no effect.
*/

Notice The changes in the definitions of the two functions, init and cleanup. The __INIT macro frees the Init function in the built-in module after it has finished running, but the Loadable module is not affected. It is very practical to assume that you care about when the init function is called.

there is a __initdata, and the role of __init is basically the same, except that it is for variables rather than functions.

__exit macros will make those modules built into the kernel omit the cleanup function, just like __init, no effect on the Loadable module. Again, it is important to assume that you care about the timing of cleanup execution. Built-in drivers do not need to cleanup, anyway they can not quit, but loadable-type module is clearly required one.

These macros are defined in Linux/init.h , which release the kernel's memory. When you start the kernel you will see some information such as freeing unused kernel memory:236k freed, which is mostly what they do.

_init, as well as the section beginning with. init, after the module insert has completed execution, this part
memory space is freed, so this information is not available when you dump it.


kernel modules must have at least two functions: one called init_module () " Start "initialization function for insmod call, a call rmmod . In fact, 2.3.13 after the kernel these two functions no longer have to use these two names. You can give them any name, 2.3 section I will do in detail. It is actually a good idea to give these two functions a name, but there are still a lot of people using init_module and clean_module function. L

Very classically, theinit_module function will register something with the kernel, or replace some of the kernel's functions with its own code (usually after doing something or calling the original function). The clean_module function, in turn, will end the init_module , so that it can safely unload the module.

Finally, each kernel module should include linux/module.h. It is also necessary to include linux/kernel.hin this example for kerl_alert(2.1.1 ).

Macros in the Linux kernel: __init and __exit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.