Linux Kernel中list解读

一:

demo.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


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

#ifndef container_of
/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:	the pointer to the member.
 * @type:	the type of the container struct this is embedded in.
 * @member:	the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({			\
	const typeof(((type *)0)->member) * __mptr = (ptr);	\
	(type *)((char *)__mptr - offsetof(type, member)); })
#endif

/**
 * list_for_each	-	iterate over a list
 * @pos:	the &struct list_head to use as a loop cursor.
 * @head:	the head for your list.
 */
#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)

struct list_head {
	struct list_head *next, *prev;
};

static inline void INIT_LIST_HEAD(struct list_head *list)
{
	list->next = list;
	list->prev = list;
}

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
	struct list_head name = LIST_HEAD_INIT(name)

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


	
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;
}

/**
 * list_add - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
static inline void list_add(struct list_head *new, struct list_head *head)
{
	__list_add(new, head, head->next);
}

typedef struct _Teacher
{
    int age;
    double salary;
    char name[64];
	  struct list_head node;
}Teacher;

int main()
{
  struct list_head head, *plist;
  Teacher t1,t2,t3;
  t1.age = 1;t1.salary = 100.0;strcpy(t1.name,"john");
  t2.age = 2;t2.salary = 100.1;strcpy(t2.name,"jack");
  t3.age = 3;t3.salary = 100.2;strcpy(t3.name,"jobs");
  
	int offset = offsetof(Teacher,name);
	printf("offset is %d\n",offset);
	printf("sizeof(double) = %lu\n",sizeof(double));
	
	INIT_LIST_HEAD(&head);
  list_add(&t1.node, &head);
	list_add(&t2.node, &head);
  list_add(&t3.node, &head);
  
  list_for_each(plist,&head)
  {
     Teacher *t = list_entry(plist, Teacher, node);
     printf("age = %d\n",t->age);
     printf("salary = %f\n",t->salary);
     printf("name = %s\n",t->name);
  }
	return 0;

}

offset is 16
sizeof(double) = 8
age = 3
salary = 100.200000
name = jobs
age = 2
salary = 100.100000
name = jack
age = 1
salary = 100.000000
name = john

二:

LinuxKernel中,常常需要使用双向链表。在~/include/Linux/list.h中,就定义了双向链表和常用的function.

 /*
 * Simple doubly linked list implementation.
 *
 * Some of the internal functions ("__xxx") are useful when
 * manipulating whole lists rather than single entries, as
 * sometimes we already know the next/prev entries and we can
 * generate better code by using them directly rather than
 * using the generic single-entry routines.
 */

链表头如下:

struct list_head {
 struct list_head *next, *prev;
};

 

2.0.创建双向链表(doubly linkedlist):

INIT_LIST_HEAD(struct list_head*list)

代码如下:

static inline void INIT_LIST_HEAD(struct list_head *list)
{
 list->next = list;
 list->prev = list;
}
将List的头和尾都指向自身。

 

2. 添加内容到双向链表:

2.1: 平常的添加:

2.1.1:将新项目添加到list的头部(head之后第一个位置)。注意,此处head是指此双向链表头。

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

将参数一(new)添加到head之后。它调用

__list_add(new, head,head->next);也就是说,把new添加到head和head->next之间。

static inline void __list_add(structlist_head *new,
        struct list_head *prev,
        struct list_head *next) //它只是将new添加到prev和next之间
{
 next->prev = new;
 new->next = next;
 new->prev = prev;
 prev->next = new;
}

 

2.1.2:将新项目添加双向链表最后一个位置(也就是head的priv)。注意此处head表示list头。

static inline void list_add_tail(structlist_head *new, struct list_head *head)
{
 __list_add(new, head->prev,head);
}

则将new添加到head->prev和head之间了。

 

2.2:读拷贝更新(rcu)模式的添加(smp_wmb())(请看背景知识)

2.2.1: 将新项目加到以知的prev和next之间:

static inline void __list_add_rcu(structlist_head * new,
  struct list_head * prev, structlist_head * next)
{
 new->next = next;
 new->prev = prev;
 smp_wmb();
 next->prev = new;
 prev->next = new;
}//此处注意:smp_wmb();
smp_wmb()防止编译器和CPU优化代码执行的顺序。在这里,smp_wmb保证在它之前的两行代码执行完了之后再执行后两行

 

2.2.2:将新项目添加到list的头部(head之后第一个位置)。注意,此处head是指此双向链表头。

