`
dsea
  • 浏览: 43879 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

linux内核链表以及list_entry

 
阅读更多

链表是一些包含数据的独立数据结构的集合,链表中的每一个节点通过链或者指针连接在一起。程序通过指针访问链表中的节点。链表一般分为单链表和双链表。

 

1.单链表

 

单链表中,每个节点包含指向下一个节点的指针。链表最有一个节点的指针字段值为NULL,表明链表后面不再有其它节点。下面是一张单链表的图:

 

对应的数据结构为:

 

typedef struct NODE
{
	int value;
	struct NODE *next;		
}Node;
 

2.双链表

 

在一个双链表中,每个节点都包含两个指针——指向前一个节点的指针和指向后一个节点的指针。这样的好处是我们可以从任何方向遍历双链表。

对应的节点数据类型为:

 

typedef struct NODE
{
	int value;
	struct NODE *fwd;		
	struct NODE *bwd;
}Node;
 

3.linux内核链表

 

2.6.31.13内核版本作为分析基础。不同版本之间的区别不大。链表结构定义为(节选自include/linux/list.h):

 

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

  内核链表包含指向next和prev的指针,是一个双链表,不过不同于一般的双链表,内核链表不包含数据域,通常被用作双循环链表,当需要用到十字链表时,使用内核链表也很方便。

 

3.1 声明和初始化

 

linux内核提供了两种方式初始化链表。一种是使用LIST_HEAD()这个宏:

 

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

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

 

另外有一个内联函数用于运行时初始化:

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

3.2 添加、删除

下面都是些很基本的操作,只要弄清楚了链表的原理,都很容易理解。

 

/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
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);
}

/**
* 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);
}

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

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

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

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

static inline void list_move_tail(struct list_head *list,
                                  struct list_head *head)
{
        __list_del(list->prev, list->next);
        list_add_tail(list, head);
}

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

 

3.3 获取链表节点

 

linux链表中仅保存了list_head成员变量的地址,那么我们如何通过这个list_head的成员访问到它所有者节点的数据呢?linux提供了list_entry这个宏,ptr是指向该数据中list_head成员的指针,type是节点的类型,member是节点类型中list_head成员的变量名。

 

/**
* 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宏定义在include/linux/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) );})
 

offsetof在include/linux/stddef.h中

 

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

 

获得节点对象指针的原理图如下所示:

((type *)0)->member,它将0地址强制转换为type结构的指针,再访问到type结构中的member成员。offsetof取得list_head成员msg_node相对于结构体的偏移量。将指向当前节点对象member的地址减去偏移量,就可以得到节点地址,再将它转成指向节点结构类型的指针。

 

linux链表的基本操作已经完成了,其它如链表遍历的操作可查看list.h源码,有很详细的说明。

 

 

  • 大小: 17.2 KB
  • 大小: 21 KB
  • 大小: 12.1 KB
分享到:
评论

相关推荐

    C语言的双链表

    内核链表是一种链表,Linux内核中的链表都是用这种形式实现的 1.特性 内核链表是一种双向循环链表,内核链表的节点节点结构中只有指针域 使用内核链表的时候,将内核链表作为一个成员放入到一个结构体中使用 ...

    内核数据结构之双向循环链表

    从初学者的角度深入的分析了内核数据结构的双向循环链表 list_head结构如何构建链表 分析了list_entry LIST_HEAD list_for_each 并最后给出了一个例子以便学习

    C语言的单链表

    内核链表是一种链表,Linux内核中的链表都是用这种形式实现的 1.特性 内核链表是一种双向循环链表,内核链表的节点节点结构中只有指针域 使用内核链表的时候,将内核链表作为一个成员放入到一个结构体中使用 ...

    从汇编语言到Windows内核编程

    4.2.2 使用LIST_ENTRY 4.2.3 使用长长整型数据 4.2.4 使用自旋锁 第5章 文件与注册表操作 5.1 文件操作 5.1.1 使片OBJECT_ATTRIBUTES 5.1.2 打开和关闭文件 5.1.3 文件读,写操作 5.2 注册表操作 5.2.1 注册表键的...

    Windows驱动编程基础教程-楚狂人.doc

    2.2 使用LIST_ENTRY 12 2.3 使用长长整型数据 14 2.4使用自旋锁 15 第三章 文件操作 18 3.1 使用OBJECT_ATTRIBUTES 18 3.2 打开和关闭文件 18 3.3 文件的读写操作 21 第四章 操作注册表 25 4.1 注册键的打开操作 25 ...

    天书夜读:从汇编语言到Windows内核编程(完整版一)

     4.2.2 使用LIST_ENTRY 46  4.2.3 使用长长整型数据 49  4.2.4 使用自选锁 50  第5章 文件与注册表操作 52  5.1 文件操作 53  5.1.1 使用OBJECT_ATTRIBUTES 53  5.1.2 打开和关闭文件 54  5.1.3 文件读/写...

    天书夜读:从汇编语言到Windows内核编程(完整版 二)

     4.2.2 使用LIST_ENTRY 46  4.2.3 使用长长整型数据 49  4.2.4 使用自选锁 50  第5章 文件与注册表操作 52  5.1 文件操作 53  5.1.1 使用OBJECT_ATTRIBUTES 53  5.1.2 打开和关闭文件 54  5.1.3 文件读/写...

    Windows驱动编程基础教程.pdf

    2.2 使用LIST_ENTRY 12 2.3 使用长长整型数据 14 2.4使用自旋锁 15 第三章 文件操作 18 3.1 使用OBJECT_ATTRIBUTES 18 3.2 打开和关闭文件 18 3.3 文件的读写操作 21 第四章 操作注册表 25 4.1 注册键的打开操作 25 ...

    天书夜谈:从汇编语言到Windows内核编程

     4.2.2 使用LIST_ENTRY 46  4.2.3 使用长长整型数据 49  4.2.4 使用自选锁 50  第5章 文件与注册表操作 52  5.1 文件操作 53  5.1.1 使用OBJECT_ATTRIBUTES 53  5.1.2 打开和关闭文件 54  5.1.3 文件读/写...

    Windows内核安全与驱动开发光盘源码

    3.2.2 使用LIST_ENTRY 41 3.2.3 使用长长整型数据 43 3.3 自旋锁 44 3.3.1 使用自旋锁 44 3.3.2 在双向链表中使用自旋锁 45 3.3.3 使用队列自旋锁提高性能 46 第4章 文件、注册表、线程 47 4.1 文件操作 47 ...

    Windows内核安全驱动开发(随书光盘)

    3.2.2 使用LIST_ENTRY 41 3.2.3 使用长长整型数据 43 3.3 自旋锁 44 3.3.1 使用自旋锁 44 3.3.2 在双向链表中使用自旋锁 45 3.3.3 使用队列自旋锁提高性能 46 第4章 文件、注册表、线程 47 4.1 文件操作 47 ...

    0day学习笔记(3)Windows定位API引起的惨案(原理)

    WinNT内核下内存采用保护模式,段寄存器的意义与实模式汇编下的意义不同.另外,FS存的是段选择子,而不是实模式下的高16位基地址。 FS寄存器指向当前活动线程的TEB结构(线程结构) 下面为FS寄存器下偏移的相关信息...

Global site tag (gtag.js) - Google Analytics