linux內核——鏈表結構分析

          http://blog.csdn.net/tigerjibo/article/details/8299584

簡單整理(使用linux3.0內核)
這裏首先學習的是內核中一種抽象定義的雙向鏈表,爲了提高其擴展性。

內核中鏈表的描述數據結構
位置:Types.h (linux-3.0.12\include\linux)     5444     2011/11/29
222:
struct list_head {
    struct list_head *next, *prev;
};
         這是一個不含數據域的結構
使用內核鏈表結構構造自定義結構
         我們可以這樣定義我們的鏈表節點
         struct my_list_node {
              數據域
              ......
              struct list_head index;
              ......
         };
如果需要構造某類對象的特定列表,則在其結構中定義一個類型爲
struct list_head的成員,通過這個成員將這類對象連接起來,形成所需列表,並通過通用鏈表函數對其進行操作。其優點是隻需編寫通用鏈表函數,即可構造和操作不同對象的列表,而無需爲每類對象的每種列表編寫專用函數,實現了代碼的重用。
使用方法:以struct list_head 爲基本對象,對鏈表進行插入、刪除、合併以及遍歷等各種操作。
內核鏈表頭的初始化定義
位置:List.h (linux-3.0.12\include\linux)     21209     2011/11/29
19:
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)
static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}
其中name是一個struct list_head類型變量,list是struct list_head類型指針,上面是兩種初始化的方法,使用宏定義和內聯函數
自定義鏈表頭初始化
struct list_head my_list_head;
LIST_HEAD(my_list_head);或者
INIT_LIST_HEAD(&my_list_head);
調用後頭結點my_list_head的next, prev都指向自己,構成一個空鏈表。所以,可以藉助next是否指向自己 (頭結點)來判斷鏈表是否爲空。
內核鏈表操作的定義
判斷鏈表是否爲空
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}

/**
* list_empty_careful - tests whether a list is empty and not being modified
* @head: the list to test
*
* Description:
* tests whether a list is empty _and_ checks that no other CPU might be
* in the process of modifying either member (next or prev)
*
* NOTE: using list_empty_careful() without synchronization
* can only be safe if the only activity that can happen
* to the list entry is list_del_init(). Eg. it cannot be used
* if another CPU could re-list_add() it.
*/
static inline int list_empty_careful(const struct list_head *head)
{
    struct list_head *next = head->next;
    return (next == head) && (next == head->prev);
}
返回值:
    爲空返回1,不爲空返回0
插入節點
static inline void __list_add(struct list_head *new,
                    struct list_head *prev,
                    struct list_head *next)
{
    next->prev = new;
    new->next = next;
    new->prev = prev;
    prev->next = new;
}

/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}

/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}
遍歷鏈表
首先看下需要用到額外的宏定義
offsetof:計算某結構體成員在結構體中的偏移地址
位置:Stddef.h (linux-3.0.12\include\linux)     435     2011/11/29
24:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
對這個宏的講解我們大致可以分爲以下4步進行講解:
1>( (TYPE *)0 )  0地址強制 "轉換" 爲 TYPE結構類型的指針;
2>((TYPE *)0)->MEMBER   訪問TYPE結構中的MEMBER數據成員;
3>&( ( (TYPE *)0 )->MEMBER)取出TYPE結構中的數據成員MEMBER的地址;
4>(size_t)(&(((TYPE*)0)->MEMBER))結果轉換爲size_t類型。
宏offsetof的巧妙之處在於將0地址強制轉換爲 TYPE結構類型的指針,TYPE結構以內存空間首地址0作爲起始地址,則成員地址自然爲偏移地址。可能有的讀者會想是不是非要用0呢?當然不是,我們僅僅是爲了計算的簡便。也可以使用其他的值,只是算出來的結果還要再減去該數值纔是偏移地址。

