博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

ypxing

学而不思则罔,思而不学则殆

见贤思齐焉,见不贤而内自省也

人不知而不愠,不亦君子乎?

   ypxing.cublog.cn
关于作者  
姓名:星云鹏 (Yunpeng Xing)
职业:IT相关
年龄:28
位置:北京
个性介绍:
Love me, feed me, 
never leave me.
失败只有一种, 那就是半途而废

我的分类  




[转载]从kernel和其他opensource 扒出来的常见的宏
比如比较两个数的大小 : 一般我们都习惯了这样写:
#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    ;\
})

 原文地址 http://blog.chinaunix.net/u/22617/showart_297163.html
 发表于: 2007-09-15,修改于: 2007-09-15 13:52 已浏览354次,有评论0条 推荐 投诉

  网友评论

  发表评论



Copyright © 2001-2006 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们
页面生成时间:0.04293

京ICP证041476号