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 /* This does not expand the hash table, that's up to caller. */
205 static void ht_add(struct htable *ht, const void *new, size_t h)
208 uintptr_t perfect = ht_perfect_mask(ht);
210 i = hash_bucket(ht, h);
212 while (entry_is_valid(ht->table[i])) {
214 i = (i + 1) & ((1 << ht->bits)-1);
216 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
219 static COLD bool double_table(struct htable *ht)
222 size_t oldnum = (size_t)1 << ht->bits;
223 uintptr_t *oldtable, e;
225 oldtable = ht->table;
226 ht->table = htable_alloc(ht, sizeof(size_t) << (ht->bits+1));
228 ht->table = oldtable;
233 /* If we lost our "perfect bit", get it back now. */
234 if (ht->perfect_bitnum == NO_PERFECT_BIT && ht->common_mask) {
235 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
236 if (ht->common_mask & ((size_t)2 << i)) {
237 ht->perfect_bitnum = i;
243 if (oldtable != &ht->common_bits) {
244 for (i = 0; i < oldnum; i++) {
245 if (entry_is_valid(e = oldtable[i])) {
246 void *p = get_raw_ptr(ht, e);
247 ht_add(ht, p, ht->rehash(p, ht->priv));
250 htable_free(ht, oldtable);
254 (void)htable_debug(ht, HTABLE_LOC);
258 static COLD void rehash_table(struct htable *ht)
261 uintptr_t e, perfect = ht_perfect_mask(ht);
263 /* Beware wrap cases: we need to start from first empty bucket. */
264 for (start = 0; ht->table[start]; start++);
266 for (i = 0; i < (size_t)1 << ht->bits; i++) {
267 size_t h = (i + start) & ((1 << ht->bits)-1);
271 if (e == HTABLE_DELETED)
273 else if (!(e & perfect)) {
274 void *p = get_raw_ptr(ht, e);
276 ht_add(ht, p, ht->rehash(p, ht->priv));
280 (void)htable_debug(ht, HTABLE_LOC);
283 /* We stole some bits, now we need to put them back... */
284 static COLD void update_common(struct htable *ht, const void *p)
287 uintptr_t maskdiff, bitsdiff;
289 if (ht->elems == 0) {
290 /* Always reveal one bit of the pointer in the bucket,
291 * so it's not zero or HTABLE_DELETED (1), even if
292 * hash happens to be 0. Assumes (void *)1 is not a
294 for (i = sizeof(uintptr_t)*CHAR_BIT - 1; i > 0; i--) {
295 if ((uintptr_t)p & ((uintptr_t)1 << i))
299 ht->common_mask = ~((uintptr_t)1 << i);
300 ht->common_bits = ((uintptr_t)p & ht->common_mask);
301 ht->perfect_bitnum = 0;
302 (void)htable_debug(ht, HTABLE_LOC);
306 /* Find bits which are unequal to old common set. */
307 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
309 /* These are the bits which go there in existing entries. */
310 bitsdiff = ht->common_bits & maskdiff;
312 for (i = 0; i < (size_t)1 << ht->bits; i++) {
313 if (!entry_is_valid(ht->table[i]))
315 /* Clear the bits no longer in the mask, set them as
317 ht->table[i] &= ~maskdiff;
318 ht->table[i] |= bitsdiff;
321 /* Take away those bits from our mask, bits and perfect bit. */
322 ht->common_mask &= ~maskdiff;
323 ht->common_bits &= ~maskdiff;
324 if (ht_perfect_mask(ht) & maskdiff)
325 ht->perfect_bitnum = NO_PERFECT_BIT;
326 (void)htable_debug(ht, HTABLE_LOC);
329 bool htable_add_(struct htable *ht, size_t hash, const void *p)
331 if (ht->elems+1 > ht_max(ht) && !double_table(ht))
333 if (ht->elems+1 + ht->deleted > ht_max_with_deleted(ht))
336 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
337 update_common(ht, p);
344 bool htable_del_(struct htable *ht, size_t h, const void *p)
346 struct htable_iter i;
349 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
351 htable_delval(ht, &i);
358 void htable_delval_(struct htable *ht, struct htable_iter *i)
360 assert(i->off < (size_t)1 << ht->bits);
361 assert(entry_is_valid(ht->table[i->off]));
364 ht->table[i->off] = HTABLE_DELETED;
368 struct htable *htable_check(const struct htable *ht, const char *abortstr)
371 struct htable_iter i;
374 /* Use non-DEBUG versions here, to avoid infinite recursion with
375 * CCAN_HTABLE_DEBUG! */
376 for (p = htable_first_(ht, &i); p; p = htable_next_(ht, &i)) {
377 struct htable_iter i2;
379 size_t h = ht->rehash(p, ht->priv);
384 /* Open-code htable_get to avoid CCAN_HTABLE_DEBUG */
385 for (c = htable_firstval_(ht, &i2, h);
387 c = htable_nextval_(ht, &i2, h)) {
397 "%s: element %p in position %zu"
398 " cannot find itself\n",
405 if (n != ht->elems) {
408 "%s: found %zu elems, expected %zu\n",
409 abortstr, n, ht->elems);
415 return (struct htable *)ht;