内核链表深度分析

内核链表深度分析:

相对于数组,链表具有动态性,

节点:数据域和指针域。

通过指针域串联就是链表。

内核链表是双向循环链表。

 

1.内核链表与普通链表区别:

内核链表能够创建一套统一链表

struct list_head

{

struct list_head *next, *prev;

}

list_head结构包含了两个指向list_head结构的指针,prevnext,由此组成双向循环链表

主要函数有:

static inline void INIT_LIST_HEAD(struct list_head *list)//初始化链表头

static inline void list_add(struct list_head *new, struct list_head *head)//链表节点插入

static inline void list_add_tail(struct list_head *new, struct list_head *head)

//在链表尾部插入

 

static inline void list_del(struct list_head *entry)

//删除节点

 

list_entry  //取出节点

list_for_each://遍历链表

 

 

剖析container_of的含义:

list.h (z:\kernel3.10\include\linux) 21603 2014-9-1

中定义了宏list_entry(ptr, type, member)

#define list_entry(ptr, type, member) \

container_of(ptr, type, member)

 

继续查找container_of看到:

#define container_of(ptr, type, member) ({                      \

const typeof( ((type *)0)->member ) *__mptr = (ptr);    \

(type *)( (char *)__mptr - offsetof(type,member) );})

首先说明:这个宏定义的意思是要通过结构体中的变量地址找到这个结构体的地址。

解析:container_of(ptr, type, member) 

参数: ptr:是一个指针,指向你已知的结构体的某个成员(某个成员的地址);

type:是这个结构体的类型

member:是同类型结构体中与你已知结构体成员(名字)相同的成员名字;

typeof( ((type *)0)->member ) *__mptr = (ptr)

这是个赋值语句,看(某类型)*__mptr = (ptr),也印证了前面说的ptr是个地址的说法

__mptr是个某类型的指针。且看具体类型:(type *)0:可以理解为将0地址强转成(type=已知的结构体类型)已知的结构体类型的起始地址,(type *)0)->member :在这个结构体中找到结构体成员member(你知道地址的结构体,当然这个具体的地址存在ptr中)

typeof就是取得这个结构体member成员的类型,这就得到了具体的类型。这个类型与ptr指向的地址类型一定是相同的。

 

offsetof(type,member)

#define  offsetof(TYPE, MEMBER)  ((size_t) &((TYPE *)0)->MEMBER)

 

&((TYPE *)0)->MEMBER):这个分析方法同上,将0地址转化为(TYPE *)类型,找到成员MEMBER后取其地址,再将这个地址转为整型。这个整形数据就是这个成员在结构体中的偏移。

 

这里要明确ptr指向的是真是的地址

 

链表相关函数分析:

1.INIT_LIST_HEAD:

static inline void INIT_LIST_HEAD(struct list_head *list)

{

list->next = list;

list->prev = list;

}

初始化链表头。

 

2.

static inline void list_add(struct list_head *new, struct list_head *head)

{

__list_add(new, head, head->next);

}

 

在头节点的后面添加一个新节点

实现代码是:这个段代码是个通用的代码,意思是在prevnext节点之间添加new节点

static inline 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;

}

 

3.

static inline void list_add_tail(struct list_head *new, struct list_head *head)

{

__list_add(new, head->prev, head);

}

调用函数__list_add(new, head->prev, head);意思是:在尾部和头部之间添加一个节点。

 

//删除节点 entry节点

#define LIST_POISON1  ((void *) 0x00100100)

#define LIST_POISON2  ((void *) 0x00200200)

static inline void list_del(struct list_head *entry)

{

__list_del(entry->prev, entry->next);

entry->next = (struct list_head*)LIST_POISON1;//避免删除后出现野指针

entry->prev = (struct list_head*)LIST_POISON2;

}

 

 

static inline void __list_del(struct list_head * prev, struct list_head * next)

{

next->prev = prev;

prev->next = next;

}

 

 

4.list_entry();这个函数非常精妙参见上述

#define list_entry(ptr, type, member) \

container_of(ptr, type, member)

这个就是上面的container_of(ptr, type, member):

 

5.list_for_each://遍历链表

#define list_for_each_entry(pos, head, member) \

for (pos = list_entry((head)->next, typeof(*pos), member); \

     &pos->member != (head);  \

     pos = list_entry(pos->member.next, typeof(*pos), member))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值