以前都是自己写链表或者所用框架都自带链表操作,本次工作换了框架没有找到框架自带的链表操作,所以尝试使用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#