剛剛把hlist有關的函數和宏定義都過了一遍,在此做了一下整理。希望對大家以後學習linux有用,也歡迎大家來拍磚
/*Linux鏈表設計者(認為雙頭(next、prev)的雙鏈表對於HASH表來說"過於浪費",因而另行設計了一套用於HASH表應用的hlist資料結構--單指標表頭雙迴圈鏈表,hlist的表頭僅有一個指向首節點的指標,而沒有指向尾節點的指標,這樣在可能是海量的HASH表中儲存的表頭就能減少一半的空間消耗。
*/
//HASH LIST
//表頭結點(不同於結點的資料結構)
struct hlist_head { struct hlist_node *first;};
/*pprev首先也是一個指標,不過它是指向指標的指標(這點請仔細體會),在hlist中pprev指向的是當前hlist_node變數中的前一個變數的next的地址,
如果是第一個元素的話,這個值指向的是first的地址,如果是最後一個節點的話,指向的是NULL。如果再深入理解一下,
其實*pprev和next這兩個變數分別表示指向自己的指標和指向其他節點的指標。
*/
struct hlist_node { //結點 struct hlist_node *next, **pprev;};
//給struct hlist_head變數來進行初始化的。
#define HLIST_HEAD_INIT { .first = NULL }
//定義一個一個struct hlist_head節點,並且給其初始化為NULL
#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
//給struct hlist_head節點進行初始化的。
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
//這個宏函數是對struct hlist_node節點的一個變數進行初始化的操作。這個宏用在刪除節點之後對節點的操作當中。
#define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
/*h:struct hlist_node節點指標。
判斷h->prev是不是為空白,如果pprev的指向是空的話,表示這個節點沒有添加到這個鏈表當中來,如果是空,返回true,否則返回false*/
static inline int hlist_unhashed(const struct hlist_node *h){ return !h->pprev;}
/*h:struct hlist_head節點指標(hlist鏈表的前端節點)。
判斷hlist鏈表是不是空鏈表,如果是,返回true,否則返回false。
*/
static inline int hlist_empty(const struct hlist_head *h){ return !h->first;}
/*真正意義上實現刪除操作的函數。
n:要刪除的節點。
對於刪除操作的話,要注意n是不是末尾節點,如果是末尾節點的話,next就是NULL,所以就沒有指向的pprev,就更不能進行相應的修改了,否則進行修改。
*/
static inline void __hlist_del(struct hlist_node *n){ struct hlist_node *next = n->next; struct hlist_node **pprev = n->pprev; *pprev = next; if (next) next->pprev = pprev;}
/*n:要刪除的節點。
在這個函數中首先刪除了n節點,之後將n節點的兩個指標指向了LIST_POSION,表示不可使用的地方
*/
static inline void hlist_del(struct hlist_node *n){ __hlist_del(n); n->next = LIST_POISON1; n->pprev = LIST_POISON2;}
/*n:要刪除的節點。
首先判斷要刪除的pprev是不是空,如果是,則不能刪除,否則進行刪除操作,刪除完成之後,將n節點的next和pprev都指向NULL(初始化節點)。
*/
static inline void hlist_del(struct hlist_node *n){ __hlist_del(n); n->next = LIST_POISON1; n->pprev = LIST_POISON2;}
/*
n:要添加的新的節點。
h:hlist鏈表的表前端節點。
這個函數是給h的下一個和first節點中添加一個新的hlist_node節點,類似與頭插。
*/
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h){ struct hlist_node *first = h->first; n->next = first; if (first) first->pprev = &n->next; h->first = n; n->pprev = &h->first;}
/* next must be != NULL */
/*
n:要添加的新的節點。
next:在next節點之前添加n。
在next節點的前面添加一個新的節點n,在使用這個函數中要特別注意,next不能為NULL。
*/
static inline void hlist_add_before(struct hlist_node *n, struct hlist_node *next){ n->pprev = next->pprev; n->next = next; next->pprev = &n->next; *(n->pprev) = n;}
/*n:表示在n節點之後添加next。
next:要添加的新的節點。
在n節點的後面添加一個新的節點next,這裡也要求n不能為NULL
*/
static inline void hlist_add_after(struct hlist_node *n, struct hlist_node *next){ next->next = n->next; n->next = next; next->pprev = &n->next; if(next->next) next->next->pprev = &next->next;}
/*說明:有關hlist中的宏定義與list中的宏定義大同小異,所以在此只是簡單分析,具體分析見上面代碼*/
/*ptr:表示struct hlist_node類型的一個地址。
type:結構體名
member:type結構體中的hlist_node成員變數的名稱
這個宏在list鏈表中分析過了,表示得到ptr所指地址的這個結構體的首地址
*/
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
/*pos:struct hlist_node類型的一個指標;
head:struct hlist_head類型的一個指標,表示hlist鏈表的頭結點。
這個實際上就是一個for迴圈,從頭到尾遍曆鏈表。
*/
#define hlist_for_each(pos, head) \ for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ pos = pos->next)
/*這個實際上就是一個for迴圈,從頭到尾遍曆鏈表。這個和前面的不同的是多了一個n,這麼做是為了遍曆過程中防止斷鏈的發生。
pos:struct hlist_node類型的一個指標;
n:struct hlist_node類型的一個指標;
head:struct hlist_head類型的一個指標,表示hlist鏈表的頭結點。
*/
#define hlist_for_each_safe(pos, n, head) \ for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ pos = n)
/*tops:用來存放遍曆到的資料結構的地址,類型是type *;
pos:struct hlist_node類型的一個指標;
head:hlist鏈表的頭結點;
member:struct hlist_node在type結構體中的變數的名稱。
在迴圈中,我們就可以使用tops來指向type類型結構體的任何一個變數了。
*/
#define hlist_for_each_entry(tpos, pos, head, member) \ for (pos = (head)->first; \ pos && ({ prefetch(pos->next); 1;}) && \ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ pos = pos->next)
/*tops:用來存放遍曆到的資料結構的地址,類型是type *;
pos:struct hlist_node類型的一個指標;
member:struct hlist_node在type結構體中的變數的名稱。
這個宏是從pos這個地址的下一個元素處開始繼續往下遍曆。
*/
#define hlist_for_each_entry_continue(tpos, pos, member) \ for (pos = (pos)->next; \ pos && ({ prefetch(pos->next); 1;}) && \ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ pos = pos->next)
/*tops:用來存放遍曆到的資料結構的地址,類型是type *;
pos:struct hlist_node類型的一個指標;
member:struct hlist_node在type結構體中的變數的名稱。
這個宏是從pos這個地址處開始繼續往下遍曆。
*/
#define hlist_for_each_entry_from(tpos, pos, member) \ for (; pos && ({ prefetch(pos->next); 1;}) && \ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ pos = pos->next)
/*tops:用來存放遍曆到的資料結構的地址,類型是type *;
pos:struct hlist_node類型的一個指標;
n:struct hlist_node類型的一個指標;
head:hlist鏈表的頭結點;
member:struct hlist_node在type結構體中的變數的名稱。
在迴圈中,我們就可以使用tops來指向type類型結構體的任何一個變數了。這個宏函數也是為了防止在遍曆的時候刪除節點而引入的。
*/
#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ for (pos = (head)->first; \ pos && ({ n = pos->next; 1; }) && \ ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ pos = n)#endif