[THIS_MODULE]
模块是一种能够在内核运转过程中动态加载、卸载的内核功用组件。2.6内核中模块在被运用时,是不允许被卸载的。编程是需求用”运用计数”来描绘模块是否在被运用。THIS_MODULE就充当了这个功用。
[likely& unlikely]
在2.6的内核中经常看到这两个符号,表面上看if(likely(value))和if(unlikely(value))其实都等同于if(value),但是在实际上履行是不同,加likely的认识着value为真的可能性要大;unlikely与之相反;加上这两个宏编译器会对其进行优化,进步程序功率。
[BUG_ON]
#define BUG_ON(condition) do { \
if(unlikely(condiTIon)) BUG(); \
} while(0)
一些内核调用能够用来便利符号bug,供给断语并输出信息。最常用的两个是BUG()和BUG_ON()。当被调用的时分,它们会引发oops,导致栈的回溯和错误信息的打印。为什么这些声明会导致 oops跟硬件的体系结构是相关的。大部分体系结构把BUG()和BUG_ON()界说成某种非法操作,这样自然会发生需求的oops。
[IS_ERR& PTR_ERR & ERR_PTR]
#define IS_ERR_VALUE(x) unlikely((x) >=(unsigned long)-MAX_ERRNO)
IS_ERR宏用来检测x地址是否有用;
staTIc inline void *ERR_PTR(long error)
{
return(void *) error;
}
staTIc inline long PTR_ERR(const void *ptr)
{
return(long) ptr;
}
[container_of]
/**
*container_of – cast a member of a structure out to the containing structure
*@ptr: the pointer to the member.
*@type: the type of the container structthis is embedded in.
*@member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member)({ \
consttypeof(((type *)0)->member)*__mptr = (ptr); \
(type *)((char *)__mptr – offsetof(type,member)); })
经过指向成员member的指针ptr,来获取包括该成员的结构体type的指针。
[__init& __initdata & __exit & __exitdata]
这些宏界说的作用是告知编译器将这些函数或许数据放入相应的secTIon中,而在模块加载的阶段,.ko文件中的代码和数据的加载区域是依据section来加载的
比方:假如函数的界说中带有__init,那么这个函数的一切代码被放入.init.text的section中;
假如函数的界说中带有__initdata,那么这个函数的一切代码被放入.init.data的section中
之所以要运用这个宏界说,其间一个原因是符号为初始化的函数和数据,标明该函数和数据仅在初始化期间运用,在模块装载之后,模块就会将初始化函数丢掉。这样能够将该函数占用的内存释放出来。