前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux内核源码 -- list链表

linux内核源码 -- list链表

作者头像
扫帚的影子
发布2018-09-05 16:52:08
2.3K0
发布2018-09-05 16:52:08
举报
  • linux kernel中的list估计已经被各位前辈们写烂了,但是我还是想在这里记录一下;
  • linux kernel里的很多数据结构都很经典, list链表就是其中之一
  • 本篇要介绍的内容:
    1. list的定义
    2. list提供的操作方法
    3. 注意事项
    4. 使用实例

List 所在文件:
  • List的所有操作可以在 include/linux/list.h找到;
  • List head的定义可以在 include/linux/types.h找到;
定义
  • 实际上这就是一个双向循环链表, 且有一个头指针
  • list head的定义:
struct list_head {
    struct list_head *next, *prev;
};
  • 这个定义中只有前向和后向指针,没任何的数据部分, 那我们基本上就知道了, 它不是被单独使用的,而是把它嵌入到用户定义的struct中, 将用户定义的数据结构串起来,作成list;
  • 思想很巧妙, 对用户定义的数据结构侵入性很小, 实现了c++中std::List模板的功能;
  • 虽然这个定义是叫head, 但其实嵌入到用户定义的数据结构中的也是这个.
初始化
// 静态初始化
#define LIST_HEAD_INIT(name) { &(name), &(name) }

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

// 调用INIT_LIST_HEAD来初始化, **WRITE_ONCE**这个后面我们专门介绍
static inline void INIT_LIST_HEAD(struct list_head *list)
{
    WRITE_ONCE(list->next, list);
    list->prev = list;
}
插入操作
  • 将一个元素插入到两个元素之间, 即将 new插入到prevnext中, 这个函数是下面在头部和尾部插入的实现基础
static inline void __list_add(struct list_head *new,
                  struct list_head *prev,
                  struct list_head *next)
{
    if (!__list_add_valid(new, prev, next))
        return;

       // 前后向指针的改写赋值
    next->prev = new;
    new->next = next;
    new->prev = prev;
    WRITE_ONCE(prev->next, new);
}
  • 在头部插入, 在头指针和第一个元素间插入
static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}
  • 在尾部插入,在最后一个元素间和头指针间插入, 因为是循环链表嘛~
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}
删除操作
  • 删除两个元素之间的元素
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    WRITE_ONCE(prev->next, next);
}
  • 删除一个已知元素entry
static inline void __list_del_entry(struct list_head *entry)
{
    if (!__list_del_entry_valid(entry))
        return;

    __list_del(entry->prev, entry->next);
}
替换操作

都是指针的变换

static inline void list_replace(struct list_head *old,
                struct list_head *new)
{
    new->next = old->next;
    new->next->prev = new;
    new->prev = old->prev;
    new->prev->next = new;
}
移动操作
  • 将一个元素移动到另一个list的头部
static inline void list_move(struct list_head *list, struct list_head *head)
{
       // 从原处的list后摘掉
    __list_del_entry(list);
       // 添加到新链表的头部
    list_add(list, head);
}
  • 将一个元素移动到另一个list的队尾
static inline void list_move_tail(struct list_head *list,
                  struct list_head *head)
{
       // 从原处的list后摘掉
    __list_del_entry(list);
       // 添加到新链表的队尾
    list_add_tail(list, head);
}
拆分操作, 将一个队列由指定的位置拆成两个队列

list是新队列的head指针, 包括的元素从原head队列的第一个元素到entry, head队列仅包括余下的元素

static inline void __list_cut_position(struct list_head *list,
        struct list_head *head, struct list_head *entry)
{
    struct list_head *new_first = entry->next;
    list->next = head->next;
    list->next->prev = list;
    list->prev = entry;
    entry->next = list;
    head->next = new_first;
    new_first->prev = head;
}
合并操作:
  • 将list列表中除了list本身插入到prev和next之间
static inline void __list_splice(const struct list_head *list,
                 struct list_head *prev,
                 struct list_head *next)
{
    struct list_head *first = list->next;
    struct list_head *last = list->prev;

    first->prev = prev;
    prev->next = first;

    last->next = next;
    next->prev = last;
}
  • 将一个列表插入到另一个列表的头部
static inline void list_splice(const struct list_head *list,
                struct list_head *head)
{
    if (!list_empty(list))
        __list_splice(list, head, head->next);
}
  • 将一个列表插入到另一个列表的尾部
static inline void list_splice_tail(struct list_head *list,
                struct list_head *head)
{
    if (!list_empty(list))
        __list_splice(list, head->prev, head);
}
list_entry

按之前说的, 这个list_head都有要嵌入到用户定义的struct中,这个宏就是由这个list_head ptr来获取当前所处的struct对象的指针, 用了linux的经典宏定义 container_of

#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)
一堆宏定义, 用来各种遍历, 获取entry
注意事项
  • 只说一个,就是多线程操作同一个list, 还是需要加锁
使用实例
typedef struct _Foo {                              
    int data_;                                     
    struct list_head link;                         
} Foo;                                             
                                                   
int main(int argn, char* argv[]) {                 
    LIST_HEAD(test_link);                          
                                                   
    Foo f1;                                        
    f1.data_ = 1;                                  
    LIST_HEAD_INIT(f1.link);                       
                                                   
    Foo f2;                                        
    f1.data_ = 2;                                  
    LIST_HEAD_INIT(f2.link);                       
                                                   
    list_add(&f1.link, &test_link)                 
    list_add(&f2.link, &test_link)                 
                                                   
    struct Foo* pos;                               
    list_for_each_entry(pos, &test_link, link) {   
        printf("%d\n", pos->data_);                
    }                                              
                                                   
    return 0;                                      
}                                                  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • List 所在文件:
  • 定义
  • 初始化
  • 插入操作
  • 删除操作
  • 替换操作
  • 移动操作
  • 拆分操作, 将一个队列由指定的位置拆成两个队列
  • 合并操作:
  • list_entry宏
  • 一堆宏定义, 用来各种遍历, 获取entry
  • 注意事项
  • 使用实例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档