鏈表的遍歷


 當我們在鏈表添加/修改多個節點后,我們最終是要通過查找鏈表中的某一個節點並對其數據進行操作,我們將逐一分析kernel/include/linux/list.h中關於鏈表遍歷的接口。

 

1,list_entry用於獲取struct list_head結構體指針所在結構體變量的首地址。

@ptr:指向我們要求首地址的結構體內的struct list_head成員變量,ptr的類型也為struct list_head。

@type:要求首地址的結構體類型,即struct list_head變量所在的結構體的類型。

@member:要求首地址結構體類型內struct list_head變量的變量名。

/**
 * list_entry - get the struct for this entry
 * @ptr:        the &struct list_head pointer.
 * @type:       the type of the struct this is embedded in.
 * @member:     the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) \
        container_of(ptr, type, member)

 list_entry調用了contianer_of宏,container_of宏的定義與分析將在另一篇隨筆中講述。

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:        the pointer to the member.
 * @type:       the type of the container struct this is embedded in.
 * @member:     the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})

  

2,list_first_entry用於獲取鏈表中第一個節點所在結構體的首地址。

@ptr:鏈表頭節點;

@type:鏈表節點struct list_head變量所在結構體的類型;

@member:鏈表節點在所求首地址結構體內的成員變量名;

/**
 * list_first_entry - get the first element from a list
 * @ptr:        the list head to take the element from.
 * @type:       the type of the struct this is embedded in.
 * @member:     the name of the list_struct within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define list_first_entry(ptr, type, member) \
        list_entry((ptr)->next, type, member)

 

3,list_for_each遍歷一個鏈表。

@pos:struct list_head類型的指針,用於指向我們遍歷的鏈表節點;

@head:我們要遍歷鏈表的頭節點;

/**
 * list_for_each        -       iterate over a list
 * @pos:        the &struct list_head to use as a loop cursor.
 * @head:       the head for your list.
 */     
#define list_for_each(pos, head) \      
        for (pos = (head)->next; pos != (head); pos = pos->next)

 

4,__list_for_each與list_for_each完全一樣,遍歷一個鏈表,同時也不做預取。

/**
 * __list_for_each      -       iterate over a list
 * @pos:        the &struct list_head to use as a loop cursor.
 * @head:       the head for your list.
 *
 * This variant doesn't differ from list_for_each() any more.
 * We don't do prefetching in either case.
 */
#define __list_for_each(pos, head) \
        for (pos = (head)->next; pos != (head); pos = pos->next)

 

5,__list_for_each_prev用於從后向前反向遍歷。

/**
 * list_for_each_prev   -       iterate over a list backwards
 * @pos:        the &struct list_head to use as a loop cursor.
 * @head:       the head for your list.
 */
#define list_for_each_prev(pos, head) \
        for (pos = (head)->prev; pos != (head); pos = pos->prev)

 

6,list_for_each_safe安全的遍歷一個鏈表,其機制是我們多傳入一個struct list_head的指針n,用於指向pos的下一個節點,以保證我們在刪除pos指向的節點時,仍能繼續遍歷鏈表的剩余節點。

/**
 * list_for_each_safe - iterate over a list safe against removal of list entry
 * @pos:        the &struct list_head to use as a loop cursor.
 * @n:          another &struct list_head to use as temporary storage
 * @head:       the head for your list.
 */
#define list_for_each_safe(pos, n, head) \
        for (pos = (head)->next, n = pos->next; pos != (head); \
                pos = n, n = pos->next)

 

7,list_for_each_prev_safe反向遍歷,安全查找。

/**
 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
 * @pos:        the &struct list_head to use as a loop cursor.
 * @n:          another &struct list_head to use as temporary storage
 * @head:       the head for your list.
 */
#define list_for_each_prev_safe(pos, n, head) \
        for (pos = (head)->prev, n = pos->prev; \
             pos != (head); \
             pos = n, n = pos->prev)

 

