解释Linux内核源码中的container_of宏及其标准C版本实现

Linux内核源码文件 include/linux/kernel.h中,定义了container_of宏,源码如下:

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:        the pointer to the member.
 * @type:       the type of the container struct this is embedded in.
 * @member:     the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

对于这个宏的准确理解是进入Linux内核源码分析的必不可少条件,我自己百度了一下,有很多博文对此进行了解释,然而没有一个能解释的十分清楚。于是google了一下,参考了一些英文资料,终于把这个问题搞清楚了,记录下来供自己和大家参考。

1 container_of宏的作用与实现原理

这个宏的唯一目的就是根据一个结构体实例的成员所在的内存地址,推算出该结构体的起始内存地址。在C语言中,已知结构体定义的情况下,编译器负责安排结构体实例的内存布局,当然编译器对于每个成员变量在结构体中的偏移量非常清楚。

struct Student
{
    int age;
    float score;
    char* name;
};
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

根据成员变量的地址来计算结构体的起始地址也就非常简单了:成员变量地址 - 成员变量在结构体中的偏移量。
总之,在c语言中,编译器在编译期间能够确定成员变量在结构体中的偏移量。

2 几个关键语法

2.1 如何获取结构体中成员变量的内存偏移量

尽管C语言编译器对成员变量的内存偏移了如指掌,然而C语言标准中并没有提供一个非常直观的语法来让程序员获取此偏移量。也许C语言设计者认为这种需求主要实在编译器内部,对于C程序员来说并不常用。为此需要一个小小的技巧,即假设结构体起始地址为A,那么(成员变量的内存地址 - A)就是偏移量了。更进一步,令A=0,那么此时成员变量的内存地址==偏移量,写成代码如下:

size_t offset_of_score = (size_t) &((struct Student*)0)->score;
  
  
  • 1
  • 1

虽然0是一个非法指针,然而此处并没有真正对其进行内存访问(运行期),只是利用其进行计算(编译器),所以不会造成任何程序错误。

也许获取成员偏移量这种需求的增多,GCC编译器的新版本中提供了专门的语法结构__copiler_offsetof(TYPE,MEMBER) 来对此进行支持。

为了兼容不同的GCC版本,Linux源码文件include/stddef.h中定义了offsetof宏,如下:

#undef offsetof
#ifdef __compiler_offsetof
#define offsetof(TYPE, MEMBER)  __compiler_offsetof(TYPE, MEMBER)
#else
#define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
#endif

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.2 GCC对标准C的扩展语法之 ({})

先给出一个简单的例子:

#include <stdio.h>

int main(int argc, char** argv)
{
    int x = ({1;2;});
    printf("x = %d\n", x);

    return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

上面例子的输出结果是 x = 2。
当然这个语法是GCC特有的,上述代码在VC++中无法成功编译。

乍一看,好像与标准C中的逗号运算符类似,在标准C中:

#include <stdio.h>

int main(int argc, char** argv)
{
    int x = (1,2);
    printf("x = %d\n", x);

    return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

效果与前面一样。
然而GCC的这个扩展支持的功能要远远大于逗号运算符。因为({})里面可以有任意的语句。如

#include <stdio.h>

int main(int argc, char** argv)
{
    int x = ({int a = 3; printf("hello\n"); 2;});
    printf("x = %d\n", x);

    return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

只要({})里面最后一个表达式为一个值,这个值就是最终的结果。

2.3 GCC对标准C的扩展语法之 typeof()

这是一个非标准的GNU C扩展,用于得到一个变量的数据类型。如:

int x = 100;
typeof(x) y = 200;
  
  
  • 1
  • 2
  • 1
  • 2

此时, typeof(x) 和 int 是等价的。需要注意的是typeof()并不是一个宏,而是编译器内建构件,所以typeof(x)并不是等于字符串”int”。编译器看到它时,自动推算变量x的数据类型,这个是在编译器确定的,要与高级语言的运行时类型识别区分开来。

由于是一个GCC扩展,所以同样上述代码在标准C中无法编译通过。

2.4 再看源码,一目了然

#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})
  
  
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

现在再看上述代码,是否已经豁然开朗?

#include <stdio.h>

#undef offsetof
#ifdef __compiler_offsetof
#define offsetof(TYPE, MEMBER)  __compiler_offsetof(TYPE, MEMBER)
#else
#define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
#endif

#define container_of(ptr, type, member) ({                  \
                const typeof( ((type*)0)->member ) *__mptr = (ptr); \
                (type*)( (char*)__mptr - offsetof(type,member) ); })

struct Student
{
    int age;
    float score;
    char* name;
};

int main(int argc, char** argv)
{
    struct Student s;
    printf("the address of s is: %p\n", &s);

    struct Student *pStu = container_of(&s.score, struct Student, score);
    printf("the address of s is calculated as: %p\n", pStu);

    return 0;
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

也许你还存在一个疑问,({})中的第一个语句有啥用?

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

的确,这个语句并不影响最后的计算结果。其作用是为了检查ptr是否是member对应的类型指针。假如程序员不小心把代码写成了如下:

struct Student *pStu = container_of(&s.age, struct Student, score);
  
  
  • 1
  • 1

那么container_of宏的第一个语句就被解析出:

const float * __mptr = &s.age;

由于age是int类型,而把一个int*赋值给float*变量是不合法的,所以编译器会发出警告,以提示程序员是否写错了代码。当然这种保护的作用有限,假如age类型也为float,则无法被编译器发现。不论如何,这种不会带来计算负担的检查还是值得的。

3 标准C如何实现

学习Linux内核源码的一个好处就是把其中的一些编程技巧应用到平时的项目中。然而Linux内核之外的世界里很少使用这么多的GCC扩展,那么是否我们可以用标准C来实现类似功能的container_of呢?

把typeof(),({}),扩展去掉后,我们就得到了一个使用标准C编写的简化版本的container_of。

#define offsetof(TYPE, MEMBER)              ((size_t)&((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member)     (type*)( (char*)(ptr) - offsetof(type, member) )
  
  
  • 1
  • 2
  • 1
  • 2

由于是兼容标准C,所以在GCC,VC++等主流编译器下均可以顺利编译通过。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值