Linux内核中的typeof


Linux内核版本:2.6.14

今天分析内核时又看到了typeof,只知道它大概是返回变量的类型,后来上网查了下发现这个关键字在linux中用的非常多。如果你对sizeof很熟悉的话,那么大可进行类推,sizeof(exp)返回的是exp的数据类型大小,那么typeof(exp.)返回的就是exp的数据类型。下面是linux内核中typeof的一些例子。

include/linux/kernel.h

/*
 * min()/max() macros that also do
 * strict type-checking.. See the
 * "unnecessary" pointer comparison.
 */
#define min(x,y) ({ \
 typeof(x) _x = (x); \  //_x的数据类型和x一样
 typeof(y) _y = (y); \
 (void) (&_x == &_y);  \
 _x < _y ? _x : _y; })


#define max(x,y) ({ \
 typeof(x) _x = (x); \
 typeof(y) _y = (y); \
 (void) (&_x == &_y);  \

linux/include/asm-arm/uaccess.h

#define get_user(x,p)       \
 ({        \
  const register typeof(*(p)) __user *__p asm("r0") = (p);\  //__p的数据类型和*(p)的指针数据类型是一样的,__p = p
  register typeof(*(p)) __r2 asm("r2");   \  //__r2的数据类型和*(p)的数据类型是一样的
  register int __e asm("r0");    \
  switch (sizeof(*(__p))) {    \
  case 1:       \
   __get_user_x(__r2, __p, __e, 1, "lr");  \
          break;      \
  case 2:       \
   __get_user_x(__r2, __p, __e, 2, "r3", "lr"); \
   break;      \
  case 4:       \
          __get_user_x(__r2, __p, __e, 4, "lr");  \
   break;      \
  case 8:       \
   __get_user_x(__r2, __p, __e, 8, "lr");  \
          break;      \
  default: __e = __get_user_bad(); break;   \
  }       \
  x = __r2;      \
  __e;       \
 })

下面写一个小程序示例一下:

#include <stdio.h>

typedef struct
{
 int x;
 char y;
}astruct, * pastrcut;

int main()
{
 int sizem, sizew;
 int x = 3;
 typeof(&x) m = &x;
 sizem = sizeof(m);
 *m = 5;
 typeof(((astruct *)5)->y) w;
 sizew = sizeof(w);
 w = "a";
 return 1;
}

首先看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。

相关内容