前言
曾经写过一篇《Linux内核中链表的使用》, 这篇文章只是参考LKD3e介绍了内核中链表的使用方法,并没详细介绍链表的具体实现和该链表的组织方式,本文将更深入学习内核中的链表相关的知识,并解答上述问题。
传统双向链表和内核中的双向链表的区别
Linux内核中实现的链表,和平时看到的链表不太一样,它不是把具体的数据结构塞入到链表中,而是把链表节点存放到用户的数据结构中。这个设计的主要目的是因为操作链表是相同的,但涉及到具体的数据结构的操作时是不同的,所以内核设计者要把相同的东西抽出来,而具体的数据操作由用户自己去实现。
传统双向链表如下图所示:
- 有个单独的头结点(head)
- 每个节点(node)除了包含必要的数据之外,还有2个指针(pre,next)
- pre指针指向前一个节点(node),next指针指向后一个节点(node)
- 头结点(head)的pre指针指向链表的最后一个节点
- 最后一个节点的next指针指向头结点(head)

传统的链表中每个node中的data1,data2等等都是不确定的(无论是个数还是类型)。linux中的链表巧妙的解决了这个问题,linux的链表不是将用户数据保存在链表节点中,而是将链表节点保存在用户数据中。这样的话,链表的节点将独立于用户数据之外,便于实现链表的共同操作。
如下图所示为内核中双向循环链表的组织形式,它们之前使用pre和next互相连接,但是不是指向数据本身,而是指向结构体中代表节点的统一的结构体成员,一般为struct list_head list。

内核中链表的具体实现
上面说了,内核中的链表实现是把链表节点放到具体的用户数据结构中的,这个链表节点是很关键的,它也是抽象出来共同的东西,实际上很简单,就是带两个指针的结构体变量。除了基本的数据结构外,链表的常用操作还有:初始化,增加,遍历,删除。当然还有一些高级的操作,如链表移动,合并,反向遍历就不一一列举了。
基本数据结构
struct list_head {
struct list_head *next;
struct list_head *prev;
};
初始化
在使用链表时,需要定义一个链表头,方便索引整个链表,由LIST_HEAD()实现,给定一个入参name,即链表头变量,把它定义为struct list_head,再把它的prev和next都指向自己。
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
如初始化一个链表头addr_list
LIST_HEAD(addr_list);
如果已知是一个struct list_head变量,则使用如下函数初始化
void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
链表增加
在进行增加之前,先自定义一个用户的数据结构,随便构造一个,注意的是里面一定要包含struct list_head成员。
struct ipstore{
unsigned long time;
unsigned int addr[4];
struct list_head list;
};
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_tail`
void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
list_add_tail实际也是调用了__list_add,只不过传参的时候反了下。
void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
如向链表中增加一个成员ist1
LIST_HEAD(addr_list);
struct ipstore *ist1;
ist1 = malloc(sizeof(struct ipstore));//测试没有容错
ist1->time = 12;
INIT_LIST_HEAD(&ist1->list);
list_add(&ist1->list, &addr_list);
链表遍历,并取出节点的成员
这里就要用到之前学的list_entry和container_of()了
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
struct list_head *p;
struct ipstore *store;
list_for_each(p, &addr_list)
{
store = list_entry(p, struct ipstore, list);//这里的指针p指向addr_list->next,就是结构体中的struct list_head成员的地址,通过该成员地址、结构体类型和该变量名,取得该结构体成员的首地址,存到store变量中。
printf("%d\n", store->time);
}
具体的链表插入时每个指针的变量,看链表的基本操作即可,其它的操作也没什么特殊的了。
完整代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef offsetof
#define offsetof(type, memb) \
((unsigned long)(&((type *)0)->memb))
#endif
#define max(x, y) ({ \
typeof(x) _max1 = (x); \
typeof(y) _max2 = (y); \
(void) (&_max1 == &_max2); \
_max1 > _max2 ? _max1 : _max2; })
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
struct list_head {
struct list_head *next;
struct list_head *prev;
};
struct ipstore{
unsigned long time;
unsigned int addr[4];
struct list_head list;
};
void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
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;
}
void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
void list_test()
{
LIST_HEAD(addr_list);
struct ipstore *ist1;
ist1 = malloc(sizeof(struct ipstore));//测试没有容错
ist1->time = 12;
INIT_LIST_HEAD(&ist1->list);
list_add(&ist1->list, &addr_list);
struct list_head *p;
struct ipstore *store;
list_for_each(p, &addr_list)
{
store = list_entry(p, struct ipstore, list);
printf("%d\n", store->time);
}
}
int main(int argc, char *argv[])
{
list_test();
return 0;
}

本文深入探讨了Linux内核中的链表实现,与传统双向链表的区别在于,内核链表不直接存储数据,而是将链表节点置于用户数据结构中。详细介绍了链表的基本数据结构、初始化、增加节点以及遍历节点的方法,帮助理解内核链表的高效设计。

5702

被折叠的 条评论
为什么被折叠?



