位于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);
Linux/kernel.h中强大的container
内容版权声明:除非注明,否则皆为本站原创文章。