注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

民主与科学

独立之人格,自由之思想

 
 
 

日志

 
 

linux内存中的__init和__exit宏  

2011-10-08 16:42:19|  分类: Linux内核 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
本文翻译整理自:http://www.faqs.org/docs/kernel/x277.html
 This demonstrates a feature of kernel 2.2 and later. Notice the change in the definitions of the init and cleanup functions. 
__init__exit这两种修饰是在android的内核2.2中才引入的。它们主要用于释放函数的代码所占的内存。
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 perfect sense.
在一个函数前添加__init修饰的话,那么在这个函数首次执行完成后,该函数的代码所占的内存,将被释放掉__init主要用于驱动的初始话。另外,在loadable的模块中函数,使用__init,将无效。
There is also an __initdata which works similarly to __init but for init variables rather than functions.
__initdata__init相似,只是前者用于变量,后者用于函数。
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.
在一个函数前添加__exit修饰的话,那么在the module is built into the kernel后,该函数所占的内存,将被释放掉。另外它和__init一样,在loadable的模块中函数将无效。
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.
示例1
Example 2-5. hello-3.c

/*  hello-3.c - Illustrating the __init, __initdata and __exit macros.
 */
#include <linux/module.h>      /* Needed by all modules */
#include <linux/kernel.h>      /* Needed for KERN_ALERT */
#include <linux/init.h>        /* Needed for the macros */
static int hello3_data __initdata = 3;
static int __init hello_3_init(void)
{
   printk(KERN_ALERT "Hello, world %d\n", hello3_data);
   return 0;
}
static void __exit hello_3_exit(void)
{
   printk(KERN_ALERT "Goodbye, world 3\n");
}
module_init(hello_3_init);
module_exit(hello_3_exit);

By the way, you may see the directive "__initfunction()" in drivers written for Linux 2.2 kernels:
 __initfunction(int init_module(void))
{
   printk(KERN_ALERT "Hi there.\n");
   return 0;
}
This macro served the same purpose as __init, but is now very deprecated in favor of __init. 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.
  评论这张
 
阅读(824)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017