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 /* We clear out the bits which are always the same, and put metadata there. */
15 static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
18 return e & ht->common_mask;
21 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
23 return (void *)((e & ~ht->common_mask) | ht->common_bits);
26 static inline uintptr_t make_hval(const struct htable *ht,
27 const void *p, uintptr_t bits)
29 return ((uintptr_t)p & ~ht->common_mask) | bits;
32 static inline bool entry_is_valid(uintptr_t e)
34 return e > HTABLE_DELETED;
37 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
40 /* Shuffling the extra bits (as specified in mask) down the
41 * end is quite expensive. But the lower bits are redundant, so
42 * we fold the value first. */
43 return (hash ^ (hash >> ht->bits))
44 & ht->common_mask & ~ht->perfect_bit;
47 void htable_init(struct htable *ht,
48 size_t (*rehash)(const void *elem, void *priv), void *priv)
50 struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL);
54 ht->table = &ht->perfect_bit;
57 /* We've changed ht->bits, update ht->max and ht->max_with_deleted */
58 static void htable_adjust_capacity(struct htable *ht)
60 ht->max = ((size_t)3 << ht->bits) / 4;
61 ht->max_with_deleted = ((size_t)9 << ht->bits) / 10;
64 bool htable_init_sized(struct htable *ht,
65 size_t (*rehash)(const void *, void *),
66 void *priv, size_t expect)
68 htable_init(ht, rehash, priv);
70 /* Don't go insane with sizing. */
71 for (ht->bits = 1; ((size_t)3 << ht->bits) / 4 < expect; ht->bits++) {
76 ht->table = calloc(1 << ht->bits, sizeof(size_t));
78 ht->table = &ht->perfect_bit;
81 htable_adjust_capacity(ht);
82 (void)htable_debug(ht, HTABLE_LOC);
86 void htable_clear(struct htable *ht)
88 if (ht->table != &ht->perfect_bit)
89 free((void *)ht->table);
90 htable_init(ht, ht->rehash, ht->priv);
93 bool htable_copy_(struct htable *dst, const struct htable *src)
95 uintptr_t *htable = malloc(sizeof(size_t) << src->bits);
102 memcpy(dst->table, src->table, sizeof(size_t) << src->bits);
106 static size_t hash_bucket(const struct htable *ht, size_t h)
108 return h & ((1 << ht->bits)-1);
111 static void *htable_val(const struct htable *ht,
112 struct htable_iter *i, size_t hash, uintptr_t perfect)
114 uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
116 while (ht->table[i->off]) {
117 if (ht->table[i->off] != HTABLE_DELETED) {
118 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
119 return get_raw_ptr(ht, ht->table[i->off]);
121 i->off = (i->off + 1) & ((1 << ht->bits)-1);
127 void *htable_firstval_(const struct htable *ht,
128 struct htable_iter *i, size_t hash)
130 i->off = hash_bucket(ht, hash);
131 return htable_val(ht, i, hash, ht->perfect_bit);
134 void *htable_nextval_(const struct htable *ht,
135 struct htable_iter *i, size_t hash)
137 i->off = (i->off + 1) & ((1 << ht->bits)-1);
138 return htable_val(ht, i, hash, 0);
141 void *htable_first_(const struct htable *ht, struct htable_iter *i)
143 for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
144 if (entry_is_valid(ht->table[i->off]))
145 return get_raw_ptr(ht, ht->table[i->off]);
150 void *htable_next_(const struct htable *ht, struct htable_iter *i)
152 for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
153 if (entry_is_valid(ht->table[i->off]))
154 return get_raw_ptr(ht, ht->table[i->off]);
159 void *htable_prev_(const struct htable *ht, struct htable_iter *i)
165 if (entry_is_valid(ht->table[i->off]))
166 return get_raw_ptr(ht, ht->table[i->off]);
170 /* This does not expand the hash table, that's up to caller. */
171 static void ht_add(struct htable *ht, const void *new, size_t h)
174 uintptr_t perfect = ht->perfect_bit;
176 i = hash_bucket(ht, h);
178 while (entry_is_valid(ht->table[i])) {
180 i = (i + 1) & ((1 << ht->bits)-1);
182 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
185 static COLD bool double_table(struct htable *ht)
188 size_t oldnum = (size_t)1 << ht->bits;
189 uintptr_t *oldtable, e;
191 oldtable = ht->table;
192 ht->table = calloc(1 << (ht->bits+1), sizeof(size_t));
194 ht->table = oldtable;
198 htable_adjust_capacity(ht);
200 /* If we lost our "perfect bit", get it back now. */
201 if (!ht->perfect_bit && ht->common_mask) {
202 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
203 if (ht->common_mask & ((size_t)1 << i)) {
204 ht->perfect_bit = (size_t)1 << i;
210 if (oldtable != &ht->perfect_bit) {
211 for (i = 0; i < oldnum; i++) {
212 if (entry_is_valid(e = oldtable[i])) {
213 void *p = get_raw_ptr(ht, e);
214 ht_add(ht, p, ht->rehash(p, ht->priv));
221 (void)htable_debug(ht, HTABLE_LOC);
225 static COLD void rehash_table(struct htable *ht)
230 /* Beware wrap cases: we need to start from first empty bucket. */
231 for (start = 0; ht->table[start]; start++);
233 for (i = 0; i < (size_t)1 << ht->bits; i++) {
234 size_t h = (i + start) & ((1 << ht->bits)-1);
238 if (e == HTABLE_DELETED)
240 else if (!(e & ht->perfect_bit)) {
241 void *p = get_raw_ptr(ht, e);
243 ht_add(ht, p, ht->rehash(p, ht->priv));
247 (void)htable_debug(ht, HTABLE_LOC);
250 /* We stole some bits, now we need to put them back... */
251 static COLD void update_common(struct htable *ht, const void *p)
254 uintptr_t maskdiff, bitsdiff;
256 if (ht->elems == 0) {
257 /* Always reveal one bit of the pointer in the bucket,
258 * so it's not zero or HTABLE_DELETED (1), even if
259 * hash happens to be 0. Assumes (void *)1 is not a
261 for (i = sizeof(uintptr_t)*CHAR_BIT - 1; i > 0; i--) {
262 if ((uintptr_t)p & ((uintptr_t)1 << i))
266 ht->common_mask = ~((uintptr_t)1 << i);
267 ht->common_bits = ((uintptr_t)p & ht->common_mask);
269 (void)htable_debug(ht, HTABLE_LOC);
273 /* Find bits which are unequal to old common set. */
274 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
276 /* These are the bits which go there in existing entries. */
277 bitsdiff = ht->common_bits & maskdiff;
279 for (i = 0; i < (size_t)1 << ht->bits; i++) {
280 if (!entry_is_valid(ht->table[i]))
282 /* Clear the bits no longer in the mask, set them as
284 ht->table[i] &= ~maskdiff;
285 ht->table[i] |= bitsdiff;
288 /* Take away those bits from our mask, bits and perfect bit. */
289 ht->common_mask &= ~maskdiff;
290 ht->common_bits &= ~maskdiff;
291 ht->perfect_bit &= ~maskdiff;
292 (void)htable_debug(ht, HTABLE_LOC);
295 bool htable_add_(struct htable *ht, size_t hash, const void *p)
297 if (ht->elems+1 > ht->max && !double_table(ht))
299 if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
302 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
303 update_common(ht, p);
310 bool htable_del_(struct htable *ht, size_t h, const void *p)
312 struct htable_iter i;
315 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
317 htable_delval(ht, &i);
324 void htable_delval_(struct htable *ht, struct htable_iter *i)
326 assert(i->off < (size_t)1 << ht->bits);
327 assert(entry_is_valid(ht->table[i->off]));
330 ht->table[i->off] = HTABLE_DELETED;
334 struct htable *htable_check(const struct htable *ht, const char *abortstr)
337 struct htable_iter i;
340 /* Use non-DEBUG versions here, to avoid infinite recursion with
341 * CCAN_HTABLE_DEBUG! */
342 for (p = htable_first_(ht, &i); p; p = htable_next_(ht, &i)) {
343 struct htable_iter i2;
345 size_t h = ht->rehash(p, ht->priv);
350 /* Open-code htable_get to avoid CCAN_HTABLE_DEBUG */
351 for (c = htable_firstval_(ht, &i2, h);
353 c = htable_nextval_(ht, &i2, h)) {
363 "%s: element %p in position %zu"
364 " cannot find itself\n",
371 if (n != ht->elems) {
374 "%s: found %zu elems, expected %zu\n",
375 abortstr, n, ht->elems);
381 return (struct htable *)ht;