Linux/kernel.h中强大的container

位于Linux/kernel.h中强大的container_of宏

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

简单应用:
struct A_t
{
 char a;
 char b;
 int  c;
};
struct B_t
{
 char a;
 int  *c;
};
A_t A;
B_t B;
A_t *A_add;
A.c = (int *)malloc(sizeof(int),1);
B.c = &A.c;
dev = container_of(inode->i_cdev, struct scullc_dev, cdev);
A_add = container_of(B.c,struct A_t,c);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzywfx.html