2、双向链表在Linux内核中的实现
Linux内核对双向循环链表的设计非常巧妙,链表的所有运算都基于只有两个指针域的list_head结构体来进行。
/* linux-2.6.38.8/include/linux/types.h */ struct list_head { struct list_head *next, *prev; };
链表的运算(源代码都在linux-2.6.38.8/include/linux/list.h文件中定义,并且假定CONFIG_DEBUG_LIST未定义):
(1)、链表头结点的创建
2.1.1 静态创建
#define LIST_HEAD_INIT(name) { &(name), &(name) } #define LIST_HEAD(name) \ struct list_head name = LIST_HEAD_INIT(name)
通过LIST_HEAD宏创建一个list_head结构体变量name,并把name的所有成员(next和prev)都初始化为name的首地址。
2.1.2 动态创建
static inline void INIT_LIST_HEAD(struct list_head *list) { list->next = list; list->prev = list; }
把list_head结构体变量的首地址传递给INIT_LIST_HEAD函数来对其成员进行初始化。
(2)、结点的添加
list_add函数是把新结点new添加到head结点的后面,而list_add_tail函数是把新结点new插入到结点head的前面。
函数源代码如下:
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); } static inline void list_add_tail(struct list_head *new, struct list_head *head) { __list_add(new, head->prev, head); }
图示如下:
(3)、结点的删除
list_del函数的作用是将结点*entry从链表中移走,并把此结点的两个成员分别初始化为LIST_POISON1和LIST_POISON2。注意,这里的*entry结点所占用的内存并没有被释放。
list_del_init函数的作用也是将结点*entry从链表中移走,但它把此结点的两个成员初始化为entry。
static inline void __list_del(struct list_head * prev, struct list_head * next) { next->prev = prev; prev->next = next; } static inline void __list_del_entry(struct list_head *entry) { __list_del(entry->prev, entry->next); } static inline void list_del(struct list_head *entry) { __list_del(entry->prev, entry->next); entry->next = LIST_POISON1; entry->prev = LIST_POISON2; } static inline void list_del_init(struct list_head *entry) { __list_del_entry(entry); INIT_LIST_HEAD(entry); }
LIST_POISON1和LIST_POISON2的值定义在linux-2.6.38.8/include/linux/poison.h文件中:
#define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA) #define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
其中POISON_POINTER_DELTA的值在CONFIG_ILLEGAL_POINTER_VALUE未配置时为0。
(4)、结点的替换
list_replace函数的作用是用结点*new替换掉结点*old,list_replace_init函数的作用与list_replace相同,除了它还会把*old结点的两个成员初始化为old外。
static inline void list_replace(struct list_head *old, struct list_head *new) { new->next = old->next; new->next->prev = new; new->prev = old->prev; new->prev->next = new; } static inline void list_replace_init(struct list_head *old, struct list_head *new) { list_replace(old, new); INIT_LIST_HEAD(old); }
(5)、结点的移动
list_move函数的作用是把*list结点从它所在的链表中移除,然后把它添加到*head结点的后面。list_move_tail函数的作用与list_move相同,但它把*list插入到*head结点的前面。
static inline void list_move(struct list_head *list, struct list_head *head) { __list_del_entry(list); list_add(list, head); } static inline void list_move_tail(struct list_head *list, struct list_head *head) { __list_del_entry(list); list_add_tail(list, head); }
(6)、判断*list是否是链表head的最后一个结点,是则返回1,否则返回0
static inline int list_is_last(const struct list_head *list, const struct list_head *head) { return list->next == head; }
(7)、判断head是否为空表,是则返回1,否则返回0
static inline int list_empty(const struct list_head *head) { return head->next == head; } static inline int list_empty_careful(const struct list_head *head) { struct list_head *next = head->next; return (next == head) && (next == head->prev); }
(8)、翻转链表
static inline void list_rotate_left(struct list_head *head) { struct list_head *first; if (!list_empty(head)) { first = head->next; list_move_tail(first, head); } }
(9)、判断链表是否只有一个结点,是则返回1,否则返回0
static inline int list_is_singular(const struct list_head *head) { return !list_empty(head) && (head->next == head->prev); }
(10)、切割链表
list_cut_position函数的功能是将链表head从头结点head(不包含)开始到entry(包含,并且它是链表head中的结点)结点结束之间的所有结点都切割下来,并添加到list上,以组成一个新的链表list。原来的head链表将组成一个新的小链表。
static inline void __list_cut_position(struct list_head *list, struct list_head *head, struct list_head *entry) { struct list_head *new_first = entry->next; list->next = head->next; list->next->prev = list; list->prev = entry; entry->next = list; head->next = new_first; new_first->prev = head; } static inline void list_cut_position(struct list_head *list, struct list_head *head, struct list_head *entry) { if (list_empty(head)) return; if (list_is_singular(head) && (head->next != entry && head != entry)) return; if (entry == head) INIT_LIST_HEAD(list); else __list_cut_position(list, head, entry); }
(11)、合并链表
list_splice函数的作用是将链表list(不包含结点*list)插入到链表head的head结点后,而list_splice_tail函数的作用是将链表list(不包含结点*list)插入到链表head的head结点前。
list_splice_init和list_splice_tail_init函数的作用与其相应的函数相同,除了它们都初始化*list结点为list。
static inline void __list_splice(const struct list_head *list, struct list_head *prev, struct list_head *next) { struct list_head *first = list->next; struct list_head *last = list->prev; first->prev = prev; prev->next = first; last->next = next; next->prev = last; } static inline void list_splice(const struct list_head *list, struct list_head *head) { if (!list_empty(list)) __list_splice(list, head, head->next); } static inline void list_splice_tail(struct list_head *list, struct list_head *head) { if (!list_empty(list)) __list_splice(list, head->prev, head); } static inline void list_splice_init(struct list_head *list, struct list_head *head) { if (!list_empty(list)) { __list_splice(list, head, head->next); INIT_LIST_HEAD(list); } } static inline void list_splice_tail_init(struct list_head *list, struct list_head *head) { if (!list_empty(list)) { __list_splice(list, head->prev, head); INIT_LIST_HEAD(list); } }
(12)、通过成员指针获得整个结构体的指针
链表操作如果仅仅针对list_head结构体就没有什么意义,所以必须要获得包含它的整个结构体的地址。它们只是对container_of宏的封装,关于container_of宏的使用方法请参考http://blog.csdn.net/npy_lp/article/details/7010752。
#define list_entry(ptr, type, member) \ container_of(ptr, type, member) #define list_first_entry(ptr, type, member) \ list_entry((ptr)->next, type, member)
(13)、遍历链表
list_for_each函数是根据list_head的next成员来遍历整个链表,而list_for_each_prev函数是根据prev成员。其中参数head一般是链表的头结点。
#define list_for_each(pos, head) \ for (pos = (head)->next; prefetch(pos->next), pos != (head); \ pos = pos->next) #define __list_for_each(pos, head) \ for (pos = (head)->next; pos != (head); pos = pos->next) #define list_for_each_prev(pos, head) \ for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ pos = pos->prev)
list_for_each_safe和list_for_each_prev_safe函数使用list_head结构体变量n作为临时存储变量。
#define list_for_each_safe(pos, n, head) \ for (pos = (head)->next, n = pos->next; pos != (head); \ pos = n, n = pos->next) #define list_for_each_prev_safe(pos, n, head) \ for (pos = (head)->prev, n = pos->prev; \ prefetch(pos->prev), pos != (head); \ pos = n, n = pos->prev)
list_for_each_entry和list_for_each_entry_reverse函数的作用是根据head的下一个或前一个结点来遍历整个head链表,并返回包含list_head结构体成员的大结构体指针,member是list_head结构体在大结构体中的成员名。
#define list_for_each_entry(pos, head, member) \ for (pos = list_entry((head)->next, typeof(*pos), member); \ prefetch(pos->member.next), &pos->member != (head); \ pos = list_entry(pos->member.next, typeof(*pos), member)) #define list_for_each_entry_reverse(pos, head, member) \ for (pos = list_entry((head)->prev, typeof(*pos), member); \ prefetch(pos->member.prev), &pos->member != (head); \ pos = list_entry(pos->member.prev, typeof(*pos), member))
list_for_each_entry_continue和list_for_each_entry_continue_reverse函数是以pos的下一个或前一个结点开始遍历链表head。
#define list_prepare_entry(pos, head, member) \ ((pos) ? : list_entry(head, typeof(*pos), member)) #define list_for_each_entry_continue(pos, head, member) \ for (pos = list_entry(pos->member.next, typeof(*pos), member); \ prefetch(pos->member.next), &pos->member != (head); \ pos = list_entry(pos->member.next, typeof(*pos), member)) #define list_for_each_entry_continue_reverse(pos, head, member) \ for (pos = list_entry(pos->member.prev, typeof(*pos), member); \ prefetch(pos->member.prev), &pos->member != (head); \ pos = list_entry(pos->member.prev, typeof(*pos), member))
list_for_each_entry_from函数以当前结点pos开始遍历。
#define list_for_each_entry_from(pos, head, member) \ for (; prefetch(pos->member.next), &pos->member != (head); \ pos = list_entry(pos->member.next, typeof(*pos), member))
list_for_each_entry_safe、list_for_each_entry_safe_continue、list_for_each_entry_safe_from和list_for_each_entry_safe_reverse这四个函数中的n参数与pos的数据类型相同,其他功能与它们相应的函数是相同的。
#define list_for_each_entry_safe(pos, n, head, member) \ for (pos = list_entry((head)->next, typeof(*pos), member), \ n = list_entry(pos->member.next, typeof(*pos), member); \ &pos->member != (head); \ pos = n, n = list_entry(n->member.next, typeof(*n), member)) #define list_for_each_entry_safe_continue(pos, n, head, member) \ for (pos = list_entry(pos->member.next, typeof(*pos), member), \ n = list_entry(pos->member.next, typeof(*pos), member); \ &pos->member != (head); \ pos = n, n = list_entry(n->member.next, typeof(*n), member)) #define list_for_each_entry_safe_from(pos, n, head, member) \ for (n = list_entry(pos->member.next, typeof(*pos), member); \ &pos->member != (head); \ pos = n, n = list_entry(n->member.next, typeof(*n), member)) #define list_for_each_entry_safe_reverse(pos, n, head, member) \ for (pos = list_entry((head)->prev, typeof(*pos), member), \ n = list_entry(pos->member.prev, typeof(*pos), member); \ &pos->member != (head); \ pos = n, n = list_entry(n->member.prev, typeof(*n), member))
list_safe_reset_next函数的作用是根据结点pos获得n。
#define list_safe_reset_next(pos, n, member) \ n = list_entry(pos->member.next, typeof(*pos), member)