static inline void list_add_rcu(structlist_head *new, struct list_head *head)
{
 __list_add_rcu(new, head,head->next);
}

 

2.2.3:将新项目添加双向链表最后一个位置(也就是head的priv)。注意此处head表示list头。staticinline void list_add_tail_rcu(struct list_head *new,
     structlist_head *head)
{
 __list_add_rcu(new, head->prev,head);
}

 

 

3. 从双向链表删除项目:

3.1:基本删除函数:

static inline void __list_del(structlist_head * prev, struct list_head * next)
{
 next->prev = prev;
 prev->next = next;
}//只是将前一个和后一个互指

 

3.2:删除指定项:

static inline void list_del(structlist_head *entry)
{
 __list_del(entry->prev,entry->next);
 entry->next = LIST_POISON1;
 entry->prev = LIST_POISON2;
}

 

3.3: 安全的删除指定项:

static inline void list_del_rcu(structlist_head *entry)
{
 __list_del(entry->prev,entry->next);
 entry->prev = LIST_POISON2;
}

此处Sam并不很清楚怎么回事。

 

3.4:删除并初始化某一项:

static inline void list_del_init(structlist_head *entry)
{
 __list_del(entry->prev,entry->next);
 INIT_LIST_HEAD(entry);
}

 

4.替换某项:

4.1 使用new 替换 old:

static inline void list_replace(structlist_head *old,
    structlist_head *new)
{
 new->next =old->next;
 new->next->prev =new;
 new->prev =old->prev;
 new->prev->next =new;
}

 

4.2 替换并初始化:

static inline voidlist_replace_init(struct list_head *old,
     structlist_head *new)
{
 list_replace(old, new);
 INIT_LIST_HEAD(old);
}

 

4.3:安全替换:

static inline void list_replace_rcu(structlist_head *old,
    structlist_head *new)
{
 new->next =old->next;
 new->prev =old->prev;
 smp_wmb();
 new->next->prev =new;
 new->prev->next =new;
 old->prev = LIST_POISON2;
}

 

5. 移动项:

5.1移动到头部

static inline void list_move(structlist_head *list, struct list_head *head)
{
 __list_del(list->prev,list->next);
 list_add(list, head);
}

 

 

5.2移动到尾部
static inline void list_move_tail(structlist_head *list,
     struct list_head *head)
{
 __list_del(list->prev,list->next);
 list_add_tail(list, head);
}

6.测试项目是否为最后一项:

static inline int list_is_last(conststruct list_head *list,
    conststruct list_head *head)
{
 return list->next == head;
}

7. 测试list是否为空:

static inline int list_empty(const structlist_head *head)
{
 return head->next == head;
}

8. 两个链表连接起来:

8.1:将list链表连接如head链表头部:

static inline void __list_splice(structlist_head *list,
    struct list_head *head)
{
 struct list_head *first =list->next;
 struct list_head *last =list->prev;
 struct list_head *at =head->next;

 first->prev =head;
 head->next = first;

 last->next = at;
 at->prev = last;
}

8.2:连接

static inline void list_splice(structlist_head *list, struct list_head *head)
{
 if (!list_empty(list))
  __list_splice(list,head);
}

 

8.3:连接并初始化:

将list连接到head头部,再将list初始化:

static inline void list_splice_init(structlist_head *list,
       struct list_head *head)
{
 if (!list_empty(list)) {
  __list_splice(list,head);
  INIT_LIST_HEAD(list);
 }
}

 

9.一些有用的宏:

9.1得到 list_entry(ptr, type, member)

简单的讲,这个宏的作用是:通过结构(type)中的某个变量(member)的指针(ptr)获取结构本身的指针.

也就是说,type中包含一个成员变量member.且某个结构体实体中member的指针为ptr.则list_entry()则返回的是:这个结构体实体的指针。至于如何做到的,请看背景知识3---container_of。

 

9.2:list_first_entry(ptr, type,member) 
得到ptr链表中下一个的struct的实体。

 

9.3:  list_for_each(pos,head)

#define list_for_each(pos, head) /
 for (pos = (head)->next;prefetch(pos->next), pos != (head); /
        pos = pos->next)

它其实就是一个for循环,循环双向链表一圈。

prefetch()是档案快取技术,不用深究。

 

下面几个宏与之类似:

__list_for_each(pos, head)  //不用档案快取技术的循环

list_for_each_prev(pos, head) //向前循环

 

9.4: list_for_each_entry(pos,head, member)

