Linux利用list_head结构实现双向链表
来源:互联网 发布:手机屏幕长截图软件 编辑:程序博客网 时间:2024/06/05 17:08
转自http://www.cnblogs.com/bastard/archive/2012/10/19/2731107.html
通常实现双向链表的数据结构:
struct list_node1{ struct list_node1 *next,*prev; type1 m1; type2 m2;};struct list_node2{ struct list_node2 *next,*prev; type1 m1; type2 m2;};……
对于每一种数据结构都定义了其特定的实现链表的结构和对应的方法(add/del)操作链表;
但对于具有大量不同数据结构,都要使用链表的系统中,如果为每一种数据结构定义特定的结构,和操作方法,
无疑使代码变得重复和臃肿,需要实现一种通用的双向链表方法,对各种数据结构都能适用。
C语言中又没有C++里面的模板,该如何实现呢?
linux内核中大量使用如下数据结构实现双向链表:
struct list_head { struct list_head *next, *prev;};
如果需要有某种数据结构的双向队列,就在这种结构内部放一个list_head数据结构成员。
struct kobject { const char *name; struct list_head entry; struct kobject *parent; struct kset *kset;}
形成了如下结构:
如何通过kobject 链表结构中的 list_head 成员entry访问下一个成员呢?
系统提供了宏list_entry:
#define list_entry(ptr, type, member) \ ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) //访问链表成员kobject *obj = objList;kobject *nextObj = (kobject *)list_entry(obj->entry->next,struct kobject,entry);
这个list_entry是什么道理呢:
ptr:是指向当前kobject结构对象中的数据成员entry,(char *)(ptr):entry成员地址 ——AddrA;
(unsigned long)(&((type *)0)->member)):将地址0转化为类型为type(struct kobject )对象,取member(entry)成员的地址——AddrB。
(type *)(AddrA - AddrB):得到kobject结构对象的首地址,转化为kobject对象。
这里需要关注的就是AddrB:将地址0转化为类型为type(struct kobject )对象,取member(entry)成员的地址。
表示当struct kobject 对象首地址为0时,得到成员member(entry)的地址,相对首地址的偏移地址。
通过struct kobject 对象中member(entry)的地址 ,以及相对首地址的偏移量,就能计算出struct kobject 对象的首地址。
如下结构:
充分利用了C语言中直接操作内存地址的特性。
- Linux利用list_head结构实现双向链表
- Linux利用list_head结构实现双向链表
- Linux利用list_head结构实现双向链表
- Linux利用list_head结构实现双向链表
- linux 内核分析之list_head 双向链表结构
- list_head 双向循环链表的结构
- linux内核源码“双向链表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(下)
- 深入浅出linux内核源代码之双向链表list_head(上)
- 深入浅出linux内核源代码之双向链表list_head(上)
- linux内核数据结构之双向循环链表struct list_head
- 【Eternallyc】素数筛选法
- 判断升序 oj118
- 如何实现可迭代对象和迭代器对象(二)
- HMTL5(五)
- 阶乘因式分解
- Linux利用list_head结构实现双向链表
- 在线查询树上最近公共祖先
- js实现一些简单文本的加载
- 杨辉三角 oj120
- AngularJS中的MVC
- Socket 简例
- 深入c语言_链接属性
- 并查集:CDOJ1593-老司机破阵 (假的并查集拆除)
- JavaScript 变量 数据类型