?? linux內(nèi)核源碼分析-鏈表代碼分析 .txt
字號(hào):
---------------------typeof()--------------------
--->我開(kāi)始不懂,源代碼中也查不到,網(wǎng)上發(fā)貼請(qǐng)教。由liubo1977在www.linuxforum.net上的Linux內(nèi)核技術(shù)論壇上解答,QQ:84915771
答復(fù):
unsigned int i;
typeof(i) x;
x=100;
printf("x:%d\n",x);
typeof() 是 gcc 的擴(kuò)展,和 sizeof() 類似。
------------------------
container_of()和offsetof()并不僅用于鏈表操作,這里最有趣的地方是 ((type *)0)->member,它將0地址強(qiáng)制 "轉(zhuǎn)換" 為 type 結(jié)構(gòu)的指針,再訪問(wèn)到 type 結(jié)構(gòu)中的 member 成員。在 container_of 宏中,它用來(lái)給 typeof() 提供參數(shù),以獲得 member 成員的數(shù)據(jù)類型;
---------------container_of()--------------------
container_of() 來(lái)自\linux\kernel.h
內(nèi)核中的注釋:container_of - cast a member of a tructure 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 truct.
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
自己分析:
1.(type *)0->member為設(shè)計(jì)一個(gè)type類型的結(jié)構(gòu)體,起始地址為0,編譯器將結(jié)構(gòu)體的起始的地址加上此結(jié)構(gòu)體成員變量的偏移得到此結(jié)構(gòu)體成員變量的偏移地址,由于結(jié)構(gòu)體起始地址為0,所以此結(jié)構(gòu)體成員變量的偏移地址就等于其成員變量在結(jié)構(gòu)體內(nèi)的距離結(jié)構(gòu)體開(kāi)始部分的偏移量。即:&(type *)0->member就是取出其成員變量的偏移地址。而其等于其在結(jié)構(gòu)體內(nèi)的偏移量:即為:(size_t)(& ((type *)0)->member)經(jīng)過(guò)size_t的強(qiáng)制類型轉(zhuǎn)換后,其數(shù)值為結(jié)構(gòu)體內(nèi)的偏移量。該偏移量這里由offsetof()求出。
2.typeof( ( (type *)0)->member )為取出member成員的變量類型。用其定義__mptr指針.ptr為指向該成員變量的指針。__mptr為member數(shù)據(jù)類型的常量指針,其指向ptr所指向的變量處。
3.(char *)__mptr轉(zhuǎn)換為字節(jié)型指針。(char *)__mptr - offsetof(type,member) )用來(lái)求出結(jié)構(gòu)體起始地址(為char *型指針),然后(type *)( (char *)__mptr - offsetof(type,member) )在(type *)作用下進(jìn)行將字節(jié)型的結(jié)構(gòu)體起始指針轉(zhuǎn)換為type *型的結(jié)構(gòu)體起始指針。
這就是從結(jié)構(gòu)體某成員變量指針來(lái)求出該結(jié)構(gòu)體的首指針。指針類型從結(jié)構(gòu)體某成員變量類型轉(zhuǎn)換為該結(jié)構(gòu)體類型。
-----------茶余飯后一點(diǎn)小資料----------------------
學(xué)辛苦了,看點(diǎn)收集的小東東:
以下文字摘自微軟中國(guó)研究院前任院長(zhǎng),現(xiàn)微軟高級(jí)副總裁李開(kāi)復(fù)先生《一封寫(xiě)給中國(guó)學(xué)生的信》:
“我的老板 Rick室Rashid博士是目前微軟公司主管研究的高級(jí)副總裁,他已經(jīng)功成名就,卻始終保持一顆學(xué)習(xí)和進(jìn)取的心。現(xiàn)在,他每年仍然編寫(xiě)大約50,000行程序。他認(rèn)為:用最新的技術(shù)編程可以使他保持對(duì)計(jì)算機(jī)最前沿技術(shù)的敏感,使自己能夠不斷進(jìn)步。今天,有些博士生帶低年級(jí)的本科生和碩士生做項(xiàng)目,就自滿地認(rèn)為自己已經(jīng)沒(méi)有必要再編程了。其實(shí),這樣的做法是很不明智的。”
--------------arch-v32\cache.h------------------
#ifndef _ASM_CRIS_ARCH_CACHE_H
#define _ASM_CRIS_ARCH_CACHE_H
/* A cache-line is 32 bytes. */
#define L1_CACHE_BYTES 32
#define L1_CACHE_SHIFT 5
#define L1_CACHE_SHIFT_MAX 5
#endif /* _ASM_CRIS_ARCH_CACHE_H */
分析:
也可用#define L1_CACHE_BYTES (1UL<<L1_CACHE_SHIFT)來(lái)實(shí)現(xiàn)
-------------asm-i386\cache.h--------------------
#ifndef __ARCH_I386_CACHE_H
#define __ARCH_I386_CACHE_H
/* L1 cache line size */
#define L1_CACHE_SHIFT (CONFIG_X86_L1_CACHE_SHIFT)
#define L1_CACHE_BYTES (1 << L1_CACHE_SHIFT)
//largest L1 which this arch supports
#define L1_CACHE_SHIFT_MAX 7
#endif
分析:cache行在32位平臺(tái)上多為32字節(jié),但在I386平臺(tái)上也有128字節(jié)的。
----------\linux\prefetch.h--------------------
這里是內(nèi)核中的解釋:(含有自己的分析)
/*
prefetch(x) attempts to pre-emptively get the memory pointed to
by address "x" into the CPU L1 cache.
prefetch(x) should not cause any kind of exception, prefetch(0) is
specifically ok.
prefetch() should be defined by the architecture, if not, the
#define below provides a no-op define.
prefetch()當(dāng)由體系結(jié)構(gòu)來(lái)決定,否則就定義為空宏
有3類prefetch()宏:
There are 3 prefetch() macros:
prefetch(x) - prefetches the cacheline at "x" for read-->預(yù)取讀
prefetchw(x) - prefetches the cacheline at "x" for write-->預(yù)取寫(xiě)
spin_lock_prefetch(x) - prefectches the spinlock *x for taking
there is also PREFETCH_STRIDE which is the architecure-prefered
"lookahead" size for prefetching streamed operations.
PREFETCH_STRIDE用于預(yù)取操作流。
These cannot be do{}while(0) macros.
*/
#define _LINUX_PREFETCH_H
#ifndef ARCH_HAS_PREFETCH
static inline void prefetch(const void *x) {;}
#endif
#ifndef ARCH_HAS_PREFETCHW
static inline void prefetchw(const void *x) {;}
#endif
#ifndef ARCH_HAS_SPINLOCK_PREFETCH
#define spin_lock_prefetch(x) prefetchw(x)
#endif
#ifndef PREFETCH_STRIDE
#define PREFETCH_STRIDE (4*L1_CACHE_BYTES)
#endif //PREFETCH_STRIDE
static inline void prefetch_range(void *addr, size_t len)
{
#ifdef ARCH_HAS_PREFETCH
char *cp;
char *end = addr + len;
for (cp = addr; cp < end; cp += PREFETCH_STRIDE)
prefetch(cp);
#endif //ARCH_HAS_PREFETCH
}
#endif //_LINUX_PREFETCH_H
-----asm-x86_64\processor.h---prefetch()---------
static inline void prefetch(void *x)
{
asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
}
分析:
將x指針作強(qiáng)制類型轉(zhuǎn)換為unsigned long *型,然后取出該內(nèi)存操作數(shù),送入高速緩存。
----------------list_for_each()------------------
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next)
----------------__list_for_each()-----------------
Linux Kernel 2.6.14中的解釋中的精華部分:
/**
* This variant differs from list_for_each() in that it's the simplest possible list iteration code, no prefetching is done.Use this for code that knows the list to be very short (empty or 1 entry) most of the time.
*/
#define __list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
list_for_each()有prefetch()用于復(fù)雜的表的遍歷,而__list_for_each()無(wú)prefetch()用于簡(jiǎn)單的表的遍歷.
注意:head在宏定義中用了括號(hào)將其括起來(lái).
----------------list_for_each_prev()-------------
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
pos = pos->prev)
解釋類似上面的list_for_each()。
----------------list_for_each_safe()--------------
內(nèi)核中解釋的精華部分:
/*
* list_for_each_safe - iterate over a list safe against removal of list entry
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
這是說(shuō)你可以邊遍歷邊刪除,這就叫safe。十分精彩。剛開(kāi)始時(shí),我也一直不理解safe的意思,后來(lái)在www.linuxforum.net論壇上搜索list_for_each_safe找到了解答。
----------------list_entry()--------------------
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
分析:
list_entry()函數(shù)用于將指向某鏈表結(jié)點(diǎn)成員的指針調(diào)整到該鏈表的開(kāi)始處,并指針轉(zhuǎn)換為該鏈表結(jié)點(diǎn)的類型。
-------------list_for_each_entry()---------------
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, t ypeof(*pos), member); \
prefetch(pos->member.next), &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
分析:
1.楊沙洲--國(guó)防科技大學(xué)計(jì)算機(jī)學(xué)院--2004年8月指出:
大多數(shù)情況下,遍歷鏈表的時(shí)候都需要獲得鏈表節(jié)點(diǎn)數(shù)據(jù)項(xiàng),也就是說(shuō)list_for_each()和list_entry()總是同時(shí)使用。對(duì)此Linux給出了一個(gè)list_for_each_entry()宏。
2.這是用于嵌套的結(jié)構(gòu)體中的宏:(這個(gè)程序例子來(lái)自:《Linux內(nèi)核分析及編程》作者:倪繼利 電子工業(yè)出版社)
struct example_struct
{
struct list_head list;
int priority;
... //其他結(jié)構(gòu)體成員
};
struct example_struct *node = list_entry(ptr,struct example_struct,list);
自己分析:對(duì)比list_entry(ptr,type,member)可知有以下結(jié)果:
其中l(wèi)ist相當(dāng)于member成員,struct example_struct相當(dāng)于type成員,ptr相當(dāng)于ptr成員。而list{}成員嵌套于example_struct{}里面。ptr指向example_struct{}中的list成員變量的。在list_entry()作用下,將ptr指針回轉(zhuǎn)指向struct example_struct{}結(jié)構(gòu)體的開(kāi)始處。
3.pos當(dāng)指向外層結(jié)構(gòu)體,比如指向struct example_struct{}的結(jié)點(diǎn),最開(kāi)始時(shí)候,pos當(dāng)指向第一個(gè)結(jié)點(diǎn)。而head開(kāi)始時(shí)候也是指向第一個(gè)外層結(jié)點(diǎn)的里面的這個(gè)內(nèi)嵌的鏈表結(jié)構(gòu)體struct list_head{},(head)->next則指向后繼的一個(gè)外層結(jié)點(diǎn)的內(nèi)嵌的鏈表結(jié)點(diǎn)struct list_head{} list。member即是指出該list為其內(nèi)嵌的結(jié)點(diǎn)。
思路:用pos指向外層結(jié)構(gòu)體的結(jié)點(diǎn),用head指向內(nèi)層嵌入的結(jié)構(gòu)體的結(jié)點(diǎn)。用(head)->next,pos->member.next(即:ptr->list.next)來(lái)在內(nèi)嵌的結(jié)構(gòu)體結(jié)點(diǎn)鏈表中遍歷。每遍歷一個(gè)結(jié)點(diǎn),就用list_entry()將內(nèi)嵌的pos->member.next指針回轉(zhuǎn)為指向該結(jié)點(diǎn)外層結(jié)構(gòu)體起始處的指針,并將指針進(jìn)行指針類型轉(zhuǎn)換為外層結(jié)構(gòu)體型pos。&pos->member! = (head)用pos外層指針引用member即:list成員,與內(nèi)層嵌入的鏈表之頭結(jié)點(diǎn)比較來(lái)為循環(huán)結(jié)束條件。
-------------list_for_each_entry_reverse()-------
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), m+ember); \
prefetch(pos->member.prev), &pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
分析類似上面。
---------------list_prepare_entry()---------------
1.函數(shù)背景:來(lái)自楊沙洲.國(guó)防科技大學(xué)計(jì)算機(jī)學(xué)院.2004年8月.www.linuxforum.net Linux 內(nèi)核技術(shù)論壇:
楊在貼子中指出:如果遍歷不是從鏈表頭開(kāi)始,而是從已知的某個(gè)pos結(jié)點(diǎn)開(kāi)始,則可以使用list_for_each_entry_continue(pos,head,member)。有時(shí)還會(huì)出現(xiàn)這種需求,即經(jīng)過(guò)一系列計(jì)算后,如果pos有值,則從pos開(kāi)始遍歷,如果沒(méi)有,則從鏈表頭開(kāi)始,為此,Linux專門(mén)提供了一個(gè)list_prepare_entry(pos,head,member)宏,將它的返回值作為list_for_each_entry_continue()的pos參數(shù),就可以滿足這一要求。
2.內(nèi)核中的list_prepare_entry()的注釋及代碼:
/**
* list_prepare_entry - prepare a pos entry for use as a start point in
* @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.
*/
內(nèi)核源代碼:
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member))
分析:
:前面是個(gè)空值,即:若pos不為空,則pos為其自身。等效于:
(pos)? (pos): list_entry(head,typeof(*pos),member)
注意內(nèi)核格式::前后都加了空格。
------------list_for_each_entry_continue()--------
3.內(nèi)核中的list_for_each_entry_continue()的注釋及代碼:
/**
* list_for_each_entry_continue - iterate over list of given type
*continuing after existing point
* @pos: the type * to use as a loop counter.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
內(nèi)核源代碼:
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_entry(pos->member.next, typeof(*pos), member); \
prefetch(pos->member.next), &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
分析見(jiàn)list_prepare_entry()中的函數(shù)背景。
-------------list_for_each_entry_safe()-----------
1.函數(shù)背景:來(lái)自楊沙洲.國(guó)防科技大學(xué)計(jì)算機(jī)學(xué)院.2004年8月.www.linuxforum.net Linux 內(nèi)核技術(shù)論壇:
楊在貼子中指出:list_for_each_entry_safe(pos, n, head,member),它們要求調(diào)用者另外提供一個(gè)與pos同類型的指針n,在for循環(huán)中暫存pos下一個(gè)節(jié)點(diǎn)的地址,避免因pos節(jié)點(diǎn)被釋放而造成的斷鏈。
2.內(nèi)核中的注釋與源代碼:
/**
* 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 counter.
* @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()-------
#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))
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -