1 #include <ccan/htable/htable.h>
2 #include <ccan/compiler/compiler.h>
8 /* This means a struct htable takes at least 512 bytes / 1k (32/64 bits). */
9 #define HTABLE_BASE_BITS 7
11 /* We use 0x1 as deleted marker. */
12 #define HTABLE_DELETED (0x1)
15 size_t (*rehash)(const void *elem, void *priv);
18 size_t elems, deleted, max, max_with_deleted;
19 /* These are the bits which are the same in all pointers. */
20 uintptr_t common_mask, common_bits;
21 uintptr_t perfect_bit;
25 /* We clear out the bits which are always the same, and put metadata there. */
26 static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
29 return e & ht->common_mask;
32 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
34 return (void *)((e & ~ht->common_mask) | ht->common_bits);
37 static inline uintptr_t make_hval(const struct htable *ht,
38 const void *p, uintptr_t bits)
40 return ((uintptr_t)p & ~ht->common_mask) | bits;
43 static inline bool entry_is_valid(uintptr_t e)
45 return e > HTABLE_DELETED;
48 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
51 /* Shuffling the extra bits (as specified in mask) down the
52 * end is quite expensive. But the lower bits are redundant, so
53 * we fold the value first. */
54 return (hash ^ (hash >> ht->bits))
55 & ht->common_mask & ~ht->perfect_bit;
58 struct htable *htable_new(size_t (*rehash)(const void *elem, void *priv),
61 struct htable *ht = malloc(sizeof(struct htable));
63 ht->bits = HTABLE_BASE_BITS;
68 ht->max = (1 << ht->bits) * 2 / 3;
69 ht->max_with_deleted = (1 << ht->bits) * 4 / 5;
70 /* This guarantees we enter update_common first add. */
74 ht->table = calloc(1 << ht->bits, sizeof(uintptr_t));
83 void htable_free(const struct htable *ht)
85 free((void *)ht->table);
89 static size_t hash_bucket(const struct htable *ht, size_t h)
91 return h & ((1 << ht->bits)-1);
94 static void *htable_val(const struct htable *ht,
95 struct htable_iter *i, size_t hash, uintptr_t perfect)
97 uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
99 while (ht->table[i->off]) {
100 if (ht->table[i->off] != HTABLE_DELETED) {
101 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
102 return get_raw_ptr(ht, ht->table[i->off]);
104 i->off = (i->off + 1) & ((1 << ht->bits)-1);
110 void *htable_firstval(const struct htable *ht,
111 struct htable_iter *i, size_t hash)
113 i->off = hash_bucket(ht, hash);
114 return htable_val(ht, i, hash, ht->perfect_bit);
117 void *htable_nextval(const struct htable *ht,
118 struct htable_iter *i, size_t hash)
120 i->off = (i->off + 1) & ((1 << ht->bits)-1);
121 return htable_val(ht, i, hash, 0);
124 void *htable_first(const struct htable *ht, struct htable_iter *i)
126 for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
127 if (entry_is_valid(ht->table[i->off]))
128 return get_raw_ptr(ht, ht->table[i->off]);
133 void *htable_next(const struct htable *ht, struct htable_iter *i)
135 for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
136 if (entry_is_valid(ht->table[i->off]))
137 return get_raw_ptr(ht, ht->table[i->off]);
142 /* This does not expand the hash table, that's up to caller. */
143 static void ht_add(struct htable *ht, const void *new, size_t h)
146 uintptr_t perfect = ht->perfect_bit;
148 i = hash_bucket(ht, h);
150 while (entry_is_valid(ht->table[i])) {
152 i = (i + 1) & ((1 << ht->bits)-1);
154 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
157 static COLD_ATTRIBUTE bool double_table(struct htable *ht)
160 size_t oldnum = (size_t)1 << ht->bits;
161 uintptr_t *oldtable, e;
163 oldtable = ht->table;
164 ht->table = calloc(1 << (ht->bits+1), sizeof(size_t));
166 ht->table = oldtable;
171 ht->max_with_deleted *= 2;
173 /* FIXME: If we lost our perfect bit, we could reclaim it here! */
174 for (i = 0; i < oldnum; i++) {
175 if (entry_is_valid(e = oldtable[i])) {
176 void *p = get_raw_ptr(ht, e);
177 ht_add(ht, p, ht->rehash(p, ht->priv));
185 static COLD_ATTRIBUTE void rehash_table(struct htable *ht)
190 /* Beware wrap cases: we need to start from first empty bucket. */
191 for (start = 0; ht->table[start]; start++);
193 for (i = 0; i < (size_t)1 << ht->bits; i++) {
194 size_t h = (i + start) & ((1 << ht->bits)-1);
198 if (e == HTABLE_DELETED)
200 else if (!(e & ht->perfect_bit)) {
201 void *p = get_raw_ptr(ht, e);
203 ht_add(ht, p, ht->rehash(p, ht->priv));
209 /* We stole some bits, now we need to put them back... */
210 static COLD_ATTRIBUTE void update_common(struct htable *ht, const void *p)
213 uintptr_t maskdiff, bitsdiff;
215 if (ht->elems == 0) {
216 ht->common_mask = -1;
217 ht->common_bits = (uintptr_t)p;
222 /* Find bits which are unequal to old common set. */
223 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
225 /* These are the bits which go there in existing entries. */
226 bitsdiff = ht->common_bits & maskdiff;
228 for (i = 0; i < (size_t)1 << ht->bits; i++) {
229 if (!entry_is_valid(ht->table[i]))
231 /* Clear the bits no longer in the mask, set them as
233 ht->table[i] &= ~maskdiff;
234 ht->table[i] |= bitsdiff;
237 /* Take away those bits from our mask, bits and perfect bit. */
238 ht->common_mask &= ~maskdiff;
239 ht->common_bits &= ~maskdiff;
240 ht->perfect_bit &= ~maskdiff;
243 bool htable_add(struct htable *ht, size_t hash, const void *p)
245 if (ht->elems+1 > ht->max && !double_table(ht))
247 if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
250 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
251 update_common(ht, p);
258 bool htable_del(struct htable *ht, size_t h, const void *p)
260 struct htable_iter i;
263 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
265 htable_delval(ht, &i);
272 void htable_delval(struct htable *ht, struct htable_iter *i)
274 assert(i->off < (size_t)1 << ht->bits);
275 assert(entry_is_valid(ht->table[i->off]));
278 ht->table[i->off] = HTABLE_DELETED;