linux内核链表的实现图解,Linux内核链表深度剖析

math?formula=~~~~~Linux内核链表是Linux内核最经典的数据结构之一,Linux内核链表最大的优点就是节省内存,对链表的各项操作较快,实现的思路耳目一新,而且在Linux内核里频繁使用,比如:内存管理,进程调度等。

math?formula=~~~~~Linux内核链表是一个双向循环链表,核心思想就是把链表结点放在数据结点里面,通过前后指针分别指向前后数据结点里面的链表结点,这样就可以将各个数据结点连接在一起。

一、链表结点定义

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;

}

INIT_LIST_HEAD函数prev指针和next指针指向链表头结点。LIST_HEAD_INIT宏初始化链表头结点next指针和prev指针保存头结点地址,实际上和INI_LIST_HEAD函数初始化效果一样,LIST_HEAD宏就是链表结点的完整初始化。我们以实际代码为例:

struct list_head list;

LIST_HEAD(list); //list = {&list, &list}

三、内核链表插入结点

/*

* Insert a new entry between two known consecutive entries.

*

* This is only for internal list manipulation where we know

* the prev/next entries already!

*/

#ifndef CONFIG_DEBUG_LIST

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;

}

#else

extern void __list_add(struct list_head *new,

struct list_head *prev,

struct list_head *next);

#endif

/**

* list_add - add a new entry

* @new: new entry to be added

* @head: list head to add it after

*

* Insert a new entry after the specified head.

* This is good for implementing stacks.

*/

#ifndef CONFIG_DEBUG_LIST

static inline void list_add(struct list_head *new, struct list_head *head)

{

__list_add(new, head, head->next);

}

#else

extern void list_add(struct list_head *new, struct list_head *head);

#endif

/**

* list_add_tail - add a new entry

* @new: new entry to be added

* @head: list head to add it before

*

* Insert a new entry before the specified head.

* This is useful for implementing queues.

*/

static inline void list_add_tail(struct list_head *new, struct list_head *head)

{

__list_add(new, head->prev, head);

}

math?formula=~~~~我们作图解释会更容易理解内核链表插入操作。

直接看代码我们即可知道内核链表实际上采用的是尾插法,并且有一个头结点。

内核链表插入结点前如图1所示:

897374437fc1

插入结点前.PNG

图1

内核链表插入结点后如图2所示:

897374437fc1

插入结点后.PNG

math?formula=~~~~结合list_add_tail函数分析可知,假设链表list插入结点node,则next=list->next,prev=list;再调用__list_add则可将新结点插入到链表里面。

四、内核链表删除结点

/*

* 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(struct list_head *entry)

{

__list_del(entry->prev, entry->next);

entry->next = LIST_POISON1;

entry->prev = LIST_POISON2;

}

#else

extern void list_del(struct list_head *entry);

#endif

math?formula=~~~~直接分析代码可知,删除结点实际上就是将目标结点后面一个结点的前向指针指向目标结点的前向结点,目标结点的前向结点的后向指针指向目标结点的后向结点,这样就可以把目标结点分离出来了。

结尾

math?formula=~~~~Linux内核链表还有很多有意思的实现,例如遍历,获取数据结点结构体首地址等,下一章再详细分析。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值