what is __init in linux kernel code.

Dec 15, 2005 4 Replies

Hi, I was looking in linux kernel file init/main.c what is the __init in this declaration "static void __init profile_setup(char *str, int *ints);"



also is there any special purpose to put underscore (_) or double underscore (__) before varible or function name?



Thanks in Advance . Anil Gupta



I have no idea, but in python the __ (double underscore) preceding a function name says.. "special function" .. you would do that for main function (even though it would be "__main__").

Now __init__ in python is just the constructor of a class.

Now taking a better look at your question makes me think that what I just said does not apply and that __init is just a gcc keyword which does something... mysterious.

__init is a makro defined in (/usr/src/linux/)include/linux/init.h Read the comments there about the usage of __init.

These are only macros to locate some parts of the linux code into special areas in the final executing binary. __init for example (or better the __attribute__ ((__section__ (".init.text"))) this macro expands to) instructs the compiler to mark this function in a special way. At the end the linker collects all functions with this mark at the end (or begin) of the binary file. When the kernel starts, this code runs only once (initialization). After it runs, the kernel can free this memory to reuse it and you will see the kernel message: Freeing unused kernel memory: 108k freed

To use this feature, you need a special linker script file, that tells the linker where to locate all the marked functions. (see arch/arm/kernel/vmlinux.lds.S)

Only to avoid name conflicts.

JB

The __init macro tells the compiler that the associate function or variable is used only upon initialization. The compiler places all code marked with __init into a special memory section that is freed after the initialization phase ends.

HTH

Zhen Wang

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required