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- /*
- * min()/max() macros that also do
- * strict type-checking.. See the
- * "unnecessary" pointer comparison.
- */
- #define min(x,y) ({ \
- typeof(x) _x = (x); \ //_x has the same data type as 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); \
- #define get_user(x,p) \
- ({ \
- 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
- register typeof(*(p)) __r2 asm("r2"); \ //The data type of __r2 is the same as the data type of *(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
- 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;
- }
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.
Previous article:IS_ERR in linux kernel
Next article:dup system call in linux kernel
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- TI Voltage Reference (VREF) Application Design Tips and Tricks
- Banknote number recognition system based on ARM.pdf
- My knowledge of computers
- Sampling rate of MCU ADC
- Let’s talk about the first big event in 2019...
- Wifi controlled LED screen
- IIoT opens up more possibilities beyond the factory floor
- Recommended one: [Lingsheng] MCU code automatic generator (automatic programming tool)
- Learning 3D visualization scene hierarchy from scratch (1)
- What are the differences and connections between compilers and integrated development environments?