注1:以下所说的“结构体类型”,是指一个内部包含了structlist_head链表的结构体类型。如下面见到的structfox,我这里都称它为“结构体类型”,由它定义的变量称为“结构体变量”,如果只说“结构体”,就不区分它是类型还是变量,两者皆有之;为了区分,这里特称实现链表的数据类型为“链表类型”(如structlist_head),它可以定义“链表变量”。请紧记类型和变量的不同,声明结构体类型,通过类型名可以定义变量。一个“链表变量”称为“节点”,节点中含有“数据域”和“指针域”。
注2:1.1到1.3总结了内核链表的实现原理,1.4提供了使用范例,1.5补充了常用的函数。
1. 内核链表的实现与使用
1.1 内核声明了一个通用的链表类型:
struct list_head {
struct list_head *next,*prev;
};
这个链表类型的特别之处在于:只有指针域,而没有数据域。next指向后件,prev指向前件。如果你是刚接触Linux内核数据结构,你一定奇怪没有数据域的链表有什么用,你很快就会明白Linux内核这种独树一帜的实现方法。
1.2结构体类型中包含链表类型
如下,在struct fox中有struct list_head类型的成员list。各个struct fox类型的变量中,list成员变量指向前件和后件,形成链表。注意前件和后件都是structlist_head类型,而不是struct fox类型。
struct fox {
unsigned long tail_length; /* length in centimeters of tail */
unsigned long weight; /* weight in kilograms */
bool is_fantastic; /* is this fox fantastic? */
struct list_head list; /* list of all fox structures */
};
struct fox *red_fox;/* 定义一个struct fox 类型的指针 */
red_fox = kmalloc(sizeof(*red_fox), GFP_KERNEL);
/* 分配内存空间,GFP_KERNEL是什么意思暂时不需要知道,请照抄不误 */
然后,惊人地,内核中只有一种链表类型,即struct list_head链表类型。因为只有一种链表类型,内核能够封通用的装链表操作方法(通过宏实现),如增加一个节点、删除一个节点、移动和合并节点等。更重要的是,历遍列表也是使用通用的宏。这样的好处显而易见:简化了实现链表的过程,统一了内核的编码风格。
1.3 访问结构体的数据成员(重要)
struct list_head list只把自身连起来,red_fox->list.prev 和red_fox->list.next都指向了struct list_head list类型的数据,所以你一直访问到的都是prev和next这样的指针,可你想要的是数据,如red_fox的tail_length和weight。要如何访问struct fox结构体中的每一个数据成员呢?要找到结构体的入口。怎么找到结构体入口?三个字,“偏移值”。已知red_fox->list.next指向了下一只“狐狸”(struct fox *blue_fox;)中的blue_fox->list成员。从blue_fox->list往回“跑多远”才能找到(*blue_fox)的入口地址,这个就是所谓的偏移值了。内核中用以下的宏实现:
/**
* list_entry - get the struct for this entry
* 无需多言,list_entry等于container_of。
* 而一般使用list_entry这个名字
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
要理解这些参数是什么,请先思考:要算出偏移值,你需要什么已知条件(放在宏中就成为参数了)?
答案是: 。list字名和struct fox型类道知要这个宏返回的是结构体的入口地址,ptr 是指针变量,详细点,是&struct list_head指针变量,如red_fox->list.next;type 是结构体的类型,如struct fox;menber是struct list_struct在结构体中声明的名字,如list。
这样实现:list_entry(red_fox->list.next, struct fox, list);
想看看蓝狐狸多重?
struct fox *f;
f = list_entry(red_fox->list.next, struct fox, list);
f->weight;/* 就是这么重*/
由上面的定义可见,list_entry(ptr, type, member) 是由container_of(ptr, type, member)实现的。这个实现很巧妙,如下:
/**
* container_of - cast a member of a structure out to the containing structure
//意思是:从一个结构体成员member去到包含它的结构体哪里
* @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) );})
用链表操作时,member肯定是struct list_head类型的。所以,第一句等价于:
const struct list_head *__mptr = (ptr);
ptr先保存到__mptr中,注意他是const的。offsetof(type,member)算出了list_head在结构体中的偏移地址,__mptr减去偏移值后就得出了结构体的首地址。
这里值得注意的问题是:1)为什么__mptr定义为const的,直接用ptr减偏移值行不行;2)(char *)__mptr强制转为char *类型的用意是什么;3)offsetof()是怎样实现的。
只看看offsetof()得源码,不一一解答:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)注意到偏移值最后强转型为size_t,即“字”。结构体中的元素是按字对齐的,具体可以参考《Linux Kernel Development 3ed》可移植性这章的相关内容,特别留意有关“结构体填补”的说明。
/* 以下内容打算改为
* 创建链表,添加、删除、合并节点,历遍打印链表的例程
* 写好后在linux上成功运行在贴上来。
*/
1.4 实例
以上说明了声明链表类型,把它嵌入到结构体中,找到结构体的入口地址(首地址),最后实现结构体的访问。有了以上的知识,自行阅读源码就可以理解下面用到的宏,源码在<linux/list.h>记住这个文件,我用的内核版本是2.6.34。内核帮助我们完成了大部分的工作。当我们使用链表时,哪些工作是我们做的呢?看看下面的例子:
(1)定义自己的结构体,并把struct list_head包含到结构体中
struct fox {
unsigned long tail_length; /* length in centimeters of tail */
unsigned long weight; /* weight in kilograms */
bool is_fantastic; /* is this fox fantastic? */
struct list_head list; /* list of all fox structures */
};
(2)定义并初始化结构体,初始化链表。注意以下Linux独特的编程风格。
struct fox red_fox = {
.tail_length = 40,
.weight = 6,
.list = LIST_HEAD_INIT(red_fox.list),
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
//逗号运算符的使用,参考数组的赋值:int a[6] = { 0, 0, 15, 0, 29, 0 };
所以,.list = LIST_HEAD_INIT(red_fox.list),
相当于:
.list = &red_fox.list, &red_fox.list,
red_fox.list.prev和red_fox.list.next指针都指向了red_fox.list自身。完成了一个双向链表的初始化。
(3)通常,定义一个struct list_head变量,作为历遍链表的开始
static LIST_HEAD(fox_list);
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
/**
* 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; prefetch(pos->next), pos != (head); \
pos = pos->next)// prefetch(pos->next)不是必要的语句,见下。
/**
* __list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*
* This variant differs from list_for_each() in that it's the
* simplest possible list iteration code, no prefetching is done.
* Use this for code that knows the list to be very short (empty
* or 1 entry) most of the time.
*/
#define __list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
eg:
struct list_head *p;
list_for_each(p, fox_list) {
/* p points to an entry in the list */
}
通常使用list_for_each_entry历遍链表。这个宏内部也使用了list_entry宏的返回值。
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#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))
eg:
struct list_head *p;
struct fox *f;
list_for_each(p, &fox_list) {
/* f points to the structure in which the list is embedded*/
f = list_entry(p, struct fox, list);
}
1.5操作链表
/**
* 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);
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
#ifndef CONFIG_DEBUG_LIST
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;
}
#else
类似地,在链表的尾部加入一个节点:
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
1.6 其它常用方法
1.6.1 操作链表
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop cursor.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
/**
* list_splice - join two lists, this is designed for stacks
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(const struct list_head *list,
struct list_head *head)