linux内核中的offsetof、container_of、双链表list.h实践

2023-10-11 09:52

本文主要是介绍linux内核中的offsetof、container_of、双链表list.h实践,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

先直接上程序,代码中包含了注释已经说清楚。在linux的应用层中编译、测试:

感谢李慧芹的B站课程:史上最强最细腻的linux嵌入式C语言学习教程【李慧芹老师】_哔哩哔哩_bilibili

#include <stdio.h>
#include <stdlib.h>// 下面的宏来自于: <linux/kernel.h>
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)#define container_of(ptr, type, member) ({			\const typeof( ((type *)0)->member ) *__mptr = (ptr);	\(type *)( (char *)__mptr - offsetof(type,member) );})// 下面的结构体定义来自于 <linux/types.h>
struct list_head {struct list_head 	*next;struct list_head  	*prev;
};// 	下面的宏及函数摘自于 <linux/list.h>
#define list_entry(ptr, type, member) \container_of(ptr, type, member)#define list_for_each(pos, head) \for (pos = (head)->next; pos != (head); pos = pos->next)	#define LIST_HEAD_INIT(name) { &(name), &(name) }#define LIST_HEAD(name) \struct list_head name = LIST_HEAD_INIT(name)void INIT_LIST_HEAD(struct list_head *list)
{list->next = list;list->prev = list;
}// 将 new 插入到 prev 和 next 的中间
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;
}// 将 new 插入到 head 的后面
void list_add(struct list_head *new, struct list_head *head)
{__list_add(new, head, head->next);
}// 将 new 插入到 head 的前面
void list_add_tail(struct list_head *new, struct list_head *head)
{__list_add(new, head->prev, head);
}void __list_del(struct list_head * prev, struct list_head * next)
{next->prev = prev;prev->next = next;
}void __list_del_entry(struct list_head *entry)
{__list_del(entry->prev, entry->next);
}//
// 下面是业务层应用代码
//
// 应用层业务的结构体定义:
struct student
{int 				id;char 				name[128];int 				ch;		//语文分数int 				ma;		//数学分数int 				en;		//英语分数struct list_head 	list;	//包含一个 list_head
};void print_stu(struct student *st);int main()
{//// 0.1 测试宏 offsetof 使用//printf("id 		offset=%ld\n", offsetof(struct student, id));printf("name 	offset=%ld\n", offsetof(struct student, name));printf("ch 		offset=%ld\n", offsetof(struct student, ch));printf("ma 		offset=%ld\n", offsetof(struct student, ma));printf("en		offset=%ld\n", offsetof(struct student, en));printf("list	offset=%ld\n", offsetof(struct student, list));
/*	id              offset=0name    		offset=4ch              offset=132ma              offset=136en              offset=140list    		offset=144上面看出,宏offsetof(TYPE, MEMBER),就是返回成员MEMBER相对首的偏移!
*/// 0.2 测试宏 container_of 使用struct student stu={100, "std100", 78, 88, 98, NULL,};struct student *p=container_of(&stu.list, struct student, list);printf("&stu=%p\n", &stu);printf("&stu.list=%p\n", &stu.list);printf("&p=%p\n", p);
/*&stu=		0x7fffa2f4e1b0&stu.list=	0x7fffa2f4e240		0x240-0x1b0=144 即是上述list的偏移off&p=			0x7fffa2f4e1b0		上面看出,宏container_of(ptr, type, member) 即是返回结构体变量的首地址。那么问题来了,为何搞这么复杂的一个转换来获取首地址呢?直接使用&stu不就得到完了嘛!别急,看下面的应用!
*/int i=0;LIST_HEAD(head);//// 1. 创建5个结构体,使用 list 连起来//for(i=0; i<5; i++){struct student *st=malloc(sizeof(struct student));sprintf(st->name, "stu%02d", i+1);st->id=i+1;st->ch=rand()%100;st->ma=rand()%100;st->en=rand()%100;printf("id=%d, name=%s, ch=%d, ma=%d, en=%d\n",st->id, st->name, st->ch, st->ma, st->en);list_add(&(st->list), &head); //这里每次插入到head的后面!}
/*id=1, name=stu01, ch=83, ma=86, en=77id=2, name=stu02, ch=15, ma=93, en=35id=3, name=stu03, ch=86, ma=92, en=49id=4, name=stu04, ch=21, ma=62, en=27id=5, name=stu05, ch=90, ma=59, en=63注意上述创建的原始顺序!
*/printf("\n");	//// 2. 遍历打印//struct list_head *c;list_for_each(c, &head){struct student *st=container_of(c, struct student, list);print_stu(st);}
/*id=5, name=stu05, ch=90, ma=59, en=63id=4, name=stu04, ch=21, ma=62, en=27id=3, name=stu03, ch=86, ma=92, en=49id=2, name=stu02, ch=15, ma=93, en=35id=1, name=stu01, ch=83, ma=86, en=77因为是每次插入到head的后面,所以链表里面的顺序是5、4、3....
*/	//// 3. 查找一个节点//list_for_each(c, &head){struct student *st=container_of(c, struct student, list);if(st->id==3){printf("\nfind it!\n");print_stu(st);}}//// 4. 删除一个节点//list_for_each(c, &head){struct student *st=container_of(c, struct student, list);if(st->id==3){__list_del_entry(&st->list);free(st);}}//// 5. 再次输出打印//printf("\nreprintf:\n");		list_for_each(c, &head){struct student *st=container_of(c, struct student, list);print_stu(st);}	
}void print_stu(struct student *st)
{printf("id=%d, name=%s, ch=%d, ma=%d, en=%d\n",st->id, st->name, st->ch, st->ma, st->en);	
}

这篇关于linux内核中的offsetof、container_of、双链表list.h实践的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/187227

相关文章

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4: