Linux 中list.h使用实例和坑

以前都是自己写链表或者所用框架都自带链表操作,本次工作换了框架没有找到框架自带的链表操作,所以尝试使用linux自带的list.h中定义的相关宏和函数写了简单的链表操作,竟然踩坑了,记录一下。

一、list.h简介

list.h一般放在include/list.h,当然有的linux系统放list.h地方在别的地方,比如ubuntu。可以使用find / -name list.h找到,使用gcc编译的时候include上。

list.h是Linux内核中,为了提供统一的链表操作,减少结构体的额外开支提供的链表操作。list.h中定义的链表了一个双向循环链表,和一个哈希表。

1,基本结构

cpp 复制代码
struct list_head {
        struct list_head *next, *prev;
};

2,初始化定义

cpp 复制代码
#define LIST_HEAD_INIT(name) { &(name), &(name) }

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

3,功能定义

cpp 复制代码
/**添加节点
 * 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);
}

/**删除节点
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 * Note: list_empty() on entry does not return true after this, the entry is
 * in an undefined state.
 */
static inline void list_del(struct list_head *entry)
{
        __list_del(entry->prev, entry->next);
        entry->next = (struct list_head*)LIST_POISON1;
        entry->prev = (struct list_head*)LIST_POISON2;
}

/**链表判断
 * 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;
}

4,查询定义

cpp 复制代码
/** 直接读取节点的查询方法1
 * 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_head 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))

/**直接读取节点的查询方法2
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:        the type * to use as a loop cursor.
 * @n:          another type * to use as temporary storage
 * @head:       the head for your list.
 * @member:     the name of the list_head within the struct.
 */
#define list_for_each_entry_safe(pos, n, head, member)                  \
        for (pos = list_entry((head)->next, typeof(*pos), member),      \
                n = list_entry(pos->member.next, typeof(*pos), member); \
             &pos->member != (head);                                    \
             pos = n, n = list_entry(n->member.next, typeof(*n), member))

二、实战

程序大概的意思是:创建一个5个成员的stu链表,打印;删除其中一个成员,再打印。带参数的可执行程序,./test 1:标识删除第一个,后面数字表示删除第几个。

cpp 复制代码
#include "list.h"
#include <stdlib.h>
#include <stdio.h>

typedef struct _ststudent_
{
        int age;
        int grade;
}STU_T;

typedef struct _stulist_
{
        STU_T stStu;
        struct list_head list;
}STU_LIST_T;

LIST_HEAD(g_stStuList_Head);
void main(int argc,char *argv[])
{
        STU_LIST_T *pstStu=NULL;
        STU_LIST_T *pstStu_out=NULL;
        STU_LIST_T *n=NULL;
        int i=0;
        for(i=0;i<5;i++)
        {
                pstStu=(STU_LIST_T*)malloc(sizeof(STU_LIST_T));
                pstStu->stStu.age=i+10;
                pstStu->stStu.grade=i+100;
                list_add_tail(&pstStu->list,&g_stStuList_Head);
        }

        printf("----------------add---------------------\n");
        list_for_each_entry(pstStu_out,&g_stStuList_Head,list)
        {
                printf("pstStu_out->stStu.age=%d,grade=%d\n",pstStu_out->stStu.age,pstStu_out->stStu.grade);
        }


        printf("----------------del---------------------\n");

#ifdef TEST_TRUE
        list_for_each_entry(pstStu_out,&g_stStuList_Head,list)
        {
                if(atoi(argv[1])==pstStu_out->stStu.age-10)
                {
                        list_del(&pstStu_out->list);
                        free(pstStu_out);
                        continue;
                }
                printf("pstStu_out->stStu.age=%d,grade=%d\n",pstStu_out->stStu.age,pstStu_out->stStu.grade);

        }

#else

        list_for_each_entry_safe(pstStu_out,n,&g_stStuList_Head,list)
        {
                if(atoi(argv[1])==pstStu_out->stStu.age-10)
                {
                        list_del(&pstStu_out->list);
                        free(pstStu_out);
                        continue;
                }
                printf("pstStu_out->stStu.age=%d,grade=%d\n",pstStu_out->stStu.age,pstStu_out->stStu.grade);

        }
#endif
        return;
}

