1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #include <ccan/htable/htable.h>
3 #include <ccan/compiler/compiler.h>
11 /* We use 0x1 as deleted marker. */
12 #define HTABLE_DELETED (0x1)
14 /* perfect_bitnum 63 means there's no perfect bitnum */
15 #define NO_PERFECT_BIT (sizeof(uintptr_t) * CHAR_BIT - 1)
17 static void *htable_default_alloc(struct htable *ht, size_t len)
19 return calloc(len, 1);
22 static void htable_default_free(struct htable *ht, void *p)
27 static void *(*htable_alloc)(struct htable *, size_t) = htable_default_alloc;
28 static void (*htable_free)(struct htable *, void *) = htable_default_free;
30 void htable_set_allocator(void *(*alloc)(struct htable *, size_t len),
31 void (*free)(struct htable *, void *p))
34 alloc = htable_default_alloc;
36 free = htable_default_free;
41 /* We clear out the bits which are always the same, and put metadata there. */
42 static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
45 return e & ht->common_mask;
48 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
50 return (void *)((e & ~ht->common_mask) | ht->common_bits);
53 static inline uintptr_t make_hval(const struct htable *ht,
54 const void *p, uintptr_t bits)
56 return ((uintptr_t)p & ~ht->common_mask) | bits;
59 static inline bool entry_is_valid(uintptr_t e)
61 return e > HTABLE_DELETED;
64 static inline uintptr_t ht_perfect_mask(const struct htable *ht)
66 return (uintptr_t)2 << ht->perfect_bitnum;
69 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
72 /* Shuffling the extra bits (as specified in mask) down the
73 * end is quite expensive. But the lower bits are redundant, so
74 * we fold the value first. */
75 return (hash ^ (hash >> ht->bits))
76 & ht->common_mask & ~ht_perfect_mask(ht);
79 void htable_init(struct htable *ht,
80 size_t (*rehash)(const void *elem, void *priv), void *priv)
82 struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL);
86 ht->table = &ht->common_bits;
89 static inline size_t ht_max(const struct htable *ht)
91 return ((size_t)3 << ht->bits) / 4;
94 static inline size_t ht_max_with_deleted(const struct htable *ht)
96 return ((size_t)9 << ht->bits) / 10;
99 bool htable_init_sized(struct htable *ht,
100 size_t (*rehash)(const void *, void *),
101 void *priv, size_t expect)
103 htable_init(ht, rehash, priv);
105 /* Don't go insane with sizing. */
106 for (ht->bits = 1; ((size_t)3 << ht->bits) / 4 < expect; ht->bits++) {
111 ht->table = htable_alloc(ht, sizeof(size_t) << ht->bits);
113 ht->table = &ht->common_bits;
116 (void)htable_debug(ht, HTABLE_LOC);
120 void htable_clear(struct htable *ht)
122 if (ht->table != &ht->common_bits)
123 htable_free(ht, (void *)ht->table);
124 htable_init(ht, ht->rehash, ht->priv);
127 bool htable_copy_(struct htable *dst, const struct htable *src)
129 uintptr_t *htable = htable_alloc(dst, sizeof(size_t) << src->bits);
136 memcpy(dst->table, src->table, sizeof(size_t) << src->bits);
140 static size_t hash_bucket(const struct htable *ht, size_t h)
142 return h & ((1 << ht->bits)-1);
145 static void *htable_val(const struct htable *ht,
146 struct htable_iter *i, size_t hash, uintptr_t perfect)
148 uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
150 while (ht->table[i->off]) {
151 if (ht->table[i->off] != HTABLE_DELETED) {
152 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
153 return get_raw_ptr(ht, ht->table[i->off]);
155 i->off = (i->off + 1) & ((1 << ht->bits)-1);
161 void *htable_firstval_(const struct htable *ht,
162 struct htable_iter *i, size_t hash)
164 i->off = hash_bucket(ht, hash);
165 return htable_val(ht, i, hash, ht_perfect_mask(ht));
168 void *htable_nextval_(const struct htable *ht,
169 struct htable_iter *i, size_t hash)
171 i->off = (i->off + 1) & ((1 << ht->bits)-1);
172 return htable_val(ht, i, hash, 0);
175 void *htable_first_(const struct htable *ht, struct htable_iter *i)
177 for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
178 if (entry_is_valid(ht->table[i->off]))
179 return get_raw_ptr(ht, ht->table[i->off]);
184 void *htable_next_(const struct htable *ht, struct htable_iter *i)
186 for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
187 if (entry_is_valid(ht->table[i->off]))
188 return get_raw_ptr(ht, ht->table[i->off]);
193 void *htable_prev_(const struct htable *ht, struct htable_iter *i)
199 if (entry_is_valid(ht->table[i->off]))
200 return get_raw_ptr(ht, ht->table[i->off]);
204 /* Another bit currently in mask needs to be exposed, so that a bucket with p in
205 * it won't appear invalid */
206 static COLD void unset_another_common_bit(struct htable *ht,
212 for (i = sizeof(uintptr_t) * CHAR_BIT - 1; i > 0; i--) {
213 if (((uintptr_t)p & ((uintptr_t)1 << i))
214 && ht->common_mask & ~*maskdiff & ((uintptr_t)1 << i))
217 /* There must have been one, right? */
220 *maskdiff |= ((uintptr_t)1 << i);
223 /* We want to change the common mask: this fixes up the table */
224 static COLD void fixup_table_common(struct htable *ht, uintptr_t maskdiff)
230 bitsdiff = ht->common_bits & maskdiff;
232 for (i = 0; i < (size_t)1 << ht->bits; i++) {
234 if (!entry_is_valid(e = ht->table[i]))
237 /* Clear the bits no longer in the mask, set them as
241 /* If this made it invalid, restart with more exposed */
242 if (!entry_is_valid(e)) {
243 unset_another_common_bit(ht, &maskdiff, get_raw_ptr(ht, e));
249 /* Take away those bits from our mask, bits and perfect bit. */
250 ht->common_mask &= ~maskdiff;
251 ht->common_bits &= ~maskdiff;
252 if (ht_perfect_mask(ht) & maskdiff)
253 ht->perfect_bitnum = NO_PERFECT_BIT;
256 /* Limited recursion */
257 static void ht_add(struct htable *ht, const void *new, size_t h);
259 /* We tried to add this entry, but it looked invalid! We need to
260 * let another pointer bit through mask */
261 static COLD void update_common_fix_invalid(struct htable *ht, const void *p, size_t h)
265 assert(ht->elems != 0);
268 unset_another_common_bit(ht, &maskdiff, p);
269 fixup_table_common(ht, maskdiff);
271 /* Now won't recurse */
275 /* This does not expand the hash table, that's up to caller. */
276 static void ht_add(struct htable *ht, const void *new, size_t h)
279 uintptr_t perfect = ht_perfect_mask(ht);
281 i = hash_bucket(ht, h);
283 while (entry_is_valid(ht->table[i])) {
285 i = (i + 1) & ((1 << ht->bits)-1);
287 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
288 if (!entry_is_valid(ht->table[i]))
289 update_common_fix_invalid(ht, new, h);
292 static COLD bool double_table(struct htable *ht)
295 size_t oldnum = (size_t)1 << ht->bits;
296 uintptr_t *oldtable, e;
298 oldtable = ht->table;
299 ht->table = htable_alloc(ht, sizeof(size_t) << (ht->bits+1));
301 ht->table = oldtable;
306 /* If we lost our "perfect bit", get it back now. */
307 if (ht->perfect_bitnum == NO_PERFECT_BIT && ht->common_mask) {
308 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
309 if (ht->common_mask & ((size_t)2 << i)) {
310 ht->perfect_bitnum = i;
316 if (oldtable != &ht->common_bits) {
317 for (i = 0; i < oldnum; i++) {
318 if (entry_is_valid(e = oldtable[i])) {
319 void *p = get_raw_ptr(ht, e);
320 ht_add(ht, p, ht->rehash(p, ht->priv));
323 htable_free(ht, oldtable);
327 (void)htable_debug(ht, HTABLE_LOC);
331 static COLD void rehash_table(struct htable *ht)
334 uintptr_t e, perfect = ht_perfect_mask(ht);
336 /* Beware wrap cases: we need to start from first empty bucket. */
337 for (start = 0; ht->table[start]; start++);
339 for (i = 0; i < (size_t)1 << ht->bits; i++) {
340 size_t h = (i + start) & ((1 << ht->bits)-1);
344 if (e == HTABLE_DELETED)
346 else if (!(e & perfect)) {
347 void *p = get_raw_ptr(ht, e);
349 ht_add(ht, p, ht->rehash(p, ht->priv));
353 (void)htable_debug(ht, HTABLE_LOC);
356 /* We stole some bits, now we need to put them back... */
357 static COLD void update_common(struct htable *ht, const void *p)
361 if (ht->elems == 0) {
362 ht->common_mask = -1;
363 ht->common_bits = ((uintptr_t)p & ht->common_mask);
364 ht->perfect_bitnum = 0;
365 (void)htable_debug(ht, HTABLE_LOC);
369 /* Find bits which are unequal to old common set. */
370 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
372 fixup_table_common(ht, maskdiff);
373 (void)htable_debug(ht, HTABLE_LOC);
376 bool htable_add_(struct htable *ht, size_t hash, const void *p)
378 /* Cannot insert NULL, or (void *)1. */
380 assert(entry_is_valid((uintptr_t)p));
382 if (ht->elems+1 > ht_max(ht) && !double_table(ht))
384 if (ht->elems+1 + ht->deleted > ht_max_with_deleted(ht))
386 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
387 update_common(ht, p);
394 bool htable_del_(struct htable *ht, size_t h, const void *p)
396 struct htable_iter i;
399 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
401 htable_delval(ht, &i);
408 void htable_delval_(struct htable *ht, struct htable_iter *i)
410 assert(i->off < (size_t)1 << ht->bits);
411 assert(entry_is_valid(ht->table[i->off]));
414 ht->table[i->off] = HTABLE_DELETED;
418 void *htable_pick_(const struct htable *ht, size_t seed, struct htable_iter *i)
421 struct htable_iter unwanted;
425 i->off = seed % ((size_t)1 << ht->bits);
426 e = htable_next(ht, i);
428 e = htable_first(ht, i);
432 struct htable *htable_check(const struct htable *ht, const char *abortstr)
435 struct htable_iter i;
438 /* Use non-DEBUG versions here, to avoid infinite recursion with
439 * CCAN_HTABLE_DEBUG! */
440 for (p = htable_first_(ht, &i); p; p = htable_next_(ht, &i)) {
441 struct htable_iter i2;
443 size_t h = ht->rehash(p, ht->priv);
448 /* Open-code htable_get to avoid CCAN_HTABLE_DEBUG */
449 for (c = htable_firstval_(ht, &i2, h);
451 c = htable_nextval_(ht, &i2, h)) {
461 "%s: element %p in position %zu"
462 " cannot find itself\n",
469 if (n != ht->elems) {
472 "%s: found %zu elems, expected %zu\n",
473 abortstr, n, ht->elems);
479 return (struct htable *)ht;