内核链表在用户程序中的移植和使用

news2024/10/7 10:18:06

基础知识

struct list_head {
	struct list_head *next, *prev;
};

初始化:

#define LIST_HEAD_INIT(name) { (name)->next = (name); (name)->prev = (name);}

相比于下面这样初始化,前面初始化的好处是,处理链表的时候,不用判空了。太厉害了。

#define LIST_HEAD_INIT(name) { (name)->next = (NULL); (name)->prev = (NULL);}

 遍历链表:

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

添加节点:(表头开始添加)

/*
 * Insert a new entry between two known consecutive entries.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
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 - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}

添加节点:(表尾开始添加) 

/*
 * Insert a new entry between two known consecutive entries.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
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_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 *
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 */
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
	__list_add(new, head->prev, head);
}

删除节点

/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
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);
	LIST_HEAD_INIT(entry);
}

判空:

/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static int list_empty(const struct list_head *head)
{
	return (head->next) == head;
}

基本功能就这些了

测试代码

#include <stdio.h>
#include <stdlib.h>

#define _DEBUG_INFO
#ifdef _DEBUG_INFO
    #define DEBUG_INFO(format,...)	\
        printf("%s:%d -- "format"\n",\
        __func__,__LINE__,##__VA_ARGS__)
#else
    #define DEBUG_INFO(format,...)
#endif

struct list_head {
	struct list_head *next, *prev;
};

struct rcu_private_data{
    struct list_head list;
};

struct my_list_node{
    struct list_head node;
    int number;
};

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

#define LIST_HEAD_INIT(name) { (name)->next = (name); (name)->prev = (name);}

#define offsetof(TYPE, MEMBER)	((size_t)&((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({				\
	void *__mptr = (void *)(ptr);					\
	((type *)(__mptr - offsetof(type, member))); })


/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static int list_empty(const struct list_head *head)
{
	return (head->next) == head;
}

/*
 * Insert a new entry between two known consecutive entries.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
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 - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}

/**
 * list_add_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 *
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 */
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
	__list_add(new, head->prev, head);
}

/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
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);
	LIST_HEAD_INIT(entry);
}

static int list_size(struct rcu_private_data *p){
    struct list_head *pos;
    struct list_head *head = &p->list;
    int count = 0;
    if(list_empty(&p->list)){
        DEBUG_INFO("list is empty");
        return 0;
    }

    list_for_each(pos,head){count++;}
    return count;
}

static int show_list_nodes(struct rcu_private_data *p){
    struct list_head *pos;
    struct list_head *head = &p->list;
    int count = 0;
    struct my_list_node *pnode;

    if(list_empty(&p->list)){
        DEBUG_INFO("list is empty");
        return 0;
    }

    list_for_each(pos,head){
        pnode = (struct my_list_node*)container_of(pos,struct my_list_node,node);
        DEBUG_INFO("pnode->number = %d",pnode->number);
        count++;
    }
    return count;
}

int main(int argc, char **argv){

    int i = 0;
    static struct my_list_node * new[6];
    struct rcu_private_data *p = (struct rcu_private_data*)malloc(sizeof(struct rcu_private_data));
    LIST_HEAD_INIT(&p->list);
    DEBUG_INFO("list_empty(&p->list) = %d",list_empty(&p->list));

    for(i = 0;i < 3;i++){
        new[i] = (struct my_list_node*)malloc(sizeof(struct my_list_node));
        LIST_HEAD_INIT(&new[i]->node);
        new[i]->number = i;
        list_add(&new[i]->node,&p->list);
    }

    for(i = 3;i < 6;i++){
        new[i] = (struct my_list_node*)malloc(sizeof(struct my_list_node));
        LIST_HEAD_INIT(&new[i]->node);
        new[i]->number = i;
        list_add_tail(&new[i]->node,&p->list);
    }


    //输出链表节点数
    DEBUG_INFO("list_size(&p->list) = %d",list_size(p));

    //遍历链表
    show_list_nodes(p);
    //删除指定节点
    list_del(&new[3]->node);
    
    DEBUG_INFO("list_size(&p->list) = %d",list_size(p));
    //遍历链表
    show_list_nodes(p);

    
    for(i = 0;i < 6;i++){list_del(&new[i]->node);}

    DEBUG_INFO("list_size(&p->list) = %d",list_size(p));
    //遍历链表
    show_list_nodes(p);

    for(i = 0;i < 6;i++){free(new[i]);}
    free(p);
    return 0;
}

 编译

