list.h linux内核链表分析

list.h 头文件中定义了一些Linux内核链表操作相关的函数,自己在学习数据结构时研究了一下里面的程序,里面的代码确实是简洁又高效。

Linux内核链表概述

在传统链表程序中,结构体数据中必须要有一个自身结构体类型的指针,用于指向下一个结构体。一个程序中可能要用到多个链表,不同链表用的结构体不一定相同,因此不得不为每个种结构体定义一套链表操作。而Linux内核链表把链表指针域的相关操作单独抽离出来,封装成一套接口,每一种链表都可使用这个接口。如果需要构造某种类型的链表时,只要将 struct list_head 类型成员加入到结构体中,用 struct list_head 类型成员将各个结构体连接起来,形成一个双向循环链表。使用的时候只需要调用list.h中相关函数,不用自己再为每种链表单独定义一套函数。
linux内核链表

链表结构体

struct list_head
{
    struct list_head *next;
    struct list_head *prev;
};

看得出来这是一个双向链表,里面只有两个指针,没有其他任何数据项,其他操作函数也只针对这两个指针。这就注定了list.h中的链表操作可以嵌入到其他任意类型结构体中,具有很强的通用性和灵活性。

定义链表头

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)

LIST_HEAD 宏用于定义并初始化链表头。 LIST_HEAD 展开后形式为

struct list_head name = {&name, &name}

可见LIST_HEAD定义链表头的时候把前向指针和后向指针都初始化为自身。这时候链表是一个循环双向空链表。
另外,也可以使用 INIT_LIST_HEAD 函数初始化链表头:

static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

这个链表头是一个独立的 struct list_head 结构体类型数据,而其他的结点,是用户定义的结构体类型中 struct list_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);
}

可见无论是在头部加入结点还是在尾部加入结点,都只要一步就搞定,无需遍历整个链表,这就是双向循环链表的好处。

删除结点

内核中删除链表代码如下

/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}

/**
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 * Note: list_empty() on entry does not return true after this, the entry is
 * in an undefined state.
 */
#ifndef CONFIG_DEBUG_LIST
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;
}
#else
extern void __list_del_entry(struct list_head *entry);
extern void list_del(struct list_head *entry);
#endif

用 list_del 函数删除链表结点,首先用 __list_del 函数将结点从链表中移除,再把结点的前向指针指向和后向指针分别指向 LIST_POISON1 和 LIST_POISON2。 LIST_POISON1 和 LIST_POISON2 不是NULL,但引用到这两个地址是会引起页错误。我们在调试出错代码的时候经常会返回指针的值,我们很多时候出错都是指针为NULL造成的,这时候我们如果发现是值是 LIST_POISON1 或 LIST_POISON2 的话,那我们马上可以将注意力转移到链表上来了,并且是因为误用了一个已经从链表中删除掉的节点。

链表遍历

/**
 * 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)

源码中将链表的遍历操作直接定义成宏,使用起来非常方便。但是如果在遍历链表过程中,有对链表进行插入、删除结点的操作,pos指针指向的内容被改变,就不能使用这种方法遍历了,必须使用一种更安全的方法。

/**
 * 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_for_each_save 又使用了另一个变量n,保存pos下一个结点的指针,这事及时pos被改变,也不会影响链表的遍历操作。
这些方法在链表遍历中得到的只是结构体中struct list_head类型成员的地址,而不是整个结构体的地址,要得到结构体的地址,要使用另一个宏定义。

/**
 * 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 宏 分析

有了创建链表,添加结点,删除结点,遍历链表这些函数,就实现链表的基本操作了。lish.h 中还有很多高级及操作,比如链表结点移动,链表合并等,这里就不一一介绍了。

流云非晚

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值