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 static inline size_t ht_max(const struct htable *ht)
83 return ((size_t)3 << ht->bits) / 4;
86 static inline size_t ht_max_with_deleted(const struct htable *ht)
88 return ((size_t)9 << ht->bits) / 10;
91 bool htable_init_sized(struct htable *ht,
92 size_t (*rehash)(const void *, void *),
93 void *priv, size_t expect)
95 htable_init(ht, rehash, priv);
97 /* Don't go insane with sizing. */
98 for (ht->bits = 1; ((size_t)3 << ht->bits) / 4 < expect; ht->bits++) {
103 ht->table = htable_alloc(ht, sizeof(size_t) << ht->bits);
105 ht->table = &ht->perfect_bit;
108 (void)htable_debug(ht, HTABLE_LOC);
112 void htable_clear(struct htable *ht)
114 if (ht->table != &ht->perfect_bit)
115 htable_free(ht, (void *)ht->table);
116 htable_init(ht, ht->rehash, ht->priv);
119 bool htable_copy_(struct htable *dst, const struct htable *src)
121 uintptr_t *htable = htable_alloc(dst, sizeof(size_t) << src->bits);
128 memcpy(dst->table, src->table, sizeof(size_t) << src->bits);
132 static size_t hash_bucket(const struct htable *ht, size_t h)
134 return h & ((1 << ht->bits)-1);
137 static void *htable_val(const struct htable *ht,
138 struct htable_iter *i, size_t hash, uintptr_t perfect)
140 uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
142 while (ht->table[i->off]) {
143 if (ht->table[i->off] != HTABLE_DELETED) {
144 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
145 return get_raw_ptr(ht, ht->table[i->off]);
147 i->off = (i->off + 1) & ((1 << ht->bits)-1);
153 void *htable_firstval_(const struct htable *ht,
154 struct htable_iter *i, size_t hash)
156 i->off = hash_bucket(ht, hash);
157 return htable_val(ht, i, hash, ht->perfect_bit);
160 void *htable_nextval_(const struct htable *ht,
161 struct htable_iter *i, size_t hash)
163 i->off = (i->off + 1) & ((1 << ht->bits)-1);
164 return htable_val(ht, i, hash, 0);
167 void *htable_first_(const struct htable *ht, struct htable_iter *i)
169 for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
170 if (entry_is_valid(ht->table[i->off]))
171 return get_raw_ptr(ht, ht->table[i->off]);
176 void *htable_next_(const struct htable *ht, struct htable_iter *i)
178 for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
179 if (entry_is_valid(ht->table[i->off]))
180 return get_raw_ptr(ht, ht->table[i->off]);
185 void *htable_prev_(const struct htable *ht, struct htable_iter *i)
191 if (entry_is_valid(ht->table[i->off]))
192 return get_raw_ptr(ht, ht->table[i->off]);
196 /* This does not expand the hash table, that's up to caller. */
197 static void ht_add(struct htable *ht, const void *new, size_t h)
200 uintptr_t perfect = ht->perfect_bit;
202 i = hash_bucket(ht, h);
204 while (entry_is_valid(ht->table[i])) {
206 i = (i + 1) & ((1 << ht->bits)-1);
208 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
211 static COLD bool double_table(struct htable *ht)
214 size_t oldnum = (size_t)1 << ht->bits;
215 uintptr_t *oldtable, e;
217 oldtable = ht->table;
218 ht->table = htable_alloc(ht, sizeof(size_t) << (ht->bits+1));
220 ht->table = oldtable;
225 /* If we lost our "perfect bit", get it back now. */
226 if (!ht->perfect_bit && ht->common_mask) {
227 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
228 if (ht->common_mask & ((size_t)1 << i)) {
229 ht->perfect_bit = (size_t)1 << i;
235 if (oldtable != &ht->perfect_bit) {
236 for (i = 0; i < oldnum; i++) {
237 if (entry_is_valid(e = oldtable[i])) {
238 void *p = get_raw_ptr(ht, e);
239 ht_add(ht, p, ht->rehash(p, ht->priv));
242 htable_free(ht, oldtable);
246 (void)htable_debug(ht, HTABLE_LOC);
250 static COLD void rehash_table(struct htable *ht)
255 /* Beware wrap cases: we need to start from first empty bucket. */
256 for (start = 0; ht->table[start]; start++);
258 for (i = 0; i < (size_t)1 << ht->bits; i++) {
259 size_t h = (i + start) & ((1 << ht->bits)-1);
263 if (e == HTABLE_DELETED)
265 else if (!(e & ht->perfect_bit)) {
266 void *p = get_raw_ptr(ht, e);
268 ht_add(ht, p, ht->rehash(p, ht->priv));
272 (void)htable_debug(ht, HTABLE_LOC);
275 /* We stole some bits, now we need to put them back... */
276 static COLD void update_common(struct htable *ht, const void *p)
279 uintptr_t maskdiff, bitsdiff;
281 if (ht->elems == 0) {
282 /* Always reveal one bit of the pointer in the bucket,
283 * so it's not zero or HTABLE_DELETED (1), even if
284 * hash happens to be 0. Assumes (void *)1 is not a
286 for (i = sizeof(uintptr_t)*CHAR_BIT - 1; i > 0; i--) {
287 if ((uintptr_t)p & ((uintptr_t)1 << i))
291 ht->common_mask = ~((uintptr_t)1 << i);
292 ht->common_bits = ((uintptr_t)p & ht->common_mask);
294 (void)htable_debug(ht, HTABLE_LOC);
298 /* Find bits which are unequal to old common set. */
299 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
301 /* These are the bits which go there in existing entries. */
302 bitsdiff = ht->common_bits & maskdiff;
304 for (i = 0; i < (size_t)1 << ht->bits; i++) {
305 if (!entry_is_valid(ht->table[i]))
307 /* Clear the bits no longer in the mask, set them as
309 ht->table[i] &= ~maskdiff;
310 ht->table[i] |= bitsdiff;
313 /* Take away those bits from our mask, bits and perfect bit. */
314 ht->common_mask &= ~maskdiff;
315 ht->common_bits &= ~maskdiff;
316 ht->perfect_bit &= ~maskdiff;
317 (void)htable_debug(ht, HTABLE_LOC);
320 bool htable_add_(struct htable *ht, size_t hash, const void *p)
322 if (ht->elems+1 > ht_max(ht) && !double_table(ht))
324 if (ht->elems+1 + ht->deleted > ht_max_with_deleted(ht))
327 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
328 update_common(ht, p);
335 bool htable_del_(struct htable *ht, size_t h, const void *p)
337 struct htable_iter i;
340 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
342 htable_delval(ht, &i);
349 void htable_delval_(struct htable *ht, struct htable_iter *i)
351 assert(i->off < (size_t)1 << ht->bits);
352 assert(entry_is_valid(ht->table[i->off]));
355 ht->table[i->off] = HTABLE_DELETED;
359 struct htable *htable_check(const struct htable *ht, const char *abortstr)
362 struct htable_iter i;
365 /* Use non-DEBUG versions here, to avoid infinite recursion with
366 * CCAN_HTABLE_DEBUG! */
367 for (p = htable_first_(ht, &i); p; p = htable_next_(ht, &i)) {
368 struct htable_iter i2;
370 size_t h = ht->rehash(p, ht->priv);
375 /* Open-code htable_get to avoid CCAN_HTABLE_DEBUG */
376 for (c = htable_firstval_(ht, &i2, h);
378 c = htable_nextval_(ht, &i2, h)) {
388 "%s: element %p in position %zu"
389 " cannot find itself\n",
396 if (n != ht->elems) {
399 "%s: found %zu elems, expected %zu\n",
400 abortstr, n, ht->elems);
406 return (struct htable *)ht;