1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #include <ccan/htable/htable.h>
3 #include <ccan/compiler/compiler.h>
10 /* We use 0x1 as deleted marker. */
11 #define HTABLE_DELETED (0x1)
13 /* We clear out the bits which are always the same, and put metadata there. */
14 static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
17 return e & ht->common_mask;
20 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
22 return (void *)((e & ~ht->common_mask) | ht->common_bits);
25 static inline uintptr_t make_hval(const struct htable *ht,
26 const void *p, uintptr_t bits)
28 return ((uintptr_t)p & ~ht->common_mask) | bits;
31 static inline bool entry_is_valid(uintptr_t e)
33 return e > HTABLE_DELETED;
36 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
39 /* Shuffling the extra bits (as specified in mask) down the
40 * end is quite expensive. But the lower bits are redundant, so
41 * we fold the value first. */
42 return (hash ^ (hash >> ht->bits))
43 & ht->common_mask & ~ht->perfect_bit;
46 void htable_init(struct htable *ht,
47 size_t (*rehash)(const void *elem, void *priv), void *priv)
49 struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL);
53 ht->table = &ht->perfect_bit;
56 /* We've changed ht->bits, update ht->max and ht->max_with_deleted */
57 static void htable_adjust_capacity(struct htable *ht)
59 ht->max = ((size_t)3 << ht->bits) / 4;
60 ht->max_with_deleted = ((size_t)9 << ht->bits) / 10;
63 bool htable_init_sized(struct htable *ht,
64 size_t (*rehash)(const void *, void *),
65 void *priv, size_t expect)
67 htable_init(ht, rehash, priv);
69 /* Don't go insane with sizing. */
70 for (ht->bits = 1; ((size_t)3 << ht->bits) / 4 < expect; ht->bits++) {
75 ht->table = calloc(1 << ht->bits, sizeof(size_t));
77 ht->table = &ht->perfect_bit;
80 htable_adjust_capacity(ht);
84 void htable_clear(struct htable *ht)
86 if (ht->table != &ht->perfect_bit)
87 free((void *)ht->table);
88 htable_init(ht, ht->rehash, ht->priv);
91 bool htable_copy(struct htable *dst, const struct htable *src)
93 uintptr_t *htable = malloc(sizeof(size_t) << src->bits);
100 memcpy(dst->table, src->table, sizeof(size_t) << src->bits);
104 static size_t hash_bucket(const struct htable *ht, size_t h)
106 return h & ((1 << ht->bits)-1);
109 static void *htable_val(const struct htable *ht,
110 struct htable_iter *i, size_t hash, uintptr_t perfect)
112 uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
114 while (ht->table[i->off]) {
115 if (ht->table[i->off] != HTABLE_DELETED) {
116 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
117 return get_raw_ptr(ht, ht->table[i->off]);
119 i->off = (i->off + 1) & ((1 << ht->bits)-1);
125 void *htable_firstval(const struct htable *ht,
126 struct htable_iter *i, size_t hash)
128 i->off = hash_bucket(ht, hash);
129 return htable_val(ht, i, hash, ht->perfect_bit);
132 void *htable_nextval(const struct htable *ht,
133 struct htable_iter *i, size_t hash)
135 i->off = (i->off + 1) & ((1 << ht->bits)-1);
136 return htable_val(ht, i, hash, 0);
139 void *htable_first(const struct htable *ht, struct htable_iter *i)
141 for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
142 if (entry_is_valid(ht->table[i->off]))
143 return get_raw_ptr(ht, ht->table[i->off]);
148 void *htable_next(const struct htable *ht, struct htable_iter *i)
150 for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
151 if (entry_is_valid(ht->table[i->off]))
152 return get_raw_ptr(ht, ht->table[i->off]);
157 void *htable_prev(const struct htable *ht, struct htable_iter *i)
163 if (entry_is_valid(ht->table[i->off]))
164 return get_raw_ptr(ht, ht->table[i->off]);
168 /* This does not expand the hash table, that's up to caller. */
169 static void ht_add(struct htable *ht, const void *new, size_t h)
172 uintptr_t perfect = ht->perfect_bit;
174 i = hash_bucket(ht, h);
176 while (entry_is_valid(ht->table[i])) {
178 i = (i + 1) & ((1 << ht->bits)-1);
180 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
183 static COLD bool double_table(struct htable *ht)
186 size_t oldnum = (size_t)1 << ht->bits;
187 uintptr_t *oldtable, e;
189 oldtable = ht->table;
190 ht->table = calloc(1 << (ht->bits+1), sizeof(size_t));
192 ht->table = oldtable;
196 htable_adjust_capacity(ht);
198 /* If we lost our "perfect bit", get it back now. */
199 if (!ht->perfect_bit && ht->common_mask) {
200 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
201 if (ht->common_mask & ((size_t)1 << i)) {
202 ht->perfect_bit = (size_t)1 << i;
208 if (oldtable != &ht->perfect_bit) {
209 for (i = 0; i < oldnum; i++) {
210 if (entry_is_valid(e = oldtable[i])) {
211 void *p = get_raw_ptr(ht, e);
212 ht_add(ht, p, ht->rehash(p, ht->priv));
221 static COLD void rehash_table(struct htable *ht)
226 /* Beware wrap cases: we need to start from first empty bucket. */
227 for (start = 0; ht->table[start]; start++);
229 for (i = 0; i < (size_t)1 << ht->bits; i++) {
230 size_t h = (i + start) & ((1 << ht->bits)-1);
234 if (e == HTABLE_DELETED)
236 else if (!(e & ht->perfect_bit)) {
237 void *p = get_raw_ptr(ht, e);
239 ht_add(ht, p, ht->rehash(p, ht->priv));
245 /* We stole some bits, now we need to put them back... */
246 static COLD void update_common(struct htable *ht, const void *p)
249 uintptr_t maskdiff, bitsdiff;
251 if (ht->elems == 0) {
252 /* Always reveal one bit of the pointer in the bucket,
253 * so it's not zero or HTABLE_DELETED (1), even if
254 * hash happens to be 0. Assumes (void *)1 is not a
256 for (i = sizeof(uintptr_t)*CHAR_BIT - 1; i > 0; i--) {
257 if ((uintptr_t)p & ((uintptr_t)1 << i))
261 ht->common_mask = ~((uintptr_t)1 << i);
262 ht->common_bits = ((uintptr_t)p & ht->common_mask);
267 /* Find bits which are unequal to old common set. */
268 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
270 /* These are the bits which go there in existing entries. */
271 bitsdiff = ht->common_bits & maskdiff;
273 for (i = 0; i < (size_t)1 << ht->bits; i++) {
274 if (!entry_is_valid(ht->table[i]))
276 /* Clear the bits no longer in the mask, set them as
278 ht->table[i] &= ~maskdiff;
279 ht->table[i] |= bitsdiff;
282 /* Take away those bits from our mask, bits and perfect bit. */
283 ht->common_mask &= ~maskdiff;
284 ht->common_bits &= ~maskdiff;
285 ht->perfect_bit &= ~maskdiff;
288 bool htable_add(struct htable *ht, size_t hash, const void *p)
290 if (ht->elems+1 > ht->max && !double_table(ht))
292 if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
295 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
296 update_common(ht, p);
303 bool htable_del(struct htable *ht, size_t h, const void *p)
305 struct htable_iter i;
308 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
310 htable_delval(ht, &i);
317 void htable_delval(struct htable *ht, struct htable_iter *i)
319 assert(i->off < (size_t)1 << ht->bits);
320 assert(entry_is_valid(ht->table[i->off]));
323 ht->table[i->off] = HTABLE_DELETED;