list_head结构的实现和使用
来源:互联网 发布:大数据应用趋势分析 编辑:程序博客网 时间:2024/06/05 19:24
struct list_head {
struct list_head *next, *prev;
};
#define list_entry(ptr, type, member) /
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
struct mystruct{
int a;
int b;
struct list_head link;
int c;
};
首先定义3个mystruct结构:
struct mystruct my1;
struct mystruct my2;
struct mystruct my3;
现在要利用link将3个mystruct顺序链结起来,如何实现呢?
代码如下:
my1.link.next = &my2.link;
//不是my1.link.next = &my2, 这样显然是语法错误(类型不兼容)。
my2.link.next = &my3.link;
my3.link.next = NULL;
这个时候,指针的布局是:
my1.link -> my2.link -> my3.link ->NULL
但我们期望的是:
my1 -> my2 -> my3 ->NULL
如何实现呢?list_entry干的就是这个事:由myX.link的地址得到myX的地址。
(&((type *)0)->member)得到link在mystruct中的偏移,这样,有了link地址,再加上有这个偏移,就可以计算出mystruct的地址了。
So nice!以前一直都一知半解,现在看下tfsc,一下就明白了。
http://www.google.cn/codesearch 没用过的推荐~
- list_head结构的实现和使用
- list_head结构的使用
- list_head结构的使用
- list_head结构的介绍
- list_head结构体的理解
- linux下一个计时器的实现(使用了list_head)
- linux kernal中list_head的移植和使用
- 内核数据结构list_head的使用
- linux内核list_head结构的介绍
- list_head 双向循环链表的结构
- list_head使用
- 深入理解linux内核list_head的实现
- 深入理解linux内核list_head的实现
- 深入理解linux内核list_head的实现
- 深入理解linux内核list_head的实现
- Linux利用list_head结构实现双向链表
- Linux利用list_head结构实现双向链表
- Linux利用list_head结构实现双向链表
- 无法形容的2007
- 《决战犹马镇》
- 电脑公司特别版常用软件盘
- 我的2007,记录一个走入社会的.NET程序员的成长
- 在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern “C”?
- list_head结构的实现和使用
- 我晕啦,终于成功了,原来是我的基础不稳固造成的,呵呵今天成功了!!!
- 如何解决VS中“找不到MSVCR80.dll”的问题
- 2007年简单回顾
- SQL SERVER存储过程解密方法
- 第一次滑雪(1)
- 第一次滑雪(2)
- 愿借一片水,合成两片月
- Oracle中UTL_FILE包的用法