Linux内核链表访问链表头指针,linux内核——链表结构分析
http://blog.csdn.net/tigerjibo/article/details/8299584
簡(jiǎn)單整理(使用linux3.0內(nèi)核)
這里首先學(xué)習(xí)的是內(nèi)核中一種抽象定義的雙向鏈表,為了提高其擴(kuò)展性。
內(nèi)核中鏈表的描述數(shù)據(jù)結(jié)構(gòu)
位置:Types.h (linux-3.0.12\include\linux) ? ? 5444 ? ? 2011/11/29
222:
struct list_head {
struct list_head *next, *prev;
};
這是一個(gè)不含數(shù)據(jù)域的結(jié)構(gòu)
使用內(nèi)核鏈表結(jié)構(gòu)構(gòu)造自定義結(jié)構(gòu)
我們可以這樣定義我們的鏈表節(jié)點(diǎn)
struct my_list_node {
數(shù)據(jù)域
......
struct list_head index;
......
};
如果需要構(gòu)造某類對(duì)象的特定列表,則在其結(jié)構(gòu)中定義一個(gè)類型為
struct list_head的成員,通過(guò)這個(gè)成員將這類對(duì)象連接起來(lái),形成所需列表,并通過(guò)通用鏈表函數(shù)對(duì)其進(jìn)行操作。其優(yōu)點(diǎn)是只需編寫(xiě)通用鏈表函數(shù),即可構(gòu)造和操作不同對(duì)象的列表,而無(wú)需為每類對(duì)象的每種列表編寫(xiě)專用函數(shù),實(shí)現(xiàn)了代碼的重用。
使用方法:以struct list_head 為基本對(duì)象,對(duì)鏈表進(jìn)行插入、刪除、合并以及遍歷等各種操作。
內(nèi)核鏈表頭的初始化定義
位置: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是一個(gè)struct list_head類型變量,list是struct list_head類型指針,上面是兩種初始化的方法,使用宏定義和內(nèi)聯(lián)函數(shù)
自定義鏈表頭初始化
struct list_head my_list_head;
LIST_HEAD(my_list_head);或者
INIT_LIST_HEAD(&my_list_head);
調(diào)用后頭結(jié)點(diǎn)my_list_head的next, prev都指向自己,構(gòu)成一個(gè)空鏈表。所以,可以借助next是否指向自己 (頭結(jié)點(diǎn))來(lái)判斷鏈表是否為空。
內(nèi)核鏈表操作的定義
判斷鏈表是否為空
/**
* 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
插入節(jié)點(diǎn)
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:計(jì)算某結(jié)構(gòu)體成員在結(jié)構(gòu)體中的偏移地址
位置:Stddef.h (linux-3.0.12\include\linux) ? ? 435 ? ? 2011/11/29
24:
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
對(duì)這個(gè)宏的講解我們大致可以分為以下4步進(jìn)行講解:
1>( (TYPE *)0 ) ?0地址強(qiáng)制 "轉(zhuǎn)換" 為 TYPE結(jié)構(gòu)類型的指針;
2>((TYPE *)0)->MEMBER ? 訪問(wèn)TYPE結(jié)構(gòu)中的MEMBER數(shù)據(jù)成員;
3>&( ( (TYPE *)0 )->MEMBER)取出TYPE結(jié)構(gòu)中的數(shù)據(jù)成員MEMBER的地址;
4>(size_t)(&(((TYPE*)0)->MEMBER))結(jié)果轉(zhuǎn)換為size_t類型。
宏offsetof的巧妙之處在于將0地址強(qiáng)制轉(zhuǎn)換為 TYPE結(jié)構(gòu)類型的指針,TYPE結(jié)構(gòu)以內(nèi)存空間首地址0作為起始地址,則成員地址自然為偏移地址。可能有的讀者會(huì)想是不是非要用0呢?當(dāng)然不是,我們僅僅是為了計(jì)算的簡(jiǎn)便。也可以使用其他的值,只是算出來(lái)的結(jié)果還要再減去該數(shù)值才是偏移地址。
container_of:通過(guò)結(jié)構(gòu)體中某個(gè)成員的地址,算出結(jié)構(gòu)體的地址
位置: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)); })
說(shuō)明
第一步,首先定義一個(gè)臨時(shí)的數(shù)據(jù)類型(通過(guò)typeof( ((type *)0)->member )獲得)與ptr相同的指針變量__mptr,然后用它來(lái)保存ptr的值。
說(shuō)明:typeof是GNU C對(duì)標(biāo)準(zhǔn)C的擴(kuò)展,它的作用是根據(jù)變量獲取變量的類型《typeof關(guān)鍵字在linux 內(nèi)核中很常見(jiàn)》
第二步,用(char *)__mptr減去member在結(jié)構(gòu)體中的偏移量,得到的值就是整個(gè)結(jié)構(gòu)體變量的首地址(整個(gè)宏的返回值就是這個(gè)首地址)。
遍歷鏈表的相關(guān)宏定義
/**
* 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)
通過(guò)成員指針獲得整個(gè)結(jié)構(gòu)體的指針,Linux鏈表中僅保存了節(jié)點(diǎn)中struct list_head成員變量的地址,通過(guò)list_entry宏經(jīng)struct list_head成員訪問(wèn)到作為它的所有者的節(jié)點(diǎn)起始地址。
/**
* 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指向的節(jié)點(diǎn)的next成員指向的結(jié)構(gòu)體變量地址,此處的ptr一般是一個(gè)鏈表的頭結(jié)點(diǎn)
/**
* 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)
兩個(gè)宏都是用來(lái)遍歷鏈表
pos是一個(gè)輔助指針(即鏈表類型),用于鏈表遍歷
head:鏈表的頭指針(即結(jié)構(gòu)體中成員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)
前面介紹了用于鏈表遍歷的幾個(gè)宏,它們都是通過(guò)移動(dòng)pos指針來(lái)達(dá)到遍歷的目的。但如果遍歷的操作中包含刪除pos指針?biāo)赶虻墓?jié)點(diǎn),pos指針的移動(dòng)就會(huì)被中斷,因?yàn)閘ist_del(pos)將把pos的next、prev置成LIST_POSITION2和LIST_POSITION1的特殊值。當(dāng)然,調(diào)用者完全可以自己緩存next指針使遍歷操作能夠連貫起來(lái),但為了編程的一致性,Linxu內(nèi)核鏈表要求調(diào)用者另外提供一個(gè)與pos同類型的指針n,在for循環(huán)中暫存pos下一個(gè)節(jié)點(diǎn)的地址,避免因pos節(jié)點(diǎn)被釋放而造成的斷鏈。
/**
* 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結(jié)構(gòu)體變量n作為臨時(shí)存儲(chǔ)變量。主要用于鏈表刪除時(shí)操作。
下邊遍歷鏈表宏定義,所不同的是它是根據(jù)鏈表的結(jié)構(gòu)體地址來(lái)進(jìn)行遍歷。大多數(shù)情況下,遍歷鏈表的時(shí)候都需要獲得鏈表節(jié)點(diǎn)數(shù)據(jù)項(xiàng),也就是說(shuō)list_for_each()和list_entry()總是同時(shí)使用。與list_for_each()不同,這里的pos是數(shù)據(jù)項(xiàng)結(jié)構(gòu)指針類型,而不是(struct list_head 類型。
首先是從head開(kāi)始遍歷整個(gè)鏈
/**
* 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:用于遍歷的指針,只是它的數(shù)據(jù)類型是結(jié)構(gòu)體類型而不是strut list_head 類型
head:鏈表頭指針
member:該結(jié)構(gòu)體類型定義中struct list_head成員的變量名。
n和pos類型相同
從pos后位置開(kāi)始順序遍歷到head,需要list_prepare_entry宏先對(duì)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前一位置開(kāi)始逆序遍歷到head,需要list_prepare_entry宏先對(duì)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))
從已知的某個(gè)結(jié)點(diǎn)pos后一個(gè)結(jié)點(diǎn)開(kāi)始進(jìn)行遍歷,與list_for_each_entry_continue不同的是,它主要用于鏈表進(jìn)行刪除時(shí)進(jìn)行的遍歷。
從當(dāng)前pos位置開(kāi)始遍歷到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))
根據(jù)pos得到其下一個(gè)節(jié)點(diǎn)的起始地址
/**
* 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)
鏈表節(jié)點(diǎn)的刪除
/*
* 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);
}
只是簡(jiǎn)單刪除,對(duì)被刪除的節(jié)點(diǎn)沒(méi)做處理
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()函數(shù)將刪除后的prev、next指針?lè)謩e被設(shè)為L(zhǎng)IST_POSITION2和LIST_POSITION1兩個(gè)特殊值,這樣設(shè)置是為了保證不在鏈表中的節(jié)點(diǎn)項(xiàng)不可訪問(wèn)。對(duì)LIST_POSITION1和LIST_POSITION2的訪問(wèn)都將引起頁(yè)故障。
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這個(gè)函數(shù)首先將entry從雙向鏈表中刪除之后,并且將entry初始化為一個(gè)空鏈表
移動(dòng)一個(gè)節(jié)點(diǎn)到另一個(gè)鏈表
/**
* 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);
}
剩下的函數(shù)暫時(shí)不看了!!
總結(jié)
以上是生活随笔為你收集整理的Linux内核链表访问链表头指针,linux内核——链表结构分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 分红后做t扣税吗?
- 下一篇: linux qt应用程序全屏,QT在ub