linux tricks 之 typeof用法.

typeof是gcc的扩展功能,比较简单,是用来取得参数类型,具体可参考gcc官网的解释.

 https://gcc.gnu.org/onlinedocs/gcc/Typeof.html

-------------------------------------------
本文系作者原创, 欢迎大家转载!
转载请注明出处:netwalker.blog.chinaunix.net
-------------------------------------------

由于Linux代码采用Gcc编译器编译,所以它可以采用Gcc对C语言的扩展特性,以实现高效的代码。其中运用非常广泛的扩展就是复合语句。Gcc把包含在圆括号和大括号双层括号内的复合语句看作是一个表达式,它可以出现在任何允许表达式的地方,而复合语句中可以声明局部变量,以及循环条件判断等复杂处理。而表达式的最后一条语句必须是一个表达式,它的计算结果作为返回值。

1 int a = ({typeof(a) _a = 0; ++_a;});

上例中符合表达式中声明了局部变量_a,而返回值为++_a的结果,所以a的值为1。基于这种扩展,内核可以通过在复合语句中定义局部变量而表面自加自减运算符的副作用问题。内核中的min_t和max_t宏就是这样实现的。

 
 1 include/linux/kernel.h
 2 #define min_t(type, x, y) ({            
 3     type __min1 = (x);            
 4     type __min2 = (y);            
 5     __min1 < __min2 ? __min1: __min2; })
 6 
 7 #define max_t(type, x, y) ({            
 8     type __max1 = (x);            
 9     type __max2 = (y);            
10     __max1 > __max2 ? __max1: __max2; })

尽管多数时候通过使用这类宏,可以避免参数的副作用,但是这会增加内存的开销和执行效率,所以如果能够保证参数不存在副作用,那么使用通常的如下定义即可:

 
1 #define min(a, b) ((a) > (b)? (b) : (a))
2 #define max(a, b) ((a) > (b)? (a) : (b))

以上的min_t和max_t宏适需要提供数据类型,typeof的出现使这一步也可被省略。

 
 1 include/linux/kernel.h
 2 #define min(x, y) ({                
 3     typeof(x) _min1 = (x);            
 4     typeof(y) _min2 = (y);            
 5     (void) (&_min1 == &_min2);        
 6     _min1 < _min2 ? _min1 : _min2; })
 7 
 8 #define max(x, y) ({                
 9     typeof(x) _max1 = (x);            
10     typeof(y) _max2 = (y);            
11     (void) (&_max1 == &_max2);        
12     _max1 > _max2 ? _max1 : _max2; })

观察min和max的实现,它们通过typeof获取x和y的类型,然后定义局部变量以消除参数副作用。

思考问题:注意到中间的比较运算,

(void) (&_min1 == &_min2); 
(void) (&_max1 == &_max2);  
 

这两行代码是用来做类型检查的,如果x和y的类型不同,那么编译器将提示如下警告信息,这对检查代码很有帮助。

1 xxx.c:35: warning: comparison of distinct pointer types lacks a cast

那么,类型检查是如何生效的呢?在stackoverflow上找到解释如下:

This provides for type checking, equality between pointers shall be between compatible types and gcc will provide a warning for cases where this is not so.

We can see that equality between pointers requires that the pointers be of compatible types from the draft C99 standard section 6.5.9 Equality operators which says:

One of the following shall hold:

and includes:

both operands are pointers to qualified or unqualified versions of compatible types;

and we can find what a compatible type is from section 6.2.7 Compatible type and composite type which says:

Two types have compatible type if their types are the same

This discussion on osnews also covers this and it was inspired by the the GCC hacks in the Linux kernel article which has the same code sample. The answer says:

has to do with typechecking.

Making a simple program:

int x = 10; 
long y = 20;
long r = min(x, y);

Gives the following warning: warning: comparison of distinct pointer types lacks a cast

stackoverflow对此问题的讨论已经很清楚,简单的来说,c99标准约定,对于比较运算符  == 和 != 来说,如果比较的对象是指针,那么指针所指的对象类型必须一致,否则抛出上面的warning信息.

参考资料:http://stackoverflow.com/questions/5595593/min-macro-in-kernel-h

原文地址:https://www.cnblogs.com/3me-linux/p/6203091.html