内核container_of原理及应用

一. 原理
container_of的作用是已知一个结构体的成员变量名(memmber),成员变量指针(ptr),结构体类型(type),来获取结构体的首地址。
核心原理是,成员变量的地址减去成员变量相对于结构体起始地址的偏移量,获得结构体的首地址。

#define container_of(ptr, type, member) ( \
    {                                                              \
      const typeof( ((type *)0)->member ) *__mptr = (ptr); \
      (type *)( (char *)__mptr - offsetof(type,member) );     \
    } )
    
   (1)const typeof( ((type *)0)->member ) *__mptr = (ptr); 
   		typeof 获取成员变量类型,并定义一个该类型的指针,指向成员变量;
  (2) (type *)( (char *)__mptr - offsetof(type,member) );
  		offsetof宏定义,计算成员变量相对于 结构体的地址相对偏移量(以字节为单位),
  		成员变量地址减去相对偏移量,得到成员变量所在结构体的起始地址,
  		(char*)的强制转换,是因为计算的偏移量是以字节为单位的;
  		
#define offsetof(type, member) (  (size_t) & ((type*)0) ->member  ) 


二. 示例 

#include

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

struct info {
int a ;
char ch;
int i;
};

struct data {
struct info *info;
int type;
};

int main(void)
{
struct data *data_ptr = NULL;
struct info f = {
.a = 4,
.ch = ‘C’,
.i = 12,
};

struct data d = {
    .info = &f,
    .type = 1,
};

data_ptr = container_of(&f, struct data, info);

printf("data_ptr.type = %d\n", data_ptr->type);
  return 0;

}

你可能感兴趣的:(kernel)