linux中的list_head

本文深入探讨了Linux内核中list_head双向链表的使用方式,包括基本操作、list_entry与container_of的原理分析及应用实例。详细解释了如何在C语言环境下高效地管理和操作链表数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0. 介绍:
   位置: include/linux/list.h
   作用:    

1. list_head的基本操作:

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

#define LIST_HEAD_INIT(name) { &(name), &(name) }

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

static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

void list_add(struct list_head *new, struct list_head *head);            //在head后面添加节点
void list_add_tail(struct list_head *new, struct list_head *head);        //在head的前面添加节点;
void list_replace(struct list_head *old, struct list_head *new);        //替换新旧节点
void list_move(struct list_head *list, struct list_head *head);            //把旧的list删除,然后,移动到新的head后面
void list_move_tail(struct list_head *list, struct list_head *head);    //同上,不过是移动到新的head前面
void list_del(struct list_head *entry);                                          //删除当前节点

int list_is_last(const struct list_head *list, const struct list_head *head);    //测试list的next是否是head
int list_empty(const struct list_head *head);                                         //测试head的下一个是否指向自己;指向自己就意味着空;
int list_empty_careful(const struct list_head *head);                            //测试该head是否指向自己, 下一个与前一个都是自己;

2. list_entry的操作原理分析;
#define list_entry(ptr, type, member) container_of(ptr, type, member)            //列出当前节点对应的数据;
#define container_of(ptr, type, member)                     \
({                                                            \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);\
        (type *)( (char *)__mptr - offsetof(type,member) ); \
})
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)                    //计算数据域MEMBER在数据结构TYPE中的偏移量
typeof() 是gcc的一个扩展. 功能是根据变量获取变量的类型;
所以, __mptr是一个指向member的指针地址.然后.通过offsetof获取相对的偏移量,
为何要用减呢?/因为,内存中,结构体的头部指针是低位,结尾是高位;

2.1 container_of的作用:
通过一个结构体变量的一个域成员变量的指针,获取该结构体变量的指针;
用法如下:

struct demo {
       type1 memb1;
       type2 memb2;
       type3 memb3;
};
demo a;
type2 * ptr = &a.memb2;
demo *b = container_of(ptr, demo, memb2);
通过这种方式,就获得了 demo的指针b;

2.2 list_entry用法:

struct strTest {
       int a;
       int b;
       int c;
       list_head d;
};

list_head *head;
strTest * ptr = list_entry(head, struct strTest, d);

用法说明:
    当我们使用的时候,其实是这个样子的..通过list_head, 获取当前这个结构体的指针, 然后.通过list_head. 获取下一个结构体的
指针,然后,再通过该list_entry, 获取这个list_head对应的指针,通过这种方法实际上形成了一个双向链表;真是天才的想法;

优点:
    只要在结构体里安装好了list_head, 然后,把所有的list_head指针形成一个串,那时候,就可以随意访问该结构体了.这是一个很有特色的
双向链表的实现;

缺点:
    1. 添加/删除结构体时, 要额外操作,必须把该结构体中的list_head部分,在链表中添加/清除;c++一般会在析构函数或者其它地方默认实现;
    2. 实现锁机制麻烦,需要一个全局的锁(实际也可以放在结构体中)来鉴定操作是唯一性,不像c++,直接在类中设置一个锁;
    3. 一般实现时,不会把list_head中的第一个元素放在实际结构中;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值