在研究linux核心內建的dmatest.c驅動程式過程中發現有部分的連結操作,非常迷惑,故在此記錄下來一些查閱資料後的心得體會。 0 核心鏈表的特點
普通的鏈表操作,通常包含資料域和指標域2個內容 如下所示。
typedef struct node { ElemType data; //資料域 struct node *next; //指標域 }node, *list; |
而Linux核心定義的鏈表不帶資料域,只需要兩個指標完成鏈表的操作。具有非常高的擴充性、通用性。鏈表結構定義如下所示。
struct list_head { struct list_head *next, *prev; }; |
通常有如下格式的定義,通常建議結合containner_of和offset_of擷取更大的靈活可操作性。例如下例,可以根據app_info_head的地址找出app_info的起始地址,即一個完整的app_info結構的起始地址。
typedef struct application_info { uint32_t app_id; uint32_t up_flow; uint32_t down_flow; struct list_head app_info_head; //鏈表節點 }app_info; |
1 鏈表操作及實現原理
(1) 初始化鏈表頭結點
初始化的效果是使得前驅和後繼指標都是指向頭結點的。
這裡需要十分注意Init的介面(一開始沒有注意到導致錯誤理解了代碼)LIST_HEAD_INIT、LIST_HEAD、INIT_LIST_HEAD三者的區別。
#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; } |
所以有如下兩種用法。一種是宏擴充,另一種是函數調用。
//方式一 static struct info_t{ struct list_head channels; }info = { .channels = LIST_HEAD_INIT(info.channels); } //方式二 INIT_LIST_HEAD(&info.channels); |
(2) 插入操作
list_add和list_add_tail分別是插在表頭和表尾,但是都是通過__list_add實現,因為核心實現的鏈表是雙向鏈表,所以head->prev之後就是表尾,而head->next之後就是表頭。核心實現如下表所示。
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; } static inline void list_add(struct list_head *new, struct list_head *head) { __list_add(new, head, head->next); } static inline void list_add_tail(struct list_head *new, struct list_head *head) { __list_add(new, head->prev, head); } |
(3) 刪除操作
list_del,即刪除該節點的前驅和後繼節點。需要注意,刪除後還需要將待刪除節點的前驅和後繼分別指向POSITION1和POSITION2。對POSITION1和POSITION2的操作都將引起頁故障。
static inline void __list_del(struct list_head * prev, struct list_head * next) { next->prev = prev; prev->next = next; } static inline void list_del(struct list_head *entry) { __list_del(entry->prev, entry->next); entry->next = LIST_POISON1; entry->prev = LIST_POISON2; } /* * These are non-NULL pointers that will result in page faults * under normal circumstances, used to verify that nobody uses * non-initialized list entries. */ #define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA) #define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA) |
(4) 判斷鏈表
是否為空白(list_empty)是否是最後一結點(list_is_last)。
/** * list_is_last - tests whether @list is the last entry in list @head * @list: the entry to test * @head: the head of the list */ static inline int list_is_last(const struct list_head *list, const struct list_head *head) { return list->next == head; } /** * 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; } |
(5) 遍曆鏈表
注意list_for_each只是個宏替代。
/** * 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_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) /** * 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; prefetch(pos->next), pos != (head); \ pos = pos->next) |
在遍曆時經常需要使用container_of和offset,例如list_for_each_entry(pos, head, member),就是遍曆head鏈表,head鏈表的指標類型為 member(字串),member是pos類型結構體的一個成員,再基於container_of得到結構體指標。[這點技巧在核心源碼中經常能找到] 2 鏈表使用舉例
下面以dmatest.c為例說明。需求:DMA測試程式需要實現多通道,且通道上支援多線程,需要支援能夠互相訪問。
首先,因為需求互相訪問,所以立即想到struct成員變數的方式,如下代碼所示。
struct channel_t{ struct thread_t used[100]; } struct info_t{ struct channel_t used[100]; }; static struct info_t info; |
但是,缺點也很明顯,申請了固定大小空間,要麼浪費資源,要麼資源不夠。寫到這,立即可以推出使用鏈表,但是核心提供的鏈表並沒有資料域都是指標域,如何設計成為了關鍵。
在這裡,dmatest.c給出了參考答案,通過在每個成員中添加一個node節點,作為中介節點。如下所示。
struct thread_t{ struct list_head node; } struct channel_t{ struct list_head node; struct list_head threads; } struct info_t{ struct list_head channels; }; static info_t info = |