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 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)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}
6 遍历,通过宏实现:
/**
* 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)
* 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); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
自己写了一个小程序作为本文结束,运用linux链表的设计思想,在同一链表中链接两种不同数据结构并遍历之。
#include<stdio.h>
#include<stdlib.h>
#define get_entry(ptr, structure, member) \
((structure *)((char *)ptr - (unsigned int)&(((structure *)0) -> member)))
#define init_list_head(p) do {\
(p) -> prev = (p); (p) -> next = (p); \
}while(0)
typedef struct link_pointer_tag
{
struct link_pointer_tag *prev;
struct link_pointer_tag *next;
}link_pointer;
typedef struct
{
int element1;
link_pointer p;
}link_node1;
typedef struct
{
char element2;
link_pointer p;
}link_node2;
link_pointer* head;
link_pointer* create_node1()
{
int ele;
link_node1* tmp_node1;
if ((tmp_node1 = (link_node1*)malloc(sizeof(link_node1))) == NULL)
{
printf("Not enough memery!\n");
exit(0);
}
printf("Input the element of node: element1(int)\n");
scanf("%d", &ele);
tmp_node1 -> element1 = ele;
return &(tmp_node1 -> p);
}
link_pointer* create_node2()
{
char ele;
link_node2* tmp_node2;
if ((tmp_node2 = (link_node2*)malloc(sizeof(link_node2))) == NULL)
{
printf("Not enough memery!\n");
exit(0);
}
printf("Input the element of node: element2(char)\n");
scanf("%c", &ele);
tmp_node2 -> element2 = ele;
return &(tmp_node2 -> p);
}
void add_node(link_pointer* ptr, link_pointer* pre, link_pointer* next)
{
pre -> next = ptr;
ptr -> prev = pre;
ptr -> next = next;
next -> prev = ptr;
}
void create_list()
{
int lenth, i;
head = NULL;
printf("Input lenth:");
scanf("%d", &lenth);
for (i = 1; i <= lenth; i++)
{
printf("The number of node %d:\n", i);
if(i % 2 != 0)
{
if(i == 1)
{
head = create_node1();
init_list_head(head);
}
else
{
add_node(create_node1(), head -> prev, head);
}
}
else
{
add_node(create_node2(), head -> prev, head);
}
}
}
void show_all_node()
{
link_pointer* tmp = head;
int count = 1;
do
{
printf("The %dth node: ", count);
if (count % 2 != 0)
printf("%d\n", get_entry(tmp, link_node1, p) -> element1);
if (count % 2 == 0)
printf("%c\n", get_entry(tmp, link_node2, p) -> element2);
count++;
} while ((tmp = tmp -> next) != head);
}
int main()
{
setbuf(stdin,NULL);
create_list();
show_all_node();
return 0;
}