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

这篇具有很好参考价值的文章主要介绍了linux内核中的offsetof、container_of、双链表list.h实践。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

感谢李慧芹的B站课程:史上最强最细腻的linux嵌入式C语言学习教程【李慧芹老师】_哔哩哔哩_bilibili文章来源地址https://www.toymoban.com/news/detail-726755.html

#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=0
	name    		offset=4
	ch              offset=132
	ma              offset=136
	en              offset=140
	list    		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=77
	id=2, name=stu02, ch=15, ma=93, en=35
	id=3, name=stu03, ch=86, ma=92, en=49
	id=4, name=stu04, ch=21, ma=62, en=27
	id=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=63
	id=4, name=stu04, ch=21, ma=62, en=27
	id=3, name=stu03, ch=86, ma=92, en=49
	id=2, name=stu02, ch=15, ma=93, en=35
	id=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实践的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 修改嵌入式 ARM Linux 内核映像中的文件系统

    zImage 是编译内核后在 arch/arm/boot 目录下生成的一个已经压缩过的内核映像。通常我们不会使用编译生成的原始内核映像 vmlinux ,因其体积很大。因此, zImage 是我们最常见的内核二进制,可以直接嵌入到固件,也可以直接使用 qemu 进行调试。当然,在 32 位嵌入式领域还能见到

    2024年02月10日
    浏览(67)
  • linux 内核资源配置--cgroups详解以及在docker中的应用

    1.1、cgroups 是什么 Linux cgroup (Control Groups)是 Linux 内核提供的一种机制, 用于限制进程组使用的资源(如 CPU、内存、磁盘 I/O 等) 。通过将进程组划分为层次结构,并将资源限制应用于不同层次的组,可以实现对系统资源的统一管理和限制。 cgroup 提供了一套 API,用于创建

    2024年02月16日
    浏览(34)
  • The Advantages of Using Containers in Devops Projects

    作者:禅与计算机程序设计艺术 DevOps (Development and Operations) refers to the collaboration between development and IT operations professionals to improve quality of software delivery, increase efficiency, reduce costs and time-to-market, automate processes, and provide continuous feedback loops with customers. In this article we will discuss

    2024年02月08日
    浏览(39)
  • 解决Correct the classpath of your application so that it contains compatible versions

    springboot启动失败 报错Correct the classpath of your application so that it contains compatible versions of the classes org.springframework.web.servlet.handler.AbstractHandlerMethodMapping and org.springframework.web.method.HandlerMethod 排查发现:pom依赖同时引用了两个不同版本的web包。 删掉一个web依赖重新构建以后问题直

    2024年02月09日
    浏览(36)
  • 【Java遇错】Correct the classpath of your application so that it contains a single, compatible version..

    问题描述: 启动服务出现以下错误: 启动测试出现以下错误: 问题原因: 错误:Correct the classpath of your application so that it contains a single, compatible version of com.baomidou.mybatisplus.core.toolkit.ReflectionKit(更正应用程序的类路径,使其包含com.baomidou.mybatisplus.core.toolkit.ReflectionKit的单个

    2024年02月03日
    浏览(34)
  • 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘se

     Navicat Premium 16 版本 这个错误是由于 MySQL 的新版本中默认开启了 ONLY_FULL_GROUP_BY 模式,即在 GROUP BY 语句中的 SELECT 列表中,只能包含分组或聚合函数,不能包含其他列。而你的查询语句中出现了一个列 senior_two.score.student_id ,它既没有被分组也没有被聚合,因此 MySQL 报出了这

    2024年02月15日
    浏览(36)
  • mysql遇见Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggre问题解决

    目录 起因 问题产生原因 解决方案 方式一 方式二 今天在mysql5.7.x 升级到8.0.x版本的时候 项目接口报错。最后发现是使用group by的sql语句时候发现mysql出现如下问题: Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column \\\'xxx\\\' which is not functionally dependent on columns i

    2024年02月15日
    浏览(41)
  • 【C语言】结构体与offsetof实现

    远看山有色,近听水无声。春去花还在,人来鸟不惊。 — 唐代·王维《画》 这篇博客我们会详细介绍结构体相关知识,干货满满。 一般来说结构体应该有成员列表和变量列表这两个基础的模式。 例如描述一个学生: 当然也不是只有这一种声明。 在声明结构的时候,可以不

    2024年02月16日
    浏览(29)
  • MySQL Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column异常处理

    使用联表查询时,group by两个字段出现了错误 意思是select字段里包含了没有被group by 条件唯一确定的字段。 MySQL版本5.7之后会检测函数依赖,默认启用的模式是ONLY_FULL_GROUP_BY,使用GROUP BY 语句违背了sql_mode=only_full_group_by。该模式的意思是只有确定唯一字段的group by才能执行。

    2024年01月24日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包