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# 
相关推荐
Dola_Pan21 分钟前
Linux文件IO(二)-文件操作使用详解
java·linux·服务器
wang_book24 分钟前
Gitlab学习(007 gitlab项目操作)
java·运维·git·学习·spring·gitlab
孤寂大仙v1 小时前
【C++】STL----list常见用法
开发语言·c++·list
prcyang1 小时前
Docker Compose
运维·docker·容器
脚踏实地的大梦想家1 小时前
【Docker】安装全流程与配置完整镜像源(可安装 nginx)
运维·docker·容器
城南云小白2 小时前
Linux网络服务只iptables防火墙工具
linux·服务器·网络
咩咩大主教2 小时前
C++基于select和epoll的TCP服务器
linux·服务器·c语言·开发语言·c++·tcp/ip·io多路复用
Zww08912 小时前
docker部署个人网页导航
运维·docker·容器
Flying_Fish_roe2 小时前
linux-网络管理-网络配置
linux·网络·php
运维小白。。2 小时前
Nginx 反向代理
运维·服务器·nginx·http