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;
90 static inline size_t ht_max(const struct htable *ht)
92 return ((size_t)7 << ht->bits) / 8;
95 /* Clean deleted if we're full, and more than 12.5% deleted */
96 static inline size_t ht_max_deleted(const struct htable *ht)
98 return ((size_t)1 << ht->bits) / 8;
101 bool htable_init_sized(struct htable *ht,
102 size_t (*rehash)(const void *, void *),
103 void *priv, size_t expect)
105 htable_init(ht, rehash, priv);
107 /* Don't go insane with sizing. */
108 for (ht->bits = 1; ht_max(ht) < expect; ht->bits++) {
113 ht->table = htable_alloc(ht, sizeof(size_t) << ht->bits);
115 ht->table = &ht->common_bits;
118 (void)htable_debug(ht, HTABLE_LOC);
122 void htable_clear(struct htable *ht)
124 if (ht->table != &ht->common_bits)
125 htable_free(ht, (void *)ht->table);
126 htable_init(ht, ht->rehash, ht->priv);
129 bool htable_copy_(struct htable *dst, const struct htable *src)
131 uintptr_t *htable = htable_alloc(dst, sizeof(size_t) << src->bits);
138 memcpy(dst->table, src->table, sizeof(size_t) << src->bits);
142 static size_t hash_bucket(const struct htable *ht, size_t h)
144 return h & ((1 << ht->bits)-1);
147 static void *htable_val(const struct htable *ht,
148 struct htable_iter *i, size_t hash, uintptr_t perfect)
150 uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
152 while (ht->table[i->off]) {
153 if (ht->table[i->off] != HTABLE_DELETED) {
154 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
155 return get_raw_ptr(ht, ht->table[i->off]);
157 i->off = (i->off + 1) & ((1 << ht->bits)-1);
163 void *htable_firstval_(const struct htable *ht,
164 struct htable_iter *i, size_t hash)
166 i->off = hash_bucket(ht, hash);
167 return htable_val(ht, i, hash, ht_perfect_mask(ht));
170 void *htable_nextval_(const struct htable *ht,
171 struct htable_iter *i, size_t hash)
173 i->off = (i->off + 1) & ((1 << ht->bits)-1);
174 return htable_val(ht, i, hash, 0);
177 void *htable_first_(const struct htable *ht, struct htable_iter *i)
179 for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
180 if (entry_is_valid(ht->table[i->off]))
181 return get_raw_ptr(ht, ht->table[i->off]);
186 void *htable_next_(const struct htable *ht, struct htable_iter *i)
188 for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
189 if (entry_is_valid(ht->table[i->off]))
190 return get_raw_ptr(ht, ht->table[i->off]);
195 void *htable_prev_(const struct htable *ht, struct htable_iter *i)
201 if (entry_is_valid(ht->table[i->off]))
202 return get_raw_ptr(ht, ht->table[i->off]);
206 /* Another bit currently in mask needs to be exposed, so that a bucket with p in
207 * it won't appear invalid */
208 static COLD void unset_another_common_bit(struct htable *ht,
214 for (i = sizeof(uintptr_t) * CHAR_BIT - 1; i > 0; i--) {
215 if (((uintptr_t)p & ((uintptr_t)1 << i))
216 && ht->common_mask & ~*maskdiff & ((uintptr_t)1 << i))
219 /* There must have been one, right? */
222 *maskdiff |= ((uintptr_t)1 << i);
225 /* We want to change the common mask: this fixes up the table */
226 static COLD void fixup_table_common(struct htable *ht, uintptr_t maskdiff)
232 bitsdiff = ht->common_bits & maskdiff;
234 for (i = 0; i < (size_t)1 << ht->bits; i++) {
236 if (!entry_is_valid(e = ht->table[i]))
239 /* Clear the bits no longer in the mask, set them as
243 /* If this made it invalid, restart with more exposed */
244 if (!entry_is_valid(e)) {
245 unset_another_common_bit(ht, &maskdiff, get_raw_ptr(ht, e));
251 /* Take away those bits from our mask, bits and perfect bit. */
252 ht->common_mask &= ~maskdiff;
253 ht->common_bits &= ~maskdiff;
254 if (ht_perfect_mask(ht) & maskdiff)
255 ht->perfect_bitnum = NO_PERFECT_BIT;
258 /* Limited recursion */
259 static void ht_add(struct htable *ht, const void *new, size_t h);
261 /* We tried to add this entry, but it looked invalid! We need to
262 * let another pointer bit through mask */
263 static COLD void update_common_fix_invalid(struct htable *ht, const void *p, size_t h)
267 assert(ht->elems != 0);
270 unset_another_common_bit(ht, &maskdiff, p);
271 fixup_table_common(ht, maskdiff);
273 /* Now won't recurse */
277 /* This does not expand the hash table, that's up to caller. */
278 static void ht_add(struct htable *ht, const void *new, size_t h)
281 uintptr_t perfect = ht_perfect_mask(ht);
283 i = hash_bucket(ht, h);
285 while (entry_is_valid(ht->table[i])) {
287 i = (i + 1) & ((1 << ht->bits)-1);
289 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
290 if (!entry_is_valid(ht->table[i]))
291 update_common_fix_invalid(ht, new, h);
294 static COLD bool double_table(struct htable *ht)
297 size_t oldnum = (size_t)1 << ht->bits;
298 uintptr_t *oldtable, e;
300 oldtable = ht->table;
301 ht->table = htable_alloc(ht, sizeof(size_t) << (ht->bits+1));
303 ht->table = oldtable;
308 /* If we lost our "perfect bit", get it back now. */
309 if (ht->perfect_bitnum == NO_PERFECT_BIT && ht->common_mask) {
310 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
311 if (ht->common_mask & ((size_t)2 << i)) {
312 ht->perfect_bitnum = i;
318 if (oldtable != &ht->common_bits) {
319 for (i = 0; i < oldnum; i++) {
320 if (entry_is_valid(e = oldtable[i])) {
321 void *p = get_raw_ptr(ht, e);
322 ht_add(ht, p, ht->rehash(p, ht->priv));
325 htable_free(ht, oldtable);
329 (void)htable_debug(ht, HTABLE_LOC);
333 static COLD void rehash_table(struct htable *ht)
336 uintptr_t e, perfect = ht_perfect_mask(ht);
338 /* Beware wrap cases: we need to start from first empty bucket. */
339 for (start = 0; ht->table[start]; start++);
341 for (i = 0; i < (size_t)1 << ht->bits; i++) {
342 size_t h = (i + start) & ((1 << ht->bits)-1);
346 if (e == HTABLE_DELETED)
348 else if (!(e & perfect)) {
349 void *p = get_raw_ptr(ht, e);
351 ht_add(ht, p, ht->rehash(p, ht->priv));
355 (void)htable_debug(ht, HTABLE_LOC);
358 /* We stole some bits, now we need to put them back... */
359 static COLD void update_common(struct htable *ht, const void *p)
363 if (ht->elems == 0) {
364 ht->common_mask = -1;
365 ht->common_bits = ((uintptr_t)p & ht->common_mask);
366 ht->perfect_bitnum = 0;
367 (void)htable_debug(ht, HTABLE_LOC);
371 /* Find bits which are unequal to old common set. */
372 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
374 fixup_table_common(ht, maskdiff);
375 (void)htable_debug(ht, HTABLE_LOC);
378 bool htable_add_(struct htable *ht, size_t hash, const void *p)
380 /* Cannot insert NULL, or (void *)1. */
382 assert(entry_is_valid((uintptr_t)p));
384 /* Getting too full? */
385 if (ht->elems+1 + ht->deleted > ht_max(ht)) {
386 /* If we're more than 1/8 deleted, clean those,
387 * otherwise double table size. */
388 if (ht->deleted > ht_max_deleted(ht))
390 else if (!double_table(ht))
393 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
394 update_common(ht, p);
401 bool htable_del_(struct htable *ht, size_t h, const void *p)
403 struct htable_iter i;
406 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
408 htable_delval(ht, &i);
415 void htable_delval_(struct htable *ht, struct htable_iter *i)
417 assert(i->off < (size_t)1 << ht->bits);
418 assert(entry_is_valid(ht->table[i->off]));
421 ht->table[i->off] = HTABLE_DELETED;
425 void *htable_pick_(const struct htable *ht, size_t seed, struct htable_iter *i)
428 struct htable_iter unwanted;
432 i->off = seed % ((size_t)1 << ht->bits);
433 e = htable_next(ht, i);
435 e = htable_first(ht, i);
439 struct htable *htable_check(const struct htable *ht, const char *abortstr)
442 struct htable_iter i;
445 /* Use non-DEBUG versions here, to avoid infinite recursion with
446 * CCAN_HTABLE_DEBUG! */
447 for (p = htable_first_(ht, &i); p; p = htable_next_(ht, &i)) {
448 struct htable_iter i2;
450 size_t h = ht->rehash(p, ht->priv);
455 /* Open-code htable_get to avoid CCAN_HTABLE_DEBUG */
456 for (c = htable_firstval_(ht, &i2, h);
458 c = htable_nextval_(ht, &i2, h)) {
468 "%s: element %p in position %zu"
469 " cannot find itself\n",
476 if (n != ht->elems) {
479 "%s: found %zu elems, expected %zu\n",
480 abortstr, n, ht->elems);
486 return (struct htable *)ht;