Linux内核--链表结构

news2024/9/22 23:20:54

一、前言
    Linux内核链表结构是一种双向循环链表结构,与传统的链表结构不同,Linux内核链表结构仅包含前驱和后继指针,不包含数据域。使用链表结构,仅需在结构体成员中包含list_head*成员就行;链表结构的定义在linux/list.h头文件。

二、链表初始化

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

宏LIST_HEAD_INIT(name)和LIST_HEAD(name)的作用在于初始化一个链表头节点,并使其前驱指针和后继指针指向自身;内联函数INIT_LIST_HEAD同理;
在这里插入图片描述
三、添加节点

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);
}
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}

list_add:在头节点后插入节点,图示如下,node2为新增的节点:
在这里插入图片描述
list_add_tail在头节点前插入节点,图示如下,node2为新增的节点:
在这里插入图片描述
四、删除节点

static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}
static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = LIST_POISON1;
    entry->prev = LIST_POISON2;
}
static inline void list_del_init(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    INIT_LIST_HEAD(entry);
}

list_del:删除链表中的entry节点,entry节点的前驱后继指针指向LIST_POSITION1和LIST_POSITION2两个特殊值,这样设置是为了保证不在链表中的节点项不可访问,对LIST_POSITION1和LIST_POSITION2的访问都将引起页故障。
list_del_init:删除原链表中的entry节点,然后重新初始化entry节点为头节点(使其前驱后继指针都指向自身)。

/*
 * Architectures might want to move the poison pointer offset
 * into some well-recognized area such as 0xdead000000000000,
 * that is also not mappable by user-space exploits:
 */
#ifdef CONFIG_ILLEGAL_POINTER_VALUE
# define POISON_POINTER_DELTA _AC(CONFIG_ILLEGAL_POINTER_VALUE, UL)
#else
# define POISON_POINTER_DELTA 0
#endif
 
/*
 * These are non-NULL pointers that will result in page faults
 * under normal circumstances, used to verify that nobody uses
 * non-initialized list entries.
 */
#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)

链表删除的图示如下:
在这里插入图片描述
五、节点替换

static inline void list_replace(struct list_head *old,
                struct list_head *new)
{
    new->next = old->next;
    new->next->prev = new;
    new->prev = old->prev;
    new->prev->next = new;
}
 
static inline void list_replace_init(struct list_head *old,
                    struct list_head *new)
{
    list_replace(old, new);
    INIT_LIST_HEAD(old);
}

list_replace:将旧节点替换为新节点,函数头两句对应下图2,新节点next指针指向node1,node1节点的prev指针指向新节点。后两句对应图3,新节点prev指针指向head,head节点的next指针指向新节点。此时old节点的next和prev指针指向仍保留着;
list_replace_init:将旧节点替换为新节点,并将旧节点重新初始化为头节点(前驱后继指针指向自身),对应下图4。
在这里插入图片描述
在这里插入图片描述
六、移动节点

static inline void list_move(struct list_head *list, struct list_head *head)
{
    __list_del(list->prev, list->next);
    list_add(list, head);
}
static inline void list_move_tail(struct list_head *list,
                  struct list_head *head)
{
    __list_del(list->prev, list->next);
    list_add_tail(list, head);
}

list_move:将list节点移动至head节点后(对应下图示的node1节点移动);
在这里插入图片描述
list_move_tail:将list节点移动至head节点前(对应下图示的node2节点移动);
在这里插入图片描述

七、尾节点判断

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

链表的最后一个节点特性:其后继指针next必将指向头节点head

八、链表空判断

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

list_empty和list_empty_careful都是判断链表是否为空。list_empty判断节点的后继指针next是否指向自身;list_empty_careful判断节点的后继指针和前驱指针是否均指向自身,其可用来判断链表是否为空且当前是否正在被修改。

九、链表旋转

static inline void list_rotate_left(struct list_head *head)
{
    struct list_head *first;
 
    if (!list_empty(head)) {
        first = head->next;
        list_move_tail(first, head);
    }
}

list_rotate_left:链表节点向左移动,原先左边的节点向右移。相当于与前一节点互换位置。图示如下:
在这里插入图片描述
十、判断链表是否仅含单个节点

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

