从kernel和其他opensource 扒出来的常见的宏

本文介绍了一种避免宏参数多次计算的方法,并给出了GCC扩展中确保参数仅计算一次的宏定义实例。此外,还列举了Linux内核中常用的高效宏定义,如MAX_CHECK、offsetof和container_of等。
http://www.xxlinux.com/linux/article/development/soft/20070816/9335.html

比如比较两个数的大小 : 一般我们都习惯了这样写:
#define MAX(a,b) (((a)>(b))?(a)b)) //实际上宏参数计算两次tyLLinux联盟
可是宏是有副作用的, 比如下面这个例子: MAX(a++,b) 又如何呢?
所以kernel里面的gcc扩展提供了很好的办法:tyLLinux联盟
 

#define MAX_CHECK(x,y)     / //宏参数仅仅计算一次tyLLinux联盟
tyLLinux联盟
({                /tyLLinux联盟
    typeof(x) __x = (x);    /tyLLinux联盟
    typeof(y) __y = (y);    /tyLLinux联盟
    (void) (&__x == &__y);    /tyLLinux联盟
    (__x>__y) ?__x:__y    ;/tyLLinux联盟
})注意上面的是语句表达式(就是 用() 括起来) , 当if(MAX_CHECK(10,20) ) 是没有副作用的。 typeof 是个宏, 取出变量的类型tyLLinux联盟

当然上面的宏 按照kernel里面的写法也可以写成这样:

#define MAX_CHECK2(x,y)     /tyLLinux联盟
do{                /tyLLinux联盟
    typeof(x) __x = (x);    /tyLLinux联盟
    typeof(y) __y = (y);    /tyLLinux联盟
    (void) (&__x == &__y);    /tyLLinux联盟
    (__x>__y) ?__x:__y    ;/tyLLinux联盟
}while(0)
//注意可没有;但是用法就不一样了,语句表达返回的是个值 , 但是 这点用 do {} while(0) 格式就不幸了。 tyLLinux联盟
tyLLinux联盟

比如 你可以这样 max = MAX_CHECK(a,b);tyLLinux联盟
但是这样就错了: max = MAX_CHECK2(a,b) ; 编译就出错了。 tyLLinux联盟
但是如果 一行里面直接写 MAX_CHECK2(a,b) 肯定就没有问题了。

---

下面是就是聪kernel里面扒出来的:

 

tyLLinux联盟
#define ARRAY_SIZE(X) ((sizeof((X)))/(sizeof((X)[0])))tyLLinux联盟
#define ALIGN(x,a)         (((x)+(a)-1) & (~((a)-1)))tyLLinux联盟
#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))tyLLinux联盟
tyLLinux联盟
#define abs(x) ({                    /tyLLinux联盟
        int __x = (x);                /tyLLinux联盟
        (__x < 0) ? -__x : __x;        /tyLLinux联盟
    })tyLLinux联盟
    tyLLinux联盟
tyLLinux联盟
//太有用的宏了, 一定要掌握tyLLinux联盟
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)tyLLinux联盟

//也非常有用

/**tyLLinux联盟
 * container_of - cast a member of a structure out to the containing structuretyLLinux联盟
 * @ptr:    the pointer to the member.tyLLinux联盟
 * @type:    the type of the container struct this is embedded in.tyLLinux联盟
 * @member:    the name of the member within the struct.tyLLinux联盟
 *tyLLinux联盟
 */
tyLLinux联盟
#define container_of(ptr, type, member) ({            /tyLLinux联盟
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    /tyLLinux联盟
        (type *)( (char *)__mptr - offsetof(type,member) );})tyLLinux联盟
        tyLLinux联盟
        tyLLinux联盟
//#define container_of(ptr,type,member)tyLLinux联盟
tyLLinux联盟
//({                                                            /tyLLinux联盟
tyLLinux联盟
//    const typeof(((type *)0)->member) *__mptr = (ptr);    / //仅仅计算一次tyLLinux联盟
tyLLinux联盟
//    (type *)((char *)__mptr - offsetof(type,member)) ;        /tyLLinux联盟
tyLLinux联盟
//})tyLLinux联盟
tyLLinux联盟
    tyLLinux联盟
tyLLinux联盟
tyLLinux联盟
/**tyLLinux联盟
 * list_entry - get the struct for this entrytyLLinux联盟
 * @ptr:    the &struct list_head pointer.tyLLinux联盟
 * @type:    the type of the struct this is embedded in.tyLLinux联盟
 * @member:    the name of the list_struct within the struct.tyLLinux联盟
 */
tyLLinux联盟
#define list_entry(ptr, type, member) /tyLLinux联盟
    container_of(ptr, type, member)tyLLinux联盟
tyLLinux联盟
//#define abs(x)    ((x) > 0)?(x):(-(x))tyLLinux联盟
tyLLinux联盟
tyLLinux联盟
/*tyLLinux联盟
 * ..and if you can't take the stricttyLLinux联盟
 * types, you can specify one yourself.tyLLinux联盟
 *tyLLinux联盟
 * Or not use min/max at all, of course.tyLLinux联盟
 */
tyLLinux联盟
#define MIN_T(type,x,y) /tyLLinux联盟
    ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })tyLLinux联盟
#define MAX_T(type,x,y) /tyLLinux联盟
    ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })tyLLinux联盟
tyLLinux联盟
#define MAX(x,y)    ( (x)>(y)?(x):(y) )tyLLinux联盟
#define MAX_CHECK(x,y)     /tyLLinux联盟
({                /tyLLinux联盟
    typeof(x) __x = (x);    /tyLLinux联盟
    typeof(y) __y = (y);    /tyLLinux联盟
    (void) (&__x == &__y);    /tyLLinux联盟
    (__x>__y) ?__x:__y    ;/tyLLinux联盟
})






({ /tyLLinux联盟

typeof(x) __x = (x); /tyLLinux联盟

typeof(y) __y = (y); /tyLLinux联盟

(void) (&__x == &__y); /tyLLinux联盟

(__x>__y) ?__x:__y ;/tyLLinux联盟

}) tyLLinux联盟

其中(void) (&__x == &__y);的目的是什么? tyLLinux联盟

tyLLinux联盟

检查两个变量的类型是否相同,如果不相同,编译的时候会有警告。tyLLinux联盟

比如 tyLLinux联盟

int a=8;tyLLinux联盟

flat b = 1.5 ; tyLLinux联盟
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值