zoukankan      html  css  js  c++  java
  • Linux中__init、__devinit等初始化宏

    内核使用了大量不同的宏来标记具有不同作用的函数和数据结构。如宏__init 、__devinit 等。这些宏在include/linux/init.h 头文件中定义。编译器通过这些宏可以把代码优化放到合适的内存位置,以减少内存占用和提高内核效率。

    下面是一些常用的宏:

    ?  ?__init ,标记内核启动时使用的初始化代码,内核启动完成后不再需要。以此标记的代码位于.init.text 内存区域。它的宏定义是这样的:

    ? #define _ _init _ _attribute_ _ ((_ _section_ _ (".text.init")))

    ?  ?__exit ,标记退出代码,对于非模块无效。

    ?  ?__initdata ,标记内核启动时使用的初始化数据结构,内核启动完成后不再需要。以此标记的代码位于.init.data 内存区域。

    ?  ?__devinit ,标记设备初始化使用的代码。

    ?  ?__devinitdata ,标记初始化设备数据结构的函数。

    ?  ?__devexit ,标记移除设备时使用的代码。

    ?  ?xxx_initcall ,一系列的初始化代码,按降序优先级排列。

    初始化代码的内存结构 重生之大文豪

    _init_begin -------------------

    | .init.text | ---- __init

    |-------------------|

    | .init.data | ---- __initdata

    _setup_start |-------------------|

    | .init.setup | ---- __setup_param

    __initcall_start |-------------------|

    | .initcall1.init | ---- core_initcall

    |-------------------|

    | .initcall2.init | ---- postcore_initcall

    |-------------------|

    | .initcall3.init | ---- arch_initcall

    |-------------------|

    | .initcall4.init | ---- subsys_initcall

    |-------------------|

    | .initcall5.init | ---- fs_initcall

    |-------------------|

    | .initcall6.init | ---- device_initcall

    |-------------------|

    | .initcall7.init | ---- late_initcall

    __initcall_end |-------------------|

    | |饮品店加盟 www.qidouzl.com

    | ... ... ... |

    | |

    __init_end -------------------

    初始化代码的特点是:在系统启动运行,且一旦运行后马上退出内存,不再占用内存。

    对于驱动程序模块来说,这些优化标记使用的情况如下:

    ?  ?通过module_init() 和module_exit() 函数调用的函数就需要使用__init 和__exit 宏来标记。

    ?  ?pci_driver 数据结构不需标记。茶叶

    ?  ?probe() 和remove() 函数应该使用__devinit 和__devexit 标记,且只能标记probe() 和remove()

    ?  ?如果remove() 使用__devexit 标记,则在pci_driver 结构中要用__devexit_p(remove) 来引用remove() 函数。

    ?  ?如果你不确定需不需要添加优化宏则不要添加。

  • 相关阅读:
    hdu2112 HDU Today 基础最短路
    HDU 4597 Play Game(记忆化搜索,深搜)
    HDU 4496 D-City(并查集,逆思维)
    集训心情记录,,,,(2014.6.20-6.29)
    ZOJ 1115 Digital Roots(简单,字符串与数)
    ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)
    HDU 3623 Best Cow Line, Gold(模拟,注意思路,简单)
    poj 3903 Stock Exchange(最长上升子序列,模版题)
    POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)
    hdu 2571 命运(递推,请小心)
  • 原文地址:https://www.cnblogs.com/jiangye/p/3556995.html
Copyright © 2011-2022 走看看