linux内核中的typeof
今天分析内核时又看到了typeof,只知道它大概是返回变量的类型,后来上网查了下发现这个关键字在linux中用的非常多。如果你对sizeof很熟悉的话,那么大可进行类推,sizeof(exp)返回的是exp的数据类型大小,那么typeof(exp.)返回的就是exp的数据类型。下面是linux内核中typeof的一些例子。
本文引用地址:https://www.eepw.com.cn/article/201611/319991.htminclude/linux/kernel.h[plain]view plaincopyprint?
- /*
- *min()/max()macrosthatalsodo
- *stricttype-checking..Seethe
- *"unnecessary"pointercomparison.
- */
- #definemin(x,y)({
- typeof(x)_x=(x);//_x的数据类型和x一样
- typeof(y)_y=(y);
- (void)(&_x==&_y);
- _x<_y?_x:_y;})
- #definemax(x,y)({
- typeof(x)_x=(x);
- typeof(y)_y=(y);
- (void)(&_x==&_y);
[plain]view plaincopyprint?
- #defineget_user(x,p)
- ({
- constregistertypeof(*(p))__user*__pasm("r0")=(p);//__p的数据类型和*(p)的指针数据类型是一样的,__p=p
- registertypeof(*(p))__r2asm("r2");//__r2的数据类型和*(p)的数据类型是一样的
- registerint__easm("r0");
- switch(sizeof(*(__p))){
- case1:
- __get_user_x(__r2,__p,__e,1,"lr");
- break;
- case2:
- __get_user_x(__r2,__p,__e,2,"r3","lr");
- break;
- case4:
- __get_user_x(__r2,__p,__e,4,"lr");
- break;
- case8:
- __get_user_x(__r2,__p,__e,8,"lr");
- break;
- default:__e=__get_user_bad();break;
- }
- x=__r2;
- __e;
- })
[plain]view plaincopyprint?
- #include
- typedefstruct
- {
- intx;
- chary;
- }astruct,*pastrcut;
- intmain()
- {
- intsizem,sizew;
- intx=3;
- typeof(&x)m=&x;
- sizem=sizeof(m);
- *m=5;
- typeof(((astruct*)5)->y)w;
- sizew=sizeof(w);
- w="a";
- return1;
- }
首先看main函数里的m变量,这个变量的类型就是typeof(&x), 由于x是int型的(这里与x是否被赋值一点关系都没有),所以&x应该是int *类型,那么typeof(&x)返回的类型就是int*,所以m自然也就是个int*类型的。
然后我们看w变量,其类型是 typeof(((astruct *)5)->y), 其中astruct是一个被定义的结构类型,其中的y元素是char类型,那么((astruct *)5)->y是啥意思呢?在这里5并不是真正的变量,可以把它理解为一个替代使用的符号,当然这个符号最好是一个数,其意思更可以理解为一个被赋值了的变量,这个数可以是0,3,也可以是8也可以随便什么都可以。那么((astruct *)5)->y仅仅就是表示了y这个变量,所以typeof的结果就是y元素的类型,也就是char。
评论