1.楔子
我在学习list_head这个结构体的时候偶然发现了0地址这个东西,其中list_head是内嵌于许多数据结构的
比如用的最多的页
struct page {
......
struct list_head lru;
.....
}
在伙伴系统中,可以利用page中lru的地址来找到page的地址,其中就是使用的0地址的思想
2. "0地址"的使用
#define list_entry(ptr,type,member) container_of(ptr,type,member)
#define offsetof(TYPE,MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
#define container_of(ptr,type,member) ( \
{\
const typeof( ((type*)0)->member ) *__mptr=(ptr);\
(type*)( (char*)__mptr - offsetof(type,member) );\
} )
首先,offsetof宏利用了0地址求某个成员在所处数据结构TYPE中的偏移量,
然后,container_of使用该成员数据结构实际的内存地址减去偏移量就得到TYPE的内存地址
3.0地址使用的另一个例子
可以利用0地址求得数据结构中各个成员的所占的字节数
#include<stdio.h>
struct Test
{
int a;
long long b;
char c;
int d;
};
int main()
{
struct Test *p = 0;
int offset_a = (int)(&p->a);
int offset_b = (int)(&p->b);
int offset_c = (int)(&p->c);
int offset_d = (int)(&p->d);
printf("offseta:%d\n offsetb:%d\n offsetc:%d\n offsetd:%d\n",
offset_a, offset_b, offset_c, offset_d);
return 0;
}
结果
offseta:0
offsetb:8
offsetc:16
offsetd:20