8,3、4、5、6、7在遍歷鏈表時返回的是struct list_head指針的地址。當我們使用struct list_head型變量將一個節點掛到一個鏈表時,我們不是為了僅僅操縱這個光凸凸的節點,而是將struct list_head變量放到一個結構體內,根據對鏈表上struct list_head的遍歷來得出strcut list_head所在結構體的首地址,list_for_each_entry正是為了完成這一功能而實現。

/**
 * list_for_each_entry  -       iterate over list of given type
 * @pos:        the type * to use as a loop cursor.
 * @head:       the head for your list.
 * @member:     the name of the list_struct within the struct.
 */
#define list_for_each_entry(pos, head, member)                          \
        for (pos = list_entry((head)->next, typeof(*pos), member);      \
             &pos->member != (head);    \
             pos = list_entry(pos->member.next, typeof(*pos), member))

我們將所求結構體類型的指針變量pos、鏈表的頭head和所求結構體內struct list_head的變量名member傳到list_for_each_entry之后, list_entry的第一個參數用head->next指向下一個節點,此節點的地址也就是在所屬結構體內的struct list_head成員變量的地址,第二個參數用typeof(*pos)求得pos的結構體類型,第三個參數為所求結構體內struct list_head的變量名。

 

9,list_for_each_entry_reverse反向遍歷鏈表並返回鏈表節點所在的結構體的首地址

/**
 * list_for_each_entry_reverse - iterate backwards over list of given type.
 * @pos:        the type * to use as a loop cursor.
 * @head:       the head for your list.
 * @member:     the name of the list_struct within the struct.
 */
#define list_for_each_entry_reverse(pos, head, member)                  \
        for (pos = list_entry((head)->prev, typeof(*pos), member);      \
             &pos->member != (head);    \
             pos = list_entry(pos->member.prev, typeof(*pos), member))


10,list_prepare_entry用於准備一個結構體的首地址,用在list_for_each_entry_contine()中。

/**
 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
 * @pos:        the type * to use as a start point
 * @head:       the head of the list
 * @member:     the name of the list_struct within the struct.
 *
 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
 */
#define list_prepare_entry(pos, head, member) \
        ((pos) ? : list_entry(head, typeof(*pos), member))

 

11,list_for_each_entry_contine從當前pos的下一個節點開始繼續遍歷剩余的鏈表,不包括pos。

如果我們將pos、head、member傳入list_for_each_entry,此宏將會從鏈表的頭節點開始遍歷。

/**
 * list_for_each_entry_continue - continue iteration over list of given type
 * @pos:        the type * to use as a loop cursor.
 * @head:       the head for your list.
 * @member:     the name of the list_struct within the struct.
 *
 * Continue to iterate over list of given type, continuing after
 * the current position.
 */
#define list_for_each_entry_continue(pos, head, member)                 \
        for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
             &pos->member != (head);    \
             pos = list_entry(pos->member.next, typeof(*pos), member))

 

12,list_for_each_entry_contine_reverse從當前的pos的前一個節點開始繼續反向遍歷剩余的鏈表,不包括pos。

/**
 * list_for_each_entry_continue_reverse - iterate backwards from the given point
 * @pos:        the type * to use as a loop cursor.
 * @head:       the head for your list.
 * @member:     the name of the list_struct within the struct.
 *
 * Start to iterate over list of given type backwards, continuing after
 * the current position.
 */
#define list_for_each_entry_continue_reverse(pos, head, member)         \
        for (pos = list_entry(pos->member.prev, typeof(*pos), member);  \
             &pos->member != (head);    \
             pos = list_entry(pos->member.prev, typeof(*pos), member))

 

13,list_for_each_entry_from從pos開始遍歷剩余的鏈表。

/**
 * list_for_each_entry_from - iterate over list of given type from the current point
 * @pos:        the type * to use as a loop cursor.
 * @head:       the head for your list.
 * @member:     the name of the list_struct within the struct.
 *
 * Iterate over list of given type, continuing from current position.
 */
