offsetof
C语言中有这样一个宏offsetof,它的作用是计算结构体成员相对于结构体的偏移,原型如下:
#define offsetof(type, member) (size_t) &((type*)0)->member
参数说明:
- type:结构体类型
- member:结构体成员
原理分析:
偏移 = 成员地址 - 结构体地址,若结构体地址为0,则偏移 = 成员地址;
示例:
#include <stdio.h>
#define offsetof(type, member) (size_t) &((type*)0)->member
typedef struct
{
char a;
int b;
int c;
}test;
void main(void)
{
printf("offset: %ld %ld\r\n", offsetof(test, b), offsetof(test, c));
}
输出:
offset: 4 8
container_of
C语言中有这样一个宏container_of,它的作用是通过结构体的成员,结构体成员的地址以及结构体的类型来获取结构体的首地址,原型如下:
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
参数说明:
- ptr:结构体成员的地址
- type:结构体类型
- member:结构体成员
原理分析:
结构体首地址 = 成员地址 - 成员偏移,成员偏移通过offsetof宏求出;
示例:
#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
{
char a;
int b;
float c;
}test_t;
void main(void)
{
test_t test, *p_test;
int *container_test = &(test.b);
p_test = container_of(container_test, test_t, b);
if(&test == p_test)
{
printf("Addr is samed\r\n");
}
}
输出:
Addr is samed