Saturday 13 July 2013

[Kernel Programming #3] __init* macros usage

If you look at the previous kernel programming concepts #1 and #2, you will find a macro __init at the initialization function.
This post will tell us the importance of this macro in the Kernel.
Please go through the previous Kernel Programming post #1 and Post #2.

__init* macro role in Kernel:

If you declare initialization function with __init macro then the compiler puts this function in ".init.text" section.
Similarly, if you declare any variable with __initdata macro then the compiler puts the variable in ".init.data" section.

The macro __init* tells the compiler to put the functions and the variables if any declared using this macro, in a special section which is declared in vmlinux.ids.

The function which is used for initialization purpose and it is used only once during the kernel boot can be declare using __init macro.
Once a function is declared using __init macro then its memory is freed after this function is called and the task of this function is over. The function free_initmem() will do the task of memory cleanup.
If you look at the kernel log, we can find the below message which is an example of memory cleanup.
Freeing unused kernel memory: xxxk freed
The function free_initmem() will free the text and data sections that are associated with the function (if the function declaration uses __init macro)
This functionality is very useful in memory optimization, since it allows kernel to free the memory which is occupied by the init function and this function is not used anymore.

No comments:

Post a Comment

You might also like

Related Posts Plugin for WordPress, Blogger...