1. linux内核中的list用法
在linus内核中,list一般这样使用:
struct list_head {struct list_head *priv;struct list_head *next;
}struct xxx {int value; // 结构体的真实成员struct list_head *lh;
}
2. list遍历
结构体xxx里面有struct list_head *lh,同时lh通过priv和next和其他list_head*建立联系,这样就形成了结构体链表。
// 已知ptr是type结构体里面的member,求这个type结构体的地址
#define list_entry(ptr, type, member) container_of(ptr, type, member)// ① 不要修改链表头
// ② cur_struct是传入的、可以被修改的、遍历找到的、成员包含list_head的结构体
#define list_for_each_entry(cur_struct, cur_list_head, member) \for (cur_struct = list_entry(cur_list_head, typeof(cur_struct), member); \&cur_struct->member != cur_list_head; // linux list是一个“双向环状”列表cur_struct = list_entry(cur_struct->member.next, typeof(*cur_struct), member))