1.offset宏的讲解
# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
1)((type *) 0) =》0地址强制转化为type类型的指针;
2) ((type *)0)->MEMBER =》 访问type结构中的MEMBER数据成员
3) &(((type *)0)->MEMBER) =》去除type结构中数据成员member的地址
4) (size_t)(&(((type *)0)->Member)) =》将结果转化为size_t类型
2.container_of宏的讲解
# define container_of(ptr, type, member) ({ \
typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
1)typeof( ((type *)0)->member ) *__mptr = (ptr);
首先定义了一个临时的数据类型(通过 typedef(((type *)0)->member)获得)与ptr相同的指针变量_mptr,然后用它来保存ptr的值
2)用(char *)__mptr减去member在结构体中的偏移量,得到的值就是整个结构体变量的首地址(整个宏的返回值就是这个首地址)。
eg)
#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) );})
struct test_struct {
int num;
char ch;
float f1;
};
int main(void)
{
struct test_struct *test_struct;
struct test_struct init_struct ={12,'a',12.3};
char *ptr_ch = &init_struct.ch;
test_struct = container_of(ptr_ch,struct test_struct,ch);
printf("test_struct->num =%d\n",test_struct->num);
printf("test_struct->ch =%c\n",test_struct->ch);
printf("test_struct->ch =%f\n",test_struct->f1);
return 0;
}
执行结果:
jibo@jibo-VirtualBox:~/cv_work/work/list/container_of $ ./main
test_struct->num =12
test_struct->ch =a
test_struct->ch =12.300000