Linux內核鏈表之哈希鏈表

/*
 * <Filename : hlist.h>
 * Date : 2017年6月4日
 * Weather : cloudy day
 * Author : goup 更多內容,請點擊http://blog.csdn.net/weixin_37977062?viewmode=contents
 */


 Linux內核數據結構之哈希鏈表



/* 
 * 1.因爲哈希鏈表並不需要雙向循環的技能,它一般適用於單向散列的場景。
 *   所以,爲了減少開銷,並沒有用struct hlist_node{}來代表哈希表頭,
 *   而是重新設計struct hlist_head{}這個數據結構
 * 
 * 2.此時,一個哈希表頭就只需要4Byte了,相比於struct hlist_node{}來說,
 *   存儲空間已經減少了一半。這樣一來,在需要大量用到哈希鏈表的場景,
 *   其存儲空間的節約是非常明顯的,特別是在嵌入式設備領域。
 */


結點


//include/linux/types.h
struct list_head {
    struct list_head *next, //指向下一個結點的指針
*prev; //指向上一個結點的next指針的地址
};


//哈希鏈表的表頭
struct hlist_head {
    struct hlist_node *first; //指向每一個hash桶的第一個結點的指針
};


/*
 * 注意:struct hlist_node的前驅結點是一個2級指針
 *
 * 1.兩級指針的宿命,爲了間接改變一級指針所指的內存地址
 * 
 * 2.node節點裏的pprev其實指向的是其前一個節點裏的第一個指針元素的地址,對於hlist_head來說,
 *   它裏面只有一個指針元素,就是first指針;而對於hlist_node來說,第一個指針元素就是next
 *
 * 3.所以,記住,當我們在代碼中看到類似與*(hlist_node->pprev)這樣的代碼時,我們心裏應該清楚,
 *   此時正在哈希表裏操作當前節點前一個節點裏的第一個指針元素所指向的內存地址,只是以間接的方式實現罷了
 *
 */


//哈希鏈表結點
struct hlist_node {
//*prev指向的是前驅的next域
    struct hlist_node *next, **pprev;
};






//list.h
#define HLIST_HEAD_INIT { .first = NULL }
#define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)


/***********************************************INIT_HLIST_NODE**********************************************/
/**************************************************初始化結點************************************************/


static inline void INIT_HLIST_NODE(struct hlist_node *h)
{
h->next = NULL;
h->pprev = NULL;
}


/***********************************************hlist_unhashed***********************************************/
/********************************************判斷該結點是不是表頭********************************************/


static inline int hlist_unhashed(const struct hlist_node *h)
{
return !h->pprev;
}


/************************************************hlist_empty*************************************************/
/************************************************判斷是否爲空************************************************/


static inline int hlist_empty(const struct hlist_head *h)
{
return !h->first;
}


2.刪除


/************************************************************************************************************/
/***************************************************刪除*****************************************************/
/************************************************************************************************************/






/***************************************************刪除*****************************************************/


static inline void __hlist_del(struct hlist_node *n)
{
struct hlist_node *next = n->next; //獲取指向待刪除結點的下一個普通結點的指針 
struct hlist_node **pprev = n->pprev; //獲取待刪除結點的pprev域 
*pprev = next; //修改待刪除結點的pprev域,邏輯上使待刪除結點的前驅結點指向待刪除結點的後繼結點

//在標準的哈希鏈表裏,因爲最後一個節點的next=NULL
if (next)
next->pprev = pprev; //設置下一個結點的pprev域
}


/***********************************************刪除結點 n **************************************************/


static inline void hlist_del(struct hlist_node *n)
{
__hlist_del(n);
n->next = LIST_POISON1;
n->pprev = LIST_POISON2;
}


static inline void hlist_del_init(struct hlist_node *n)
{
if (!hlist_unhashed(n)) {
__hlist_del(n);
INIT_HLIST_NODE(n);
}
}



3.插入

/************************************************************************************************************/
/**************************************************插入******************************************************/
/************************************************************************************************************/






/**************************************************頭插******************************************************/


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 結點前面插入 n 結點*****************************************/




/* next must be != 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;
}




/*****************************************在 next 結點後面插入 n 結點*****************************************/




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;
}


/* after that we'll appear to be on some hlist and hlist_del will work */
static inline void hlist_add_fake(struct hlist_node *n)
{
n->pprev = &n->next;
}


/*
 * Move a list from one list head to another. Fixup the pprev
 * reference of the first entry if it exists.
 */
static inline void hlist_move_list(struct hlist_head *old,
  struct hlist_head *new)
{
new->first = old->first;
if (new->first)
new->first->pprev = &new->first;
old->first = NULL;
}




4.遍歷


/**************************************************************************************************************/
/*****************************************************遍歷*****************************************************/
/**************************************************************************************************************/




/******************************************************宏******************************************************/




/*************************************************hlist_entry**************************************************/




#define hlist_entry(ptr, type, member) container_of(ptr,type,member)




/***********************************************hlist_for_entry************************************************/


/*
 * #define hlist_for_each(pos, head) \
 *      for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
 *            pos = pos->next)
 *
 * //prefetch宏是一個空的實現,其值是1,因此for循環中的第二個表達式其實就等價於 pos
 */
 
#define hlist_for_each(pos, head) \
for (pos = (head)->first; pos ; pos = pos->next)



/*********************************************hlist_for_each_safe**********************************************/
/**********************************************用於刪除結點的遍歷**********************************************/


#define hlist_for_each_safe(pos, n, head) \
for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
    pos = n)




/*******************************************hlist_for_each_entry*********************************************/
/***********************************************從表頭開始遍歷***********************************************/


/**
 * hlist_for_each_entry - iterate over list of given type
 * @tpos: the type * to use as a loop cursor. //@tpos : 指向結構體的指針
 * @pos: the &struct hlist_node to use as a loop cursor. //@pos  :用於循環,struct hlist_node類型
 * @head: the head for your list. //@head : 哈希桶head
 * @member: the name of the hlist_node within the struct. //member: 宿主結構體成員,即哈希桶的一個結點
 */
// ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});
// 這個表達式的值也永遠爲1,但是這裏tpos指向了hlist_node所在的宿主結構
#define hlist_for_each_entry(tpos, pos, head, member) \
for (pos = (head)->first; \
    pos && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
    pos = pos->next)




/***************************************hlist_for_each_entry_continue****************************************/
/*****************************************從 pos 下一個結點開始遍歷******************************************/




/**
 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
 * @tpos: the type * to use as a loop cursor.
 * @pos: the &struct hlist_node to use as a loop cursor.
 * @member: the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_continue(tpos, pos, member) \
for (pos = (pos)->next; \
    pos && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
    pos = pos->next)






/*****************************************hlist_for_each_entry_from******************************************/
/*********************************************從 pos 結點開始遍歷********************************************/




/**
 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
 * @tpos: the type * to use as a loop cursor.
 * @pos: the &struct hlist_node to use as a loop cursor.
 * @member: the name of the hlist_node within the struct.
 */
#define hlist_for_each_entry_from(tpos, pos, member) \
for (; pos && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
    pos = pos->next)


 
/*****************************************hlist_for_each_entry_safe******************************************/
/****************************************從頭開始遍歷,用於刪除結點******************************************/






/**
 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @tpos: the type * to use as a loop cursor.
 * @pos: the &struct hlist_node to use as a loop cursor.
 * @n: another &struct hlist_node to use as temporary storage
 * @head: the head for your list.
 * @member: the name of the hlist_node within the struct.
 */
#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)


更多內容,請點擊http://blog.csdn.net/weixin_37977062?viewmode=contents

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