Linux内核通用链表设计概述
概述
Linux内核内部封装了一套通用的双向链表库,具备优异的扩展性与封装性。该库提供了一个标准的指针域结构体,开发者在使用时,只需在自定义的数据结构中包含此指针域即可。具体的内存链接与维护逻辑由内核接管,用户仅需调用提供的接口函数即可完成操作,无需关注底层实现细节。
传统链表结构的局限性
struct node{
int key;
int val;
node* prev;
node* next;
}
Linux内核通用链表库的核心设计
内核提供的标准指针域结构体定义如下:
struct list_head {
struct list_head *next, *prev;
};
开发者只需在自定义结构体中包含该定义即可:
struct node{
int val;
int key;
struct list_head* head;
}
通过引入list_head结构,实现了数据层与驱动层的彻底分离。内核提供的各类操作接口仅针对list_head结构进行逻辑处理,即在连接节点时只操作list_head,完全不依赖或关心数据层所定义的具体类型。
核心接口宏定义说明
//初始化头指针
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
//遍历链表
#define __list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
//获取节点首地址(不是list_head地址,是数据层节点首地址)
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
//container_of在Linux内核中是一个常用的宏,用于从包含在某个
//结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变
//量中某个成员的首地址进而获得整个结构体变量的首地址
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define offsetof(s,m) (size_t)&(((s *)0)->m)
典型使用方式演示
typedef struct node{
int val;
int key;
struct list_head* list;
}node;
//初始化头指针
LIST_HEAD(head);
//创建节点
node* a = malloc(sizeof(node));
node* b = malloc(sizeof(node));
//插入链表 方式一
list_add(&a->list,&head);
list_add(&b->list,&head);
//插入链表 方式二
list_add_tail(&a->list,&head);
list_add_tail(&b->list,&head);
//遍历链表
struct list_head* p;
struct node* n;
__list_for_each(p,head){
//返回list_head地址,然后再通过list_head地址反推
//节点结构体首地址.
n = list_entry(pos,struct node,list);
}
list_add接口遵循先入后出(LIFO)原则,其行为类似于栈结构。

list_add插入模式示意图
list_add_tail接口遵循先入先出(FIFO)原则,其行为类似于队列。

list_add_tail插入模式示意图
链表节点在内存中的实际分布形态

节点内存布局描述
最终形态表现为:通过指向每个结构体内部的list_head类型指针,将所有节点串联起来形成双向链表。
list_entry接口用于通过结构体成员地址反推结构体首地址。由于__list_for_each等接口通常返回list_head的地址,因此需要利用该成员地址获取所属完整结构体的起始地址,其底层依赖于container_of宏实现。

反推结构体首地址的代码实现
具体应用案例
该案例涵盖了链表的初始化、节点插入、删除及遍历等基本操作。
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
MODULE_AUTHOR("David Xie");
MODULE_DESCRIPTION("List Module");
MODULE_ALIAS("List module");
struct student //代表一个实际节点的结构
{
char name[100];
int num;
struct list_head list; //内核链表里的节点结构
};
struct student *pstudent;
struct student *tmp_student;
struct list_head student_list;
struct list_head *pos;
int mylist_init(void)
{
int i = 0;
//初始化一个链表,其实就是把student_list的prev和next指向自身
INIT_LIST_HEAD(&student_list);
pstudent = kmalloc(sizeof(struct student)*5,GFP_KERNEL);//向内核申请5个student结构空间
memset(pstudent,0,sizeof(struct student)*5); //清空,这两个函数可以由kzalloc单独做到
for(i=0;i<5;i++)
{ //为结构体属性赋值
sprintf(pstudent[i].name,"Student%d",i+1);
pstudent[i].num = i+1;
//加入链表节点,list_add的话是在表头插入,list_add_tail是在表尾插入
list_add( &(pstudent[i].list), &student_list);//参数1是要插入的节点地址,参数2是链表头地址
}
list_for_each(pos,&student_list) //list_for_each用来遍历链表,这是个宏定义
//pos在上面有定义
{
//list_entry用来提取出内核链表节点对应的实际结构节点,即根据struct list_head来提取struct student
//第三个参数list就是student结构定义里的属性list
//list_entry的原理有点复杂,也是linux内核的一个经典实现,这个在上面那篇链接文章里也有讲解
tmp_student = list_entry(pos,struct student,list);
//打印一些信息,以备验证结果
printk("<0>student %d name: %s/n",tmp_student->num,tmp_student->name);
}
return 0;
}
void mylist_exit(void)
{
int i ;
/* 实验:将for换成list_for_each来遍历删除结点,观察要发生的现象,并考虑解决办法 */
for(i=0;i<5;i++)
{
//额,删除节点,只要传个内核链表节点就行了
list_del(&(pstudent[i].list));
}
//释放空间
kfree(pstudent);
}
module_init(mylist_init);
module_exit(mylist_exit);
总结
Linux内核通用链表库还包含众多其他接口,此处未逐一列举。建议有兴趣的读者查阅源码包中的include/linux/list.h文件。阅读此类高质量源代码不仅能提升代码设计能力,还能深入掌握编程技巧及对代码细节的处理方式。
本文内容旨在帮助读者理解Linux内核通用链表机制,希望对相关学习有所帮助。