Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1617162
  • 博文数量: 197
  • 博客积分: 10046
  • 博客等级: 上将
  • 技术积分: 1983
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-07 12:36
个人简介

在外企做服务器开发, 目前是项目经理, 管理两个server开发的项目。不做嵌入式好久了。

文章分类
文章存档

2011年(2)

2010年(6)

2009年(18)

2008年(30)

2007年(100)

2006年(41)

分类: LINUX

2007-08-31 17:29:39

几个有用的宏



QUOTE:
#define time_after(a,b)                \
        (typecheck(unsigned long, a) && \
         typecheck(unsigned long, b) && \
         ((long)(b) - (long)(a) < 0))
#define time_before(a,b)        time_after(b,a)

#define time_after_eq(a,b)        \
        (typecheck(unsigned long, a) && \
         typecheck(unsigned long, b) && \
         ((long)(a) - (long)(b) >= 0))
#define time_before_eq(a,b)        time_after_eq(b,a)

/*
* Have the 32 bit jiffies value wrap 5 minutes after boot
* so jiffies wrap bugs show up earlier.
*/

-----------

我们可以看  typecheck这个宏 的定义, 对于我们自己写程序有好处的:

QUOTE:
/*
* Check at compile time that something is of a particular type.
* Always evaluates to 1 so you may use it easily in comparisons.
*/
#define typecheck(type,x) \
({        type __dummy; \
        typeof(x) __dummy2; \
        (void)(&__dummy == &__dummy2); \
        1; \
})

/*
* Check at compile time that 'function' is a certain type, or is a pointer
* to that type (needs to use typedef for the function type.)
*/
#define typecheck_fn(type,function) \
({        typeof(type) __tmp = function; \
        (void)__tmp; \
})
 
=======================================
 
 
弄个简单的应用程序测试一下:


检查变量类型的小例子:

QUOTE:
/*
* Check at compile time that something is of a particular type.
* Always evaluates to 1 so you may use it easily in comparisons.
*/
#define typecheck(type,x) \
({      type __dummy; \
        typeof(x) __dummy2; \
        (void)(&__dummy == &__dummy2); \
        1; \
})
/*
* Check at compile time that 'function' is a certain type, or is a pointer
* to that type (needs to use typedef for the function type.)
*/
#define typecheck_fn(type,function) \
({        typeof(type) __tmp = function; \
        (void)__tmp; \
})


typedef int (*fun)(int *,unsigned long *);
int main(void)
{
       
        int i=10;
        int val =0;
       
        val = typecheck(unsigned long,i);       
                printf("val=%d\n",val);
       
        return 0;
}

编译:

QUOTE:
gcc -o check check.c -Wall

check.c: In function `main':
check.c:50: warning: comparison of distinct pointer types lacks a cast[root@bobzhanglinux debug]#

执行:

QUOTE:
val =1
阅读(2132) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~