Linux Kernel 基础 - 链表

内核链表


链表作为数据结构的最基本类型,内核在各个模块基本都会使用到。单纯的链表没有任何意义。内核对于链表的使用是将链表内嵌入各种数据结果,从面向对象的角度来看就是用链表将各种对象的实例都通过链表链接起来。那让我们来看看链表在内核的实现

链表的结构体

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

此结构体就是内核链表的定义,从中可以看出内核使用的是双链表。

链表的初始化

#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)
{
    list->next = list;
    list->prev = list;
}

内核提供了以上的宏和inline函数来对链表进行初始化。

链表元素插入

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

前者是将新的元素插入到链表头部,后者是将新的元素插入到链表尾部。

链表元素删除

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

注意当调用完list_del后,会将删除的元素的next指针赋值为LIST_POISON1,将prev赋值为LIST_POISON2。从很多大学的课本上,删除链表元素以后都是将此元素的next 和 prev 指针赋值为 NULL,但是内核不是这样,因为很多时候都是NULL 指针导致一些系统的异常,如果我们把删除的链表元素赋值为特殊的值,这样就能在debug的时候快速发现是链表出现了问题。

链表的遍历

#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_for_each_entry_reverse(pos, head, member)          \
    for (pos = list_last_entry(head, typeof(*pos), member);     \
         &pos->member != (head);                    \
         pos = list_prev_entry(pos, member))

它是通过直接传入结构体,链表头,和结构体成员来遍历的。其实质上跟一般的链表遍历无异,只不过增加了通过链表成员在结构体中的位置来找到需要链表元素。

下面是一个使用链表基本操作的例子。

#define ELEMENT_NUM 20


struct example {
    struct list_head element;
    int data;
} example[ELEMENT_NUM];


static int __init example_init(void)
{
    int i;
    struct example *p;
    LIST_HEAD(list_head);
    INIT_LIST_HEAD(&list_head);

    printk("%s %d\n", __func__, __LINE__);

    p = example;

    for (i = 0; i < ELEMENT_NUM / 2; i++) {
        p->data = i;
        list_add(&p->element, &list_head);
        p++;
    }

    for (i = 10; i < ELEMENT_NUM; i++) {
        p->data = i;
        list_add_tail(&p->element, &list_head);
        p++;
    }

    printk("The example list is shown as below\n");

    list_for_each_entry(p, &list_head, element)
        printk("%d ", p->data);
    printk("\n");

    list_del(&example[5].element);

    printk("After delete item-5\n");
    list_for_each_entry(p, &list_head, element)
        printk("%d ", p->data);
    printk("\n");

    return 0;
}


static void __exit example_exit(void)
{
    printk("%s %d\n", __func__, __LINE__);
}


module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Miracle Huang");

输出结果如下:

[  232.679612] The example list is shown as below
[  232.683502] 9 8 7 6 5 4 3 2 1 0 10 11 12 13 14 15 16 17 18 19 
[  232.691355] After delete item-5
[  232.693901] 9 8 7 6 4 3 2 1 0 10 11 12 13 14 15 16 17 18 19 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值