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
这样就进行了类型检查了。
阅读(1522) | 评论(0) | 转发(1) |