前面我们说过如何用C实现通用类型的链表,比如void*的指针,零长数组等。可是小菜鸟毕竟赶不上大师,还是Linux内核巧妙啊,这里面的链表,才是链表中的“奇葩”。
源码的路径是include/linux/list.h
我们先找几个简单的读一读吧。
struct list_head { struct list_head *next, *prev; };这个是在include/linux/types.h中定义的。虽然名字叫list_head,让人感觉是头节点,其实这就是通用的节点,和头一点关系都没有。你也许会奇怪,怎么没有数据域?是的,就是没有数据域。那数据怎么办?后面你就知道了,这个节点是内嵌在用户数据中的。
static inline void INIT_LIST_HEAD(struct list_head *list) { list->next = list; list->prev = list; }
节点的初始化函数,自己指向自己。
#define LIST_HEAD_INIT(name) { &(name), &(name) } #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)定义头结点并且初始化。
static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next) { next->prev = new; new->next = next; new->prev = prev; prev->next = new; }在两个节点中间插入一个新节点。
static inline void list_add(struct list_head *new, struct list_head *head) { __list_add(new, head, head->next); }假如head是头结点的话,这个就是头插了。
static inline void list_add_tail(struct list_head *new, struct list_head *head) { __list_add(new, head->prev, head); }head->prev就是最后一个节点了,这个节点的后面那个就是head,所以这个函数是尾插。
static inline void __list_del(struct list_head * prev, struct list_head * next) { next->prev = prev; prev->next = next; }删除掉一个节点(必须知道这个节点的前驱和后继)
static inline void list_del(struct list_head *entry) { __list_del(entry->prev, entry->next); }删除某个节点。
static inline int list_empty(struct list_head *head) { return head->next == head; }判断链表是不是为空,这里的head是头结点。
/** * 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_safe - iterate over a list safe against removal of list entry * @pos: the &struct list_head to use as a loop cursor. * @n: another &struct list_head to use as temporary storage * @head: the head for your list. */ #define list_for_each_safe(pos, n, head) for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next)安全遍历,看注释就明白,这个在遍历的过程中删除节点也是可以用的,因为当前节点的下一个已经保存了哦。
/** * list_entry - get the struct for this entry * @ptr: the &struct list_head pointer. * @type: the type of the struct this is embedded in. * @member: the name of the list_struct within the struct. */ #define list_entry(ptr, type, member) container_of(ptr, type, member)关于container_of宏,我在另一篇博文中已经解释了,这里不再赘述。其实这个宏就是给container_of换了个马甲,叫
list_entry, ptr是指向list_head 的指针(简称小指针),type是结构体的类型,比如struct XXXX, member 是list_head 在结构体里的名字。最后就得到了指向struct XXXX的指针(简称大指针)。我们把这个宏叫做“小指针转大指针”。
/** * list_first_entry - get the first element from a list * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the list_struct within the struct. * * Note, that list is expected to be not empty. */ #define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member)ptr在这里表示头指针,这个宏得到了第一个结构体的地址(大指针)。
/** * list_next_entry - get the next element in list * @pos: the type * to cursor * @member: the name of the list_struct within the struct. */ #define list_next_entry(pos, member) list_entry((pos)->member.next, typeof(*(pos)), member)pos在这里是一个结构体的指针(大指针),这个宏得到了下一个结构体的地址。
/** * list_for_each_entry - iterate over list of given type * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_struct within the struct. */ #define list_for_each_entry(pos, head, member) for (pos = list_first_entry(head, typeof(*pos), member); &pos->member != (head); pos = list_next_entry(pos, member))遍历链表中的每个大指针。
pos:大指针,head:头结点,member:list_head 在结构体中的名字
基本的就说到这里,下面我们写个例子。
#include <stdio.h> #include "list.h" struct person { char name[12]; struct list_head list; char sex; unsigned char age; }; int main() { struct person persons[] = { {"Marry", NULL,NULL,'f',20}, {"Mike",NULL,NULL,'m',23}, {"Leslie",NULL,NULL,'m', 36}, {"Ann",NULL,NULL,'f',27} }; LIST_HEAD(head);//头结点 int i; for (i = 0; i < sizeof(persons)/sizeof(persons[0]); ++i) { list_add(&persons[i].list, &head); } struct list_head *cur = NULL; struct person *pdata = NULL; list_for_each(cur, &head) { pdata = container_of(cur, struct person, list); printf("%s:%d ", pdata->name, pdata->age); } printf("list_for_each_entry "); list_for_each_entry(pdata, &head, list) { printf("%s:%d ", pdata->name, pdata->age); } return 0; }
运行结果:
Ann:27
Leslie:36
Mike:23
Marry:20
list_for_each_entry
Ann:27
Leslie:36
Mike:23
Marry:20