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);
19 /* These are the bits which are the same in all pointers. */
20 uintptr_t common_mask, common_bits;
24 /* We clear out the bits which are always the same, and put metadata there. */
25 static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
28 return e & ht->common_mask;
31 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
33 return (void *)((e & ~ht->common_mask) | ht->common_bits);
36 static inline uintptr_t make_hval(const struct htable *ht,
37 const void *p, uintptr_t bits)
39 return ((uintptr_t)p & ~ht->common_mask) | bits;
42 static inline bool entry_is_valid(uintptr_t e)
44 return e > HTABLE_DELETED;
47 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
50 /* Shuffling the extra bits (as specified in mask) down the
51 * end is quite expensive. But the lower bits are redundant, so
52 * we fold the value first. */
53 return (hash ^ (hash >> ht->bits)) & ht->common_mask;
56 struct htable *htable_new(size_t (*rehash)(const void *elem, void *priv),
59 struct htable *ht = malloc(sizeof(struct htable));
61 ht->bits = HTABLE_BASE_BITS;
65 ht->max = (1 << ht->bits) * 3 / 4;
66 /* This guarantees we enter update_common first add. */
69 ht->table = calloc(1 << ht->bits, sizeof(uintptr_t));
78 void htable_free(const struct htable *ht)
80 free((void *)ht->table);
84 static size_t hash_bucket(const struct htable *ht, size_t h)
86 return h & ((1 << ht->bits)-1);
89 static void *htable_val(const struct htable *ht,
90 struct htable_iter *i, size_t hash)
92 uintptr_t h2 = get_hash_ptr_bits(ht, hash);
94 while (ht->table[i->off]) {
95 if (ht->table[i->off] != HTABLE_DELETED) {
96 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
97 return get_raw_ptr(ht, ht->table[i->off]);
99 i->off = (i->off + 1) & ((1 << ht->bits)-1);
104 void *htable_firstval(const struct htable *ht,
105 struct htable_iter *i, size_t hash)
107 i->off = hash_bucket(ht, hash);
108 return htable_val(ht, i, hash);
111 void *htable_nextval(const struct htable *ht,
112 struct htable_iter *i, size_t hash)
114 i->off = (i->off + 1) & ((1 << ht->bits)-1);
115 return htable_val(ht, i, hash);
118 void *htable_first(const struct htable *ht, struct htable_iter *i)
120 for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
121 if (entry_is_valid(ht->table[i->off]))
122 return get_raw_ptr(ht, ht->table[i->off]);
127 void *htable_next(const struct htable *ht, struct htable_iter *i)
129 for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
130 if (entry_is_valid(ht->table[i->off]))
131 return get_raw_ptr(ht, ht->table[i->off]);
136 /* This does not expand the hash table, that's up to caller. */
137 static void ht_add(struct htable *ht, const void *new, size_t h)
141 i = hash_bucket(ht, h);
143 while (entry_is_valid(ht->table[i]))
144 i = (i + 1) & ((1 << ht->bits)-1);
146 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h));
149 static COLD_ATTRIBUTE bool double_table(struct htable *ht)
152 size_t oldnum = (size_t)1 << ht->bits;
155 oldtable = ht->table;
156 ht->table = calloc(1 << (ht->bits+1), sizeof(size_t));
158 ht->table = oldtable;
164 for (i = 0; i < oldnum; i++) {
165 if (entry_is_valid(e = oldtable[i])) {
166 void *p = get_raw_ptr(ht, e);
167 ht_add(ht, p, ht->rehash(p, ht->priv));
174 /* We stole some bits, now we need to put them back... */
175 static COLD_ATTRIBUTE void update_common(struct htable *ht, const void *p)
178 uintptr_t maskdiff, bitsdiff;
180 if (ht->elems == 0) {
181 ht->common_mask = -1;
182 ht->common_bits = (uintptr_t)p;
186 /* Find bits which are unequal to old common set. */
187 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
189 /* These are the bits which go there in existing entries. */
190 bitsdiff = ht->common_bits & maskdiff;
192 for (i = 0; i < (size_t)1 << ht->bits; i++) {
193 if (!entry_is_valid(ht->table[i]))
195 /* Clear the bits no longer in the mask, set them as
197 ht->table[i] &= ~maskdiff;
198 ht->table[i] |= bitsdiff;
201 /* Take away those bits from our mask and set. */
202 ht->common_mask &= ~maskdiff;
203 ht->common_bits &= ~maskdiff;
206 bool htable_add(struct htable *ht, size_t hash, const void *p)
208 if (ht->elems+1 > ht->max && !double_table(ht))
211 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
212 update_common(ht, p);
219 /* If every one of the following buckets are DELETED (up to the next unused
220 one), we can actually mark them all unused. */
221 static void delete_run(struct htable *ht, unsigned int num)
223 unsigned int i, last = num + 1;
224 size_t mask = (((size_t)1 << ht->bits)-1);
226 while (ht->table[last & mask]) {
227 if (entry_is_valid(ht->table[last & mask]))
232 /* Now see if we can step backwards to find previous deleted ones. */
233 for (i = num-1; ht->table[i & mask] == HTABLE_DELETED; i--);
235 for (i++; i < last; i++)
236 ht->table[i & ((1 << ht->bits)-1)] = 0;
239 bool htable_del(struct htable *ht, size_t h, const void *p)
241 struct htable_iter i;
244 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
246 htable_delval(ht, &i);
253 void htable_delval(struct htable *ht, struct htable_iter *i)
255 assert(i->off < (size_t)1 << ht->bits);
256 assert(entry_is_valid(ht->table[i->off]));
259 ht->table[i->off] = HTABLE_DELETED;
260 delete_run(ht, i->off);