-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickhash.h
354 lines (308 loc) · 9.58 KB
/
quickhash.h
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* 2001 Clemson University and The University of Chicago
*
* See COPYING in top-level directory.
*/
#ifndef QUICKHASH_H
#define QUICKHASH_H
#ifdef __KERNEL__
#define qhash_malloc(x) kmalloc(x, GFP_KERNEL)
#define qhash_free(x) kfree(x)
#define qhash_head list_head
#define INIT_QHASH_HEAD INIT_LIST_HEAD
#define qhash_entry list_entry
#define qhash_add_tail list_add_tail
#define qhash_del list_del
#define qhash_for_each list_for_each
#define qhash_for_each_safe list_for_each_safe
#define qhash_lock_init(lock_ptr) spin_lock_init(lock_ptr)
#define qhash_lock(lock_ptr) spin_lock(lock_ptr)
#define qhash_unlock(lock_ptr) spin_unlock(lock_ptr)
#else
#include <stdlib.h>
#include <stdint.h>
#include "quicklist.h"
#include "pvfs2-internal.h"
#define qhash_malloc(x) malloc(x)
#define qhash_free(x) free(x)
#define qhash_head qlist_head
#define INIT_QHASH_HEAD INIT_QLIST_HEAD
#define qhash_entry qlist_entry
#define qhash_add_tail qlist_add_tail
#define qhash_del qlist_del
#define qhash_for_each qlist_for_each
#define qhash_for_each_safe qlist_for_each_safe
#define qhash_lock_init(lock_ptr) do {} while(0)
#define qhash_lock(lock_ptr) do {} while(0)
#define qhash_unlock(lock_ptr) do {} while(0)
#endif /* __KERNEL__ */
struct qhash_table
{
struct qhash_head *array;
int table_size;
int (*compare) (const void *key, struct qhash_head * link);
int (*hash) (const void *key, int table_size);
#ifdef __KERNEL__
spinlock_t lock;
#endif
};
/* qhash_init()
*
* creates a new hash table with the specified table size. The
* hash function and compare function must be provided.
* table_size should be a good prime number.
*
* returns pointer to table on success, NULL on failure
*/
static inline struct qhash_table *qhash_init(
int (*compare) (const void *key,
struct qhash_head * link),
int (*hash) (const void *key,
int table_size),
int table_size)
{
int i = 0;
struct qhash_table *new_table = NULL;
/* create struct to contain table information */
new_table = (struct qhash_table *)
qhash_malloc(sizeof(struct qhash_table));
if (!new_table)
{
return (NULL);
}
/* fill in info */
new_table->compare = compare;
new_table->hash = hash;
new_table->table_size = table_size;
/* create array for actual table */
new_table->array = (struct qhash_head *)
qhash_malloc(sizeof(struct qhash_head) * table_size);
if (!new_table->array)
{
qhash_free(new_table);
return (NULL);
}
/* initialize a doubly linked at each hash table index */
for (i = 0; i < table_size; i++)
{
INIT_QHASH_HEAD(&new_table->array[i]);
}
qhash_lock_init(&new_table->lock);
return (new_table);
}
/* qhash_finalize()
*
* frees any resources created by the hash table
*
* no return value
*/
static inline void qhash_finalize(
struct qhash_table *old_table)
{
qhash_free(old_table->array);
qhash_free(old_table);
return;
}
/* qhash_add()
*
* adds a new link onto the hash table, hashes based on given key
*
* no return value
*/
static inline void qhash_add(
struct qhash_table *table,
const void *key,
struct qhash_head *linkp)
{
int indx = 0;
/* hash on the key */
indx = table->hash(key, table->table_size);
/* add to the tail of the linked list at that offset */
qhash_lock(&table->lock);
qhash_add_tail(linkp, &(table->array[indx]));
qhash_unlock(&table->lock);
return;
}
/* qhash_search()
*
* searches for a link in the hash table
* that matches the given key
*
* returns pointer to link on success, NULL on failure (or item
* not found)
*/
static inline struct qhash_head *qhash_search(
struct qhash_table *table,
const void *key)
{
int indx = 0;
struct qhash_head *tmp_link = NULL;
/* find the hash value */
indx = table->hash(key, table->table_size);
/* linear search at index to find match */
qhash_lock(&table->lock);
qhash_for_each(tmp_link, &(table->array[indx]))
{
if (table->compare(key, tmp_link))
{
qhash_unlock(&table->lock);
return (tmp_link);
}
}
qhash_unlock(&table->lock);
return (NULL);
}
/* qhash_search_at_index()
*
* searches for a link in the list matching
* the specified index
*
* returns pointer to link on success, NULL on failure (or item
* not found)
*/
static inline struct qhash_head *qhash_search_at_index(
struct qhash_table *table,
int indx)
{
struct qhash_head *tmp_link = NULL;
if(indx >= table->table_size)
{
return NULL;
}
qhash_lock(&table->lock);
qhash_for_each(tmp_link, &(table->array[indx]))
{
qhash_unlock(&table->lock);
return (tmp_link);
}
qhash_unlock(&table->lock);
return (NULL);
}
/* qhash_search_and_remove()
*
* searches for and removes a link in the hash table
* that matches the given key
*
* returns pointer to link on success, NULL on failure (or item
* not found). On success, link is removed from hashtable.
*/
static inline struct qhash_head *qhash_search_and_remove(
struct qhash_table *table,
const void *key)
{
int indx = 0;
struct qhash_head *tmp_link = NULL, *tmp_link_safe = NULL;
/* find the hash value */
indx = table->hash(key, table->table_size);
/* linear search at index to find match */
qhash_lock(&table->lock);
qhash_for_each_safe(tmp_link, tmp_link_safe, &(table->array[indx]))
{
if (table->compare(key, tmp_link))
{
qhash_del(tmp_link);
qhash_unlock(&table->lock);
return (tmp_link);
}
}
qhash_unlock(&table->lock);
return (NULL);
}
/* qhash_search_and_remove_at_index()
*
* searches for and removes the first link in the list
* matching the specified index
*
* returns pointer to link on success, NULL on failure (or item
* not found). On success, link is removed from hashtable.
*/
static inline struct qhash_head *qhash_search_and_remove_at_index(
struct qhash_table *table,
int indx)
{
struct qhash_head *tmp_link = NULL, *tmp_link_safe = NULL;
if(indx >= table->table_size)
{
return NULL;
}
qhash_lock(&table->lock);
qhash_for_each_safe(tmp_link, tmp_link_safe, &(table->array[indx]))
{
qhash_del(tmp_link);
qhash_unlock(&table->lock);
return (tmp_link);
}
qhash_unlock(&table->lock);
return (NULL);
}
#define qhash_destroy_and_finalize(_oldtable, _entry_type, _link, _destructor) \
do \
{ \
int i = 0; \
struct qhash_head *entry; \
struct qhash_head *tmpe; \
qhash_lock(&_oldtable->lock); \
for(i = 0; i < _oldtable->table_size; ++i) \
{ \
qhash_for_each_safe(entry, tmpe, &(_oldtable->array[i])) \
{ \
qhash_del(entry); \
_destructor(qhash_entry(entry, _entry_type, _link)); \
} \
} \
qhash_unlock(&_oldtable->lock); \
\
qhash_finalize(_oldtable); \
\
} while(0)
/* http://www.cris.com/~Ttwang/tech/inthash.htm */
static inline int quickhash_32bit_hash(const void *k, int table_size)
{
int32_t key = *(const int32_t *)k;
key = ~key + (key << 15); /* key = (key << 15) - key - 1; */
key = key ^ (key >> 12);
key = key + (key << 2);
key = key ^ (key >> 4);
key = key * 2057; /* key = (key + (key << 3)) + (key << 11); */
key = key ^ (key >> 16);
return (int) (key & (table_size - 1));
}
static inline int quickhash_64bit_hash(const void *k, int table_size)
{
uint64_t key = *(const uint64_t *)k;
key = (~key) + (key << 18); /* key = (key << 18) - key - 1; */
key = key ^ (key >> 31);
key = key * 21; /* key = (key + (key << 2)) + (key << 4); */
key = key ^ (key >> 11);
key = key + (key << 6);
key = key ^ (key >> 22);
return (int) (key & ((uint64_t)(table_size - 1)));
}
/**
* derived from an algorithm found in Aho, Sethi and Ullman's
* {Compilers: Principles, Techniques and Tools}, published by Addison-Wesley.
* This algorithm comes from P.J. Weinberger's C compiler.
*/
static inline int quickhash_string_hash(const void *k, int table_size)
{
const char *str = (const char *)k;
uint32_t g, h = 0;
while(*str)
{
h = (h << 4) + *str++;
if((g = (h & 0xF0UL)))
{
h ^= g >> 24;
h ^= g;
}
}
return (int)(h & ((uint64_t)(table_size - 1)));
}
#endif /* QUICKHASH_H */
/*
* Local variables:
* c-indent-level: 4
* c-basic-offset: 4
* End:
*
* vim: ts=8 sts=4 sw=4 expandtab
*/