#define list_for_each_entry_from(pos, head, member)                     \
        for (; &pos->member != (head);  \
             pos = list_entry(pos->member.next, typeof(*pos), member))

 

14,list_for_each_entry_safe遍歷鏈表,返回type類型的結構體的首地址,並防止因刪除鏈表節點而導致的遍歷出錯。

/**
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:        the type * to use as a loop cursor.
 * @n:          another type * to use as temporary storage
 * @head:       the head for your list.
 * @member:     the name of the list_struct within the struct.
 */
#define list_for_each_entry_safe(pos, n, head, member)                  \
        for (pos = list_entry((head)->next, typeof(*pos), member),      \
                n = list_entry(pos->member.next, typeof(*pos), member); \
             &pos->member != (head);                                    \
             pos = n, n = list_entry(n->member.next, typeof(*n), member))

 

15,list_for_each_entry_safe_continue從pos節點的下一個節點開始遍歷剩余的鏈表,並防止因刪除鏈表節點而導致的遍歷出錯。

/**
 * list_for_each_entry_safe_continue - continue list iteration safe against removal
 * @pos:        the type * to use as a loop cursor.
 * @n:          another type * to use as temporary storage
 * @head:       the head for your list.
 * @member:     the name of the list_struct within the struct.
 *
 * Iterate over list of given type, continuing after current point,
 * safe against removal of list entry.
 */
#define list_for_each_entry_safe_continue(pos, n, head, member)                 \
        for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
                n = list_entry(pos->member.next, typeof(*pos), member);         \
             &pos->member != (head);                                            \
             pos = n, n = list_entry(n->member.next, typeof(*n), member))

 

16,list_for_each_entry_safe_from從pos節點開始繼續遍歷剩余的鏈表,並防止因刪除鏈表節點而導致的遍歷出錯。其與list_for_each_entry_safe_contine的不同在於在第一次遍歷時,pos沒有指向它的下一個節點,而是從pos開始遍歷。

/**
 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
 * @pos:        the type * to use as a loop cursor.
 * @n:          another type * to use as temporary storage
 * @head:       the head for your list.
 * @member:     the name of the list_struct within the struct.
 *
 * Iterate over list of given type from current point, safe against
 * removal of list entry.
 */
#define list_for_each_entry_safe_from(pos, n, head, member)                     \
        for (n = list_entry(pos->member.next, typeof(*pos), member);            \
             &pos->member != (head);                                            \
             pos = n, n = list_entry(n->member.next, typeof(*n), member))

 

17,list_for_each_entry_safe_reverse從pos的前一個節點開始反向遍歷一個鏈表,並防止因刪除鏈表節點而導致的遍歷出錯。

/**
 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
 * @pos:        the type * to use as a loop cursor.
 * @n:          another type * to use as temporary storage
 * @head:       the head for your list.
 * @member:     the name of the list_struct within the struct.
 *
 * Iterate backwards over list of given type, safe against removal
 * of list entry.
 */
#define list_for_each_entry_safe_reverse(pos, n, head, member)          \
        for (pos = list_entry((head)->prev, typeof(*pos), member),      \
                n = list_entry(pos->member.prev, typeof(*pos), member); \
             &pos->member != (head);                                    \
             pos = n, n = list_entry(n->member.prev, typeof(*n), member))

 

18,list_safe_reset_next返回當前pos節點的下一個節點的type結構體首地址。

/**
 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
 * @pos:        the loop cursor used in the list_for_each_entry_safe loop
 * @n:          temporary storage used in list_for_each_entry_safe
 * @member:     the name of the list_struct within the struct.
 *
 * list_safe_reset_next is not safe to use in general if the list may be
 * modified concurrently (eg. the lock is dropped in the loop body). An
 * exception to this is if the cursor element (pos) is pinned in the list,
 * and list_safe_reset_next is called after re-taking the lock and before
 * completing the current iteration of the loop body.
 */
#define list_safe_reset_next(pos, n, member)                            \
        n = list_entry(pos->member.next, typeof(*pos), member)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM