[Reprinted] _ init and _ exit

Source: Internet
Author: User

_ Init and _ exit

Source: http://blog.csdn.net/musein/archive/2006/05/17/742609.aspx

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 encoded
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 supported DED
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 shoshould 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 shoshould 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 ".

Its main function is initialization.

2) module_init:
/**
* 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
* "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.
*/

Pay attention to the changes in the definition of init and cleanup functions. The _ init macro releases the init function in the built-in module after execution, but the modules that can be loaded are not affected. This is useful if you are concerned about when the init function is called.

There is also _ initdata, which is basically the same as _ init, but it is for variables rather than functions.

The _ exit Macro will omit the cleanup function for those built-in modules. However, like _ init, it does not affect the loadable module. Again, if you are concerned about the time when cleanup runs, this is important. The built-in drivers do not require cleanup. They cannot exit, but the loadable module obviously requires one.

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

_ Init modifier and section starting with. init. After the module is inserted and run
Memory space is released, so this information cannot be obtained during dump.

The kernel module must have at least two functions: one is the "Start" initialization function called init_module (), and the other is called clean_module () the "end" clearing function of is called when rmmod is used. In fact, the kernel functions after 2.3.13 no longer need to use these two names. You can give them all their names. What will I do in Section 2.3. It is actually a good idea to name these two functions, but many people still use the init_module and clean_module functions. L

Classic: The init_module function registers something to the kernel, or you can use your own code to replace some features of the kernel (usually they call the original function after doing something ). The clean_module function closes the tasks of init_module to safely uninstall the module.

Finally, each kernel module must contain Linux/module. h. Linux/kernel. h must be included in this example only for the purpose of kerl_alert (mentioned in section 2.1.1.

 

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.