linux内核学习中--"list.h" del move splice 函数理解

第三篇博客,主要是针对删除,移动,合并函数的理解,很简单的,相信大家一看就明白了。

static inline void __list_del(struct list_head * prev, struct list_head * next)     //删除结点。删除链表中prev与next之间的元素
{
        next->prev = prev;
        prev->next = next;
}

static inline void list_del(struct list_head *entry)               //删除一个结点entry,并将删除结点地址置为0
{                                                     
        __list_del(entry->prev, entry->next);
        entry->next = LIST_POISON1;  //指针清除0
        entry->prev = LIST_POISON2;  //指针清除0
}





static inline void list_del_init(struct list_head *entry)      //从链表中删除元素entry,并将其初始化为新的链表。

{
        __list_del(entry->prev, entry->next);
        INIT_LIST_HEAD(entry);        //初始化就是把指针指向自己                       
}

static inline void list_move(struct list_head *list, struct list_head *head) //将该结点摘除并插入到链表头部
{
        __list_del(list->prev, list->next);
        list_add(list, head);
}

static inline void list_move_tail(struct list_head *list,    //将该结点摘除并插入到链表尾部部
                                  struct list_head *head)
{
        __list_del(list->prev, list->next);
        list_add_tail(list, head);
}

static inline int list_empty(const struct list_head *head)        //测试链表是否为空
{
        return head->next == head;
}

static inline int list_empty_careful(const struct list_head *head)   
{
        struct list_head *next = head->next;
        return (next == head) && (next == head->prev);
}
/*
基本的list_empty()仅以头指针的next是否指向自己来判断链表是否为空,Linux链表另行提供了一个list_empty_careful()宏,
它同时判断头指针的next和prev,仅当两者都指向自己时才返回真。
这主要是为了应付另一个cpu正在处理同一个链表而造成next、prev不一致的情况。
但代码注释也承认,这一安全保障能力有限:除非其他cpu的链表操作只有list_del_init(),否则仍然不能保证安全,也就是说,还是需要加锁保护。
*/
static inline void __list_splice(struct list_head *list,             //合并链表, 将链表list与head合并。
                                 struct list_head *head)
{
        struct list_head *first = list->next;
        struct list_head *last = list->prev;
        struct list_head *at = head->next;

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

        last->next = at;
        at->prev = last;
}

/**
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(struct list_head *list, struct list_head *head) //list不为空的情况下,调用__list_splice()实现list与head的合并
{
        if (!list_empty(list))
                __list_splice(list, head);
}

/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
static inline void list_splice_init(struct list_head *list,       //将两链表合并,并将list初始化。
                                    struct list_head *head)
{
        if (!list_empty(list)) {
                __list_splice(list, head);
                INIT_LIST_HEAD(list);
        }
}


 

你可能感兴趣的:(JOIN,linux,struct,list,测试,linux内核)