判断条件为链表不为空,且头指针的前驱和后继均指向同个节点

十一、合并链表

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);
}
 
/**
 * list_splice_tail - join two lists, each list being a queue
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 */
static inline void list_splice_tail(struct list_head *list,
                struct list_head *head)
{
    if (!list_empty(list))
        __list_splice(list, head->prev, head);
}
 
/**
 * list_splice_init - join two lists and reinitialise the emptied list.
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 *
 * The list at @list is reinitialised
 */
static inline void list_splice_init(struct list_head *list,
                    struct list_head *head)
{
    if (!list_empty(list)) {
        __list_splice(list, head, head->next);
        INIT_LIST_HEAD(list);
    }
}
 
/**
 * list_splice_tail_init - join two lists and reinitialise the emptied list
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 *
 * Each of the lists is a queue.
 * The list at @list is reinitialised
 */
static inline void list_splice_tail_init(struct list_head *list,
                     struct list_head *head)
{
    if (!list_empty(list)) {
        __list_splice(list, head->prev, head);
        INIT_LIST_HEAD(list);
    }
}

链表初始状态:
在这里插入图片描述
first->prev = prev;

prev->next = first;

这里prev即head节点

在这里插入图片描述
last->next = next;

next->prev = last;

这里next即node1节点
在这里插入图片描述
INIT_LIST_HEAD(list);

最后一步,把list节点重新初始化为头节点,使其前驱后继指针指向自身。
在这里插入图片描述
上述图示描述了list_splice_init的链表合并过程,函数的作用是把list链表(除list节点自身)插入到head节点后(即head和head->next之间),并重新初始化list节点;

list_splice_tail_init则是与list_splice_init的区别仅是插入的位置不同,其是插入到head节点之前(即head->prev和head之间)。

linux中定义了很多优美的宏,值得我们深入学习。如下:
一、container_of和offsetof

首先介绍两个很好用的宏container_of和offsetof。offsetof宏用于计算结构体成员基于结构体首地址的偏移量,container_of宏用于获取结构体首地址(根据成员指针)。

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

offsetof宏接受两个入参,分别为结构体类型和结构体成员名,该宏将0强制转换成结构体类型的指针,并取其成员的地址。结构体首地址为0,对应成员的地址即成员相对结构体首地址的偏移量。

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

container_of宏接受三个入参,指向结构体成员的指针ptr,结构体类型type,结构体成员名member。该宏首先定义一个结构体成员类型的指针_mptr,类型的获取通过typeof,_mptr = ptr,并将_mptr强转为char*型,减去offsetof计算的偏移量,即得到结构体首地址。

二、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)

list_entry即根据结构体成员指针ptr取得结构体首地址,如下例子使用:

/** 结构体定义 **/
struct student{
    int id;
    char name[20];
    list_head node;
}struct student stu;
char* ptr = &stu.node;
 
/** 宏使用如下 **/
struct student* s = list_entry(ptr, struct student, node);

三、list_first_entry

/**
 * list_first_entry - get the first element from a list
 * @ptr:    the list head to take the element from.
 * @type:    the type of the struct this is embedded in.
 * @member:    the name of the list_struct within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define list_first_entry(ptr, type, member) \
    list_entry((ptr)->next, type, member)

ptr为链表头节点指针,type为结构体类型,member为结构体内成员名(结构体的链表成员)。list_first_entry宏取得链表首个节点的结构体首地址(头节点不算在内)。
四、list_for_each

#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)

从链表首节点(不包含头节点)开始往后遍历。

六、list_for_each_prev

#define list_for_each_prev(pos, head) \
	for (pos = (head)->prev; pos != (head); pos = pos->prev)

从链表首节点(不包含头节点)开始往前遍历。
七、list_for_each_safe

/**
 * list_for_each_safe - iterate over a list safe against removal of list entry
 * @pos:    the &struct list_head to use as a loop cursor.
 * @n:        another &struct list_head to use as temporary storage
 * @head:    the head for your list.
 */
