Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2229076
  • 博文数量: 668
  • 博客积分: 10016
  • 博客等级: 上将
  • 技术积分: 8588
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-29 19:22
文章分类

全部博文(668)

文章存档

2011年(1)

2010年(2)

2009年(273)

2008年(392)

分类:

2008-10-24 21:53:53

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

#define MAX_CHECK(x,y)     \ //宏参数仅仅计算一次

({                \
    typeof(x) __x = (x);    \
    typeof(y) __y = (y);    \
    (void) (&__x == &__y);    \
    (__x>__y) ?__x:__y    ;\
})注意上面的是语句表达式(就是 用() 括起来) , 当if(MAX_CHECK(10,20) ) 是没有副作用的。 typeof 是个宏, 取出变量的类型

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

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

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

---

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

 


#define ARRAY_SIZE(X) ((sizeof((X)))/(sizeof((X)[0])))
#define ALIGN(x,a)         (((x)+(a)-1) & (~((a)-1)))
#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))

#define abs(x) ({                    \
        int __x = (x);                \
        (__x < 0) ? -__x : __x;        \
    })
    

//太有用的宏了, 一定要掌握
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

//也非常有用

/**
 * 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 struct this is embedded in.
 * @member:    the name of the member within the struct.
 *
 */

#define container_of(ptr, type, member) ({            \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})
        
        
//#define container_of(ptr,type,member)

//({                                                            \

//    const typeof(((type *)0)->member) *__mptr = (ptr);    \ //仅仅计算一次

//    (type *)((char *)__mptr - offsetof(type,member)) ;        \

//})

    


/**
 * list_entry - get the struct for this entry
 * @ptr:    the &struct list_head pointer.
 * @type:    the type of the struct this is embedded in.
 * @member:    the name of the list_struct within the struct.
 */

#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)

//#define abs(x)    ((x) > 0)?(x):(-(x))


/*
 * ..and if you can't take the strict
 * types, you can specify one yourself.
 *
 * Or not use min/max at all, of course.
 */

#define MIN_T(type,x,y) \
    ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
#define MAX_T(type,x,y) \
    ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })

#define MAX(x,y)    ( (x)>(y)?(x):(y) )
#define MAX_CHECK(x,y)     \
({                \
    typeof(x) __x = (x);    \
    typeof(y) __y = (y);    \
    (void) (&__x == &__y);    \
    (__x>__y) ?__x:__y    ;\
})

阅读(713) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~