include/Linux/stddef.h中macro offsetof define,list:
#define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
offsetof macro对于上述示例的展开剂分析:&((struct example_struct *)0)->list表示当结构example_struct正好在地址0上时其成员list的地址,即成员位移.
traverse linked list member element;
下面使用list_entry macro遍历链表得到链表指针,再从链表指针映射回对应结构example_struct的指针,然后,对其成员priority进行操作,函数example_add_entry的功能是给链表加入新的结构成员
void example_add_entry(struct example_struct *new)
{
struct list_head *ptr;
struct example_struct *entry;
//遍历链表
for (ptr = example_list.next;ptr != &example_list;ptr = ptr->next) {
//映射回 对应结构example_struct的指针
entry = list_entry(ptr,struct todo_Struct,list);
}
}