offset linux,Linux 宏定义之 offsetof 与 container_of(十九)

今天我们来看看 Linux 中的两个经典的宏:offsetof 与 container_of。下来我们先来看看它们两个的宏定义,如下#ifndef offsetof

#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE*)0)->MEMBER)

#endif

#ifndef container_of

#define container_of(ptr, type, member) ({                \

const typeof(((type*)0)->member)* __mptr = (ptr); \

(type*)((char*))__mptr - offsetof(type, member); })

#endif

要想看懂这两个宏,我们就先来看看编译器做了什么? offsetof 是用于计算 TYPE 结构体中 MEMBER 成员的偏移位置。编译器清楚的知道结构体成员变量的偏移位置,通过结构体变量首地址与偏移量定位成员变量。下来我们通过测试代码来进行说明#include 

#ifndef offsetof

#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE*)0)->MEMBER)

#endif

struct ST

{

int i;     // 0

int j;     // 4

char c;    // 8

};

void func(struct ST* pst)

{

int* pi = &(pst->i);    //  0

int* pj = &(pst->j);    //  4

char* pc = &(pst->c);   //  8

printf("pst = %p\n", pst);

printf("pi = %p\n", pi);

printf("pj = %p\n", pj);

printf("pc = %p\n", pc);

}

int main()

{

struct ST s = {0};

func(&s);

func(NULL);

printf("offset i: %d\n", offsetof(struct ST, i));

printf("offset j: %d\n", offsetof(struct ST, j));

printf("offset c: %d\n", offsetof(struct ST, c));

return 0;

}

我们来看看结果

b17bb9ec330f1fe4b7c8cbfe30d0c602.png

我们看到 pst 和 pi 打印的地址值是一样的,J 和 c 分别加 4。以 NULL 为参数传进去更加看的明显,而直接调用 offsetof 宏,它的效果和 NULL 是一样的。由此,它的作用就显而易见了,用于获取 TYPE 结构体中的 MEMBER 的偏移量。

下来我们来看看 container_of 宏,首先讲解下({ }),它是 GNU C 编译器的语法扩展,它与逗号表达式的作用类似,结果为最后一个语句的值。如下所示

4f870dbee1e2e07dd21a1c682964cdf8.png

typeof 是 GNU C 编译器特有的关键字,它只在编译器生效,用于得到变量的类型。用法如下

d323efa952b3bf39bbb1d304dad86851.png

最后的原理如下图所示

f63894035548c8f618686fdf15859c0e.png

下来我们来编程进行分析说明#include 

#ifndef offsetof

#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE*)0)->MEMBER)

#endif

#ifndef container_of

#define container_of(ptr, type, member) ({         \

const typeof(((type*)0)->member)* __mptr = (ptr);   \

(type*)((char*)__mptr - offsetof(type, member)); })

#endif

#ifndef container_of_new

#define container_of_new(ptr, type, member) ((type*)((char*)(ptr) - offsetof(type, member)))

#endif

struct ST

{

int i;     // 0

int j;     // 4

char c;    // 8

};

void method_1()

{

int a = 0;

int b = 0;

int r = (

a = 1,

b = 2,

a + b

);

printf("r = %d\n", r);

}

void method_2()

{

int r = ( {

int a = 1;

int b = 2;

a + b;

} );

printf("r = %d\n", r);

}

void type_of()

{

int i = 100;

typeof(i) j = i;

const typeof(j)* p = &j;

printf("sizeof(j) = %d\n", sizeof(j));

printf("j = %d\n", j);

printf("*p = %d\n", *p);

}

int main()

{

method_1();

method_2();

type_of();

struct ST s = {0};

char* pc = &s.c;

int e = 0;

int* pe = &e;

struct ST* pst = container_of(pc, struct ST, c);

printf("&s = %p\n", &s);

printf("pst = %p\n", pst);

return 0;

}

我们来编译看看结果

b2a25684994f44d98ce5f0cf11af4950.png

编译的时候报了 4 个警告,但是不影响我们的输出,看看运行结果

5eaaeb6c2fa51f993603a263d7bcdb8b.png

上面的两个输出 r 的值是一样的,它们的写法是等价的。用 container_of 宏调用的时候,s 和 pst 的地址值是一样的。那么我们用自己定义的 container_of_new 宏来调用 pe 试试呢?看看结果

52e4e7e0a4766614b1aa7f46bbb1f667.png

编译的时候已经给出警告了,说 pc 的类型是不对的。然后我们来运行看看结果

41bb2c120f0147428fd3943cd95a20c2.png

我们发现最后打印的 s 和 pst 的值竟然是不一样的。由此可以看出,原生的 container_of 宏写法虽然复杂点,但是它的安全性是最高的。通过今天对 offsetof 与 container_of 宏的剖析,总结如下:1、编译器清楚的知道结构体成员变量的偏移位置;2、({ }) 与逗号表达式类似,结果为最后一个语句的值;3、typeof 只在编译期生效,用于得到变量的类型;4、container_of 使用 ({ }) 进行类型的安全检查。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值