linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > article > Linux开发区 > 软件开发 >
栏目导栏
资料搜索
热门文章
·Linux 下 C 语言编程
·Linux下的通用线程池创建
·C++字符串转换篇
·linux C 进程操作篇
·linux上的C/C++编译器gcc/egcs
·linux C 文件权限控制篇
·GCC使用手册
·linux C 接口处理篇
·在Redhat Linux上安装 GCC 编译
·GCC使用指南
·C语言运算符
·Linux下C开发环境的构成和安装
·fopen()函数的参数说明
·GCC使用手册与常用命令
·Scheme 语言介绍
最新文章
·在Ubuntu Linux 8.04上构建GCC
·Linux操作系统下Socket编程地址
·将VC程序移植到Linux系统的几点
·Linux下malloc/free与new/dele
·Linux下用GTK和socket实现简单
·Linux操作系统下让Tomcat启动在
·Linux操作系统中如何编译C程序
·几种常被人们忽略的Linux系统下
·Eclipse编程工具 在Ubuntu下的
·Linux操作系统下的网络地址转换
·老手经验谈:Linux驱动程序开发
·Linux操作系统多线程同步Mutex
·Linux操作系统下C程序语言简易
·Linux系统平台下关于GCC编译及
·解决Linux系统下管道被接受方关
Google
 
从kernel和其他opensource 扒出来的常见的宏
[ 作者:bob_zhang2004  加入时间:2007-08-16 14:13:32  来自:CUBLOG ]
比如比较两个数的大小 : 一般我们都习惯了这样写:
#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联盟
})

发表于: 2007-05-10 ,修改于: 2007-05-10 19:12,已浏览658次,有评论2条 推荐 投诉

tyLLinux联盟
网友评论
内容:
有朋友问: 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联盟
tyLLinux联盟
编译的时候,就会有warning ,弥补了编译器不检查宏参数的问题。
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息