linux内核container_of宏使用详解

linux内核代码有很多巧妙地设计,其设计思想值得我们深入学习分析.今天来分析一下linux内核中大量使用的一个宏定义container_of(ptr, type, member)
先上功能定义:已知结构体type的成员变量member的地址是ptr,推导出结构体变量的首地址.

/**
 * 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) );})

上面就是宏定义的实现.接下来对代码进行分拆讲解:

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

首先来看一个offsetof的宏定义实现,offsetof宏定义的作用就是获取到结构体变量MEMBER相对于0地址的偏移量. size_t在内核中的定义是 unsigned long. &((TYPE *)0)->MEMBER)可以理解为在0地址定义了个TYPE类型的结构体变量, MEMBER成员变量的地址就是MEMBER相对于结构体首地址的偏移量.注意这里我们虽然使用了0地址,但是只是数据类型的转换,并没有直接读写0地址内容.所以运行不会出错.

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

typeof的作用的是获取变量的数据类型,上述代码的含义就是获取到结构体成员变量member的数据类型,然后定义一个与member相同数据类型的的指针变量__mptr指向ptr.

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

__mptr是member在结构体中的实际的地址, offsetof得到的是member在结构体中的偏移量,二者相减便得到了结构体的首地址.

下面通过测试程序加深对container_of宏定义的理解.

#include <stdio.h>

#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) );})

typedef struct test_t{
    int x;
    int y;
    int z;
}test_T;

int main (int argc, char **argv) 
{
    test_T sttest = {1,2,3};
    int *p = &sttest.z;
    /*
        已知结构体test_T的成员变量z的地址p, 得出结构体的首地址.
    */
    test_T *psttest = container_of(p, test_T, z);

    printf ("%p, %p\r\n", &sttest,psttest);
    printf ("%d, %d, %d\r\n", psttest->x, psttest->y, psttest->z);

    return 0; 
}

程序运行输出结果如下:

wh@ubuntu:/work/c/containr_of$ ./container_of 
0x7ffe59e6e020, 0x7ffe59e6e020
1, 2, 3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值