我用 LinuxLinux学习之路首页推荐

linux内核源码 -- list链表

2018-01-10  本文已影响267人  扫帚的影子

List 所在文件:
定义
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)

// 调用INIT_LIST_HEAD来初始化, **WRITE_ONCE**这个后面我们专门介绍
static inline void INIT_LIST_HEAD(struct list_head *list)
{
    WRITE_ONCE(list->next, list);
    list->prev = list;
}

插入操作
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);
}
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;
}
移动操作
static inline void list_move(struct list_head *list, struct list_head *head)
{
       // 从原处的list后摘掉
    __list_del_entry(list);
       // 添加到新链表的头部
    list_add(list, head);
}
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;
}
合并操作:
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
注意事项
使用实例
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;                                      
}                                                  
上一篇下一篇

猜你喜欢

热点阅读