gcc -o app app.c

执行结果:

 小结

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

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

相关文章

Python中运行取消Python console模式

在Python里run的时候突然会发现&#xff0c;进入的不是run模式&#xff0c;而是console模式&#xff0c;这种运行模式能保留你每次的运行历史&#xff0c;因为会重开一个运行小页面&#xff0c;关闭操作如下&#xff1a;

GAMES104里渲染等一些剩下的问题

渲染的一些剩下的问题 1. 如何理解渲染中的AO(环境光遮蔽) 环境光遮蔽 我们先从一个简单的效果开始—环境光遮蔽(Ambient Occlusion,以下简称AO)。大家可以看到&#xff0c;下图中的场景没有任何渲染效果&#xff0c;也没有任何着色效果&#xff0c;但场景呈现出了非常清晰的…

大数据学习教程:Linux 基础教程(上)

1 操作系统概述 1.1 计算机原理 现代计算机大部分都是基于冯.诺依曼结构&#xff0c;该结构的核心思想是将程序和数据都存放在计算机中&#xff0c;按存储器的存储程序首地址执行程序的第一条指令&#xff0c;然后进行数据的处理计算。 计算机应包括运算器、控制器、储存器、…

PLC学习的步骤与重点:

熟悉基础元器件的原理和使用方法&#xff1a;了解按钮、断路器、继电器、接触器、24V开关电源等基础元器件的原理和使用方法&#xff0c;并能够应用它们来实现简单的逻辑电路&#xff0c;例如电机的正反转和单按钮的启停控制。 掌握PLC的接线方法&#xff1a;了解PLC的输入输出…

【C++进阶:map和set】

本节涉及到的所有代码见以下链接&#xff0c;欢迎参考指正&#xff01; ​​​​​​​ practice: 课程代码练习 - Gitee.comhttps://gitee.com/ace-zhe/practice/tree/master/map%E5%92%8Cset ​​​​​​​ 关联式容器 在C初阶阶段&#xff0c;已经学习并总了STL中的部分…

麒麟信安携手先进数通发布“多云协同,加速推进服务器虚拟化国产平滑迁移”信创联合解决方案

金融行业是现代经济的核心&#xff0c;金融行业信息技术应用创新是关系“国家安全”和“科技强国”战略的重要工程。为满足银行等金融机构数字化转型和信创发展的双重需求&#xff0c;麒麟信安与北京先进数通信息技术股份公司携手推出“多云协同&#xff0c;加速推进服务器虚拟…

Elisp之message为内容增加颜色(二十六)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

驱动 CAN总线

Controller Area Network&#xff08;控制器局部网络&#xff09;是一种有效支持分布式控制或实时控制的串行异步半双工的现场总线&#xff0c;支持多主机多从机&#xff0c;需要 CAN控制器 和 CAN收发器 的硬件支持 标准内容ISO 11898-1:2015定义数据链路层&#xff08;包括逻…

「二叉树与递归的一些框架思维」

文章目录 0 二叉树1 思路2 刷题2.1 二叉树的最大深度题解Code结果 2.2 二叉树的直径题解Code结果 2.3 在每个树行中找最大值题解Code结果 2.4 翻转二叉树题解Code结果 2.5 二叉树展开为链表题解Code结果 2.6 每日一题&#xff1a;数组中两元素的最大乘积题解Code结果 0 二叉树 …

华为开源自研AI框架昇思MindSpore应用案例:Vision Transformer图像分类

目录 一、环境准备1.进入ModelArts官网2.使用CodeLab体验Notebook实例 二、环境准备与数据读取三、模型解析Transformer基本原理Attention模块 Transformer EncoderViT模型的输入整体构建ViT 四、模型训练与推理模型训练模型验证模型推理 近些年&#xff0c;随着基于自注意&…

Spring——Spring是什么?IoC容器是什么?