这个宏是双向链表中最常用的,也是最有用的。表示从以head为头的双向循环列表中,一个一个拿出包含此list项目的结构体(pos的类型),并放到pos中。

#define list_for_each_entry(pos, head,member)    /
 for (pos =list_entry((head)->next, typeof(*pos),member); /
     prefetch(pos->member.next),&pos->member != (head); /
     pos = list_entry(pos->member.next, typeof(*pos),member))

因为有上面list_entry()的铺垫,所以非常简单。

参数一:pos就是一个结构体指针。这个结构体中会包含成员变量member.

参数二:head就是一个双向链表头。

参数三:pos结构体中的成员变量名。

pos = list_entry((head)->next, typeof(*pos),member):pos得到双向链表中第一个链表被包含的结构体实体。

&pos->member !=(head):此结构体中的链表不是头。

pos = list_entry(pos->member.next, typeof(*pos),member): pos得到双向链表中下一个结构体实体。

 

 

Linux kernel 中双向循环链表的使用:

在Linux内核链表中,需要用链表组织起来的数据通常会包含一个structlist_head成员,结构都通过这个list成员组织在一个链表中。

例如:在hid-core.c中,要组织一个report链表。

 

于是,首先使用

1)

INIT_LIST_HEAD(&device->report_enum[i].report_list)

struct hid_report {
 struct list_head list;
 unsignedid;     
 unsignedtype;     
 struct hid_field*field[HID_MAX_FIELDS]; 
 unsignedmaxfield;    
 unsignedsize;     
 struct hid_device*device;   
};

这就是需要用链表组织起来的数据通常会包含一个struct list_head成员。

 

2)。

list_add_tail(&report->list,&report_enum->report_list);

 将report类型的项目添加到刚才初始化的list中。

 

3).

list_for_each_entry(report,&hid->report_enum[HID_INPUT_REPORT].report_list,list)

遍历 hid->report_enum[HID_INPUT_REPORT].report_list,从其中一个一个得到report.放到report中。

 

 

 

背景知识:

背景知识一:typeof:

typeof不是标准C的运算符,这是gcc的一个扩展.

它与sizeof() 语义类似,sizeof(exp)代表返回exp长度。则typeof(exp)返回的是exp类型。

 

例1:

int a;

typeof(&a) b;

因为a 为int型。所以&a为int*.

也就是说b 为int* 类型。

 

例2:

typedef struct

{

int size;

char t;

} ngate, *pngate;

 

typeof(((ngate *)0)->t) w;

这其实就是表示,w 的类型为:ngate的t的类型。

在这里0并不是真正的变量,可以把它理解为一个替代使用的符号。其意思更可以理解为一个被赋值了的变量,这个数可以不是0,,随便什么数字都可以。

 

背景知识二:offsetof

kernel中定义如下:

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

与上面所以类似,(TYPE *)0 表示:0是指向TYPE的指针

则 &(TYPE *)0->MEMBER表示:TYPE类型的实体0的变量MEMBER的地址,因为从0开始,所以它的地址就成为offset.再用size_t强制转换,就是从struct头到成员变量MEMBER的offset.

 

背景知识三:container_of(ptr, type,member)

Kernel中如下定义:

#define container_of(ptr, type, member)({   /
 const typeof( ((type *)0)->member) *__mptr = (ptr); /
 (type *)( (char *)__mptr - offsetof(type,member));})

(type *)0: 表明某个实体为type类型的。

((type *)0)->member表明这个实体的某个成员变量。

typeof(((type *)0)->member) *__mptr表明定了一个指向此成员变量类型的指针。

 

offsetof(type,member)表明成员变量member到结构体类型type头的offset.

(type *)( (char*)__mptr - offsetof(type,member)则表明:返回的是一个指向type的指针,此指针指向一个type类型的实体。而参数ptr则是这个实体中的某一个成员变量位置。

 

 

背景知识四:RCU(Read-CopyUpdate)

RCU是2.5/2.6内核中引入的新技术,它通过延迟写操作来提高同步性能。

系统中数据读取操作远多于写操作,而rwlock机制在smp环境下随着处理机增多性能会迅速下降。针对这一应用背景,IBMLinux技术中心的Paul E.McKenney提出了"读拷贝更新"的技术,并将其应用于Linux内核中。RCU技术的核心是写操作分为写-更新两步,允许读操作在任何时候无阻访问,当系统有写操作时,更新动作一直延迟到对该数据的所有读操作完成为止。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值