Linux源码-通用双向链表的实现

我们之前在数据结构课程中学习的链表都是将数据放在链表节点中,然后将这些节点串成一个链表。例如:

c 复制代码
typedef struct Node {
    Node* prev;
    Node* next;
    
    T data_obj;
};

void addHead(Node* head, T data_obj);
......

这种实现方案直观容易理解,但是对于C语言这种非泛型的语言来说,需要为每个需要使用链表的数据结构都定义链表节点和一套链表操作,有没有更优雅的实现方案?

有的兄弟!Linux使用C语言开发,并且其各个模块中大量使用了链表。Linux的解决方案可以总结为一句话:"不将数据对象放在链表节点中,而是将链表节点存储在数据对象中"。

例如include/linux/mm.h中的mem_map_t数据结构:

c 复制代码
typedef struct page {
    struct list_head list;
......
    struct list_head lru;
......
} mem_map_t;

其中使用了两个链表,Linux并不是定义了两种链表节点同时管理该数据结构,而是在该数据结构中插入了两个链表节点。

关键问题:根据宿主结构体找list_head容易,但是如何根据list_head找到宿主结构体?

list_head的实现在include/linux/list.h中:

c 复制代码
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;
}

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

其中链表的初始化和链表操作都很简单。重点关注如何根据链表节点指针,找到其宿主结构体的首地址,实现方法在宏函数list_entry()中:

c 复制代码
#define list_entry(ptr, type, member) \
    ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

参数解释:

  • ptr,list_head节点的地址
  • type,宿主结构体的类型,例如上例中的struct page
  • member,ptr指向的list_head在宿主结构体中的字段名,例如上例中的lru

其中(&((type *)0)->member)))表示的是假设将0作为宿主结构体的起始地址,然后取其member的地址(其实就是member字段在宿主结构体中的偏移)。然后使用ptr减去该偏移,就得到了宿主结构体的首地址。

相关推荐
未济3 天前
linux 配置环境变量
linux
傲世仙尊3 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
_艾伦 耶格尔.3 天前
进程间通信
linux
Liuqy-053 天前
Linux IO编程——静态库、动态库
linux
彧azz4 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅4 天前
linux(8) 软硬链接
linux·运维·服务器
AIgorithmGEEK4 天前
[Linux]线程三部曲(上):一个执行流的诞生——从操作系统一路拆到 pthread_create
linux·线程·pid
琥珀色糖4 天前
SimlpeHttp
linux·服务器
qeen874 天前
【Linux】操作系统之进程介绍(二)
linux·笔记·学习·进程
Zenova EdgeOS4 天前
Linux ss 工业边缘网络分析实战
linux·python·边缘计算·工业网关