1,为啥不用#include <list.h> ?

因为我自己系统带的list.h不在标准库。。

bash 复制代码
root@ubuntu:/home/Ctest/clistTest# find / -name list.h
/lib/firmware/carl9170fw/tools/include/list.h
find: '/run/user/1000/gvfs': Permission denied
/usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-150/include/linux/list.h
/usr/src/linux-headers-5.4.0-150-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-150-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-150-generic/include/config/system/revocation/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-136/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-136/include/linux/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-135/scripts/kconfig/list.h
/usr/src/linux-hwe-5.4-headers-5.4.0-135/include/linux/list.h
/usr/src/linux-headers-5.4.0-135-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-135-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-135-generic/include/config/system/revocation/list.h
/usr/src/linux-headers-5.4.0-136-generic/scripts/kconfig/list.h
/usr/src/linux-headers-5.4.0-136-generic/include/config/system/blacklist/hash/list.h
/usr/src/linux-headers-5.4.0-136-generic/include/config/system/revocation/list.h

2,编译

bash 复制代码
 gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ -DTEST_TRUE

3,-D是为了方便测试 预编译宏。

用来分别编译两个可执行程序说明遇到的坑

4,运行结果,带参数运行

坑:如果只查询使用list_for_each_entry()list_for_each_entry_safe()都可以,但是如果查出来要进行删除操作就需要用list_for_each_entry_safe(),否则会宕机。具体原因可以自己查看实现。

bash 复制代码
root@ubuntu:/home/Ctest/clistTest# gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ -DTEST_TRUE
root@ubuntu:/home/Ctest/clistTest# ./test 1
----------------add---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=11,grade=101
pstStu_out->stStu.age=12,grade=102
pstStu_out->stStu.age=13,grade=103
pstStu_out->stStu.age=14,grade=104
----------------del---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=593,grade=0
Segmentation fault (core dumped)
root@ubuntu:/home/Ctest/clistTest# gcc -o test listtest.c -I /usr/src/linux-hwe-5.4-headers-5.4.0-150/scripts/kconfig/ 
root@ubuntu:/home/Ctest/clistTest# ./test 1
----------------add---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=11,grade=101
pstStu_out->stStu.age=12,grade=102
pstStu_out->stStu.age=13,grade=103
pstStu_out->stStu.age=14,grade=104
----------------del---------------------
pstStu_out->stStu.age=10,grade=100
pstStu_out->stStu.age=12,grade=102
pstStu_out->stStu.age=13,grade=103
pstStu_out->stStu.age=14,grade=104
root@ubuntu:/home/Ctest/clistTest# 
相关推荐
scoone4 分钟前
ssh登陆服务器后支持Tab键命令补全
linux·shell
微刻时光11 分钟前
Docker部署Nginx
运维·nginx·docker·容器·经验
小安运维日记26 分钟前
CKA认证 | Day3 K8s管理应用生命周期(上)
运维·云原生·容器·kubernetes·云计算·k8s
小han的日常1 小时前
接口自动化环境搭建
运维·自动化
小扳1 小时前
Docker 篇-Docker 详细安装、了解和使用 Docker 核心功能(数据卷、自定义镜像 Dockerfile、网络)
运维·spring boot·后端·mysql·spring cloud·docker·容器
运维小文1 小时前
服务器硬件介绍
运维·服务器·计算机网络·缓存·硬件架构
是安迪吖1 小时前
nfs服务器
运维·服务器
鱼骨不是鱼翅1 小时前
模拟回显服务器
运维·服务器
运维佬1 小时前
CentOS 9 配置网卡
linux·centos
轩轩曲觞阁1 小时前
Linux网络——网络初识
linux·网络