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

相关文章

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

Jvm sandbox mock机制的实践过程

《Jvmsandboxmock机制的实践过程》:本文主要介绍Jvmsandboxmock机制的实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、背景二、定义一个损坏的钟1、 Springboot工程中创建一个Clock类2、 添加一个Controller

Mysql中的用户管理实践

《Mysql中的用户管理实践》:本文主要介绍Mysql中的用户管理实践,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录13. 用户管理13.1 用户 13.1.1 用户信息 13.1.2 创建用户 13.1.3 删除用户 13.1.4 修改用户

Python中合并列表(list)的六种方法小结

《Python中合并列表(list)的六种方法小结》本文主要介绍了Python中合并列表(list)的六种方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录一、直接用 + 合并列表二、用 extend() js方法三、用 zip() 函数交叉合并四、用

在Java中基于Geotools对PostGIS数据库的空间查询实践教程

《在Java中基于Geotools对PostGIS数据库的空间查询实践教程》本文将深入探讨这一实践,从连接配置到复杂空间查询操作,包括点查询、区域范围查询以及空间关系判断等,全方位展示如何在Java环... 目录前言一、相关技术背景介绍1、评价对象AOI2、数据处理流程二、对AOI空间范围查询实践1、空间查

qtcreater配置opencv遇到的坑及实践记录

《qtcreater配置opencv遇到的坑及实践记录》我配置opencv不管是按照网上的教程还是deepseek发现都有些问题,下面是我的配置方法以及实践成功的心得,感兴趣的朋友跟随小编一起看看吧... 目录电脑环境下载环境变量配置qmake加入外部库测试配置我配置opencv不管是按照网上的教程还是de

linux lvm快照的正确mount挂载实现方式

《linuxlvm快照的正确mount挂载实现方式》:本文主要介绍linuxlvm快照的正确mount挂载实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux lvm快照的正确mount挂载1. 检查快照是否正确创建www.chinasem.cn2.

Linux给磁盘扩容(LVM方式)的方法实现

《Linux给磁盘扩容(LVM方式)的方法实现》本文主要介绍了Linux给磁盘扩容(LVM方式)的方法实现,涵盖PV/VG/LV概念及操作步骤,具有一定的参考价值,感兴趣的可以了解一下... 目录1 概念2 实战2.1 相关基础命令2.2 开始给LVM扩容2.3 总结最近测试性能,在本地打数据时,发现磁盘空

Linux网络配置之网桥和虚拟网络的配置指南

《Linux网络配置之网桥和虚拟网络的配置指南》这篇文章主要为大家详细介绍了Linux中配置网桥和虚拟网络的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、网桥的配置在linux系统中配置一个新的网桥主要涉及以下几个步骤:1.为yum仓库做准备,安装组件epel-re