● 存儲在glib_htable里的對象由外部而不是內部負責創建,這個對象必須直接或間接地組合了list_head成員(間接組合,包含下文中的glib_hentry即可),這里引用UML中的術語組合,意在強調不是聚合關系。
● 刪除操作的語義是從Hash表移去對象的鏈接,但釋放對象是可選的。
● 桶的個數由外部指定而不是內部維護。
綜上可見glib_htable是使用對象已存在的內嵌成員list_head來鏈接到Hash表中的,比一般的通用Hash表,每個表項節省了1個指針的空間,如下圖所示。

結構定義
1
struct glib_hentry {
2
struct list_head list;
3
void *data;
4
};
5
6
typedef unsigned int (*glib_htable_hashfun_t)(const void*,unsigned int);
7
typedef int (*glib_htable_cmpfun_t)(const void*, const void*);
8
typedef void (*glib_htable_cbfun_t)(struct glib_hentry*);
9
typedef void (*glib_htable_freefun_t)(struct glib_hentry*);
10
11
struct glib_htable {
12
struct list_head *bucket;
13
unsigned int size;
14
unsigned int vmalloced;
15
16
rwlock_t lock;
17
18
glib_htable_hashfun_t hashfun;
19
glib_htable_cmpfun_t cmpfun;
20
glib_htable_cbfun_t cbfun;
21
glib_htable_freefun_t freefun;
22
};
struct glib_hentry {2
struct list_head list;3
void *data;4
};5

6
typedef unsigned int (*glib_htable_hashfun_t)(const void*,unsigned int);7
typedef int (*glib_htable_cmpfun_t)(const void*, const void*);8
typedef void (*glib_htable_cbfun_t)(struct glib_hentry*);9
typedef void (*glib_htable_freefun_t)(struct glib_hentry*);10

11
struct glib_htable {12
struct list_head *bucket;13
unsigned int size;14
unsigned int vmalloced;15
16
rwlock_t lock;17
18
glib_htable_hashfun_t hashfun;19
glib_htable_cmpfun_t cmpfun;20
glib_htable_cbfun_t cbfun;21
glib_htable_freefun_t freefun;22
};2)glib_htable抽象了Hash表,size表示桶個數,考慮到size可能很多,需要占用大塊內存,所以在分配連續物理頁失敗的情況下,再使用vmalloc嘗試分配不連續的物理頁,所以引入了vmalloced表示分配方式,非零表示用vmalloc,零則用__get_free_pages;hashfun和cmpfun是實現Hash表的兩個缺一不可的關鍵函數,cbfun用于查找成功時的回調處理,如打印、增加引用計數等,freefun用于釋放對象,提供這個回調接口是為了方便從Hash表移除對象后可以釋放對象,而不必由外部釋放,增加了靈活性。
主要接口
以下所有操作中的第1參數ht表示glib_htable對象。
● 初始化
int glib_htable_init(struct glib_htable *ht, unsigned int size, glib_htable_hashfun_t hashfun, glib_htable_cmpfun_t cmpfun);● 增加
void glib_htable_add(struct glib_htable *ht, struct glib_hentry *he, int num);● 查找
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);● 刪除
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);● 清空
void glib_htable_clear(struct glib_htable *ht);● 銷毀
void glib_htable_free(struct glib_htable *ht);接口實現
其它接口實現比較簡單,略過講解。對于查找接口,如果增加一個參數來指示遍歷方向,那么雖然接口總數減半,但在使用特別是在一個循環內調用時,每次都進行不必要的方向判斷而降低了性能,所以對于正向和反向遍歷,每個都給出一個接口,正如c庫中的strchr與strrchr、c++容器中的iterator與reverse_iterator,這樣一來更清晰明確。在實現上除了遍歷方向不同外,其它代碼都相同,因此為避免手工編碼冗余,使用了3組宏來生成。
輔助函數宏生成
1
#define DEFINE_GLIB_HTABLE_GET_HELP(name) \
2
static struct glib_hentry* __glib_htable_##name(struct glib_htable *ht, unsigned int hash, const void *data) \
3
{\
4
struct glib_hentry *he; \
5
\
6
glib_htable_list_##name(he,&ht->bucket[hash],list){ \
7
if(ht->cmpfun(he->data,data)){ \
8
if(ht->cbfun) \
9
ht->cbfun(he); \
10
return he; \
11
} \
12
} \
13
\
14
return NULL; \
15
}
16
17
DEFINE_GLIB_HTABLE_GET_HELP(get)
18
DEFINE_GLIB_HTABLE_GET_HELP(rget)
19
20
#define DEFINE_GLIB_HTABLE_COND_GET_HELP(name) \
21
static struct glib_hentry* __glib_htable_c##name(struct glib_htable *ht, unsigned int hash, int(*cmp)(const struct glib_hentry*, void*), void *arg) \
22
{ \
23
struct glib_hentry *he; \
24
\
25
glib_htable_list_##name(he,&ht->bucket[hash],list){ \
26
if(cmp(he, arg)){ \
27
if(ht->cbfun) \
28
ht->cbfun(he); \
29
return he; \
30
} \
31
} \
32
\
33
return NULL; \
34
}
35
36
DEFINE_GLIB_HTABLE_COND_GET_HELP(get)
37
DEFINE_GLIB_HTABLE_COND_GET_HELP(rget)
#define DEFINE_GLIB_HTABLE_GET_HELP(name) \2
static struct glib_hentry* __glib_htable_##name(struct glib_htable *ht, unsigned int hash, const void *data) \3
{\4
struct glib_hentry *he; \5
\6
glib_htable_list_##name(he,&ht->bucket[hash],list){ \7
if(ht->cmpfun(he->data,data)){ \8
if(ht->cbfun) \9
ht->cbfun(he); \10
return he; \11
} \12
} \13
\14
return NULL; \15
}16

