设为首页 收藏本站
查看: 583|回复: 0

[经验分享] linux内存中的__init和__exit宏

[复制链接]

尚未签到

发表于 2016-2-22 08:57:44 | 显示全部楼层 |阅读模式
本文翻译整理自:<wbr style="line-height:25px"><a rel="nofollow" href="http://www.faqs.org/docs/kernel/x277.html" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">http://www.faqs.org/docs/kernel/x277.html</a><div style="line-height:25px"><div style="line-height:25px">This demonstrates a feature of kernel 2.2 and later. Notice the change in the definitions of the init and cleanup functions.</div><div style="line-height:25px"><span style="color:#993300; line-height:25px">__init</span><span style="color:#003366; line-height:25px">和</span><span style="color:#993300; line-height:25px">__exit</span><span style="color:#003366; line-height:25px">这两种修饰是在</span><span style="color:#0000ff; line-height:25px">android</span><span style="color:#003366; line-height:25px">的内核2.2中才引入的。它们主要用于释放函数的代码所占的内存。</span></div><div style="line-height:25px">The __init macro causes the init function to be discarded and its memory freed once the init function finishes for built-in drivers, but not loadable modules. If you think about when the init function is invoked, this makes perfectsense.</div><div style="line-height:25px"><span style="color:#003366; line-height:25px">在一个函数前添加</span><span style="line-height:25px; color:rgb(153,51,0)">__init</span><span style="color:#003366; line-height:25px">修饰的话,那么在这个函数首次执行完成后,该函数的代码所占的内存,将被释放掉</span>。<span style="line-height:25px; color:rgb(153,51,0)">__init</span><span style="color:#003366; line-height:25px">主要用于驱动的初始话。另外,在</span><span style="color:#0000ff; line-height:25px">loadable</span><span style="color:#003366; line-height:25px">的模块中函数,使用</span><span style="line-height:25px; color:rgb(153,51,0)">__init</span><span style="color:#003366; line-height:25px">,将无效。</span></div><div style="line-height:25px">There is also an __initdata which works similarly to __init but for init variables rather than functions.</div><div style="line-height:25px"><span style="color:#993300; line-height:25px">__initdata</span><span style="color:#000080; line-height:25px">和</span><span style="line-height:25px; color:rgb(153,51,0)">__init</span><span style="color:#000080; line-height:25px">相似,只是前者用于变量,后者用于函数。</span></div><div style="line-height:25px">The __exit macro causes the omission of the function when the module is built into the kernel, and like __init, has no effect for loadable modules. Again, if you consider when the cleanup function runs, this makes complete sense;built-in drivers don't need a cleanup function, while loadable modules do.</div><div style="line-height:25px"><span style="color:#000080; line-height:25px">在一个函数前添加__</span><span style="color:#800000; line-height:25px">exit</span><span style="color:#000080; line-height:25px">修饰的话,那么在the module is built into the kernel后,该函数所占的内存,将被释放掉。另外它和__init一样,在loadable的模块中函数将无效。</span></div><div style="line-height:25px">These macros are defined in linux/init.h and serve to free up kernel memory. When you boot your kernel and see something like Freeing unused kernel memory: 236k freed, this is precisely what the kernel is freeing.</div></div><div style="line-height:25px"><span style="line-height:25px">示例1</span>:</div><div style="line-height:25px"><div style="line-height:25px">Example 2-5. hello-3.c</div><div style="line-height:25px"><br style="line-height:25px"></div><div style="line-height:25px"><span style="color:#808080; line-height:25px">/* hello-3.c - Illustrating the __init, __initdata and __exit macros.</span></div><div style="line-height:25px"><span style="color:#808080; line-height:25px">*/</span></div><div style="line-height:25px">#include &lt;linux/module.h&gt;   /* Needed by all modules */</div><div style="line-height:25px">#include &lt;linux/kernel.h&gt;   /* Needed for KERN_ALERT */</div><div style="line-height:25px">#include &lt;linux/init.h&gt;    /* Needed for the macros */</div><div style="line-height:25px"><span style="color:#993300; line-height:25px">static int</span><span style="color:#0000ff; line-height:25px">hello3_data __initdata = 3;</span></div><div style="line-height:25px"><span style="color:#993300; line-height:25px">static int</span><span style="color:#0000ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">__init</span><span style="color:#0000ff; line-height:25px">hello_3_init(void)</span></div><div style="line-height:25px"><span style="color:#0000ff; line-height:25px">{</span></div><div style="line-height:25px"><span style="color:#0000ff; line-height:25px"> printk(KERN_ALERT "Hello, world %d\n", hello3_data);</span></div><div style="line-height:25px"><span style="color:#0000ff; line-height:25px"> return 0;</span></div><div style="line-height:25px"><span style="color:#0000ff; line-height:25px">}</span></div><div style="line-height:25px"><span style="color:#993300; line-height:25px">static void</span><span style="color:#0000ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">__exit</span><span style="color:#0000ff; line-height:25px">hello_3_exit(void)</span></div><div style="line-height:25px"><span style="color:#0000ff; line-height:25px">{</span></div><div style="line-height:25px"><span style="color:#0000ff; line-height:25px"> printk(KERN_ALERT "Goodbye, world 3\n");</span></div><div style="line-height:25px"><span style="color:#0000ff; line-height:25px">}</span></div><div style="line-height:25px"><span style="color:#0000ff; line-height:25px">module_init(</span><span style="color:#ff6600; line-height:25px">hello_3_init)</span><span style="color:#0000ff; line-height:25px">;</span></div><div style="line-height:25px"><span style="color:#0000ff; line-height:25px">module_exit(</span><span style="color:#ff6600; line-height:25px">hello_3_exit)</span><span style="color:#0000ff; line-height:25px">;</span></div><div style="line-height:25px"><span style="color:#0000ff; line-height:25px"><br style="line-height:25px"></span></div><div style="line-height:25px">By the way, you may see the directive "__initfunction()" in drivers written for Linux 2.2 kernels:</div><div style="line-height:25px"><span style="color:#3366ff; line-height:25px">__initfunction(int init_module(void))</span></div><div style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span></div><div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> printk(KERN_ALERT "Hi there.\n");</span></div><div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> return 0;</span></div><div style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span></div><div style="line-height:25px"><span style="color:#000080; line-height:25px">This macro served the same purpose as __init, but is now very</span><span style="color:#ff6600; line-height:25px">deprecated</span><span style="color:#000080; line-height:25px">infavor of</span><span style="color:#ff9900; line-height:25px">__init</span><span style="color:#000080; line-height:25px">. I only mention it because you might see it modern kernels. As of 2.4.18, there are 38 references to __initfunction(), and of 2.4.20,there are 37 references. However, don't use it in your own code.</span></div></div></wbr>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-181114-1-1.html 上篇帖子: linux 进程通信――C语言实现 下篇帖子: 在Linux安装Oracle 11g r2
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表