Linux-struct list_head的快速使用

Linux-struct list_head的快速使用

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

初始化

LIST_HEAD_INIT

#define LIST_HEAD_INIT(name) { &(name), &(name) }//结合LIST_HEAD看,直接next,prev赋值
#define LIST_HEAD(name)
struct list_head name = LIST_HEAD_INIT(name)//定义并初始化链表头结点
例如:LIST_HEAD(test_head);

INIT_LIST_HEAD

#define INIT_LIST_HEAD(ptr) do { \ //初始化已经定义了的链表头结点
(ptr)->next = (ptr); (ptr)->prev = (ptr);
} while (0)
例如:MSG_Q_ID QId[MAX_Q]; INIT_LIST_HEAD(&QId[msgKey].MsgQ_List);

static inline void prefetch(const void *x) {;} //prefetch预读取  目前实现为空,或许是留下接口,方便实现真正的预读取逻辑

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

==========================================================
static __inline__ 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 __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
{
	__list_add(new, head->prev, head);//尾部结点:next是头部,prev是头部的prev
}
==========================================================
#define list_entry(ptr, type, member) \ //获取ptr->member
	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
==========================================================
#define list_for_each(pos, head) \ //遍历head链表
	for (pos = (head)->next, prefetch(pos->next); pos != (head); \
        	pos = pos->next, prefetch(pos->next))
#define list_for_each_safe(pos, n, head) \ //用了一个中间参数,safe?
	for (pos = (head)->next, n = pos->next; pos != (head); \
		pos = n, n = pos->next)
==========================================================
#define list_for_each_prev(pos, head) \ //前向遍历
	for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
        	pos = pos->prev, prefetch(pos->prev))
==========================================================

实例

LIST_HEAD和INIT_LIST_HEAD的区别:
1. 主要是初始化部分,变量是否已经定义
LIST_HEAD => struct list_head
INIT_LIST_HEAD => struct XXX
2. 需要注意的是:以下代码运行成功的前提是:第一个成员变量类型必须是list_head

#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#define WAY1
#undef WAY1
typedef struct test
{
    struct list_head L;
    int a;
    int b;
    int c;
} test;
int main()
{
#ifdef WAY1
    LIST_HEAD(TEST);
#else
    test TEST;
    INIT_LIST_HEAD(&TEST.L);
#endif
    int i;
    for (i = 1; i < 10; i++)
    {
        test *p = NULL;
        p = malloc(sizeof(test));
        if (p)
        {
            p->a = i;
            p->b = i;
            p->c = i;
#ifdef WAY1
            list_add_tail(p, &TEST);
#else
            list_add_tail(p, &TEST.L);
#endif
            printf("p->a:%d, p->b:%d, p->c:%d\n", p->a, p->b, p->c);
        }
    }
    struct list_head *pos = NULL;
#ifdef WAY1
    list_for_each(pos, &TEST)
#else
    list_for_each(pos, &TEST.L)
#endif
    {
        test *p2 = list_entry(pos, test, L);
        printf("p2->a:%d, p2->b:%d, p2->c:%d\n", p2->a, p2->b, p2->c);
    }
    return 0;
}

如果想设置第一个成员变量不是struct list_head类型,则如下修改:
区别就在于list_add_tail加入的结点需要指定成员。

#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#define WAY1
//#undef WAY1
typedef struct test
{    
    int a;
    int b;
    struct list_head L;
    int c;
} test;
int main()
{
#ifdef WAY1
    LIST_HEAD(TEST);
#else
    test TEST;
    INIT_LIST_HEAD(&TEST.L);
#endif
    int i;
    for (i = 1; i < 10; i++)
    {
        test *p = NULL;
        p = malloc(sizeof(test));
        if (p)
        {
            p->a = i;
            p->b = i;
            p->c = i;
#ifdef WAY1
            list_add_tail(&p->L, &TEST);//函数,参数都是指针
#else
            list_add_tail(&p->L, &TEST.L);
#endif
            printf("p->a:%d, p->b:%d, p->c:%d\n", p->a, p->b, p->c);
        }
    }
    struct list_head *pos = NULL;
#ifdef WAY1
    list_for_each(pos, &TEST)//pos就是list_head类型的遍历的迭代器
#else
    list_for_each(pos, &TEST.L)
#endif
    {
        test *p2 = list_entry(pos, test, L);//pos指向test类型的L成员变量
        printf("p2->a:%d, p2->b:%d, p2->c:%d\n", p2->a, p2->b, p2->c);
    }

#ifdef WAY1
    list_for_each(pos, &TEST)
#else
    list_for_each(pos, &TEST.L)
#endif
    {
        test * p3 = list_entry(pos, test, L);
        list_del(pos);
        free(p3);
    }
    return 0;
}

相关推荐

  1. Linux-struct list_head快速使用

    2024-06-08 06:16:04       28 阅读
  2. Linux收集内存快照使用crash分析方法

    2024-06-08 06:16:04       42 阅读
  3. 基于Linux系统使用Kind快速安装体验kubernetes

    2024-06-08 06:16:04       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-08 06:16:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 06:16:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 06:16:04       82 阅读
  4. Python语言-面向对象

    2024-06-08 06:16:04       91 阅读

热门阅读

  1. 调用plt函数报错not ‘KeyboardModifier’

    2024-06-08 06:16:04       28 阅读
  2. 理解和实现 LRU 缓存置换算法

    2024-06-08 06:16:04       28 阅读
  3. 【Numpy】04 深入理解NumPy的高级索引技术

    2024-06-08 06:16:04       35 阅读
  4. MYSQL内存占用查询语句

    2024-06-08 06:16:04       24 阅读
  5. springboot防止表单重复提交

    2024-06-08 06:16:04       28 阅读
  6. 0104__Linux 中 nm 命令简介

    2024-06-08 06:16:04       25 阅读
  7. [力扣题解] 28. 找出字符串中第一个匹配项的下标

    2024-06-08 06:16:04       22 阅读
  8. 相对路径vs绝对路径 python文件的添加与删除

    2024-06-08 06:16:04       32 阅读
  9. QT的窗口坐标和全局坐标

    2024-06-08 06:16:04       29 阅读
  10. mongodb 增删改查

    2024-06-08 06:16:04       31 阅读
  11. 在docker中运行mysql容器

    2024-06-08 06:16:04       29 阅读