list_head用法(一)




最近在准备“进攻”linux驱动的 学习,在内核代码里存在着大量的链表,其中list_head用法经常在Linux的kernel里使用。经过网上各位前人的学习笔记,转载一篇记录一下知识点
(1)list.h文件在include/linux/list.h

#ifndef _LIST_H
#define _LIST_H

#define _INLINE_ static inline

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

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

#define LIST_HEAD(name) \ //定义并初始化头结点head
    struct list_head name = LIST_HEAD_INIT(name)

#define INIT_LIST_HEAD(ptr) do {\             //初始化头结点ptr,因此需要首先定义ptr
    (ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)

_INLINE_ void __list_add(struct list_head *add,
        struct list_head *prev,
        struct list_head *next)
{
    next->prev = add;
    add->next = next;
    add->prev = prev;
    prev->next = add;
}

_INLINE_ void list_add(struct list_head *add, struct list_head *head)//每次添加节点到head之后,始终都是添加到头结点之后
{
    __list_add(add, head, head->next);
}

_INLINE_ void list_add_tail(struct list_head *add, struct list_head *head)//每次添加节点都是头结点之前,由于是循环链表,就是说添加到链表尾部
{
    __list_add(add, head->prev, head);
}

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

_INLINE_ void list_del(struct list_head *entry)//删除节点
{
    __list_del(entry->prev, entry->next);
}

_INLINE_ void list_del_init(struct list_head *entry)
//删除节点,并初始化被删除的结点(也就是使被删除的结点的prev和next都指向自己)
{
    __list_del(entry->prev, entry->next);
    INIT_LIST_HEAD(entry);
}

_INLINE_ int list_empty(struct list_head *head)//判断链表是否为空
{
    return head->next == head;
}

_INLINE_ void list_splice(struct list_head *list, struct list_head *head)
//通过两个链表的head,进行连接
{
    struct list_head *first = list->next;

    if (first != list) {
        struct list_head *last = list->prev;
        struct list_head *at = head->next;

        first->prev = head;
        head->next = first;

        last->next = at;
        at->prev = last;
    }
}

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

#define list_for_each(pos, head) \                                        
//遍历链表,此时删除节点的操作可能会出错
    for (pos = (head)->next; pos != (head); pos = pos->next) //新代码中出现prefetch() 可以不考虑,用于预取以提高遍历速度

#define list_for_each_safe(pos, pnext, head) \      //遍历链表,可以同时有删除节点的操作
    for (pos = (head)->next, pnext = pos->next; pos != (head); \
            pos = pnext, pnext = pos->next)

#undef _INLINE_
#endif
(2)写一个测试程序,利用各个函数实现功能

点击(此处)折叠或打开
#include<stdio.h>
#include<stdlib.h>
#include "list.h"

struct int_node
{
    int val;
    struct list_head list;
};

int main()
{
    struct list_head head,*plist;
    struct int_node a,b,c;

    a.val = 1;
    b.val = 2;
    c.val = 3;

    INIT_LIST_HEAD(&head);            //初始化链表头
    list_add_tail(&a.list,&head);     //添加节点
    list_add_tail(&b.list,&head);
    list_add_tail(&c.list,&head);

    list_for_each(plist,&head)//遍历链表,打印结果
    {
        struct int_node *node = list_entry(plist,struct int_node,list);//然后取得数据项,因此一般来说和list_for_each配合使用
        printf("val = %d\n",node->val);
    }//print 1 2 3 

    printf("*******************************************\n");
    list_del_init(&b.list);            //删除节点b
    list_for_each(plist,&head)         //重新遍历链表,打印结果
    {
        struct int_node *node = list_entry(plist,struct int_node,list);
        printf("val = %d\n",node->val);
    }//print 1 3 

    printf("*******************************************\n");
    struct int_node d,e;
    struct list_head head1;
    d.val = 4;
    e.val = 5;
    INIT_LIST_HEAD(&head1);            //重新建立链表,表头为head1
    list_add_tail(&d.list,&head1);
    list_add_tail(&e.list,&head1);

    list_splice(&head1,&head);        //把两个链表进行连接
    list_for_each(plist,&head)
    {
        struct int_node *node = list_entry(plist,struct int_node,list);
        printf("val = %d\n",node->val);
    }//print 4 5 1 3

    printf("*******************************************\n");
    if(!list_empty(&head))          //判断链表是否为空
    {
        printf("the list is not empty!\n");

    }

    return 0;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157

(2)写一个测试程序,利用各个函数实现功能

#include<stdio.h>
#include<stdlib.h>
#include "list.h"

struct int_node
{
    int val;
    struct list_head list;
};

int main()
{
    struct list_head head,*plist;
    struct int_node a,b,c;

    a.val = 1;
    b.val = 2;
    c.val = 3;

    INIT_LIST_HEAD(&head);            //初始化链表头
    list_add_tail(&a.list,&head);     //添加节点
    list_add_tail(&b.list,&head);
    list_add_tail(&c.list,&head);

    list_for_each(plist,&head)//遍历链表,打印结果
    {
        struct int_node *node = list_entry(plist,struct int_node,list);//然后取得数据项,因此一般来说和list_for_each配合使用
        printf("val = %d\n",node->val);
    }//print 1 2 3 

    printf("*******************************************\n");
    list_del_init(&b.list);            //删除节点b
    list_for_each(plist,&head)         //重新遍历链表,打印结果
    {
        struct int_node *node = list_entry(plist,struct int_node,list);
        printf("val = %d\n",node->val);
    }//print 1 3 

    printf("*******************************************\n");
    struct int_node d,e;
    struct list_head head1;
    d.val = 4;
    e.val = 5;
    INIT_LIST_HEAD(&head1);            //重新建立链表,表头为head1
    list_add_tail(&d.list,&head1);
    list_add_tail(&e.list,&head1);

    list_splice(&head1,&head);        //把两个链表进行连接
    list_for_each(plist,&head)
    {
        struct int_node *node = list_entry(plist,struct int_node,list);
        printf("val = %d\n",node->val);
    }//print 4 5 1 3

    printf("*******************************************\n");
    if(!list_empty(&head))          //判断链表是否为空
    {
        printf("the list is not empty!\n");

    }

    return 0;
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

输出结果如下:
val = 1
val = 2
val = 3


val = 1
val = 3


val = 4
val = 5
val = 1
val = 3


the list is not empty!

(3)list_for_each()与list_for_each_safe()

#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next)
 
 
  • 1
  • 2
  • 3

由定义可知,list_del(pos)(将pos的前后指针指向undefined state)panic,list_del_init(pos)(将pos前后指针指向自身)导致死循环。

#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
 
 
  • 1
  • 2
  • 3

由定义可知,safe函数首先将pos的后指针缓存到n,处理一个流程后再赋回pos,避免了这种情况的发生。
因此只遍历链表不删除节点时可以使用前者,若有删除节点的操作,则要使用后者。

由safe的说明可知,是专门为删除节点时准备的:iterate over a list safe against removal of list entry。其他带safe的处理也基本源于这个原因




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值