对container_of的理解
时间:2010-12-29 来源:centaurwarrior
在学习Linux驱动的过程中,遇到一个很好玩的内核函数,准确的说是一个很好玩的宏,叫做container_of。该宏定义在include/linux/kernel.h中,首先来贴出它的代码:
下面说一说我对于这个container_of的实现的理解:
首先,我们将container_of(memp, struct demo_struct, type3)根据宏的定义进行展开如下:
假设结构体变量demo在实际内存中的位置如下图所示:
demo
+-------------+ 0xA000
| member1 |
+-------------+ 0xA004
| member2 |
| |
+-------------+ 0xA010
| member3 |
| |
+-------------+ 0xA018
| member4 |
+-------------+
则,在执行了上述代码的第2行之后__mptr的值即为0xA010。
再看上述代码的第3行,其中需要说明的是offsetof,它定义在include/linux/stddef.h中,其定义如下:
因 此,offsetof(struct demo_struct, member3)调用返回的值就是member3相对于demo变量的偏移。结合上述给出的变量地址分布图可知,offsetof(struct demo_struct, member3)将返回0x10。
于是,由上述分析可知,此时,__mptr==0xA010,offsetof(struct demo_struct, member3)==0x10。
因此, (char *)__mptr - ((size_t) &((struct demo_struct *)0)->member3) == 0xA010 - 0x10 == 0xA000,也就是结构体变量demo的首地址(如上图所示)。
由此,container_of实现了根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针的功能。
the specific code is as the following:
#include <stdio.h>
#include <stddef.h>
//understandinf of the container_of macro
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
struct test {
int x;
int y;
};
int main(void)
{
struct test t = { .x = 10, .y = 20 };
printf("&t = %p\n", &t);
printf("&t.x = %p\n", &t.x);
printf("&t.y = %p\n", &t.y);
printf("offsetof(struct test, y) = %d\n", offsetof(struct test, y));
printf("start position: %p\n",container_of(&t.y,struct test,y));
return 0;
}
the effect is :
thewayma@theway:~/Desktop$ ./a.out
&t = 0xbfec45c8
&t.x = 0xbfec45c8
&t.y = 0xbfec45cc
offsetof(struct test, y) = 4
start position: 0xbfec45c8
thewayma@theway:~/Desktop$
- 439/**
- 440 * container_of - cast a member of a structure out to the containing structure
- 441 * @ptr: the pointer to the member.
- 442 * @type: the type of the container struct this is embedded in.
- 443 * @member: the name of the member within the struct.
- 444 *
- 445 */
- 446#define container_of(ptr, type, member) ({ \
- 447 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
- 448 (type *)( (char *)__mptr - offsetof(type,member) );})
- struct demo_struct {
- type1 member1;
- type2 member2;
- type3 member3;
- type4 member4;
- };
-
- struct demo_struct demo;
- type3 *memp = get_member_pointer_from_somewhere();
- struct demo_struct *demop = container_of(memp, struct demo_struct, member3);
下面说一说我对于这个container_of的实现的理解:
首先,我们将container_of(memp, struct demo_struct, type3)根据宏的定义进行展开如下:
- struct demo_struct *demop = ({ \
- const typeof( ((struct demo_struct *)0)->member3 ) *__mptr = (memp); \
- (struct demo_struct *)( (char *)__mptr - offsetof(struct demo_struct, member3) );})
假设结构体变量demo在实际内存中的位置如下图所示:
demo
+-------------+ 0xA000
| member1 |
+-------------+ 0xA004
| member2 |
| |
+-------------+ 0xA010
| member3 |
| |
+-------------+ 0xA018
| member4 |
+-------------+
则,在执行了上述代码的第2行之后__mptr的值即为0xA010。
再看上述代码的第3行,其中需要说明的是offsetof,它定义在include/linux/stddef.h中,其定义如下:
- 24#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
- (struct demo_struct *)( (char *)__mptr - ((size_t) &((struct demo_struct *)0)->member3) );
因 此,offsetof(struct demo_struct, member3)调用返回的值就是member3相对于demo变量的偏移。结合上述给出的变量地址分布图可知,offsetof(struct demo_struct, member3)将返回0x10。
于是,由上述分析可知,此时,__mptr==0xA010,offsetof(struct demo_struct, member3)==0x10。
因此, (char *)__mptr - ((size_t) &((struct demo_struct *)0)->member3) == 0xA010 - 0x10 == 0xA000,也就是结构体变量demo的首地址(如上图所示)。
由此,container_of实现了根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针的功能。
the specific code is as the following:
#include <stdio.h>
#include <stddef.h>
//understandinf of the container_of macro
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
struct test {
int x;
int y;
};
int main(void)
{
struct test t = { .x = 10, .y = 20 };
printf("&t = %p\n", &t);
printf("&t.x = %p\n", &t.x);
printf("&t.y = %p\n", &t.y);
printf("offsetof(struct test, y) = %d\n", offsetof(struct test, y));
printf("start position: %p\n",container_of(&t.y,struct test,y));
return 0;
}
the effect is :
thewayma@theway:~/Desktop$ ./a.out
&t = 0xbfec45c8
&t.x = 0xbfec45c8
&t.y = 0xbfec45cc
offsetof(struct test, y) = 4
start position: 0xbfec45c8
thewayma@theway:~/Desktop$
相关阅读 更多 +