1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #include <ccan/htable/htable.h>
3 #include <ccan/compiler/compiler.h>
11 /* We use 0x1 as deleted marker. */
12 #define HTABLE_DELETED (0x1)
14 static void *htable_default_alloc(struct htable *ht, size_t len)
16 return calloc(len, 1);
19 static void htable_default_free(struct htable *ht, void *p)
24 static void *(*htable_alloc)(struct htable *, size_t) = htable_default_alloc;
25 static void (*htable_free)(struct htable *, void *) = htable_default_free;
27 void htable_set_allocator(void *(*alloc)(struct htable *, size_t len),
28 void (*free)(struct htable *, void *p))
31 alloc = htable_default_alloc;
33 free = htable_default_free;
38 /* We clear out the bits which are always the same, and put metadata there. */
39 static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
42 return e & ht->common_mask;
45 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
47 return (void *)((e & ~ht->common_mask) | ht->common_bits);
50 static inline uintptr_t make_hval(const struct htable *ht,
51 const void *p, uintptr_t bits)
53 return ((uintptr_t)p & ~ht->common_mask) | bits;
56 static inline bool entry_is_valid(uintptr_t e)
58 return e > HTABLE_DELETED;
61 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
64 /* Shuffling the extra bits (as specified in mask) down the
65 * end is quite expensive. But the lower bits are redundant, so
66 * we fold the value first. */
67 return (hash ^ (hash >> ht->bits))
68 & ht->common_mask & ~ht->perfect_bit;
71 void htable_init(struct htable *ht,
72 size_t (*rehash)(const void *elem, void *priv), void *priv)
74 struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL);
78 ht->table = &ht->perfect_bit;
81 /* We've changed ht->bits, update ht->max and ht->max_with_deleted */
82 static void htable_adjust_capacity(struct htable *ht)
84 ht->max = ((size_t)3 << ht->bits) / 4;
85 ht->max_with_deleted = ((size_t)9 << ht->bits) / 10;
88 bool htable_init_sized(struct htable *ht,
89 size_t (*rehash)(const void *, void *),
90 void *priv, size_t expect)
92 htable_init(ht, rehash, priv);
94 /* Don't go insane with sizing. */
95 for (ht->bits = 1; ((size_t)3 << ht->bits) / 4 < expect; ht->bits++) {
100 ht->table = htable_alloc(ht, sizeof(size_t) << ht->bits);
102 ht->table = &ht->perfect_bit;
105 htable_adjust_capacity(ht);
106 (void)htable_debug(ht, HTABLE_LOC);
110 void htable_clear(struct htable *ht)
112 if (ht->table != &ht->perfect_bit)
113 htable_free(ht, (void *)ht->table);
114 htable_init(ht, ht->rehash, ht->priv);
117 bool htable_copy_(struct htable *dst, const struct htable *src)
119 uintptr_t *htable = htable_alloc(dst, sizeof(size_t) << src->bits);
126 memcpy(dst->table, src->table, sizeof(size_t) << src->bits);
130 static size_t hash_bucket(const struct htable *ht, size_t h)
132 return h & ((1 << ht->bits)-1);
135 static void *htable_val(const struct htable *ht,
136 struct htable_iter *i, size_t hash, uintptr_t perfect)
138 uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
140 while (ht->table[i->off]) {
141 if (ht->table[i->off] != HTABLE_DELETED) {
142 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
143 return get_raw_ptr(ht, ht->table[i->off]);
145 i->off = (i->off + 1) & ((1 << ht->bits)-1);
151 void *htable_firstval_(const struct htable *ht,
152 struct htable_iter *i, size_t hash)
154 i->off = hash_bucket(ht, hash);
155 return htable_val(ht, i, hash, ht->perfect_bit);
158 void *htable_nextval_(const struct htable *ht,
159 struct htable_iter *i, size_t hash)
161 i->off = (i->off + 1) & ((1 << ht->bits)-1);
162 return htable_val(ht, i, hash, 0);
165 void *htable_first_(const struct htable *ht, struct htable_iter *i)
167 for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
168 if (entry_is_valid(ht->table[i->off]))
169 return get_raw_ptr(ht, ht->table[i->off]);
174 void *htable_next_(const struct htable *ht, struct htable_iter *i)
176 for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
177 if (entry_is_valid(ht->table[i->off]))
178 return get_raw_ptr(ht, ht->table[i->off]);
183 void *htable_prev_(const struct htable *ht, struct htable_iter *i)
189 if (entry_is_valid(ht->table[i->off]))
190 return get_raw_ptr(ht, ht->table[i->off]);
194 /* This does not expand the hash table, that's up to caller. */
195 static void ht_add(struct htable *ht, const void *new, size_t h)
198 uintptr_t perfect = ht->perfect_bit;
200 i = hash_bucket(ht, h);
202 while (entry_is_valid(ht->table[i])) {
204 i = (i + 1) & ((1 << ht->bits)-1);
206 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
209 static COLD bool double_table(struct htable *ht)
212 size_t oldnum = (size_t)1 << ht->bits;
213 uintptr_t *oldtable, e;
215 oldtable = ht->table;
216 ht->table = htable_alloc(ht, sizeof(size_t) << (ht->bits+1));
218 ht->table = oldtable;
222 htable_adjust_capacity(ht);
224 /* If we lost our "perfect bit", get it back now. */
225 if (!ht->perfect_bit && ht->common_mask) {
226 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
227 if (ht->common_mask & ((size_t)1 << i)) {
228 ht->perfect_bit = (size_t)1 << i;
234 if (oldtable != &ht->perfect_bit) {
235 for (i = 0; i < oldnum; i++) {
236 if (entry_is_valid(e = oldtable[i])) {
237 void *p = get_raw_ptr(ht, e);
238 ht_add(ht, p, ht->rehash(p, ht->priv));
241 htable_free(ht, oldtable);
245 (void)htable_debug(ht, HTABLE_LOC);
249 static COLD void rehash_table(struct htable *ht)
254 /* Beware wrap cases: we need to start from first empty bucket. */
255 for (start = 0; ht->table[start]; start++);
257 for (i = 0; i < (size_t)1 << ht->bits; i++) {
258 size_t h = (i + start) & ((1 << ht->bits)-1);
262 if (e == HTABLE_DELETED)
264 else if (!(e & ht->perfect_bit)) {
265 void *p = get_raw_ptr(ht, e);
267 ht_add(ht, p, ht->rehash(p, ht->priv));
271 (void)htable_debug(ht, HTABLE_LOC);
274 /* We stole some bits, now we need to put them back... */
275 static COLD void update_common(struct htable *ht, const void *p)
278 uintptr_t maskdiff, bitsdiff;
280 if (ht->elems == 0) {
281 /* Always reveal one bit of the pointer in the bucket,
282 * so it's not zero or HTABLE_DELETED (1), even if
283 * hash happens to be 0. Assumes (void *)1 is not a
285 for (i = sizeof(uintptr_t)*CHAR_BIT - 1; i > 0; i--) {
286 if ((uintptr_t)p & ((uintptr_t)1 << i))
290 ht->common_mask = ~((uintptr_t)1 << i);
291 ht->common_bits = ((uintptr_t)p & ht->common_mask);
293 (void)htable_debug(ht, HTABLE_LOC);
297 /* Find bits which are unequal to old common set. */
298 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
300 /* These are the bits which go there in existing entries. */
301 bitsdiff = ht->common_bits & maskdiff;
303 for (i = 0; i < (size_t)1 << ht->bits; i++) {
304 if (!entry_is_valid(ht->table[i]))
306 /* Clear the bits no longer in the mask, set them as
308 ht->table[i] &= ~maskdiff;
309 ht->table[i] |= bitsdiff;
312 /* Take away those bits from our mask, bits and perfect bit. */
313 ht->common_mask &= ~maskdiff;
314 ht->common_bits &= ~maskdiff;
315 ht->perfect_bit &= ~maskdiff;
316 (void)htable_debug(ht, HTABLE_LOC);
319 bool htable_add_(struct htable *ht, size_t hash, const void *p)
321 if (ht->elems+1 > ht->max && !double_table(ht))
323 if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
326 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
327 update_common(ht, p);
334 bool htable_del_(struct htable *ht, size_t h, const void *p)
336 struct htable_iter i;
339 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
341 htable_delval(ht, &i);
348 void htable_delval_(struct htable *ht, struct htable_iter *i)
350 assert(i->off < (size_t)1 << ht->bits);
351 assert(entry_is_valid(ht->table[i->off]));
354 ht->table[i->off] = HTABLE_DELETED;
358 struct htable *htable_check(const struct htable *ht, const char *abortstr)
361 struct htable_iter i;
364 /* Use non-DEBUG versions here, to avoid infinite recursion with
365 * CCAN_HTABLE_DEBUG! */
366 for (p = htable_first_(ht, &i); p; p = htable_next_(ht, &i)) {
367 struct htable_iter i2;
369 size_t h = ht->rehash(p, ht->priv);
374 /* Open-code htable_get to avoid CCAN_HTABLE_DEBUG */
375 for (c = htable_firstval_(ht, &i2, h);
377 c = htable_nextval_(ht, &i2, h)) {
387 "%s: element %p in position %zu"
388 " cannot find itself\n",
395 if (n != ht->elems) {
398 "%s: found %zu elems, expected %zu\n",
399 abortstr, n, ht->elems);
405 return (struct htable *)ht;