一 list_for_each()
遍历head(struct list_head)索引的双向链表,没找到一个节点就将该节点赋给pos(struct list_head)
#define list_for_each(pos, head) /
for (pos = (head)->next; prefetch(pos->next), pos != (head); pos = pos->next)
pos ---+
|
head -->|---------------| | - |---------------| |---------------|
|list_head *next|-+->|list_head *next|--->|list_head *next|
|list_head *prev|<---|list_head *prev|<---|list_head *prev|
|---------------| |---------------| |---------------|
二 list_entry()
#define list_entry(ptr, type, member) ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
我们使用list_entry()宏在linux链表中访问链表数据。
原理为指针ptr指向结构体type中的成员member;通过指针ptr,返回结构体type的起始地址。
定义中((size_t) &(type *)0)->member)意为:把0地址转化为type结构的指针,然后获取该结构中member成员的指针,并将其强制转换为size_t类型