1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #include <ccan/htable/htable.h>
3 #include <ccan/compiler/compiler.h>
9 /* We use 0x1 as deleted marker. */
10 #define HTABLE_DELETED (0x1)
12 /* We clear out the bits which are always the same, and put metadata there. */
13 static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
16 return e & ht->common_mask;
19 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
21 return (void *)((e & ~ht->common_mask) | ht->common_bits);
24 static inline uintptr_t make_hval(const struct htable *ht,
25 const void *p, uintptr_t bits)
27 return ((uintptr_t)p & ~ht->common_mask) | bits;
30 static inline bool entry_is_valid(uintptr_t e)
32 return e > HTABLE_DELETED;
35 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
38 /* Shuffling the extra bits (as specified in mask) down the
39 * end is quite expensive. But the lower bits are redundant, so
40 * we fold the value first. */
41 return (hash ^ (hash >> ht->bits))
42 & ht->common_mask & ~ht->perfect_bit;
45 void htable_init(struct htable *ht,
46 size_t (*rehash)(const void *elem, void *priv), void *priv)
48 struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL);
52 ht->table = &ht->perfect_bit;
55 bool htable_init_sized(struct htable *ht,
56 size_t (*rehash)(const void *, void *),
57 void *priv, size_t expect)
59 htable_init(ht, rehash, priv);
61 /* Don't go insane with sizing. */
62 for (ht->bits = 1; ((size_t)3 << ht->bits) / 4 < expect; ht->bits++) {
67 ht->table = calloc(1 << ht->bits, sizeof(size_t));
69 ht->table = &ht->perfect_bit;
72 ht->max = ((size_t)3 << ht->bits) / 4;
73 ht->max_with_deleted = ((size_t)9 << ht->bits) / 10;
78 void htable_clear(struct htable *ht)
80 if (ht->table != &ht->perfect_bit)
81 free((void *)ht->table);
82 htable_init(ht, ht->rehash, ht->priv);
85 static size_t hash_bucket(const struct htable *ht, size_t h)
87 return h & ((1 << ht->bits)-1);
90 static void *htable_val(const struct htable *ht,
91 struct htable_iter *i, size_t hash, uintptr_t perfect)
93 uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
95 while (ht->table[i->off]) {
96 if (ht->table[i->off] != HTABLE_DELETED) {
97 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
98 return get_raw_ptr(ht, ht->table[i->off]);
100 i->off = (i->off + 1) & ((1 << ht->bits)-1);
106 void *htable_firstval(const struct htable *ht,
107 struct htable_iter *i, size_t hash)
109 i->off = hash_bucket(ht, hash);
110 return htable_val(ht, i, hash, ht->perfect_bit);
113 void *htable_nextval(const struct htable *ht,
114 struct htable_iter *i, size_t hash)
116 i->off = (i->off + 1) & ((1 << ht->bits)-1);
117 return htable_val(ht, i, hash, 0);
120 void *htable_first(const struct htable *ht, struct htable_iter *i)
122 for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
123 if (entry_is_valid(ht->table[i->off]))
124 return get_raw_ptr(ht, ht->table[i->off]);
129 void *htable_next(const struct htable *ht, struct htable_iter *i)
131 for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
132 if (entry_is_valid(ht->table[i->off]))
133 return get_raw_ptr(ht, ht->table[i->off]);
138 void *htable_prev(const struct htable *ht, struct htable_iter *i)
144 if (entry_is_valid(ht->table[i->off]))
145 return get_raw_ptr(ht, ht->table[i->off]);
149 /* This does not expand the hash table, that's up to caller. */
150 static void ht_add(struct htable *ht, const void *new, size_t h)
153 uintptr_t perfect = ht->perfect_bit;
155 i = hash_bucket(ht, h);
157 while (entry_is_valid(ht->table[i])) {
159 i = (i + 1) & ((1 << ht->bits)-1);
161 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
164 static COLD bool double_table(struct htable *ht)
167 size_t oldnum = (size_t)1 << ht->bits;
168 uintptr_t *oldtable, e;
170 oldtable = ht->table;
171 ht->table = calloc(1 << (ht->bits+1), sizeof(size_t));
173 ht->table = oldtable;
177 ht->max = ((size_t)3 << ht->bits) / 4;
178 ht->max_with_deleted = ((size_t)9 << ht->bits) / 10;
180 /* If we lost our "perfect bit", get it back now. */
181 if (!ht->perfect_bit && ht->common_mask) {
182 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
183 if (ht->common_mask & ((size_t)1 << i)) {
184 ht->perfect_bit = (size_t)1 << i;
190 if (oldtable != &ht->perfect_bit) {
191 for (i = 0; i < oldnum; i++) {
192 if (entry_is_valid(e = oldtable[i])) {
193 void *p = get_raw_ptr(ht, e);
194 ht_add(ht, p, ht->rehash(p, ht->priv));
203 static COLD void rehash_table(struct htable *ht)
208 /* Beware wrap cases: we need to start from first empty bucket. */
209 for (start = 0; ht->table[start]; start++);
211 for (i = 0; i < (size_t)1 << ht->bits; i++) {
212 size_t h = (i + start) & ((1 << ht->bits)-1);
216 if (e == HTABLE_DELETED)
218 else if (!(e & ht->perfect_bit)) {
219 void *p = get_raw_ptr(ht, e);
221 ht_add(ht, p, ht->rehash(p, ht->priv));
227 /* We stole some bits, now we need to put them back... */
228 static COLD void update_common(struct htable *ht, const void *p)
231 uintptr_t maskdiff, bitsdiff;
233 if (ht->elems == 0) {
234 /* Always reveal one bit of the pointer in the bucket,
235 * so it's not zero or HTABLE_DELETED (1), even if
236 * hash happens to be 0. Assumes (void *)1 is not a
238 for (i = sizeof(uintptr_t)*CHAR_BIT - 1; i > 0; i--) {
239 if ((uintptr_t)p & ((uintptr_t)1 << i))
243 ht->common_mask = ~((uintptr_t)1 << i);
244 ht->common_bits = ((uintptr_t)p & ht->common_mask);
249 /* Find bits which are unequal to old common set. */
250 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
252 /* These are the bits which go there in existing entries. */
253 bitsdiff = ht->common_bits & maskdiff;
255 for (i = 0; i < (size_t)1 << ht->bits; i++) {
256 if (!entry_is_valid(ht->table[i]))
258 /* Clear the bits no longer in the mask, set them as
260 ht->table[i] &= ~maskdiff;
261 ht->table[i] |= bitsdiff;
264 /* Take away those bits from our mask, bits and perfect bit. */
265 ht->common_mask &= ~maskdiff;
266 ht->common_bits &= ~maskdiff;
267 ht->perfect_bit &= ~maskdiff;
270 bool htable_add(struct htable *ht, size_t hash, const void *p)
272 if (ht->elems+1 > ht->max && !double_table(ht))
274 if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
277 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
278 update_common(ht, p);
285 bool htable_del(struct htable *ht, size_t h, const void *p)
287 struct htable_iter i;
290 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
292 htable_delval(ht, &i);
299 void htable_delval(struct htable *ht, struct htable_iter *i)
301 assert(i->off < (size_t)1 << ht->bits);
302 assert(entry_is_valid(ht->table[i->off]));
305 ht->table[i->off] = HTABLE_DELETED;