linux内核链表

版权声明:本文为博主原创文章,未经博主允许不得转载。
https://blog.csdn.net/huangweiqing80/article/details/83046837

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

1、前言

   链表是一种常见的数据结构。它可以动态的进行存储分配的一种结构。用数组存放数据时,必须事先定义固定的数组长度(即元素个数)。如果有的班级有100人,而有的班级只有30人,若用同一个数组先后存放不同班级的学生数据,则必须定义长度为100的数组。如果事先难以确定一个班的最多人数,则必须把数组定得足够大,以便能存放任何班级的学生数据,显然这将会浪费内存。链表则没有这种缺点,它根据需要开辟内存单元。

在这里插入图片描述

链表有一个“头指针”变量,图中以head表示,它存放了一个地址,该地址指向一个元素。链表中每一个元素称为“结点”,每个结点都应包含两个部分:用户需要用的数据、下一个结点的地址。这样,head结点指向第一元素,第一个元素指向第二个元素…直到最后一个元素

链表由指针域和数据域构成,指针域用来指向前后的元素,数据域用来存放元素数据

2、链表介绍

  链表是非常基本的数据结构,根据链个数分为单链表、双链表,根据是否循环分为单向链表和循环链表。通常定义定义链表结构如下:

typedef struct node
{
     ElemType data;      //数据域
     struct node *next;  //指针域
}node, *list;

链表中包含数据域和指针域。链表通常包含一个头结点,不存放数据,方便链表操作。单向循环链表结构如下图所示:

双向循环链表结构如下图所示:

  这样带数据域的链表降低了链表的通用性,不容易扩展。linux内核定义的链表结构不带数据域,只需要两个指针完成链表的操作。将链表节点加入数据结构,具备非常高的扩展性,通用性。链表结构定义如下所示:

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

链表结构如下所示:

  需要用链表结构时,只需要在结构体中定义一个链表类型的数据即可。例如定义一个app_info链表,

typedef struct application_info
{
    uint32_t  app_id;
    uint32_t  up_flow;
    uint32_t  down_flow;
    struct    list_head app_info_head;  //链表节点
}app_info;

定义一个app_info链表,app_info app_info_list;通过app_info_head进行链表操作。根据C语言指针操作,通过container_of和offsetof,可以根据app_info_head的地址找出app_info的起始地址,即一个完整ap_info结构的起始地址。可以参考:http://www.cnblogs.com/Anker/p/3472271.html

3、linux内核链表实现

  内核实现的是双向循环链表,提供了链表操作的基本功能。

(1)初始化链表头结点

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

两种初始化链表头节点方式。LIST_HEAD_INIT和INIT_LIST_HEAD都是对链表进行初始化,都是使得前驱和后继指针指针指向头结点。

LIST_HEAD宏创建一个链表头结点,用LIST_HEAD_INIT宏对头结点进行赋值,使得头结点的前驱和后继指向自己。

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

static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}

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

  插入节点分为从链表头部插入list_add和链表尾部插入list_add_tail,通过调用__list_add函数进行实现,head->next指向之一个节点,head->prev指向尾部节点。

(3)删除节点

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

  从链表中删除一个节点,需要改变该节点前驱节点的后继结点和后继结点的前驱节点。最后设置该节点的前驱节点和后继结点指向LIST_POSITION1和LIST_POSITION2两个特殊值,这样设置是为了保证不在链表中的节点项不可访问,对LIST_POSITION1和LIST_POSITION2的访问都将引起页故障

/*
 * These are non-NULL pointers that will result in page faults
 * under normal circumstances, used to verify that nobody uses
 * non-initialized list entries.
 */
#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)

(4)移动节点

/**
 * 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_move_tail - delete from one list and add as another's tail
 * @list: the entry to move
 * @head: the head that will follow our entry
 */
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);
}

move将一个节点移动到头部或者尾部。

(5)判断链表

/**
 * list_is_last - tests whether @list is the last entry in list @head
 * @list: the entry to test
 * @head: the head of the list
 */