文章目录 前言一、Spring是什么1.IoC 容器 —— 容器2.IoC 容器 —— IoC传统程序开发控制反转式程序开发 3.Spring IoC 二、DI是什么总结 前言 本人是一个普通程序猿!分享一点自己的见解,如果有错误的地方欢迎各位大佬莅临指导,如果你也对编程感兴趣的话&#xff0c;互关一下…

Vue2基础四、生命周期

零、文章目录 Vue2基础四、生命周期 1、生命周期 Vue生命周期&#xff1a;一个Vue实例从 创建 到 销毁 的整个过程。生命周期四个阶段&#xff1a;① 创建 ② 挂载 ③ 更新 ④ 销毁 创建阶段&#xff1a;创建响应式数据挂载阶段&#xff1a;渲染模板更新阶段&#xff1a;修改…

基于K8s环境·使用ArgoCD部署Jenkins和静态Agent节点

今天是「DevOps云学堂」与你共同进步的第 47天 第⑦期DevOps实战训练营 7月15日已开营 实践环境升级基于K8s和ArgoCD 本文节选自第⑦期DevOps训练营 &#xff0c; 对于训练营的同学实践此文档依赖于基础环境配置文档&#xff0c; 运行K8s集群并配置NFS存储。实际上只要有个K8s集…

Spring Security内置过滤器详解

相关文章&#xff1a; OAuth2的定义和运行流程Spring Security OAuth实现Gitee快捷登录Spring Security OAuth实现GitHub快捷登录Spring Security的过滤器链机制Spring Security OAuth Client配置加载源码分析 文章目录 前言OAuth2AuthorizationRequestRedirectFilterOAuth2Log…

matlab多线程,parfor循环进度,matlab互斥锁

一. 内容简介 matlab多线程&#xff0c;parfor循环进度&#xff0c;matlab互斥锁 二. 软件环境 2.1 matlab 2022b 2.2代码链接 https://gitee.com/JJW_1601897441/csdn 三.主要流程 3.1 matlab多线程 有好几种&#xff0c;最简单的&#xff0c;最好理解的就是parfor&am…

cpolar内网穿透外网远程访问本地网站

文章目录 cpolar内网穿透外网远程访问本地网站 cpolar内网穿透外网远程访问本地网站 在现代人的生活中&#xff0c;电脑是离不开的重要设备&#xff0c;大家看到用到的各种物品都离不开电脑的支持。尽管移动电子设备发展十分迅速&#xff0c;由于其自身存在的短板&#xff0c;…

Leetcode-每日一题【剑指 Offer 66. 构建乘积数组】

题目 给定一个数组 A[0,1,…,n-1]&#xff0c;请构建一个数组 B[0,1,…,n-1]&#xff0c;其中 B[i] 的值是数组 A 中除了下标 i 以外的元素的积, 即 B[i]A[0]A[1]…A[i-1]A[i1]…A[n-1]。不能使用除法。 示例: 输入: [1,2,3,4,5]输出: [120,60,40,30,24] 提示&#xff1a; 所…

SSIS对SQL Server向Mysql数据转发表数据 (三)

1、在控制流界面&#xff0c;在左侧的组件里&#xff0c;添加一个“序列容器组件”和一个“数据流任务组件” 2、双击数据流任务&#xff0c;进入到数据流界面&#xff0c;然后再在左面添加一个OLE DB 源组件、目标源组件 3、右键源组件&#xff0c;编辑&#xff0c;选择好相关…

虎年现货黄金投资布局图

参与现货黄金交易的主要目的&#xff0c;是为了根据行情走势的变动&#xff0c;把握一些较佳的获利机会&#xff0c;在这样的一个过程中&#xff0c;如果投资者能够提前把布局的图表画好&#xff0c;那么就可能获得事半功倍的效果&#xff0c;而本文将为大家简单的介绍&#xf…

C++——STL容器之list链表的讲解

目录 一.list的介绍 二.list类成员函数的讲解 2.2迭代器 三.添加删除数据&#xff1a; 3.1添加&#xff1a; 3.2删除数据 四.排序及去重函数&#xff1a; 错误案例如下&#xff1a; 方法如下&#xff1a; 一.list的介绍 list列表是序列容器&#xff0c;允许在序列内的任何…