Linux内核数据结构 —— 内核链表

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 文件中,定义了相关的链表操作函数;

链表操作

1 初始化链表 

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;
}

2 插入节点

__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);
}

3 删除节点

从链表中删除一个节点,需要改变该节点前驱节点的后继结点和后继结点的前驱节点。最后设置该节点的前驱节点和后继结点指向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;
}

4 替换结点

从链表中使用新的节点替换一个节点:

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;
}

5 移动结点 

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);
}

6 链表判断

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;
}

7 链表拼接

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);
}

8 链表的遍历

使用 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)

9 获取被该链表嵌入的结构体的指针 

这个是相当重要的一个调用,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 调用起了主要作用,做到了根据链表来获取其结构体的实现。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值