static inline int list_is_last(const struct list_head *list,
                const struct list_head *head)
{
    return list->next == 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_is_last函数判断节点是否为末尾节点,list_empty判断链表是否为空。

(6)遍历链表

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

/**
 * list_first_entry - get the first element from a list
 * @ptr:    the list head to take the element from.
 * @type:    the type of the struct this is embedded in.
 * @member:    the name of the list_struct within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define list_first_entry(ptr, type, member) \
    list_entry((ptr)->next, type, member)

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

list_entry获取链表的结构,包括数据域,由ptr这个节点地址得到它所在的type类型结构体的地。
list_first_entry获取链表第一个节点所在容器。
list_for_each宏对链表节点进行遍历,遍历head为链表头的整个链表,pos是个中间变量。

list_entry函数解析
list_entry函数其实是从一个结构的成员指针找到其容器的指针。即寻找容器

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

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

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

ptr是找容器的那个变量的指针,即链表的指针,把它减去自己在容器中的偏移量的值就应该 得到容器的指针。(容器就是包含自己的那个结构)。指针的加减要注意类型,用(char*)ptr是为了计算字节偏移。((type *)0)->member是一个小技巧。自己理解吧。前面的(type *)再转回容器的类型。

list_entry最终的定义如下

#define list_entry(ptr, type, member) \
        ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

ptr是指向list_head类型链表的指针,type为一个结构,而member为结构type中的一个域,类型为list_head,这个宏返回指向type结构的指针。**所以list_head是由ptr这个节点地址得到它所在的type类型结构体的地址。**在内核代码中大量引用了这个宏,因此,搞清楚这个宏的含义和用法非常重要。

list_for_each_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))

list_for_each_entry 是遍历head为链表头的链表容器

总结
list_entry获取链表的结构,包括数据域,由ptr这个节点地址得到它所在的type类型结构体的地。
list_first_entry获取链表第一个节点所在容器。
list_for_each宏对链表节点进行遍历,遍历head为链表头的整个链表,pos是个中间变量。
list_for_each_entry 是遍历head为链表头的链表容器

4、测试例子

编写一个简单使用链表的程序,从而掌握链表的使用。

自定义个类似的list结构如下所示:mylist.h

# define POISON_POINTER_DELTA 0

#define LIST_POISON1  ((void *) 0x00100100 + POISON_POINTER_DELTA)
#define LIST_POISON2  ((void *) 0x00200200 + POISON_POINTER_DELTA)

//计算member在type中的位置
#define offsetof(type, member)  (size_t)(&((type*)0)->member)
//根据member的地址获取type的起始地址
#define container_of(ptr, type, member) ({          \
        const typeof(((type *)0)->member)*__mptr = (ptr);    \
    (type *)((char *)__mptr - offsetof(type, member)); })

//链表结构
struct list_head
{
    struct list_head *prev;
    struct list_head *next;
};

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

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

//从头部添加
static inline void list_add(struct list_head *new , struct list_head *head)
{
    __list_add(new, head, head->next);
}
//从尾部添加
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)
{
    prev->next = next;
    next->prev = prev;
}

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_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);
}
#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)

#define list_first_entry(ptr, type, member) \
    list_entry((ptr)->next, type, member)

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

mylist.c如下所示:

/**@brief 练习使用linux内核链表,功能包括:
 * 定义链表结构,创建链表、插入节点、删除节点、移动节点、遍历节点
 *
 *@auther Anker @date 2013-12-15
 **/
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <errno.h>
#include "mylist.h"
//定义app_info链表结构
typedef struct application_info
{
    uint32_t  app_id;
    uint32_t  up_flow;
    uint32_t  down_flow;
    struct    list_head app_info_node;//链表节点
}app_info;


app_info* get_app_info(uint32_t app_id, uint32_t up_flow, uint32_t down_flow)
{
    app_info *app = (app_info*)malloc(sizeof(app_info));
    if (app == NULL)
    {
    fprintf(stderr, "Failed to malloc memory, errno:%u, reason:%s\n",
        errno, strerror(errno));
    return NULL;
    }
    app->app_id = app_id;
    app->up_flow = up_flow;
    app->down_flow = down_flow;
    return app;
}
static void for_each_app(const struct list_head *head)
{
    struct list_head *pos;
    app_info *app;
    //遍历链表
    list_for_each(pos, head)
    {
    app = list_entry(pos, app_info, app_info_node);
    printf("ap_id: %u\tup_flow: %u\tdown_flow: %u\n",
        app->app_id, app->up_flow, app->down_flow);

    }
}

void destroy_app_list(struct list_head *head)
{
    struct list_head *pos = head->next;
    struct list_head *tmp = NULL;
    while (pos != head)
    {
    tmp = pos->next;
    list_del(pos);
    pos = tmp;
    }
}


int main()
{
    //创建一个app_info
    app_info * app_info_list = (app_info*)malloc(sizeof(app_info));
    app_info *app;
    if (app_info_list == NULL)
    {
    fprintf(stderr, "Failed to malloc memory, errno:%u, reason:%s\n",
        errno, strerror(errno));
    return -1;
    }
    //初始化链表头部
    struct list_head *head = &app_info_list->app_info_node;
    init_list_head(head);
    //插入三个app_info
    app = get_app_info(1001, 100, 200);
    list_add_tail(&app->app_info_node, head);
    app = get_app_info(1002, 80, 100);
    list_add_tail(&app->app_info_node, head);
    app = get_app_info(1003, 90, 120);
    list_add_tail(&app->app_info_node, head);
    printf("After insert three app_info: \n");
    for_each_app(head);
    //将第一个节点移到末尾
    printf("Move first node to tail:\n");
    list_move_tail(head->next, head);
    for_each_app(head);
    //删除最后一个节点
    printf("Delete the last node:\n");
    list_del(head->prev);
    for_each_app(head);
    destroy_app_list(head);
    free(app_info_list);
    return 0;
}

测试结果如下所示:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值