由於linux核心中的struct list_head已經定義了指向前驅的prev指標和指向後繼的next指標,並且提供了相關的連結串列操作方法,因此為方便複用,本文在它的基礎上封裝實現了一種使用開鏈法解決衝突的通用核心Hash表glib_htable,提供了初始化、增加、查詢、刪除、清空和銷燬6種操作,除初始化和銷燬外,其它操作都做了同步,適用於中斷和程式上下文。與一般的通用Hash表(如c++中的hash_map及某些c語言實現的泛型雜湊表)有以下不同:
● 儲存在glib_htable裡的物件由外部而不是內部負責建立,這個物件必須直接或間接地組合了list_head成員(間接組合,包含下文中的glib_hentry即可),這裡引用UML中的術語組合,意在強調不是聚合關係。
● 刪除操作的語義是從Hash表移去物件的連結,但釋放物件是可選的。
● 桶的個數由外部指定而不是內部維護。
綜上可見glib_htable是使用物件已存在的內嵌成員list_head來連結到Hash表中的,比一般的通用Hash表,每個表項節省了1個指標的空間,如下圖所示。
![](https://i.iter01.com/images/106ae8a474bdf352c4d84a0367af86d91d6fad7c622336dc1ce21e50de99a61b.jpg)
結構定義
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
struct glib_hentry { struct list_head list; void *data; }; typedef unsigned int (*glib_htable_hashfun_t)(const void*,unsigned int); typedef int (*glib_htable_cmpfun_t)(const void*, const void*); typedef void (*glib_htable_cbfun_t)(struct glib_hentry*); typedef void (*glib_htable_freefun_t)(struct glib_hentry*); struct glib_htable { struct list_head *bucket; unsigned int size; unsigned int vmalloced; rwlock_t lock; glib_htable_hashfun_t hashfun; glib_htable_cmpfun_t cmpfun; glib_htable_cbfun_t cbfun; glib_htable_freefun_t freefun; }; |
1)glib_hentry抽象了儲存物件的內嵌成員,表示Hash項,也可表示整個物件,這時內嵌成員就是物件本身了,成員data表示物件關聯的任意資料,用於計算hash值,當關聯資料大小<=sizeof(void*)時,可直接強制轉換為data儲存,而不必為資料地址。
2)glib_htable抽象了Hash表,size表示桶個數,考慮到size可能很多,需要佔用大塊記憶體,所以在分配連續物理頁失敗的情況下,再使用vmalloc嘗試分配不連續的物理頁,所以引入了vmalloced表示分配方式,非零表示用vmalloc,零則用__get_free_pages;hashfun和cmpfun是實現Hash表的兩個缺一不可的關鍵函式,cbfun用於查詢成功時的回撥處理,如列印、增加引用計數等,freefun用於釋放物件,提供這個回撥介面是為了方便從Hash表移除物件後可以釋放物件,而不必由外部釋放,增加了靈活性。
主要介面
以下所有操作中的第1引數ht表示glib_htable物件。
● 初始化
1 |
int glib_htable_init(struct glib_htable *ht, unsigned int size, glib_htable_hashfun_t hashfun, glib_htable_cmpfun_t cmpfun); |
size表示Hash表桶的個數,hashfun為Hash函式,cmpfun為比較函式;成功時返回0,ht成員cbfun和freefun設定為空,失敗返回ENOMEM。由於可能使用vmalloc分配記憶體,因此不能用於中斷上下文。
● 增加
1 |
void glib_htable_add(struct glib_htable *ht, struct glib_hentry *he, int num); |
在一次同步內新增多個物件,he為指向物件Hash項的指標,num為個數。
● 查詢
1 2 3 4 5 6 |
struct glib_hentry* glib_htable_get(struct glib_htable *ht, const void *data); struct glib_hentry* glib_htable_rget(struct glib_htable *ht, const void *data); struct glib_hentry* glib_htable_cget(struct glib_htable *ht, const void *data, int(*cmp)(const struct glib_hentry*, void*), void *arg); struct glib_hentry* glib_htable_crget(struct glib_htable *ht, const void *data, int(*cmp)(const struct glib_hentry*, void*), void *arg); struct glib_hentry* glib_htable_cget_byidx(struct glib_htable *ht, unsigned int *bucket, int(*cmp)(const struct glib_hentry*, void*), void *arg); struct glib_hentry* glib_htable_crget_byidx(struct glib_htable *ht, unsigned int *bucket, int(*cmp)(const struct glib_hentry*, void*), void *arg); |
從上到下依次為正向查詢、反向查詢、正向條件查詢、反向條件查詢、按桶定位的正向條件查詢、按桶定位的反向條件查詢,data為物件關聯資料,cmp為自定義的比較函式,arg為cmp所帶的自定義引數,bucket為桶索引,若查詢成功,則bucket更新為物件所在的桶索引。以上所有操作,當失敗時返回NULL。
● 刪除
1 2 |
void glib_htable_del(struct glib_htable *ht, struct glib_hentry *he, int num); void glib_htable_del_bydata(struct glib_htable *ht, const void **data, int num); |
第1個按物件Hash項刪除,第2個按物件關聯資料刪除,num表示個數,若ht成員freefun非空,則釋放物件。
● 清空
1 |
void glib_htable_clear(struct glib_htable *ht); |
在一次同步內刪除所有的物件,若ht成員freefun非空,則釋放物件。
● 銷燬
1 |
void glib_htable_free(struct glib_htable *ht); |
僅釋放所有桶佔用的記憶體,應在glib_htable_clear後呼叫。由於可能使用vfree釋放記憶體,因此不能用於中斷上下文。
介面實現
其它介面實現比較簡單,略過講解。對於查詢介面,如果增加一個引數來指示遍歷方向,那麼雖然介面總數減半,但在使用特別是在一個迴圈內呼叫時,每次都進行不必要的方向判斷而降低了效能,所以對於正向和反向遍歷,每個都給出一個介面,正如c庫中的strchr與strrchr、c++容器中的iterator與reverse_iterator,這樣一來更清晰明確。在實現上除了遍歷方向不同外,其它程式碼都相同,因此為避免手工編碼冗餘,使用了3組巨集來生成。
輔助函式巨集生成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#define DEFINE_GLIB_HTABLE_GET_HELP(name) \ static struct glib_hentry* __glib_htable_##name(struct glib_htable *ht, unsigned int hash, const void *data) \ {\ struct glib_hentry *he; \ \ glib_htable_list_##name(he,&ht->bucket[hash],list){ \ if(ht->cmpfun(he->data,data)){ \ if(ht->cbfun) \ ht->cbfun(he); \ return he; \ } \ } \ \ return NULL; \ } DEFINE_GLIB_HTABLE_GET_HELP(get) DEFINE_GLIB_HTABLE_GET_HELP(rget) #define DEFINE_GLIB_HTABLE_COND_GET_HELP(name) \ static struct glib_hentry* __glib_htable_c##name(struct glib_htable *ht, unsigned int hash, int(*cmp)(const struct glib_hentry*, void*), void *arg) \ { \ struct glib_hentry *he; \ \ glib_htable_list_##name(he,&ht->bucket[hash],list){ \ if(cmp(he, arg)){ \ if(ht->cbfun) \ ht->cbfun(he); \ return he; \ } \ } \ \ return NULL; \ } DEFINE_GLIB_HTABLE_COND_GET_HELP(get) DEFINE_GLIB_HTABLE_COND_GET_HELP(rget) |
生成巨集為DEFINE_GLIB_HTABLE_GET_HELP和DEFINE_GLIB_HTABLE_COND_GET_HELP,展開後就有了__glib_htable_get(rget)和__glib_htable_cget(crget) 4個不加鎖的函式,用於實現對應的加鎖介面。glib_htable_list_get和glib_htable_list_rget分別是巨集list_for_each_entry和list_for_each_entry_reverse的別名。
普通查詢巨集生成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#define DEFINE_GLIB_HTABLE_GET(name) \ struct glib_hentry* glib_htable_##name(struct glib_htable *ht, const void *data) \ { \ struct glib_hentry *he; \ unsigned int h = ht->hashfun(data,ht->size); \ \ read_lock_bh(&ht->lock); \ he = __glib_htable_##name(ht, h, data); \ read_unlock_bh(&ht->lock); \ \ return he; \ } DEFINE_GLIB_HTABLE_GET(get) DEFINE_GLIB_HTABLE_GET(rget) |
呼叫輔助函式__glib_htable_get(rget)實現,生成巨集為DEFINE_GLIB_HTABLE_GET,展開後就有了glib_htable_get(rget)介面。
條件查詢巨集生成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#define DEFINE_GLIB_HTABLE_COND_GET(name) \ struct glib_hentry* glib_htable_c##name(struct glib_htable *ht, const void *data, int(*cmp)(const struct glib_hentry*, void*), void *arg) \ { \ struct glib_hentry *he; \ unsigned int h = ht->hashfun(data,ht->size); \ \ read_lock_bh(&ht->lock); \ he = __glib_htable_c##name(ht, h, cmp, arg); \ read_unlock_bh(&ht->lock); \ \ return he; \ } DEFINE_GLIB_HTABLE_COND_GET(get) DEFINE_GLIB_HTABLE_COND_GET(rget) #define DEFINE_GLIB_HTABLE_COND_GET_BYIDX(name) \ struct glib_hentry* glib_htable_c##name##_byidx(struct glib_htable *ht, unsigned int *bucket, int(*cmp)(const struct glib_hentry*, void*), void *arg) \ { \ unsigned int h; \ struct glib_hentry *he = NULL; \ \ read_lock_bh(&ht->lock); \ \ for (h = *bucket; h < ht->size; h = (*bucket)++){ \ he = __glib_htable_c##name(ht, h, cmp, arg); \ if(he) \ break; \ } \ \ read_unlock_bh(&ht->lock); \ \ return he; \ } DEFINE_GLIB_HTABLE_COND_GET_BYIDX(get) DEFINE_GLIB_HTABLE_COND_GET_BYIDX(rget) |
前者呼叫輔助函式__glib_htable_cget(rget)實現,生成巨集為DEFINE_GLIB_HTABLE_COND_GET,展開後就有了glib_htable_cget(rget)介面;後者呼叫輔助函式__glib_htable_cget(rget)_byidx實現,生成巨集為DEFINE_GLIB_HTABLE_COND_GET_BYIDX,展開後就有了glib_htable_cget(rget)_byidx介面。
完整原始碼下載:glib_hash,包括glib_htable.h和glib_htable.c檔案。