17
DEFINE_GLIB_HTABLE_GET_HELP(get)18
DEFINE_GLIB_HTABLE_GET_HELP(rget)19

20
#define DEFINE_GLIB_HTABLE_COND_GET_HELP(name) \21
static struct glib_hentry* __glib_htable_c##name(struct glib_htable *ht, unsigned int hash, int(*cmp)(const struct glib_hentry*, void*), void *arg) \22
{ \23
struct glib_hentry *he; \24
\25
glib_htable_list_##name(he,&ht->bucket[hash],list){ \26
if(cmp(he, arg)){ \27
if(ht->cbfun) \28
ht->cbfun(he); \29
return he; \30
} \31
} \32
\33
return NULL; \34
}35

36
DEFINE_GLIB_HTABLE_COND_GET_HELP(get)37
DEFINE_GLIB_HTABLE_COND_GET_HELP(rget)普通查找宏生成
1
#define DEFINE_GLIB_HTABLE_GET(name) \
2
struct glib_hentry* glib_htable_##name(struct glib_htable *ht, const void *data) \
3
{ \
4
struct glib_hentry *he; \
5
unsigned int h = ht->hashfun(data,ht->size); \
6
\
7
read_lock_bh(&ht->lock); \
8
he = __glib_htable_##name(ht, h, data); \
9
read_unlock_bh(&ht->lock); \
10
\
11
return he; \
12
}
13
14
DEFINE_GLIB_HTABLE_GET(get)
15
DEFINE_GLIB_HTABLE_GET(rget)
#define DEFINE_GLIB_HTABLE_GET(name) \2
struct glib_hentry* glib_htable_##name(struct glib_htable *ht, const void *data) \3
{ \4
struct glib_hentry *he; \5
unsigned int h = ht->hashfun(data,ht->size); \6
\7
read_lock_bh(&ht->lock); \8
he = __glib_htable_##name(ht, h, data); \9
read_unlock_bh(&ht->lock); \10
\11
return he; \12
}13

14
DEFINE_GLIB_HTABLE_GET(get)15
DEFINE_GLIB_HTABLE_GET(rget)條件查找宏生成
1
#define DEFINE_GLIB_HTABLE_COND_GET(name) \
2
struct glib_hentry* glib_htable_c##name(struct glib_htable *ht, const void *data, int(*cmp)(const struct glib_hentry*, void*), void *arg) \
3
{ \
4
struct glib_hentry *he; \
5
unsigned int h = ht->hashfun(data,ht->size); \
6
\
7
read_lock_bh(&ht->lock); \
8
he = __glib_htable_c##name(ht, h, cmp, arg); \
9
read_unlock_bh(&ht->lock); \
10
\
11
return he; \
12
}
13
14
DEFINE_GLIB_HTABLE_COND_GET(get)
15
DEFINE_GLIB_HTABLE_COND_GET(rget)
16
17
#define DEFINE_GLIB_HTABLE_COND_GET_BYIDX(name) \
18
struct glib_hentry* glib_htable_c##name##_byidx(struct glib_htable *ht, unsigned int *bucket, int(*cmp)(const struct glib_hentry*, void*), void *arg) \
19
{ \
20
unsigned int h; \
21
struct glib_hentry *he = NULL; \
22
\
23
read_lock_bh(&ht->lock); \
24
\
25
for (h = *bucket; h < ht->size; h = (*bucket)++){ \
26
he = __glib_htable_c##name(ht, h, cmp, arg); \
27
if(he) \
28
break; \
29
} \
30
\
31
read_unlock_bh(&ht->lock); \
32
\
33
return he; \
34
}
35
36
DEFINE_GLIB_HTABLE_COND_GET_BYIDX(get)
37
DEFINE_GLIB_HTABLE_COND_GET_BYIDX(rget)
#define DEFINE_GLIB_HTABLE_COND_GET(name) \2
struct glib_hentry* glib_htable_c##name(struct glib_htable *ht, const void *data, int(*cmp)(const struct glib_hentry*, void*), void *arg) \3
{ \4
struct glib_hentry *he; \5
unsigned int h = ht->hashfun(data,ht->size); \6
\7
read_lock_bh(&ht->lock); \8
he = __glib_htable_c##name(ht, h, cmp, arg); \9
read_unlock_bh(&ht->lock); \10
\11
return he; \12
}13

14
DEFINE_GLIB_HTABLE_COND_GET(get)15
DEFINE_GLIB_HTABLE_COND_GET(rget)16

17
#define DEFINE_GLIB_HTABLE_COND_GET_BYIDX(name) \18
struct glib_hentry* glib_htable_c##name##_byidx(struct glib_htable *ht, unsigned int *bucket, int(*cmp)(const struct glib_hentry*, void*), void *arg) \19
{ \20
unsigned int h; \21
struct glib_hentry *he = NULL; \22
\23
read_lock_bh(&ht->lock); \24
\25
for (h = *bucket; h < ht->size; h = (*bucket)++){ \26
he = __glib_htable_c##name(ht, h, cmp, arg); \27
if(he) \28
break; \29
} \30
\31
read_unlock_bh(&ht->lock); \32
\33
return he; \34
}35

36
DEFINE_GLIB_HTABLE_COND_GET_BYIDX(get)37
DEFINE_GLIB_HTABLE_COND_GET_BYIDX(rget)完整源碼下載:glib_hash,包括glib_htable.h和glib_htable.c文件。



glib_htable_list_##name(he,

