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 @ 2023-04-02 20:29  流水灯  阅读(54)  评论(0编辑  收藏  举报