linux内核数据结构 --- list_head

以 struct kobject 为例,讲解如何使用链表 struct list_head
struct kobject {
    const char        *name;
    struct list_head    entry;
    struct kobject        *parent;
...
};
struct list_head类型变量作为 struct kobject 的成员(从面向对象的角度,也可以看成 struct list_head是某结构体的父类)
struct list_head {
    struct list_head *next, *prev;
};

初始化 struct list_head,其next和prev都指向自己

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

head--->head

向链表尾添加条目

static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}

添加条目A后,head--->A--->head,添加条目B后,head--->A--->B--->head

遍历链表,取出 struct kobject 实例到 k,

        list_for_each_entry(k, &list, entry)
            xxx;

 

 
 
posted @   流水灯  阅读(77)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
历史上的今天:
2021-04-02 opencv ------ 交叉编译
2021-04-02 cmake ------ CMake generator 或者 Specify the generator for this project
2021-04-02 cmake ------ 基础
点击右上角即可分享
微信分享提示