|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
| |
| 从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论坛讨论 |
|
|
|
|
|