日期:2014-05-16 浏览次数:20868 次
linux内核的链表设计非常独特。和通常的把数据结构装入链表不同,linux反其道而行之,把链表节点装入数据结构。这样的做法很好地实现了对数据的封装。并且为所有的链表操作提供了统一的接口。简单而高效。关于链表所有操作的函数都在/linux/list.h文件里
PS:由于list.h文件没有署名的注释,民间猜测内核的链表机制很有可能就是Linux Torvalds本人的作品
内核中的链表通常是一个双向循环链表,节点定义如下(linux/types.h):
struct list_head {
struct list_head *next, *prev;
};换句话说,这个节点就如同一个连接件,可以嵌入到任何数据结构中从而形成链表。当然它也可以独立存在作为一个链表的头。
这种设计方式让链表灵活了很多,比如可以在一个数据结构中插入两个list_head,使该结构同时存在于两个双链队列中,比如:内核中用于内存页面管理的page结构(mm.h)
typedef struct page{
struct list_head list;
struct list_head lru;
......
}甚至用这种方式,可以把不同类型的结构串到一个链表里,在后面的测试程序中,我就演示了这样的例子。这种设计方式还有一个最大的好处,内核中对链表中的所有操作(插入,删除,合并,遍历)都以list_head结构为参数,实现了统一的操作接口。
另一个问题随之而来,如何访问到每个节点的数据呢。且看linux精巧的设计:
由于在C中,一个结构中的变量偏移在编译时就被ABI固定下来,利用这一点,内核中的链表就可以通过list_head找到节点的入口,从而访问到节点中各个元素。
通过list.h中的宏list_entry实现:
/** * list_entry - get the struct for this entry * @ptr: the &struct list_head pointer. * @type: the type of the struct this is embedded in. * @member: the name of the list_struct within the struct. */ #define list_entry(ptr, type, member) \ container_of(ptr, type, member)
container_of 定义在kernel.h中
/**
* 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) );})注意:将__mptr转为char类型指针是为了在对其运算时,加减的单位为"1"。
offsetof定义在stddef.h中
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
返回成员MEMBER在结构TYPE中的偏移
简要解释下:
offsetof中,把0强制转换为结构的初始地址,然后对其成员取地址,所得的值即为该成员相对于结构入口的偏移。size_t是unsigned int类型,转换是为了便于后续计算。
container_of中,typeof是c语言关键字的一个新扩展,返回参数类型。该宏首先将ptr赋值给member类型的指针_mptr,然后用_mptr减去偏移,所得即为该节点的入口地址。
各种链表的操作接口,在list.h中定义,下面简要地罗列:
1、初始化链表:
linux没有链表头,使第一个节点的指针指向自己即完成初始化。
两种方式:1)
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)2)#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)相应地,链表判空,就看头的next是否指向自己:static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
2、添加节点:
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(new, head, head->next); __list_add(new, head->prev, head);
3、删除节点
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
4、搬移节点,把属于一个链表的节点移动到另一个链表
static inline void list_move(struct list_head *list, struct list_head *head);
5 还有一些其他操作,比如合并两个链表:
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
/**
* list_splice - join two lists, this is designed for stacks
* @list: the new list