Linux version: 4.14
Code link: Linux source code (v4.14) - Bootlin
一言以蔽之,内核链表就是个带头结点的循环双链表。
普通的链表是将next指针定义成为与该结构体一样的类型,这样做通用性不好。与普通的链表的定义和使用方式不一样,内核的链表定义成为了一种通用的结构:
struct list_head {
struct list_head *next, *prev;
};
在内核链表中,仅仅定义了 next 和 prev 指针,用于寻找链表中的下一个节点和前一个节点(双向链表)。通常内核使用双向循环链表来表示。相关的数据。
在 include/linux/list.h 文件中,定义了相关的链表操作函数;
LIST_HEAD 宏创建一个链表头结点,并用 LIST_HEAD_INIT 宏对头结点进行赋值,使得头结点的前驱和后继指向自己。INIT_LIST_HEAD 函数对链表进行初始化,使得前驱和后继指针指针指向头结点。
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list)
{
WRITE_ONCE(list->next, list);
list->prev = list;
}
__list_add 用于在指定的 prev 和 next 之间插入一个 new 的链表
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;
}
插入前:
插入后:
list_add 是在 __list_add 之上的应用,用于将新结点 new 插入到 head 结点之后
(注意1:head 结点可以是任意结点,不一定是头结点)
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
list_add_tail 是在用于将新结点 new 插入到 head 结点之前(同样, head 结点可以是任意结点)
(注意2:这里如果 head 结点为链表的头结点,则表示将新结点 new 插入到链表的尾部)
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
从链表中删除一个节点,需要改变该节点前驱节点的后继结点和后继结点的前驱节点。最后设置该节点的前驱节点和后继结点指向LIST_POSITION1和LIST_POSITION2两个特殊值,这样设置是为了保证不在链表中的节点项不可访问,对LIST_POSITION1和LIST_POSITION2的访问都将引起页故障
/*
* These are non-NULL pointers that will result in page faults
* under normal circumstances, used to verify that nobody uses
* non-initialized list entries.
*/
#define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
#define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
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);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
从链表中使用新的节点替换一个节点:
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;
}
move将一个节点移动到头部或者尾部,首先先将该节点在以前的链表中删除,然后插入新的链表中:
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
list_is_last 函数判断节点是否为末尾节点,list_empty 判断链表是否为空
/**
* list_is_last - tests whether @list is the last entry in list @head
* @list: the entry to test
* @head: the head of the list
*/
static inline int list_is_last(const struct list_head *list,
const struct list_head *head)
{
return list->next == head;
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
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;
}
/**
* list_splice - join two lists, this is designed for stacks
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}
/**
* list_splice_tail - join two lists, each list being a queue
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head->prev, head);
}
使用 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_entry(pos, head, member)
该函数用于遍历嵌入 list_head 变量的结构体构成的链表,遍历得到的结构体通过 pos 变量返回。
① pos 保存待遍历的的结构体的地址(指针)
② head 参数指结构体 list_head 成员变量的首地址(指针)
③ member 参数表示结构体中 list_head 成员变量的名称 (名称)
#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))
//-------------------------------------------------------------------------------
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
这个是相当重要的一个调用,Linux 的链表结构被嵌入到结构体中,只是链表与链表之间的链接,归根结底,还是需要获取其数据结构,通过这个 list_entry 调用来获取该链表被嵌入的数据结构。
/**
* 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_head within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
其中的 container_of 调用起了主要作用,做到了根据链表来获取其结构体的实现。在接下来的小文章中进行其代码的分析。