container_of:通過結構體中某個成員的地址,算出結構體的地址
位置:Kernel.h (linux-3.0.12\tools\perf\util\include\linux)     2691     2011/11/29
19:
/**
* 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)); })
說明
第一步,首先定義一個臨時的數據類型(通過typeof( ((type *)0)->member )獲得)與ptr相同的指針變量__mptr,然後用它來保存ptr的值。
說明:typeof是GNU C對標準C的擴展,它的作用是根據變量獲取變量的類型《typeof關鍵字在linux 內核中很常見》
第二步,用(char *)__mptr減去member在結構體中的偏移量,得到的值就是整個結構體變量的首地址(整個宏的返回值就是這個首地址)。
遍歷鏈表的相關宏定義
/**
* 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)
通過成員指針獲得整個結構體的指針,Linux鏈表中僅保存了節點中struct list_head成員變量的地址,通過list_entry宏經struct list_head成員訪問到作爲它的所有者的節點起始地址。
/**
* 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)
得到ptr指向的節點的next成員指向的結構體變量地址,此處的ptr一般是一個鏈表的頭結點
/**
* 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)

/**
* __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)
兩個宏都是用來遍歷鏈表
pos是一個輔助指針(即鏈表類型),用於鏈表遍歷
head:鏈表的頭指針(即結構體中成員struct list_head)

/**
* 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)
逆向遍歷

/**
* 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)
前面介紹了用於鏈表遍歷的幾個宏,它們都是通過移動pos指針來達到遍歷的目的。但如果遍歷的操作中包含刪除pos指針所指向的節點,pos指針的移動就會被中斷,因爲list_del(pos)將把pos的next、prev置成LIST_POSITION2和LIST_POSITION1的特殊值。當然,調用者完全可以自己緩存next指針使遍歷操作能夠連貫起來,但爲了編程的一致性,Linxu內核鏈表要求調用者另外提供一個與pos同類型的指針n,在for循環中暫存pos下一個節點的地址,避免因pos節點被釋放而造成的斷鏈。

/**
* 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)
功能與list_for_each_prev相同,用於逆向遍歷鏈表。不同的是使用list_head結構體變量n作爲臨時存儲變量。主要用於鏈表刪除時操作。

下邊遍歷鏈表宏定義,所不同的是它是根據鏈表的結構體地址來進行遍歷。大多數情況下,遍歷鏈表的時候都需要獲得鏈表節點數據項,也就是說list_for_each()和list_entry()總是同時使用。與list_for_each()不同,這裏的pos是數據項結構指針類型,而不是(struct list_head 類型。
首先是從head開始遍歷整個鏈
/**
* 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))

/**
* 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))

/**
* 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))

/**
* 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))
pos:用於遍歷的指針,只是它的數據類型是結構體類型而不是strut list_head 類型
head:鏈表頭指針
member:該結構體類型定義中struct list_head成員的變量名。
n和pos類型相同

從pos後位置開始順序遍歷到head,需要list_prepare_entry宏先對pos處理
/**
* 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))

/**
* 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))

從pos前一位置開始逆序遍歷到head,需要list_prepare_entry宏先對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))

#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))
從已知的某個結點pos後一個結點開始進行遍歷,與list_for_each_entry_continue不同的是,它主要用於鏈表進行刪除時進行的遍歷。

從當前pos位置開始遍歷到head
#define list_for_each_entry_from(pos, head, member)                \
    for (; &pos->member != (head);     \
         pos = list_entry(pos->member.next, typeof(*pos), member))

#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))

根據pos得到其下一個節點的起始地址
/**
* 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)

鏈表節點的刪除

/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}

static inline void __list_del_entry(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
}
只是簡單刪除,對被刪除的節點沒做處理

static inline void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    entry->next = LIST_POISON1;
    entry->prev = LIST_POISON2;
}
list_del()函數將刪除後的prev、next指針分別被設爲LIST_POSITION2和LIST_POSITION1兩個特殊值,這樣設置是爲了保證不在鏈表中的節點項不可訪問。對LIST_POSITION1和LIST_POSITION2的訪問都將引起頁故障。

static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

static inline void list_del_init(struct list_head *entry)
{
    __list_del_entry(entry);
    INIT_LIST_HEAD(entry);
}
list_del_init這個函數首先將entry從雙向鏈表中刪除之後,並且將entry初始化爲一個空鏈表

移動一個節點到另一個鏈表

/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
    __list_del_entry(list);
    list_add(list, head);
}

/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
                     struct list_head *head)
{
    __list_del_entry(list);
    list_add_tail(list, head);
}

剩下的函數暫時不看了!!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章