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;
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;
66 ht->max = (1 << ht->bits) * 2 / 3;
67 ht->max_with_deleted = (1 << ht->bits) * 4 / 5;
68 /* This guarantees we enter update_common first add. */
71 ht->table = calloc(1 << ht->bits, sizeof(uintptr_t));
80 void htable_free(const struct htable *ht)
82 free((void *)ht->table);
86 static size_t hash_bucket(const struct htable *ht, size_t h)
88 return h & ((1 << ht->bits)-1);
91 static void *htable_val(const struct htable *ht,
92 struct htable_iter *i, size_t hash)
94 uintptr_t h2 = get_hash_ptr_bits(ht, hash);
96 while (ht->table[i->off]) {
97 if (ht->table[i->off] != HTABLE_DELETED) {
98 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
99 return get_raw_ptr(ht, ht->table[i->off]);
101 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);
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);
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 /* This does not expand the hash table, that's up to caller. */
139 static void ht_add(struct htable *ht, const void *new, size_t h)
143 i = hash_bucket(ht, h);
145 while (entry_is_valid(ht->table[i]))
146 i = (i + 1) & ((1 << ht->bits)-1);
148 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h));
151 static COLD_ATTRIBUTE bool double_table(struct htable *ht)
154 size_t oldnum = (size_t)1 << ht->bits;
155 uintptr_t *oldtable, e;
157 oldtable = ht->table;
158 ht->table = calloc(1 << (ht->bits+1), sizeof(size_t));
160 ht->table = oldtable;
165 ht->max_with_deleted *= 2;
167 for (i = 0; i < oldnum; i++) {
168 if (entry_is_valid(e = oldtable[i])) {
169 void *p = get_raw_ptr(ht, e);
170 ht_add(ht, p, ht->rehash(p, ht->priv));
178 static COLD_ATTRIBUTE void rehash_table(struct htable *ht)
183 /* Beware wrap cases: we need to start from first empty bucket. */
184 for (start = 0; ht->table[start]; start++);
186 for (i = 0; i < (size_t)1 << ht->bits; i++) {
187 size_t h = (i + start) & ((1 << ht->bits)-1);
192 if (e != HTABLE_DELETED) {
193 void *p = get_raw_ptr(ht, e);
194 ht_add(ht, p, ht->rehash(p, ht->priv));
200 /* We stole some bits, now we need to put them back... */
201 static COLD_ATTRIBUTE void update_common(struct htable *ht, const void *p)
204 uintptr_t maskdiff, bitsdiff;
206 if (ht->elems == 0) {
207 ht->common_mask = -1;
208 ht->common_bits = (uintptr_t)p;
212 /* Find bits which are unequal to old common set. */
213 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
215 /* These are the bits which go there in existing entries. */
216 bitsdiff = ht->common_bits & maskdiff;
218 for (i = 0; i < (size_t)1 << ht->bits; i++) {
219 if (!entry_is_valid(ht->table[i]))
221 /* Clear the bits no longer in the mask, set them as
223 ht->table[i] &= ~maskdiff;
224 ht->table[i] |= bitsdiff;
227 /* Take away those bits from our mask and set. */
228 ht->common_mask &= ~maskdiff;
229 ht->common_bits &= ~maskdiff;
232 bool htable_add(struct htable *ht, size_t hash, const void *p)
234 if (ht->elems+1 > ht->max && !double_table(ht))
236 if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
239 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
240 update_common(ht, p);
247 bool htable_del(struct htable *ht, size_t h, const void *p)
249 struct htable_iter i;
252 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
254 htable_delval(ht, &i);
261 void htable_delval(struct htable *ht, struct htable_iter *i)
263 assert(i->off < (size_t)1 << ht->bits);
264 assert(entry_is_valid(ht->table[i->off]));
267 ht->table[i->off] = HTABLE_DELETED;