Linux内核双向循环链表 list_head 部分功能重构

一 为什么要重构Linux中的双向循环链表?
第一,首先 Linux内核双向循环链表的实现依赖于GNU编译器,里面有一些语法是GUN编译器的语法,标准C编译器不能使用它。所以需要清除平台相关代码。

1 ({})
2 typeof()
3 _builtin_prefetch() 用于优化代码
4 static inline GNU编译器中可以这样组合是用,但是标准C中不可以这样使用

第二,需要清除文件中的依赖

// #include <linux/types.h>
// #include <linux/stddef.h>
// #include <linux/poison.h>
// #include <linux/prefetch.h>

Linux内核链表的实现

1 带头结点的双向循环链表,并且,头结点是链表中的成员
2 头结点的next指针指向首节点
3 头结点的prev指针指向尾节点

在这里插入图片描述Linux 内核链表的节点的定义

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

Linux 内核链表的节点的使用

//使用 struct list_head 自定义链表节点
struct Node
{
struct list_head head;
Type1 value1;
Type2 value2;
//...
};

Linux 内核链表的创建及初始化

struct Node
{
struct list_head head;
int value;
};

int main(void)
{
	struct Node L = {0};
	struct list_head* list = (struct list_head*)&L;

	INIT_LIST_HEAD(list);
	//...
}

关于内核链表的几点注意:

1 关于INIT_LIST_HEAD() 初始化头结点

//初始化头结点 让头结点自己变成一个双向循环链表
static void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

在这里插入图片描述

2 关于 Linux 内核链表的插入操作

1 在链表头部插入: list_add(new,head)
2 在链表尾部插入: list_add_tail(new,head)


//new插入位置:prev -> new ->next
static 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 void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}


/*
  向链表尾插入新节点
 */
static void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}

在这里插入图片描述

3 关于 Linux 内核链表的删除操作

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

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

在这里插入图片描述

4 关于Linux内核链表遍历操作

1 正向遍历
2 反向遍历 


/*
正向遍历链表
 */
#define list_for_each(pos, head) \
    for (pos = (head)->next; prefetch(pos->next), pos != (head); \
            pos = pos->next)

/*
反向遍历链表
 */
#define list_for_each_prev(pos, head) \
    for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
            pos = pos->prev)

双向链表部分功能移植:

LinuxList.h

#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H

// #include <linux/types.h>
// #include <linux/stddef.h>
// #include <linux/poison.h>
// #include <linux/prefetch.h>

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

//修改 container_of,去除依赖GUN编译器的类型检查一行
#ifndef container_of
#define container_of(ptr, type, member) ((type *)((char *)ptr - offsetof(type,member)))
#endif

//去除依赖
#define prefetch(x) ((void)x)
//去除依赖
#define LIST_POISON1  (NULL)
#define LIST_POISON2  (NULL)

//节点
struct list_head {
    struct list_head *next, *prev;
};

//初始化头结点 让头结点自己变成一个双向循环链表
static void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}


#ifndef CONFIG_DEBUG_LIST

//new插入位置:prev -> new ->next
static 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;
}
#else
extern void __list_add(struct list_head *new,
                  struct list_head *prev,
                  struct list_head *next);
#endif

/*
  向链表头插入新节点
 */
static void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}


/*
  向链表尾插入新节点
 */
static void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}

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

/*
  list_entry == container_of
*/
#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)


/*
遍历链表
 */
#define list_for_each(pos, head) \
    for (pos = (head)->next; prefetch(pos->next), pos != (head); \
            pos = pos->next)


#endif

main.c

#include <stdio.h>
#include "LinuxList.h"

//struct  list_head放在自定义节点首位置
void list_demo_1()
{
    //节点类型,list_head放在节点首
    struct Node
    {
        struct list_head head;
        int value;
    };

    //创建头结点
    struct Node l = {0};
    //取头结点 list_head 地址,由于list_head位于自定义节点头,所以可以这样强制类型转换
    struct list_head* list = (struct list_head*)&l;

    struct list_head* slider = NULL;
    int i = 0;

    //将头结点自己设置为双向循环链表
    INIT_LIST_HEAD(list);

    printf("Insert begin ...\n");

    for(i=0; i<5; i++)
    {
        //创建新节点
        struct Node* n = (struct Node*)malloc(sizeof(struct Node));

        //初始化节点 value值
        n->value = i;

        //将新节点插入到链表尾部,由于list_head位于自定义节点头,所以可以这样强制类型转换
        list_add_tail((struct list_head*)n, list);
    }

    //用slider指针遍历 list链表
    list_for_each(slider, list)
    {
        //访问链表指针所在节点的value,由于list_head位于自定义节点头,所以可以这样强制类型转换
        printf("%d\n", ((struct Node*)slider)->value);
    }

    printf("Insert end ...\n");

    printf("Delete begin ...\n");

    //用slider指针遍历 list链表
    list_for_each(slider, list)
    {
        //删除value为3  的节点,由于list_head位于自定义节点头,所以可以这样强制类型转换
        if( ((struct Node*)slider)->value == 3 )
        {
            list_del(slider);
            free(slider);
            break;
        }
    }

    list_for_each(slider, list)
    {
        printf("%d\n", ((struct Node*)slider)->value);
    }

    printf("Delete end ...\n");
}

//struct  list_head放在自定义节点尾位置
void list_demo_2()
{
    //节点类型,list_head放在节点尾
    struct Node
    {
        int value;
        struct list_head head;
    };
    //创建头结点
    struct Node l = {0};
    //取头结点 list_head 地址
    struct list_head* list = &l.head;
    struct list_head* slider = NULL;
    int i = 0;

    //将头结点自己设置为双向循环链表
    INIT_LIST_HEAD(list);

    printf("Insert begin ...\n");

    for(i=0; i<5; i++)
    {
        struct Node* n = (struct Node*)malloc(sizeof(struct Node));

        n->value = i;

        //将新节点插入到链表头部,list_head位于自定义节点尾
        list_add(&n->head, list);
    }

    list_for_each(slider, list)
    {
        //list_entry == container_of, slide 指向Node节点中的 head成员,得到Node首地址
        printf("%d\n", list_entry(slider, struct Node, head)->value);
    }

    printf("Insert end ...\n");


    printf("Delete begin ...\n");

    list_for_each(slider, list)
    {
         //list_entry == container_of, slide 指向Node节点中的 head成员,得到Node首地址
        struct Node* n = list_entry(slider, struct Node, head);

        if( n->value == 3 )
        {
            list_del(slider);
            free(n);
            break;
        }
    }

    list_for_each(slider, list)
    {
        //list_entry == container_of, slide 指向Node节点中的 head成员,得到Node首地址
        printf("%d\n", list_entry(slider, struct Node, head)->value);
    }

    printf("Delete end ...\n");
}

int main()
{
     list_demo_1();
    // list_demo_2();

    return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux老A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值