]> git.ozlabs.org Git - ccan/blob - ccan/htable/htable.c
tdb: combine dead_space helper from summary.c and check.c
[ccan] / ccan / htable / htable.c
1 #include <ccan/htable/htable.h>
2 #include <ccan/compiler/compiler.h>
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <stdbool.h>
7 #include <assert.h>
8
9 /* This means a struct htable takes at least 512 bytes / 1k (32/64 bits). */
10 #define HTABLE_BASE_BITS 7
11
12 /* We use 0x1 as deleted marker. */
13 #define HTABLE_DELETED (0x1)
14
15 struct htable {
16         size_t (*rehash)(const void *elem, void *priv);
17         void *priv;
18         unsigned int bits;
19         size_t elems, deleted, max, max_with_deleted;
20         /* These are the bits which are the same in all pointers. */
21         uintptr_t common_mask, common_bits;
22         uintptr_t perfect_bit;
23         uintptr_t *table;
24 };
25
26 /* We clear out the bits which are always the same, and put metadata there. */
27 static inline uintptr_t get_extra_ptr_bits(const struct htable *ht,
28                                            uintptr_t e)
29 {
30         return e & ht->common_mask;
31 }
32
33 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
34 {
35         return (void *)((e & ~ht->common_mask) | ht->common_bits);
36 }
37
38 static inline uintptr_t make_hval(const struct htable *ht,
39                                   const void *p, uintptr_t bits)
40 {
41         return ((uintptr_t)p & ~ht->common_mask) | bits;
42 }
43
44 static inline bool entry_is_valid(uintptr_t e)
45 {
46         return e > HTABLE_DELETED;
47 }
48
49 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
50                                           size_t hash)
51 {
52         /* Shuffling the extra bits (as specified in mask) down the
53          * end is quite expensive.  But the lower bits are redundant, so
54          * we fold the value first. */
55         return (hash ^ (hash >> ht->bits))
56                 & ht->common_mask & ~ht->perfect_bit;
57 }
58
59 struct htable *htable_new(size_t (*rehash)(const void *elem, void *priv),
60                           void *priv)
61 {
62         struct htable *ht = malloc(sizeof(struct htable));
63         if (ht) {
64                 ht->bits = HTABLE_BASE_BITS;
65                 ht->rehash = rehash;
66                 ht->priv = priv;
67                 ht->elems = 0;
68                 ht->deleted = 0;
69                 ht->max = ((size_t)1 << ht->bits) * 3 / 4;
70                 ht->max_with_deleted = ((size_t)1 << ht->bits) * 9 / 10;
71                 /* This guarantees we enter update_common first add. */
72                 ht->common_mask = -1;
73                 ht->common_bits = 0;
74                 ht->perfect_bit = 0;
75                 ht->table = calloc(1 << ht->bits, sizeof(uintptr_t));
76                 if (!ht->table) {
77                         free(ht);
78                         ht = NULL;
79                 }
80         }
81         return ht;
82 }
83
84 void htable_free(const struct htable *ht)
85 {
86         free((void *)ht->table);
87         free((void *)ht);
88 }
89
90 static size_t hash_bucket(const struct htable *ht, size_t h)
91 {
92         return h & ((1 << ht->bits)-1);
93 }
94
95 static void *htable_val(const struct htable *ht,
96                         struct htable_iter *i, size_t hash, uintptr_t perfect)
97 {
98         uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
99
100         while (ht->table[i->off]) {
101                 if (ht->table[i->off] != HTABLE_DELETED) {
102                         if (get_extra_ptr_bits(ht, ht->table[i->off]) == h2)
103                                 return get_raw_ptr(ht, ht->table[i->off]);
104                 }
105                 i->off = (i->off + 1) & ((1 << ht->bits)-1);
106                 h2 &= ~perfect;
107         }
108         return NULL;
109 }
110
111 void *htable_firstval(const struct htable *ht,
112                       struct htable_iter *i, size_t hash)
113 {
114         i->off = hash_bucket(ht, hash);
115         return htable_val(ht, i, hash, ht->perfect_bit);
116 }
117
118 void *htable_nextval(const struct htable *ht,
119                      struct htable_iter *i, size_t hash)
120 {
121         i->off = (i->off + 1) & ((1 << ht->bits)-1);
122         return htable_val(ht, i, hash, 0);
123 }
124
125 void *htable_first(const struct htable *ht, struct htable_iter *i)
126 {
127         for (i->off = 0; i->off < (size_t)1 << ht->bits; i->off++) {
128                 if (entry_is_valid(ht->table[i->off]))
129                         return get_raw_ptr(ht, ht->table[i->off]);
130         }
131         return NULL;
132 }
133
134 void *htable_next(const struct htable *ht, struct htable_iter *i)
135 {
136         for (i->off++; i->off < (size_t)1 << ht->bits; i->off++) {
137                 if (entry_is_valid(ht->table[i->off]))
138                         return get_raw_ptr(ht, ht->table[i->off]);
139         }
140         return NULL;
141 }
142
143 /* This does not expand the hash table, that's up to caller. */
144 static void ht_add(struct htable *ht, const void *new, size_t h)
145 {
146         size_t i;
147         uintptr_t perfect = ht->perfect_bit;
148
149         i = hash_bucket(ht, h);
150
151         while (entry_is_valid(ht->table[i])) {
152                 perfect = 0;
153                 i = (i + 1) & ((1 << ht->bits)-1);
154         }
155         ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
156 }
157
158 static COLD bool double_table(struct htable *ht)
159 {
160         unsigned int i;
161         size_t oldnum = (size_t)1 << ht->bits;
162         uintptr_t *oldtable, e;
163
164         oldtable = ht->table;
165         ht->table = calloc(1 << (ht->bits+1), sizeof(size_t));
166         if (!ht->table) {
167                 ht->table = oldtable;
168                 return false;
169         }
170         ht->bits++;
171         ht->max *= 2;
172         ht->max_with_deleted *= 2;
173
174         /* If we lost our "perfect bit", get it back now. */
175         if (!ht->perfect_bit && ht->common_mask) {
176                 for (i = 0; i < sizeof(ht->common_mask) * CHAR_BIT; i++) {
177                         if (ht->common_mask & ((size_t)1 << i)) {
178                                 ht->perfect_bit = (size_t)1 << i;
179                                 break;
180                         }
181                 }
182         }
183
184         for (i = 0; i < oldnum; i++) {
185                 if (entry_is_valid(e = oldtable[i])) {
186                         void *p = get_raw_ptr(ht, e);
187                         ht_add(ht, p, ht->rehash(p, ht->priv));
188                 }
189         }
190         ht->deleted = 0;
191         free(oldtable);
192         return true;
193 }
194
195 static COLD void rehash_table(struct htable *ht)
196 {
197         size_t start, i;
198         uintptr_t e;
199
200         /* Beware wrap cases: we need to start from first empty bucket. */
201         for (start = 0; ht->table[start]; start++);
202
203         for (i = 0; i < (size_t)1 << ht->bits; i++) {
204                 size_t h = (i + start) & ((1 << ht->bits)-1);
205                 e = ht->table[h];
206                 if (!e)
207                         continue;
208                 if (e == HTABLE_DELETED)
209                         ht->table[h] = 0;
210                 else if (!(e & ht->perfect_bit)) {
211                         void *p = get_raw_ptr(ht, e);
212                         ht->table[h] = 0;
213                         ht_add(ht, p, ht->rehash(p, ht->priv));
214                 }
215         }
216         ht->deleted = 0;
217 }
218
219 /* We stole some bits, now we need to put them back... */
220 static COLD void update_common(struct htable *ht, const void *p)
221 {
222         unsigned int i;
223         uintptr_t maskdiff, bitsdiff;
224
225         if (ht->elems == 0) {
226                 ht->common_mask = -1;
227                 ht->common_bits = (uintptr_t)p;
228                 ht->perfect_bit = 1;
229                 return;
230         }
231
232         /* Find bits which are unequal to old common set. */
233         maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
234
235         /* These are the bits which go there in existing entries. */
236         bitsdiff = ht->common_bits & maskdiff;
237
238         for (i = 0; i < (size_t)1 << ht->bits; i++) {
239                 if (!entry_is_valid(ht->table[i]))
240                         continue;
241                 /* Clear the bits no longer in the mask, set them as
242                  * expected. */
243                 ht->table[i] &= ~maskdiff;
244                 ht->table[i] |= bitsdiff;
245         }
246
247         /* Take away those bits from our mask, bits and perfect bit. */
248         ht->common_mask &= ~maskdiff;
249         ht->common_bits &= ~maskdiff;
250         ht->perfect_bit &= ~maskdiff;
251 }
252
253 bool htable_add(struct htable *ht, size_t hash, const void *p)
254 {
255         if (ht->elems+1 > ht->max && !double_table(ht))
256                 return false;
257         if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
258                 rehash_table(ht);
259         assert(p);
260         if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
261                 update_common(ht, p);
262
263         ht_add(ht, p, hash);
264         ht->elems++;
265         return true;
266 }
267
268 bool htable_del(struct htable *ht, size_t h, const void *p)
269 {
270         struct htable_iter i;
271         void *c;
272
273         for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
274                 if (c == p) {
275                         htable_delval(ht, &i);
276                         return true;
277                 }
278         }
279         return false;
280 }
281
282 void htable_delval(struct htable *ht, struct htable_iter *i)
283 {
284         assert(i->off < (size_t)1 << ht->bits);
285         assert(entry_is_valid(ht->table[i->off]));
286
287         ht->elems--;
288         ht->table[i->off] = HTABLE_DELETED;
289         ht->deleted++;
290 }