百度了一些文章,关于list_entry的解释都不甚清楚,故google了一下,找到一个解释挺好,故翻译一下
list_entry:
#define list_entry(ptr, type, member)
简单来说,比如有两个数据结构:
struct data {
int something;
}
struct container {
int something_before;
struct data data_item;
int something_after;
}
假设你有一个指针ptr指向data,执行下面语句之后,指针cont_ptr所指向的结构会包含ptr所指向的结构,相当于宏list_entry做了一次指针转换并赋值
struct container *cont_ptr = list_entry(data_ptr, struct container, data_item)
原理:宏list_entry会计算data_item在结构container内的位置,并获取指针data_ptr的内容复制到结构container内相应位置。
list_for_each:
/**
* list_for_each iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
从定义中易知,宏list_for_each就是用于循环遍历,指针从链表head的下一个位置开始遍历,直到遍历到头指针。