Chinaunix首页 | 论坛 | 博客
  • 博客访问: 64022
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 107
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-18 21:26
文章分类

全部博文(15)

文章存档

2013年(15)

我的朋友

分类: 嵌入式

2013-05-30 16:28:46

time_after(unsigned long a, unsigned long b)  //linux/jifiles.h

/*
*        These inlines deal with timer wrapping correctly. You are
*        strongly encouraged to use them
*        1. Because people otherwise forget
*        2. Because if the timer wrap changes in future you won't have to
*           alter your driver code.
*
* time_after(a,b) returns true if the time a is after time b.
*
* Do this with "<0" and ">=0" to only test the sign of the result. A
* good compiler would generate better code (and a really good compiler
* wouldn't care). Gcc is currently neither.
*/
#define time_after(a,b)                \
        (typecheck(unsigned long, a) && \
         typecheck(unsigned long, b) && \
         ((long)(b) - (long)(a) < 0))
里面用的typecheck宏,而这个宏的定义如下:
/*
* 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; \
})


typecheck宏中(void)(&__dummy == &__dummy2)检查两个指针变量类型是否一致,若不一致,gcc会报出警告,g++报出一个错误:
comparison of distinct pointer types lacks a cast

这样就进行了类型检查了。
阅读(1018) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~