對照前面介紹過的內核通知鏈、鏈表,本章哈希表的初始化與定義如出一轍:
定義並初始化一個名為 name 的哈希鏈表表頭:
#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }初始化一個已定義好的哈希鏈表,ptr 指向表頭地址:
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
HLIST_HEAD_INIT 一般這樣用:
struct hlist_head myhlist; HLIST_HEAD_INIT(&myhlist);
對於每個 hlist_node 節點,通常調用 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_node 的 pprev 為NULL,則說明該節點尚未加入任何哈希鏈表。判斷哈希表是否為空(入參為 hlist_head 類型):
static inline int hlist_empty(const struct hlist_head *h)
{
return !h->first;
}其餘接口都很簡單,這裡不再贅述。看幾個宏:
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
它與 list_entry() 的實現、作用完全相同。對照 list 的學習,下面幾組結構作用不言而喻:
hlist_for_each / list_for_each、hlist_for_each_safe / list_for_each_safe、hlist_for_each_entry / list_for_each_entry、hlist_for_each_entry_safe / list_for_each_entry_safe。區別在於入參:由於哈希鏈表的表頭與表節點是不同數據結構,故有此差異。
以 hlist_for_each_entry(tpos, pos, head, member) 為例,tpos 是 hlist_node 所屬宿主結構體類型的指針,pos 是 hlist_node 類型指針,兩者都充當游標。例如:
typedef struct student
{
char m_name[MAX_STRING_LEN];
char m_sex;
int m_age;
struct list_head m_list; /* 組織成雙向鏈表 */
struct hlist_node m_hlist;/* 組織成哈希鏈表 */
}Student;
HLIST_HEAD(myhlist);
Student *st;
struct hlist_node *i;
hlist_for_each_entry(st, i, &myhlist, m_hlist)
{
// 通常只需關注、使用 st 變量
}使用 hlist_for_each_entry_safe(tpos, pos, n, head, member) 時,tpos 也是宿主結構體類型指針,n 是指向 pos 下一個元素的 hlist_node 指針,由該宏自身維護,開發者不用修改:
HLIST_HEAD(myhlist);
Student *st;
struct hlist_node *i,*j;
hlist_for_each_entry_safe(st, i, j, &myhlist, m_hlist)
{
// 僅使用 st 即可
}還有一組宏:
hlist_for_each_entry_continue(tpos, pos, member) hlist_for_each_entry_from(tpos, pos, member)
前者從 pos 節點開始(不含pos)向後遍歷,後者從 pos 節點開始(含pos)向後遍歷。這組宏是「不安全」的,只能用於查找遍歷,不能插入或刪除節點。
最後實際操練一把。把鏈表章節的學歷管理系統加一個需求:「按男、女分類所有學生」。這裡就能用到哈希鏈表。頭文件修改:
/*student.h*/
#ifndef __STUDENT_H_
#define __STUDENT_H_
#include <linux/list.h>
#define MAX_STRING_LEN 32
#define MAX_HLIST_COUNT 2 // 只有「男」「女」兩條哈希鏈表
typedef struct student
{
char m_name[MAX_STRING_LEN];
char m_sex;
int m_age;
struct list_head m_list;
struct hlist_node m_hlist;
}Student;
#endif源文件修改(節選核心):
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include "student.h"
MODULE_LICENSE("Dual BSD/GPL");
LIST_HEAD(g_student_list);
// g_stu_hlist[0]代表男生;g_stu_hlist[1]代表女生
struct hlist_head g_stu_hlist[MAX_HLIST_COUNT];
static void init_hlists(void)
{
int i = 0;
for(i=0;i< MAX_HLIST_COUNT;i++){
INIT_HLIST_HEAD(&g_stu_hlist[i]);
}
}
static int add_stu(char* name,char sex,int age)
{
Student *stu,*cur_stu;
list_for_each_entry(cur_stu,&g_student_list,m_list){
if(0 == strcmp(cur_stu->m_name,name)){ return -1; }
}
stu = kmalloc(sizeof(Student), GFP_KERNEL);
if(!stu){ return -1; }
memset(stu,0,sizeof(Student));
strncpy(stu->m_name,name,strlen(name));
stu->m_sex = sex;
stu->m_age = age;
INIT_LIST_HEAD(&stu->m_list);
INIT_HLIST_NODE(&stu->m_hlist);
list_add_tail(&stu->m_list,&g_student_list);
return 0;
}
EXPORT_SYMBOL(add_stu);
// 按性別分類
static void classify_stu(void)
{
Student *cur,*next;
int id = 0;
list_for_each_entry_safe(cur,next,&g_student_list,m_list){
list_del_init(&cur->m_list);
if('m' == cur->m_sex){ id = 0; }
else if('f' == cur->m_sex){ id = 1; }
else{ return; }
hlist_add_head(&(cur->m_hlist),&(g_stu_hlist[id]));
}
}
EXPORT_SYMBOL(classify_stu);
static void dump_hlist(int id)
{
Student *stu;
struct hlist_node *i;
struct hlist_head *head = &g_stu_hlist[id];
hlist_for_each_entry(stu, i, head, m_hlist){
printk("name:[%s], sex:[%c], age:[%d]\n",
stu->m_name, stu->m_sex, stu->m_age);
}
}
EXPORT_SYMBOL(dump_hlist);驗證結果如下:
每調用一次 classify_stu() 便會把 g_student_list 中的學生按性別分類,男生存入 g_stu_hlist[0],女生存入 g_stu_hlist[1];調用 add_stu() 則向 g_student_list 添加學生。

可見哈希鏈表的用法也頗為簡單。內核裡諸如通知鏈、鏈表、哈希表等基礎數據結構,掌握原理後使用起來都不難。
未完,待續……