Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
链表
链表组件
rt_container_of宏接口的使用
发布于 2024-01-16 14:31:26 浏览:824
订阅该版
[tocm] 最近在学习RT-Thread中链表的实现,一直卡在如下这个接口中rt_container_of: ```c /* the fist timer always in the last row */ static rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[]) { ... timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next, struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]); ... } ``` ```c /** * @brief get the struct for this entry * @param node the entry point * @param type the type of structure * @param member the name of list in structure */ #define rt_list_entry(node, type, member) \ rt_container_of(node, type, member) ``` ```c /**@{*/23 /** * rt_container_of - return the member address of ptr, if the type of ptr is the * struct type. */ #define rt_container_of(ptr, type, member) \ ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member))) ``` 通过Google搜索,大概得知是如下图:通过节点中的list元素来反推出对应链表的结构体地址。  下面是网上搜到的一段描述: rt_list_t成员是存放在节点中部或是尾部,不同类型的节点rt_list_t成员位置还不一样,在遍历整个链表时,获得的是后继节点(前驱节点)的rt_list_t成员的地址,那如何根据rt_list_t成员的地址访问节点中其他元素?尽管不同类型节点中rt_list_t成员位置不定,但是在确定类型节点中,rt_list_t成员的偏移是固定的,在获取rt_list_t成员地址的情况下,计算出rt_list_t成员在该节点中的偏移,即(rt_list_t成员地址)-(rt_list_t成员偏移)=节点起始地址。关键在于如何计算不同类型节点中rt_list_t成员偏移。 ```c /** * rt_container_of - return the member address of ptr, if the type of ptr is the * struct type. */ rt_container_of(ptr, type, member)这个接口的参数怎么和上面的描述对应起来,如何理解其中的(unsigned long)(&((type *)0)->member)。 ``` ## 我的理解: ptr:链表的地址,即type结构体变量中的list元素地址??? type:结构体变量类型 member:也是结构体中的列表元素???这里的参数不是很理解 如下是timer中的使用:  特此求助答疑,谢谢。
查看更多
sync
2024-01-16
这家伙很懒,什么也没写!
这里有详细介绍[Linux 中C语言的一点高级用法](https://blog.csdn.net/wubing_2010/article/details/127735190 "Linux 中C语言的一点高级用法") 
3
个回答
默认排序
按发布时间排序
liuduanfei
2024-01-17
这家伙很懒,什么也没写!
赞一个
CrazyH
2024-01-19
这家伙很懒,什么也没写!
赞一个
撰写答案
登录
注册新账号
关注者
0
被浏览
824
关于作者
邢雲子
这家伙很懒,什么也没写!
提问
4
回答
5
被采纳
0
关注TA
发私信
相关问题
1
链表使用bug:SCB_CFSR_UFSR:0x100 UNALIGNED
2
请教链表代码理解问题
3
关于这个函数 rt_list_insert_before
4
rt-thread使用链表组件编译报错
5
请问有没有介绍rtthread内部链表的材料?
6
单链表问题,rt_slist_for_each 宏问题
7
请问对 rt_slist_t 或 rt_list_t 链表的操作需要加锁吗?
8
IPC 内核链表while()死循环
9
list_for_each_entry Keil MDK 编译失败,我已经勾选了GUN,依然不行
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
RT-Thread CI编译产物artifacts自动上传功能介绍
2
STM32G030移植RT-Thread
3
CubeMX & RT-Thread Studio 联合开发说明
4
RT-Thread动态模块
5
RT-Thread项目助手v0.3 | Ubuntu与MacOS平台的RT-Thread Env
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
ESP8266
UART
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
GD32
定时器
ADC
flashDB
编译报错
socket
中断
rt_mq_消息队列_msg_queue
keil_MDK
Debug
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
lchnu
3
个答案
2
次被采纳
张世争
1
个答案
2
次被采纳
a1012112796
9
个答案
1
次被采纳
三世执戟
8
个答案
1
次被采纳
聚散无由
5
个答案
1
次被采纳
本月文章贡献
jinchanchan
12
篇文章
13
次点赞
ssdd45555
3
篇文章
2
次点赞
聚散无由
1
篇文章
4
次点赞
RTT_逍遥
1
篇文章
3
次点赞
hywing
1
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部