typeof in linux kernel

Publisher:Ziyu2022Latest update time:2016-03-02 Source: eefocusKeywords:typeof Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Kernel version: 2.6.14

When analyzing the kernel today, I saw typeof again. I only knew that it probably returned the type of a variable. Later, I checked online and found that this keyword is used a lot in Linux. If you are familiar with sizeof, then you can draw an analogy. sizeof(exp) returns the size of the data type of exp, and typeof(exp.) returns the data type of exp. Below are some examples of typeof in the Linux kernel.

include/linux/kernel.h
[plain]  view plain  copy
 
 print ?
  1. /*  
  2.  * min()/max() macros that also do  
  3.  * strict type-checking.. See the  
  4.  * "unnecessary" pointer comparison.  
  5.  */  
  6. #define min(x,y) ({ \  
  7.     typeof(x) _x = (x); \ //_x has the same data type as x  
  8.     typeof(y) _y = (y); \  
  9.     (void) (&_x == &_y); \  
  10.     _x < _y ? _x : _y; })  
  11.   
  12.   
  13. #define max(x,y) ({ \  
  14.     typeof(x) _x = (x); \  
  15.     typeof(y) _y = (y); \  
  16.     (void) (&_x == &_y); \  
linux/include/asm-arm/uaccess.h
[plain]  view plain  copy
 
 print ?
  1. #define get_user(x,p) \  
  2.     ({ \  
  3.         const register typeof(*(p)) __user *__p asm("r0") = (p);\ //The data type of __p is the same as the pointer data type of *(p), __p = p  
  4.         register typeof(*(p)) __r2 asm("r2"); \ //The data type of __r2 is the same as the data type of *(p)  
  5.         register int __e asm("r0"); \  
  6.         switch (sizeof(*(__p))) { \  
  7.         case 1: \  
  8.             __get_user_x(__r2, __p, __e, 1, "lr"); \  
  9.                 break; \  
  10.         case 2: \  
  11.             __get_user_x(__r2, __p, __e, 2, "r3", "lr"); \  
  12.             break; \  
  13.         case 4: \  
  14.                 __get_user_x(__r2, __p, __e, 4, "lr"); \  
  15.             break; \  
  16.         case 8: \  
  17.             __get_user_x(__r2, __p, __e, 8, "lr"); \  
  18.                 break; \  
  19.         default: __e = __get_user_bad(); break; \  
  20.         } \  
  21.         x = __r2; \  
  22.         __e; \  
  23.     })  
Let's write a small program as an example:
[plain]  view plain  copy
 
 print ?
  1. #include   
  2.   
  3. typedef struct   
  4. {  
  5.     int x;  
  6.     char y;  
  7. }astruct, * pastrcut;  
  8.   
  9. int main()  
  10. {  
  11.     int sizem, sizew;  
  12.     int x = 3;  
  13.     typeof(&x) m = &x;  
  14.     sizem = sizeof(m);  
  15.     *m = 5;  
  16.     typeof(((astruct *)5)->y) w;  
  17.     sizew = sizeof(w);  
  18.     w = "a";  
  19.     return 1;  
  20. }  

First, let’s look at the m variable in the main function. The type of this variable is typeof(&x). Since x is of type int (it has nothing to do with whether x is assigned a value), &x should be of type int *. Then the type returned by typeof(&x) is int*, so m is naturally of type int*.

Then we look at the w variable, its type is typeof(((astruct *)5)->y), where astruct is a defined structure type, and the y element is of type char, so what does ((astruct *)5)->y mean? Here 5 is not a real variable, it can be understood as a symbol used as a substitute, of course, this symbol is best to be a number, and its meaning can be understood as a variable that has been assigned a value, which can be 0, 3, 8 or anything else. So ((astruct *)5)->y just represents the variable y, so the result of typeof is the type of the y element, which is char.

Keywords:typeof Reference address:typeof in linux kernel

Previous article:IS_ERR in linux kernel
Next article:dup system call in linux kernel

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号