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 /* perfect_bitnum 63 means there's no perfect bitnum */
15 #define NO_PERFECT_BIT (sizeof(uintptr_t) * CHAR_BIT - 1)
17 static void *htable_default_alloc(struct htable *ht, size_t len)
19 return calloc(len, 1);
22 static void htable_default_free(struct htable *ht, void *p)
27 static void *(*htable_alloc)(struct htable *, size_t) = htable_default_alloc;
28 static void (*htable_free)(struct htable *, void *) = htable_default_free;
30 void htable_set_allocator(void *(*alloc)(struct htable *, size_t len),
31 void (*free)(struct htable *, void *p))
34 alloc = htable_default_alloc;
36 free = htable_default_free;
41 /* We clear out the bits which are always the same, and put metadata there. */
42 static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
45 return e & ht->common_mask;
48 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
50 return (void *)((e & ~ht->common_mask) | ht->common_bits);
53 static inline uintptr_t make_hval(const struct htable *ht,
54 const void *p, uintptr_t bits)
56 return ((uintptr_t)p & ~ht->common_mask) | bits;
59 static inline uintptr_t *actually_valid_pair(const struct htable *ht)
61 return ht->table + ((size_t)1 << ht->bits);
64 /* We have have two entries which look deleted, but we remember
66 static inline bool entry_actually_valid(const struct htable *ht, size_t off)
68 const uintptr_t *valid = actually_valid_pair(ht);
69 /* Empty table looks like this! */
70 if (valid == &ht->common_bits + 1)
72 return valid[0] == off || valid[1] == off;
75 /* Initialize the "actually valid" pair. */
76 static inline void init_actually_valid(struct htable *ht)
78 uintptr_t *valid = actually_valid_pair(ht);
79 valid[0] = valid[1] = ((size_t)1 << ht->bits);
82 /* Add to the "actually valid" pair: there can only ever be two! */
83 static COLD void add_actually_valid(struct htable *ht, size_t off)
85 uintptr_t *valid = actually_valid_pair(ht);
86 if (valid[0] == ((size_t)1 << ht->bits))
89 assert(valid[1] == ((size_t)1 << ht->bits));
94 static COLD void del_actually_valid(struct htable *ht, size_t off)
96 uintptr_t *validpair = actually_valid_pair(ht);
97 if (validpair[0] == off)
98 validpair[0] = ((size_t)1 << ht->bits);
100 assert(validpair[1] == off);
101 validpair[1] = ((size_t)1 << ht->bits);
105 /* If this entry looks invalid, check entry_actually_valid! */
106 static inline bool entry_looks_invalid(const struct htable *ht, size_t off)
108 return ht->table[off] <= HTABLE_DELETED;
111 static inline bool entry_is_valid(const struct htable *ht, size_t off)
113 if (!entry_looks_invalid(ht, off))
115 return entry_actually_valid(ht, off);
118 static inline bool entry_is_deleted(const struct htable *ht, size_t off)
120 return ht->table[off] == HTABLE_DELETED && !entry_actually_valid(ht, off);
123 static inline uintptr_t ht_perfect_mask(const struct htable *ht)
125 return (uintptr_t)2 << ht->perfect_bitnum;
128 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
131 /* Shuffling the extra bits (as specified in mask) down the
132 * end is quite expensive. But the lower bits are redundant, so
133 * we fold the value first. */
134 return (hash ^ (hash >> ht->bits))
135 & ht->common_mask & ~ht_perfect_mask(ht);
138 void htable_init(struct htable *ht,
139 size_t (*rehash)(const void *elem, void *priv), void *priv)
141 struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL);
145 ht->table = &ht->common_bits;
148 static inline size_t ht_max(const struct htable *ht)
150 return ((size_t)3 << ht->bits) / 4;
153 static inline size_t ht_max_with_deleted(const struct htable *ht)
155 return ((size_t)9 << ht->bits) / 10;
158 /* Includes the two trailing "not-deleted" entries */
159 static size_t htable_alloc_size(size_t bits)
161 return (sizeof(size_t) << bits) + 2 * sizeof(size_t);
164 bool htable_init_sized(struct htable *ht,
165 size_t (*rehash)(const void *, void *),
166 void *priv, size_t expect)
168 htable_init(ht, rehash, priv);
170 /* Don't go insane with sizing. */
171 for (ht->bits = 1; ((size_t)3 << ht->bits) / 4 < expect; ht->bits++) {
176 ht->table = htable_alloc(ht, htable_alloc_size(ht->bits));
178 ht->table = &ht->common_bits;
181 init_actually_valid(ht);
182 (void)htable_debug(ht, HTABLE_LOC);
186 void htable_clear(struct htable *ht)
188 if (ht->table != &ht->common_bits)
189 htable_free(ht, (void *)ht->table);
190 htable_init(ht, ht->rehash, ht->priv);
193 bool htable_copy_(struct htable *dst, const struct htable *src)
195 uintptr_t *htable = htable_alloc(dst, htable_alloc_size(src->bits));
202 memcpy(dst->table, src->table, htable_alloc_size(src->bits));
206 static size_t hash_bucket(const struct htable *ht, size_t h)
208 return h & ((1 << ht->bits)-1);
211 static void *htable_val(const struct htable *ht,
212 struct htable_iter *i, size_t hash, uintptr_t perfect)
214 uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
216 while (ht->table[i->off] || entry_actually_valid(ht, i->off)) {
217 if (!entry_is_deleted(ht, i->off)) {
218 if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
219 return get_raw_ptr(ht, ht->table[i->off]);
221 i->off = (i->off + 1) & ((1 << ht->bits)-1);
227 void *htable_firstval_(const struct htable *ht,
228 struct htable_iter *i, size_t hash)
230 i->off = hash_bucket(ht, hash);
231 return htable_val(ht, i, hash, ht_perfect_mask(ht));
234 void *htable_nextval_(const struct htable *ht,
235 struct htable_iter *i, size_t hash)
237 i->off = (i->off + 1) & ((1 << ht->bits)-1);
238 return htable_val(ht, i, hash, 0);
241 void *htable_first_(const struct htable *ht, struct htable_iter *i)
243 for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
244 if (entry_is_valid(ht, i->off))
245 return get_raw_ptr(ht, ht->table[i->off]);
250 void *htable_next_(const struct htable *ht, struct htable_iter *i)
252 for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
253 if (entry_is_valid(ht, i->off))
254 return get_raw_ptr(ht, ht->table[i->off]);
259 void *htable_prev_(const struct htable *ht, struct htable_iter *i)
265 if (entry_is_valid(ht, i->off))
266 return get_raw_ptr(ht, ht->table[i->off]);
270 /* This does not expand the hash table, that's up to caller. */
271 static void ht_add(struct htable *ht, const void *new, size_t h)
274 uintptr_t perfect = ht_perfect_mask(ht);
276 i = hash_bucket(ht, h);
278 while (entry_is_valid(ht, i)) {
280 i = (i + 1) & ((1 << ht->bits)-1);
282 ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
284 /* If it looks invalid, add it to exceptions */
285 if (ht->table[i] <= HTABLE_DELETED)
286 add_actually_valid(ht, i);
289 static COLD bool double_table(struct htable *ht)
292 struct htable oldht = *ht;
294 ht->table = htable_alloc(ht, htable_alloc_size(ht->bits+1));
296 ht->table = oldht.table;
300 init_actually_valid(ht);
302 /* If we lost our "perfect bit", get it back now. */
303 if (ht->perfect_bitnum == NO_PERFECT_BIT && ht->common_mask) {
304 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
305 if (ht->common_mask & ((size_t)2 << i)) {
306 ht->perfect_bitnum = i;
312 if (oldht.table != &ht->common_bits) {
313 for (i = 0; i < (size_t)1 << oldht.bits; i++) {
314 if (entry_is_valid(&oldht, i)) {
315 void *p = get_raw_ptr(&oldht, oldht.table[i]);
316 ht_add(ht, p, ht->rehash(p, ht->priv));
319 /* Pass ht here to callback: oldht is an internal figment */
320 htable_free(ht, oldht.table);
324 (void)htable_debug(ht, HTABLE_LOC);
328 static COLD void rehash_table(struct htable *ht)
331 uintptr_t e, perfect = ht_perfect_mask(ht);
332 uintptr_t *validpair = actually_valid_pair(ht);
334 /* Beware wrap cases: we need to start from first empty bucket. */
335 for (start = 0; ht->table[start]; start++);
337 for (i = 0; i < (size_t)1 << ht->bits; i++) {
338 size_t h = (i + start) & ((1 << ht->bits)-1);
339 uintptr_t *actually = NULL;
341 if (e <= HTABLE_DELETED) {
342 /* If it's actually valid, remember in case we move it! */
343 if (validpair[0] == h) {
344 actually = &validpair[0];
345 } else if (validpair[1] == h) {
346 actually = &validpair[1];
353 if (!(e & perfect)) {
354 void *p = get_raw_ptr(ht, e);
356 /* Clear actuallyvalid, let ht_add refill */
358 *actually = ((size_t)1 << ht->bits);
359 ht_add(ht, p, ht->rehash(p, ht->priv));
363 (void)htable_debug(ht, HTABLE_LOC);
366 /* We stole some bits, now we need to put them back... */
367 static COLD void update_common(struct htable *ht, const void *p)
370 uintptr_t maskdiff, bitsdiff;
372 if (ht->elems == 0) {
373 ht->common_mask = -1;
374 ht->common_bits = ((uintptr_t)p & ht->common_mask);
375 ht->perfect_bitnum = 0;
376 (void)htable_debug(ht, HTABLE_LOC);
380 /* Find bits which are unequal to old common set. */
381 maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
383 /* These are the bits which go there in existing entries. */
384 bitsdiff = ht->common_bits & maskdiff;
386 for (i = 0; i < (size_t)1 << ht->bits; i++) {
387 if (!entry_is_valid(ht, i))
389 /* Clear the bits no longer in the mask, set them as
391 ht->table[i] &= ~maskdiff;
392 ht->table[i] |= bitsdiff;
394 /* Make sure it's not newly falsely invalid */
395 if (ht->table[i] <= HTABLE_DELETED && !entry_actually_valid(ht, i))
396 add_actually_valid(ht, i);
399 /* Take away those bits from our mask, bits and perfect bit. */
400 ht->common_mask &= ~maskdiff;
401 ht->common_bits &= ~maskdiff;
402 if (ht_perfect_mask(ht) & maskdiff)
403 ht->perfect_bitnum = NO_PERFECT_BIT;
404 (void)htable_debug(ht, HTABLE_LOC);
407 bool htable_add_(struct htable *ht, size_t hash, const void *p)
409 if (ht->elems+1 > ht_max(ht) && !double_table(ht))
411 if (ht->elems+1 + ht->deleted > ht_max_with_deleted(ht))
414 if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
415 update_common(ht, p);
422 bool htable_del_(struct htable *ht, size_t h, const void *p)
424 struct htable_iter i;
427 for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
429 htable_delval(ht, &i);
436 void htable_delval_(struct htable *ht, struct htable_iter *i)
438 assert(i->off < (size_t)1 << ht->bits);
440 if (entry_looks_invalid(ht, i->off))
441 del_actually_valid(ht, i->off);
444 ht->table[i->off] = HTABLE_DELETED;
448 void *htable_pick_(const struct htable *ht, size_t seed, struct htable_iter *i)
451 struct htable_iter unwanted;
455 i->off = seed % ((size_t)1 << ht->bits);
456 e = htable_next(ht, i);
458 e = htable_first(ht, i);
462 struct htable *htable_check(const struct htable *ht, const char *abortstr)
465 struct htable_iter i;
467 const uintptr_t *validpair = actually_valid_pair(ht);
469 /* Use non-DEBUG versions here, to avoid infinite recursion with
470 * CCAN_HTABLE_DEBUG! */
471 for (p = htable_first_(ht, &i); p; p = htable_next_(ht, &i)) {
472 struct htable_iter i2;
474 size_t h = ht->rehash(p, ht->priv);
479 /* Open-code htable_get to avoid CCAN_HTABLE_DEBUG */
480 for (c = htable_firstval_(ht, &i2, h);
482 c = htable_nextval_(ht, &i2, h)) {
492 "%s: element %p in position %zu"
493 " cannot find itself\n",
500 if (n != ht->elems) {
503 "%s: found %zu elems, expected %zu\n",
504 abortstr, n, ht->elems);
510 /* Check validpair does actually override invalid-looking entries! */
511 if (ht->table != &ht->common_bits) {
513 for (i = 0; i < 2; i++) {
514 if (validpair[i] == ((size_t)1 << ht->bits))
516 if (validpair[i] > ((size_t)1 << ht->bits)) {
519 "%s: validpair[%zu] points at %zu"
520 " which is out of bounds\n",
521 abortstr, i, validpair[i]);
526 if (entry_looks_invalid(ht, validpair[i]))
531 "%s: validpair[%zu] points at %zu"
532 " which seems valid\n",
533 abortstr, i, validpair[i]);
540 return (struct htable *)ht;