1 /*
2 * SHL - Dynamic hash-table
3 *
4 * Written-by: Rusty Russell <rusty@rustcorp.com.au>
5 * Adjusted-by: David Herrmann <dh.herrmann@gmail.com>
6 * Licensed under LGPLv2+ - see LICENSE_htable file for details
7 */
8
9 /*
10 * Please see ccan/htable/_info at:
11 * https://github.com/rustyrussell/ccan/tree/master/ccan/htable
12 * for information on the hashtable algorithm. This file copies the code inline
13 * and is released under the same conditions.
14 *
15 * At the end of the file you can find some helpers to use this htable to store
16 * objects with "unsigned long" or "char*" keys.
17 */
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "shl_htable.h"
27
28 #define COLD __attribute__((cold))
29
30 struct htable {
31 /* KEEP IN SYNC WITH "struct shl_htable_int" */
32 size_t (*rehash)(const void *elem, void *priv);
33 void *priv;
34 unsigned int bits;
35 size_t elems, deleted, max, max_with_deleted;
36 /* These are the bits which are the same in all pointers. */
37 uintptr_t common_mask, common_bits;
38 uintptr_t perfect_bit;
39 uintptr_t *table;
40 };
41
42 #define HTABLE_INITIALIZER(name, rehash, priv) \
43 { rehash, priv, 0, 0, 0, 0, 0, -1, 0, 0, &name.perfect_bit }
44
45 struct htable_iter {
46 size_t off;
47 };
48
49 /*
50 * INLINE COPY OF ccan/htable.c
51 */
52
53 /* We use 0x1 as deleted marker. */
54 #define HTABLE_DELETED (0x1)
55
56 /* We clear out the bits which are always the same, and put metadata there. */
get_extra_ptr_bits(const struct htable * ht,uintptr_t e)57 static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
58 uintptr_t e)
59 {
60 return e & ht->common_mask;
61 }
62
get_raw_ptr(const struct htable * ht,uintptr_t e)63 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
64 {
65 return (void *)((e & ~ht->common_mask) | ht->common_bits);
66 }
67
make_hval(const struct htable * ht,const void * p,uintptr_t bits)68 static inline uintptr_t make_hval(const struct htable *ht,
69 const void *p, uintptr_t bits)
70 {
71 return ((uintptr_t)p & ~ht->common_mask) | bits;
72 }
73
entry_is_valid(uintptr_t e)74 static inline bool entry_is_valid(uintptr_t e)
75 {
76 return e > HTABLE_DELETED;
77 }
78
get_hash_ptr_bits(const struct htable * ht,size_t hash)79 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
80 size_t hash)
81 {
82 /* Shuffling the extra bits (as specified in mask) down the
83 * end is quite expensive. But the lower bits are redundant, so
84 * we fold the value first. */
85 return (hash ^ (hash >> ht->bits))
86 & ht->common_mask & ~ht->perfect_bit;
87 }
88
htable_init(struct htable * ht,size_t (* rehash)(const void * elem,void * priv),void * priv)89 static void htable_init(struct htable *ht,
90 size_t (*rehash)(const void *elem, void *priv),
91 void *priv)
92 {
93 struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL);
94 *ht = empty;
95 ht->rehash = rehash;
96 ht->priv = priv;
97 ht->table = &ht->perfect_bit;
98 }
99
htable_clear(struct htable * ht,void (* free_cb)(void * entry,void * ctx),void * ctx)100 static void htable_clear(struct htable *ht,
101 void (*free_cb) (void *entry, void *ctx),
102 void *ctx)
103 {
104 size_t i;
105
106 if (ht->table != &ht->perfect_bit) {
107 if (free_cb) {
108 for (i = 0; i < (size_t)1 << ht->bits; ++i) {
109 if (entry_is_valid(ht->table[i]))
110 free_cb(get_raw_ptr(ht, ht->table[i]),
111 ctx);
112 }
113 }
114
115 free((void *)ht->table);
116 }
117
118 htable_init(ht, ht->rehash, ht->priv);
119 }
120
htable_visit(struct htable * ht,void (* visit_cb)(void * elem,void * ctx),void * ctx)121 static void htable_visit(struct htable *ht,
122 void (*visit_cb) (void *elem, void *ctx),
123 void *ctx)
124 {
125 size_t i;
126
127 if (visit_cb && ht->table != &ht->perfect_bit) {
128 for (i = 0; i < (size_t)1 << ht->bits; ++i) {
129 if (entry_is_valid(ht->table[i]))
130 visit_cb(get_raw_ptr(ht, ht->table[i]), ctx);
131 }
132 }
133 }
134
hash_bucket(const struct htable * ht,size_t h)135 static size_t hash_bucket(const struct htable *ht, size_t h)
136 {
137 return h & ((1 << ht->bits)-1);
138 }
139
htable_val(const struct htable * ht,struct htable_iter * i,size_t hash,uintptr_t perfect)140 static void *htable_val(const struct htable *ht,
141 struct htable_iter *i, size_t hash, uintptr_t perfect)
142 {
143 uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
144
145 while (ht->table[i->off]) {
146 if (ht->table[i->off] != HTABLE_DELETED) {
147 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
148 return get_raw_ptr(ht, ht->table[i->off]);
149 }
150 i->off = (i->off + 1) & ((1 << ht->bits)-1);
151 h2 &= ~perfect;
152 }
153 return NULL;
154 }
155
htable_firstval(const struct htable * ht,struct htable_iter * i,size_t hash)156 static void *htable_firstval(const struct htable *ht,
157 struct htable_iter *i, size_t hash)
158 {
159 i->off = hash_bucket(ht, hash);
160 return htable_val(ht, i, hash, ht->perfect_bit);
161 }
162
htable_nextval(const struct htable * ht,struct htable_iter * i,size_t hash)163 static void *htable_nextval(const struct htable *ht,
164 struct htable_iter *i, size_t hash)
165 {
166 i->off = (i->off + 1) & ((1 << ht->bits)-1);
167 return htable_val(ht, i, hash, 0);
168 }
169
170 /* This does not expand the hash table, that's up to caller. */
ht_add(struct htable * ht,const void * new,size_t h)171 static void ht_add(struct htable *ht, const void *new, size_t h)
172 {
173 size_t i;
174 uintptr_t perfect = ht->perfect_bit;
175
176 i = hash_bucket(ht, h);
177
178 while (entry_is_valid(ht->table[i])) {
179 perfect = 0;
180 i = (i + 1) & ((1 << ht->bits)-1);
181 }
182 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
183 }
184
double_table(struct htable * ht)185 static COLD bool double_table(struct htable *ht)
186 {
187 unsigned int i;
188 size_t oldnum = (size_t)1 << ht->bits;
189 uintptr_t *oldtable, e;
190
191 oldtable = ht->table;
192 ht->table = calloc(1 << (ht->bits+1), sizeof(size_t));
193 if (!ht->table) {
194 ht->table = oldtable;
195 return false;
196 }
197 ht->bits++;
198 ht->max = ((size_t)3 << ht->bits) / 4;
199 ht->max_with_deleted = ((size_t)9 << ht->bits) / 10;
200
201 /* If we lost our "perfect bit", get it back now. */
202 if (!ht->perfect_bit && ht->common_mask) {
203 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
204 if (ht->common_mask & ((size_t)1 << i)) {
205 ht->perfect_bit = (size_t)1 << i;
206 break;
207 }
208 }
209 }
210
211 if (oldtable != &ht->perfect_bit) {
212 for (i = 0; i < oldnum; i++) {
213 if (entry_is_valid(e = oldtable[i])) {
214 void *p = get_raw_ptr(ht, e);
215 ht_add(ht, p, ht->rehash(p, ht->priv));
216 }
217 }
218 free(oldtable);
219 }
220 ht->deleted = 0;
221 return true;
222 }
223
rehash_table(struct htable * ht)224 static COLD void rehash_table(struct htable *ht)
225 {
226 size_t start, i;
227 uintptr_t e;
228
229 /* Beware wrap cases: we need to start from first empty bucket. */
230 for (start = 0; ht->table[start]; start++);
231
232 for (i = 0; i < (size_t)1 << ht->bits; i++) {
233 size_t h = (i + start) & ((1 << ht->bits)-1);
234 e = ht->table[h];
235 if (!e)
236 continue;
237 if (e == HTABLE_DELETED)
238 ht->table[h] = 0;
239 else if (!(e & ht->perfect_bit)) {
240 void *p = get_raw_ptr(ht, e);
241 ht->table[h] = 0;
242 ht_add(ht, p, ht->rehash(p, ht->priv));
243 }
244 }
245 ht->deleted = 0;
246 }
247
248 /* We stole some bits, now we need to put them back... */
update_common(struct htable * ht,const void * p)249 static COLD void update_common(struct htable *ht, const void *p)
250 {
251 unsigned int i;
252 uintptr_t maskdiff, bitsdiff;
253
254 if (ht->elems == 0) {
255 /* Always reveal one bit of the pointer in the bucket,
256 * so it's not zero or HTABLE_DELETED (1), even if
257 * hash happens to be 0. Assumes (void *)1 is not a
258 * valid pointer. */
259 for (i = sizeof(uintptr_t)*CHAR_BIT - 1; i > 0; i--) {
260 if ((uintptr_t)p & ((uintptr_t)1 << i))
261 break;
262 }
263
264 ht->common_mask = ~((uintptr_t)1 << i);
265 ht->common_bits = ((uintptr_t)p & ht->common_mask);
266 ht->perfect_bit = 1;
267 return;
268 }
269
270 /* Find bits which are unequal to old common set. */
271 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
272
273 /* These are the bits which go there in existing entries. */
274 bitsdiff = ht->common_bits & maskdiff;
275
276 for (i = 0; i < (size_t)1 << ht->bits; i++) {
277 if (!entry_is_valid(ht->table[i]))
278 continue;
279 /* Clear the bits no longer in the mask, set them as
280 * expected. */
281 ht->table[i] &= ~maskdiff;
282 ht->table[i] |= bitsdiff;
283 }
284
285 /* Take away those bits from our mask, bits and perfect bit. */
286 ht->common_mask &= ~maskdiff;
287 ht->common_bits &= ~maskdiff;
288 ht->perfect_bit &= ~maskdiff;
289 }
290
htable_add(struct htable * ht,size_t hash,const void * p)291 static bool htable_add(struct htable *ht, size_t hash, const void *p)
292 {
293 if (ht->elems+1 > ht->max && !double_table(ht))
294 return false;
295 if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
296 rehash_table(ht);
297 assert(p);
298 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
299 update_common(ht, p);
300
301 ht_add(ht, p, hash);
302 ht->elems++;
303 return true;
304 }
305
htable_delval(struct htable * ht,struct htable_iter * i)306 static void htable_delval(struct htable *ht, struct htable_iter *i)
307 {
308 assert(i->off < (size_t)1 << ht->bits);
309 assert(entry_is_valid(ht->table[i->off]));
310
311 ht->elems--;
312 ht->table[i->off] = HTABLE_DELETED;
313 ht->deleted++;
314 }
315
316 /*
317 * Wrapper code to make it easier to use this hash-table as map.
318 */
319
shl_htable_init(struct shl_htable * htable,bool (* compare)(const void * a,const void * b),size_t (* rehash)(const void * elem,void * priv),void * priv)320 void shl_htable_init(struct shl_htable *htable,
321 bool (*compare) (const void *a, const void *b),
322 size_t (*rehash)(const void *elem, void *priv),
323 void *priv)
324 {
325 struct htable *ht = (void*)&htable->htable;
326
327 htable->compare = compare;
328 htable_init(ht, rehash, priv);
329 }
330
shl_htable_clear(struct shl_htable * htable,void (* free_cb)(void * elem,void * ctx),void * ctx)331 void shl_htable_clear(struct shl_htable *htable,
332 void (*free_cb) (void *elem, void *ctx),
333 void *ctx)
334 {
335 struct htable *ht = (void*)&htable->htable;
336
337 htable_clear(ht, free_cb, ctx);
338 }
339
shl_htable_visit(struct shl_htable * htable,void (* visit_cb)(void * elem,void * ctx),void * ctx)340 void shl_htable_visit(struct shl_htable *htable,
341 void (*visit_cb) (void *elem, void *ctx),
342 void *ctx)
343 {
344 struct htable *ht = (void*)&htable->htable;
345
346 htable_visit(ht, visit_cb, ctx);
347 }
348
shl_htable_lookup(struct shl_htable * htable,const void * obj,size_t hash,void ** out)349 bool shl_htable_lookup(struct shl_htable *htable, const void *obj, size_t hash,
350 void **out)
351 {
352 struct htable *ht = (void*)&htable->htable;
353 struct htable_iter i;
354 void *c;
355
356 for (c = htable_firstval(ht, &i, hash);
357 c;
358 c = htable_nextval(ht, &i, hash)) {
359 if (htable->compare(obj, c)) {
360 if (out)
361 *out = c;
362 return true;
363 }
364 }
365
366 return false;
367 }
368
shl_htable_insert(struct shl_htable * htable,const void * obj,size_t hash)369 int shl_htable_insert(struct shl_htable *htable, const void *obj, size_t hash)
370 {
371 struct htable *ht = (void*)&htable->htable;
372 bool b;
373
374 b = htable_add(ht, hash, (void*)obj);
375 return b ? 0 : -ENOMEM;
376 }
377
shl_htable_remove(struct shl_htable * htable,const void * obj,size_t hash,void ** out)378 bool shl_htable_remove(struct shl_htable *htable, const void *obj, size_t hash,
379 void **out)
380 {
381 struct htable *ht = (void*)&htable->htable;
382 struct htable_iter i;
383 void *c;
384
385 for (c = htable_firstval(ht, &i, hash);
386 c;
387 c = htable_nextval(ht, &i, hash)) {
388 if (htable->compare(obj, c)) {
389 if (out)
390 *out = c;
391 htable_delval(ht, &i);
392 return true;
393 }
394 }
395
396 return false;
397 }
398
399 /*
400 * Helpers
401 */
402
shl_htable_compare_ulong(const void * a,const void * b)403 bool shl_htable_compare_ulong(const void *a, const void *b)
404 {
405 return *(const unsigned long*)a == *(const unsigned long*)b;
406 }
407
shl_htable_rehash_ulong(const void * elem,void * priv)408 size_t shl_htable_rehash_ulong(const void *elem, void *priv)
409 {
410 return (size_t)*(const unsigned long*)elem;
411 }
412
shl_htable_compare_str(const void * a,const void * b)413 bool shl_htable_compare_str(const void *a, const void *b)
414 {
415 return !strcmp(*(char**)a, *(char**)b);
416 }
417
418 /* DJB's hash function */
shl_htable_rehash_str(const void * elem,void * priv)419 size_t shl_htable_rehash_str(const void *elem, void *priv)
420 {
421 const char *str = *(char**)elem;
422 size_t hash = 5381;
423
424 for ( ; *str; ++str)
425 hash = (hash << 5) + hash + (size_t)*str;
426
427 return hash;
428 }
429