首页 > 代码库 > typeof, offsetof 和container_of
typeof, offsetof 和container_of
要理解Linux中实现的双向循环链表("侵入式"链表),首先得弄明白宏container_of。 本文尝试从gcc的关键字typeof和宏offsetof入手,循序渐进地剖析宏container_of之实现原理。
1. typeof (from: https://en.wikipedia.org/wiki/Typeof)
typeof is an operator provided by several programming languages to determine the data type of a variable. This is useful when constructing programs that must accept multiple types of data without explicitly specifying the type. The GNU compiler (GCC) extensions for the C programming language provide typeof: #define max(a, b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
typeof和sizeof一样,都是关键字。只不过typeof不是标准的c语言关键字,而是gcc的c语言的扩展关键字。 typeof的作用是取得某个变量的数据类型。例如:
unsigned int a = 1; // typeof (a) is unsigned int short b = 2; // typeof (b) is short
2. offsetof (from: include/linux/stddef.h)
1 #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
/*
* @TYPE: The type of the structure
* @MEMBER: The member within the structure to get the offset of
*/
宏offsetof的作用是获取某个成员变量(MEMBER)在其所在的结构体(TYPE)里的偏移。 上面的宏实现得非常巧妙(如果对汇编熟悉就更容易理解),剖析如下:
(1) X = (TYPE *)0 // 将地址0x0强制转化为结构体类型TYPE的首地址 (2) Y = X->MEMBER // 访问成员变量MEMBER (3) Z = &Y // 取得成员变量MEMBER的内存地址 (4) N = (size_t)Z // 将成员变量MEMBER的内存地址强制转换成偏移量, = (size_t)&Y // 由于内存地址也是整数,所以成员变量MEMBER相对于结构体首地址的偏移等于&Y = (size_t)&(X->MEMBER) = (size_t)&X->MEMBER // 注意: -> 比 & 优先级高 = (size_t)&(X)->MEMBER = (size_t)&((TYPE *)0)->MEMBER
3. container_of (from: include/linux/kernel.h)
1 /** 2 * container_of - cast a member of a structure out to the containing structure 3 * @ptr: the pointer to the member. 4 * @type: the type of the container struct this is embedded in. 5 * @member: the name of the member within the struct. 6 * 7 */ 8 #define container_of(ptr, type, member) ({ 9 const typeof( ((type *)0)->member ) *__mptr = (ptr); 10 (type *)( (char *)__mptr - offsetof(type,member) );})
- L9: 用一个临时变量__mptr保存成员变量的指针ptr
- L10: offsetof(type, member): 计算出成员变量相对于其所在的结构体的偏移,不妨记为OFFSET
- L10: __mptr - OFFSET, 就是成员变量所在的结构体的首地址
因此, 宏container_of的作用就是根据某个成员变量的内存地址,反推出其所在的结构体变量的首地址。
示例代码: foo.c
1 #include <stdio.h> 2 3 #define offsetof(TYPE, MEMBER) ((size_t)&(((TYPE *)0)->MEMBER)) 4 5 #define container_of(ptr, type, member) ({ 6 const typeof( ((type *)0)->member ) *__mptr = (ptr); 7 (type *)( (char *)__mptr - offsetof(type,member) );}) 8 9 typedef struct foo_s { 10 int m_int; 11 short m_short; 12 char m_char; 13 long long m_longlong; 14 } foo_t; 15 16 int 17 main(int argc, char *argv[]) 18 { 19 foo_t ox = {0x12345678, 0x1234, ‘A‘, 0xfedcba9876543210}; 20 char *p3 = &ox.m_char; 21 foo_t *p = container_of(p3, foo_t, m_char); 22 23 printf("foo_t ox (%p) sizeof(foo_t) = %d\n", &ox, sizeof (foo_t)); 24 printf("foo_t *p (%p) p3(%p)\n", p, p3); 25 printf("foo_t->m_int = %#x\t\t(%d)(%p)\n", 26 p->m_int, offsetof(foo_t, m_int), &ox.m_int); 27 printf("foo_t->m_short = %#x\t\t(%d)(%p)\n", 28 p->m_short, offsetof(foo_t, m_short), &ox.m_short); 29 printf("foo_t->m_char = %c\t\t\t(%d)(%p)\n", 30 p->m_char, offsetof(foo_t, m_char), &ox.m_char); 31 printf("foo_t->m_longlong = %#llx\t(%d)(%p)\n", 32 p->m_longlong, offsetof(foo_t, m_longlong), &ox.m_longlong); 33 34 return 0; 35 }
编译并运行
$ gcc -g -Wall -m32 -o foo foo.c $ ./foo foo_t ox (0xbfdfe990) sizeof(foo_t) = 16 foo_t *p (0xbfdfe990) p3(0xbfdfe996) foo_t->m_int = 0x12345678 (0)(0xbfdfe990) foo_t->m_short = 0x1234 (4)(0xbfdfe994) foo_t->m_char = A (6)(0xbfdfe996) foo_t->m_longlong = 0xfedcba9876543210 (8)(0xbfdfe998)
反汇编并结合gcc -E foo.c
(gdb) set disassembly-flavor intel (gdb) disas /m main Dump of assembler code for function main: 18 { 0x0804841d <+0>: push ebp 0x0804841e <+1>: mov ebp,esp 0x08048420 <+3>: and esp,0xfffffff0 0x08048423 <+6>: sub esp,0x40 19 foo_t ox = {0x12345678, 0x1234, ‘A‘, 0xfedcba9876543210}; 0x08048426 <+9>: mov DWORD PTR [esp+0x30],0x12345678 0x0804842e <+17>: mov WORD PTR [esp+0x34],0x1234 0x08048435 <+24>: mov BYTE PTR [esp+0x36],0x41 0x0804843a <+29>: mov DWORD PTR [esp+0x38],0x76543210 0x08048442 <+37>: mov DWORD PTR [esp+0x3c],0xfedcba98 20 char *p3 = &ox.m_char; 0x0804844a <+45>: lea eax,[esp+0x30] 0x0804844e <+49>: add eax,0x6 0x08048451 <+52>: mov DWORD PTR [esp+0x24],eax 21 foo_t *p = container_of(p3, foo_t, m_char); 0x08048455 <+56>: mov eax,DWORD PTR [esp+0x24] 0x08048459 <+60>: mov DWORD PTR [esp+0x28],eax 0x0804845d <+64>: mov eax,DWORD PTR [esp+0x28] 0x08048461 <+68>: sub eax,0x6 0x08048464 <+71>: mov DWORD PTR [esp+0x2c],eax # # --- L21‘s output from "gcc -E foo.c" --- # 001 foo_t *p = ({ # 002 const typeof( ((foo_t *)0)->m_char ) *__mptr = (p3); # 003 (foo_t *)( (char *)__mptr - ((size_t)&(((foo_t *)0)->m_char)) ); # 004 }); #
分析(TBD)
小结(TBD)
typeof, offsetof 和container_of
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。