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

版权声明:原创文章 && 转载请著名出处 https://blog.csdn.net/zhoutaopower/article/details/86480124

内核链表定义


普通的链表是将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 之上的应用,用于在指定的循环链表节点和其下一个节点插入新节点

static inline void list_add(struct list_head *new, struct list_head *head)
{
	__list_add(new, head, head->next);
}

list_add_tail 是在用于在指定的循环链表节点和其前一个节点插入新节点(尾部)

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)

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 调用起了主要作用,做到了根据链表来获取其结构体的实现。在接下来的小文章中进行其代码的分析。

猜你喜欢

转载自blog.csdn.net/zhoutaopower/article/details/86480124
今日推荐