#define list_for_each_safe(pos, n, head) \
    for (pos = (head)->next, n = pos->next; pos != (head); \
        pos = n, n = pos->next)

list_for_each的加强版,支持遍历过程的节点删除操作,提高安全性。使用变量n提前保存节点pos的后继,避免遍历过程pos节点删除后,指向错误。

八、list_for_each_prev_safe

/**
 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
 * @pos:    the &struct list_head to use as a loop cursor.
 * @n:        another &struct list_head to use as temporary storage
 * @head:    the head for your list.
 */
#define list_for_each_prev_safe(pos, n, head) \
	for (pos = (head)->prev, n = pos->prev; \
	     pos != (head); \
	     pos = n, n = pos->prev)

list_for_each_prev的加强版,支持遍历过程的节点删除操作。

九、list_for_each_entry

/**
 * 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_head within the struct.
 */
#define list_for_each_entry(pos, head, member)				\
	for (pos = list_first_entry(head, typeof(*pos), member);	\
	     &pos->member != (head);					\
	     pos = list_next_entry(pos, member))
 
/**
 * list_for_each_entry_reverse - iterate backwards 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_reverse(pos, head, member)            \
    for (pos = list_entry((head)->prev, typeof(*pos), member);    \
         prefetch(pos->member.prev), &pos->member != (head);     \
         pos = list_entry(pos->member.prev, typeof(*pos), member))

list_for_each_entry:从链表首节点(不包含头节点)开始往后遍历,pos指向的是结构体,而不是结构体内的链表节点成员。与list_for_each不同,list_for_each遍历的是链表节点,而list_for_each_entry遍历的是由链表节点串起来的结构体链表。

list_for_each_entry_reverse:与list_for_each_entry相反,是往前遍历。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/57605.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ABAP学习笔记之——第八章:报表程序

一、程序属性 创建程序类型: 状态: 根据程序状态不能使用特定 Utility。例如,选择系统程序,则不能使用 debug 功能 权限组: 分配程序执行/修改相关的权限组。若是安全相关程序有必要设置权限组。 逻辑数据库&…

C/C++中的内存管理

目录 C/C内存分布 C语言中动态内存管理方式 malloc/calloc/realloc和free C内存管理方式 new/delete操作内置类型 new/delete操作自定义类型 operator new 与 operator delete new/delete实现原理 内置类型 自定义类型 定位new表达式(placement-new&…

[附源码]Python计算机毕业设计Django路政管理信息系统

项目运行 环境配置: Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术: django python Vue 等等组成,B/S模式 pychram管理等等。 环境需要 1.运行环境:最好是python3.7.7,…

MySQL 从入门到实战讲解,京东 T5 大咖学习笔记分享,看完我哭了

数据库是一个综合系统,其背后是发展了几十年的数据库理论。也许你会觉得数据库并不难,因为你可以熟练地写出 SQL,也可以在各个客户端里玩得游刃有余。但就以最常见的 MySQL 为例,作为程员,你在使用 MySQL 的过程中&…

「Redis」04 发布和订阅

笔记整理自【尚硅谷】Redis 6 入门到精通 超详细 教程 Redis——发布和订阅 1. 什么是发布和订阅 Redis 发布订阅( pub/sub )是一种消息通信模式:发送者( pub )发送消息,订阅者( sub &#xf…

[附源码]Python计算机毕业设计Django环境保护宣传网站

项目运行 环境配置: Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术: django python Vue 等等组成,B/S模式 pychram管理等等。 环境需要 1.运行环境:最好是python3.7.7,…

matlab使用移动平均滤波器、重采样和Hampel过滤器进行信号平滑处理

此示例显示如何使用移动平均滤波器和重采样来隔离每小时温度读数的时间周期分量的影响,以及从开环电压测量中消除不需要的线路噪声。 最近我们被客户要求撰写关于信号平滑处理的研究报告,包括一些图形和统计输出。 该示例还显示了如何使用Hampel过滤器…

新时期我国信息技术产业的发展【技术论文,纪念长者,2008】

2008年10月,江泽民在《上海交通大学学报》发表了一篇题为《新时期我国信息技术产业的发展》的论文。作为上海交通大学1947届电机工程系的毕业生,发表这篇论文时,这位曾改变中国的长者已是82岁高龄。在这篇论文中,江泽民提出了“未…

URLDNS链

听说这个链子是最简单的链子之一了,但是却是来来回回看了好多遍才勉强看明白。 在 ysoserial 中我们可以看见链子是这样的: *Gadget Chain: * HashMap.readObject() * HashMap.putVal() * HashMap.hash() * URL.hashCode() 简单流程: 1.Hash…

HTML这一篇就够啦~

HTML这一篇就够啦HTML1、基础认知2、排版标签2.1 标题标签2.2 段落标签2.3 换行标签2.4 水平线标签3、文本格式化标签4、媒体标签4.1 图片标签4.2 路径4.3 音频文件4.4 视频文件5、链接标签6、列表标签、6.1 无序列表(最常用)6.2 有序列表(偶…

2021.06青少年软件编程(Python)等级考试试卷(三级)

2021.06青少年软件编程(Python)等级考试试卷(三级) 一、单选题(共25题,每题2分,共50分) 1.关于open()函数的参数,下列描述正确的是?( D ) A. "w+" 以十六进制格式打开一个文件只用于写入 B. "r+"打开一个文件用于读写。文件指针将会放在文件…

ZMQ之自杀的蜗牛模式和黑箱模式

一、检测慢订阅者(自杀的蜗牛模式) 在使用发布-订阅模式的时候,最常见的问题之一是如何处理响应较慢的订阅者。理想状况下,发布者能以全速发送消息给订阅者,但现实中,订阅者会需要对消息做较长时间的…

springboot如何增加 application.yml配置文件

新建springboot 项目,默认项目的配置文件为application.properties。 需要将application.properties 修改为application.yml配置文件。 注意: 我发现直接将application.properties文件重命名为application.yml。 新的application.yml没有配置功能的属…

Compose 动画艺术探索之属性动画

本篇文章是此专栏的第三篇文章,如果想阅读前两篇文章的话请点击下方链接: Compose 动画艺术探索之瞅下 Compose 的动画Compose 动画艺术探索之可见性动画 Compose的属性动画 属性动画是通过不断地修改值来实现的,而初始值和结束值之间的过…

Java项目:ssm实验室设备管理系统

作者主页:源码空间站2022 简介:Java领域优质创作者、Java项目、学习资料、技术互助 文末获取源码 项目介绍 ssm实验室设备管理系统。前台jsplayuieasyui等框架渲染数据、后台java语言搭配ssm(spring、springmvc、mybatis、maven) 数据库mysql5.7、8.0版…

java - 数据结构,双向链表 - LinkedList

一、双向链表 (不带头) 无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表 双向链表 和 单向链表的区别,就在于 双向 比 单向 多个 一个前驱地址。而且 你会发现 正因为有了前驱地址,所以所…

centos 安装和卸载 webmin

在centos里安装webmin 选择安装最新版本的安装包 官方下载路径可以查看下载版本http://download.webmin.com/download/yum/ wget http://download.webmin.com/download/yum/webmin-2.010-1.noarch.rpm如果安装提示 错误: 无法验证 prdownloads.sourceforge.net 的由 “/CUS…

15年架构师:再有面试官问你Kafka,就拿这篇学习笔记怼他

写在前面 Kafka是一个高度可扩展的消息系统,它在LinkedIn的中央数据库管理中扮演着十分重要的角色,因其可水平扩展和高吞吐率而被广泛使用,现在已经被多家不同类型的公司作为多种类型的数据管道和消息系统。 kafka的外在表现很像消息系统&a…

【图像分割】基于PCA结合模糊聚类算法FCM实现SAR图像分割附matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。 🍎个人主页:Matlab科研工作室 🍊个人信条:格物致知。 更多Matlab仿真内容点击👇 智能优化算法 …

[附源码]计算机毕业设计疫情网课管理系统Springboot程序

项目运行 环境配置: Jdk1.8 Tomcat7.0 Mysql HBuilderX(Webstorm也行) Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。 项目技术: SSM mybatis Maven Vue 等等组成,B/S模式 M…