热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Linux内核链表的浅析和模拟

linux内核的链表设计非常独特。和通常的把数据结构装入链表不同,linux反其道而行之,把链表节点装入数据结构。这样的做法很好地实现了对数据的封装。并且为所有的链表操作提供了统一

linux内核的链表设计非常独特。和通常的把数据结构装入链表不同,linux反其道而行之,把链表节点装入数据结构。这样的做法很好地实现了对数据的封装。并且为所有的链表操作提供了统一的接口。简单而高效。关于链表所有操作的函数都在/linux/list.h文件里

PS:由于list.h文件没有署名的注释,民间猜测内核的链表机制很有可能就是Linux Torvalds本人的作品

内核中的链表通常是一个双向循环链表,节点定义如下(linux/types.h):

struct list_head {
struct list_head *next, *prev;
};换句话说,这个节点就如同一个连接件,可以嵌入到任何数据结构中从而形成链表。当然它也可以独立存在作为一个链表的头。

这种设计方式让链表灵活了很多,比如可以在一个数据结构中插入两个list_head,使该结构同时存在于两个双链队列中,比如:内核中用于内存页面管理的page结构(mm.h)

typedef struct page{
struct list_head list;
struct list_head lru;
......
}甚至用这种方式,可以把不同类型的结构串到一个链表里,在后面的测试程序中,我就演示了这样的例子。

这种设计方式还有一个最大的好处,内核中对链表中的所有操作(插入,删除,合并,遍历)都以list_head结构为参数,实现了统一的操作接口。


另一个问题随之而来,如何访问到每个节点的数据呢。且看linux精巧的设计:

由于在C中,一个结构中的变量偏移在编译时就被ABI固定下来,利用这一点,内核中的链表就可以通过list_head找到节点的入口,从而访问到节点中各个元素。

通过list.h中的宏list_entry实现:

/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) container_of(ptr, type, member)

container_of 定义在kernel.h中

/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})注意:将__mptr转为char类型指针是为了在对其运算时,加减的单位为"1"。

offsetof定义在stddef.h中

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

返回成员MEMBER在结构TYPE中的偏移

简要解释下:

offsetof中,把0强制转换为结构的初始地址,然后对其成员取地址,所得的值即为该成员相对于结构入口的偏移。size_t是unsigned int类型,转换是为了便于后续计算。

container_of中,typeof是c语言关键字的一个新扩展,返回参数类型。该宏首先将ptr赋值给member类型的指针_mptr,然后用_mptr减去偏移,所得即为该节点的入口地址。


各种链表的操作接口,在list.h中定义,下面简要地罗列:

1、初始化链表:

linux没有链表头,使第一个节点的指针指向自己即完成初始化。

两种方式:1)

#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)2)

#define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } while (0)相应地,链表判空,就看头的next是否指向自己:

static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}

2、添加节点:

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

头尾添加,分别如下调用即可:

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

3、删除节点

static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}

4、搬移节点,把属于一个链表的节点移动到另一个链表

static inline void list_move(struct list_head *list, struct list_head *head);

5 还有一些其他操作,比如合并两个链表:

static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
/**
* list_splice - join two lists, this is designed for stacks
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}6 遍历,通过宏实现:

/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next)


* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member) for (pos = list_entry((head)->next, typeof(*pos), member); &pos->member != (head); pos = list_entry(pos->member.next, typeof(*pos), member))

自己写了一个小程序作为本文结束,运用linux链表的设计思想,在同一链表中链接两种不同数据结构并遍历之。

#include
#include
#define get_entry(ptr, structure, member) ((structure *)((char *)ptr - (unsigned int)&(((structure *)0) -> member)))
#define init_list_head(p) do { (p) -> prev = (p); (p) -> next = (p); }while(0)
typedef struct link_pointer_tag
{
struct link_pointer_tag *prev;
struct link_pointer_tag *next;
}link_pointer;
typedef struct
{
int element1;
link_pointer p;
}link_node1;
typedef struct
{
char element2;
link_pointer p;
}link_node2;
link_pointer* head;
link_pointer* create_node1()
{
int ele;
link_node1* tmp_node1;
if ((tmp_node1 = (link_node1*)malloc(sizeof(link_node1))) == NULL)
{
printf("Not enough memery!\n");
exit(0);
}
printf("Input the element of node: element1(int)\n");
scanf("%d", &ele);
tmp_node1 -> element1 = ele;
return &(tmp_node1 -> p);
}
link_pointer* create_node2()
{
char ele;
link_node2* tmp_node2;
if ((tmp_node2 = (link_node2*)malloc(sizeof(link_node2))) == NULL)
{
printf("Not enough memery!\n");
exit(0);
}
printf("Input the element of node: element2(char)\n");
scanf("%c", &ele);
tmp_node2 -> element2 = ele;
return &(tmp_node2 -> p);
}
void add_node(link_pointer* ptr, link_pointer* pre, link_pointer* next)
{
pre -> next = ptr;
ptr -> prev = pre;
ptr -> next = next;
next -> prev = ptr;
}
void create_list()
{
int lenth, i;
head = NULL;
printf("Input lenth:");
scanf("%d", &lenth);
for (i = 1; i <= lenth; i++)
{
setbuf(stdin,NULL);
printf("The number of node %d:\n", i);
if(i % 2 != 0)
{
if(i == 1)
{
head = create_node1();
init_list_head(head);
}
else
{
add_node(create_node1(), head -> prev, head);
}
}
else
{
add_node(create_node2(), head -> prev, head);
}
}
}
void show_all_node()
{
link_pointer* tmp = head;
int count = 1;
do
{
printf("The %dth node: ", count);
if (count % 2 != 0)
printf("%d\n", get_entry(tmp, link_node1, p) -> element1);
if (count % 2 == 0)
printf("%c\n", get_entry(tmp, link_node2, p) -> element2);
count++;
} while ((tmp = tmp -> next) != head);
}
int main()
{
create_list();
show_all_node();
return 0;
}

Linux内核链表的浅析和模拟,布布扣,bubuko.com


推荐阅读
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
author-avatar
天崖人B
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有