]> git.ozlabs.org Git - ccan/blob - ccan/htable/htable.c
io: typo fixes.
[ccan] / ccan / htable / htable.c
1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #include <ccan/htable/htable.h>
3 #include <ccan/compiler/compiler.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <limits.h>
7 #include <stdbool.h>
8 #include <assert.h>
9 #include <string.h>
10
11 /* We use 0x1 as deleted marker. */
12 #define HTABLE_DELETED (0x1)
13
14 static void *htable_default_alloc(struct htable *ht, size_t len)
15 {
16         return calloc(len, 1);
17 }
18
19 static void htable_default_free(struct htable *ht, void *p)
20 {
21         free(p);
22 }
23
24 static void *(*htable_alloc)(struct htable *, size_t) = htable_default_alloc;
25 static void (*htable_free)(struct htable *, void *) = htable_default_free;
26
27 void htable_set_allocator(void *(*alloc)(struct htable *, size_t len),
28                           void (*free)(struct htable *, void *p))
29 {
30         if (!alloc)
31                 alloc = htable_default_alloc;
32         if (!free)
33                 free = htable_default_free;
34         htable_alloc = alloc;
35         htable_free = free;
36 }
37
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,
40                                            uintptr_t e)
41 {
42         return e & ht->common_mask;
43 }
44
45 static inline void *get_raw_ptr(const struct htable *ht, uintptr_t e)
46 {
47         return (void *)((e & ~ht->common_mask) | ht->common_bits);
48 }
49
50 static inline uintptr_t make_hval(const struct htable *ht,
51                                   const void *p, uintptr_t bits)
52 {
53         return ((uintptr_t)p & ~ht->common_mask) | bits;
54 }
55
56 static inline bool entry_is_valid(uintptr_t e)
57 {
58         return e > HTABLE_DELETED;
59 }
60
61 static inline uintptr_t get_hash_ptr_bits(const struct htable *ht,
62                                           size_t hash)
63 {
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;
69 }
70
71 void htable_init(struct htable *ht,
72                  size_t (*rehash)(const void *elem, void *priv), void *priv)
73 {
74         struct htable empty = HTABLE_INITIALIZER(empty, NULL, NULL);
75         *ht = empty;
76         ht->rehash = rehash;
77         ht->priv = priv;
78         ht->table = &ht->perfect_bit;
79 }
80
81 /* We've changed ht->bits, update ht->max and ht->max_with_deleted */
82 static void htable_adjust_capacity(struct htable *ht)
83 {
84         ht->max = ((size_t)3 << ht->bits) / 4;
85         ht->max_with_deleted = ((size_t)9 << ht->bits) / 10;
86 }
87
88 bool htable_init_sized(struct htable *ht,
89                        size_t (*rehash)(const void *, void *),
90                        void *priv, size_t expect)
91 {
92         htable_init(ht, rehash, priv);
93
94         /* Don't go insane with sizing. */
95         for (ht->bits = 1; ((size_t)3 << ht->bits) / 4 < expect; ht->bits++) {
96                 if (ht->bits == 30)
97                         break;
98         }
99
100         ht->table = htable_alloc(ht, sizeof(size_t) << ht->bits);
101         if (!ht->table) {
102                 ht->table = &ht->perfect_bit;
103                 return false;
104         }
105         htable_adjust_capacity(ht);
106         (void)htable_debug(ht, HTABLE_LOC);
107         return true;
108 }
109         
110 void htable_clear(struct htable *ht)
111 {
112         if (ht->table != &ht->perfect_bit)
113                 htable_free(ht, (void *)ht->table);
114         htable_init(ht, ht->rehash, ht->priv);
115 }
116
117 bool htable_copy_(struct htable *dst, const struct htable *src)
118 {
119         uintptr_t *htable = htable_alloc(dst, sizeof(size_t) << src->bits);
120
121         if (!htable)
122                 return false;
123
124         *dst = *src;
125         dst->table = htable;
126         memcpy(dst->table, src->table, sizeof(size_t) << src->bits);
127         return true;
128 }
129
130 static size_t hash_bucket(const struct htable *ht, size_t h)
131 {
132         return h & ((1 << ht->bits)-1);
133 }
134
135 static void *htable_val(const struct htable *ht,
136                         struct htable_iter *i, size_t hash, uintptr_t perfect)
137 {
138         uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
139
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]);
144                 }
145                 i->off = (i->off + 1) & ((1 << ht->bits)-1);
146                 h2 &= ~perfect;
147         }
148         return NULL;
149 }
150
151 void *htable_firstval_(const struct htable *ht,
152                        struct htable_iter *i, size_t hash)
153 {
154         i->off = hash_bucket(ht, hash);
155         return htable_val(ht, i, hash, ht->perfect_bit);
156 }
157
158 void *htable_nextval_(const struct htable *ht,
159                       struct htable_iter *i, size_t hash)
160 {
161         i->off = (i->off + 1) & ((1 << ht->bits)-1);
162         return htable_val(ht, i, hash, 0);
163 }
164
165 void *htable_first_(const struct htable *ht, struct htable_iter *i)
166 {
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]);
170         }
171         return NULL;
172 }
173
174 void *htable_next_(const struct htable *ht, struct htable_iter *i)
175 {
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]);
179         }
180         return NULL;
181 }
182
183 void *htable_prev_(const struct htable *ht, struct htable_iter *i)
184 {
185         for (;;) {
186                 if (!i->off)
187                         return NULL;
188                 i->off --;
189                 if (entry_is_valid(ht->table[i->off]))
190                         return get_raw_ptr(ht, ht->table[i->off]);
191         }
192 }
193
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)
196 {
197         size_t i;
198         uintptr_t perfect = ht->perfect_bit;
199
200         i = hash_bucket(ht, h);
201
202         while (entry_is_valid(ht->table[i])) {
203                 perfect = 0;
204                 i = (i + 1) & ((1 << ht->bits)-1);
205         }
206         ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
207 }
208
209 static COLD bool double_table(struct htable *ht)
210 {
211         unsigned int i;
212         size_t oldnum = (size_t)1 << ht->bits;
213         uintptr_t *oldtable, e;
214
215         oldtable = ht->table;
216         ht->table = htable_alloc(ht, sizeof(size_t) << (ht->bits+1));
217         if (!ht->table) {
218                 ht->table = oldtable;
219                 return false;
220         }
221         ht->bits++;
222         htable_adjust_capacity(ht);
223
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;
229                                 break;
230                         }
231                 }
232         }
233
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));
239                         }
240                 }
241                 htable_free(ht, oldtable);
242         }
243         ht->deleted = 0;
244
245         (void)htable_debug(ht, HTABLE_LOC);
246         return true;
247 }
248
249 static COLD void rehash_table(struct htable *ht)
250 {
251         size_t start, i;
252         uintptr_t e;
253
254         /* Beware wrap cases: we need to start from first empty bucket. */
255         for (start = 0; ht->table[start]; start++);
256
257         for (i = 0; i < (size_t)1 << ht->bits; i++) {
258                 size_t h = (i + start) & ((1 << ht->bits)-1);
259                 e = ht->table[h];
260                 if (!e)
261                         continue;
262                 if (e == HTABLE_DELETED)
263                         ht->table[h] = 0;
264                 else if (!(e & ht->perfect_bit)) {
265                         void *p = get_raw_ptr(ht, e);
266                         ht->table[h] = 0;
267                         ht_add(ht, p, ht->rehash(p, ht->priv));
268                 }
269         }
270         ht->deleted = 0;
271         (void)htable_debug(ht, HTABLE_LOC);
272 }
273
274 /* We stole some bits, now we need to put them back... */
275 static COLD void update_common(struct htable *ht, const void *p)
276 {
277         unsigned int i;
278         uintptr_t maskdiff, bitsdiff;
279
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
284                  * valid pointer. */
285                 for (i = sizeof(uintptr_t)*CHAR_BIT - 1; i > 0; i--) {
286                         if ((uintptr_t)p & ((uintptr_t)1 << i))
287                                 break;
288                 }
289
290                 ht->common_mask = ~((uintptr_t)1 << i);
291                 ht->common_bits = ((uintptr_t)p & ht->common_mask);
292                 ht->perfect_bit = 1;
293                 (void)htable_debug(ht, HTABLE_LOC);
294                 return;
295         }
296
297         /* Find bits which are unequal to old common set. */
298         maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
299
300         /* These are the bits which go there in existing entries. */
301         bitsdiff = ht->common_bits & maskdiff;
302
303         for (i = 0; i < (size_t)1 << ht->bits; i++) {
304                 if (!entry_is_valid(ht->table[i]))
305                         continue;
306                 /* Clear the bits no longer in the mask, set them as
307                  * expected. */
308                 ht->table[i] &= ~maskdiff;
309                 ht->table[i] |= bitsdiff;
310         }
311
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);
317 }
318
319 bool htable_add_(struct htable *ht, size_t hash, const void *p)
320 {
321         if (ht->elems+1 > ht->max && !double_table(ht))
322                 return false;
323         if (ht->elems+1 + ht->deleted > ht->max_with_deleted)
324                 rehash_table(ht);
325         assert(p);
326         if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
327                 update_common(ht, p);
328
329         ht_add(ht, p, hash);
330         ht->elems++;
331         return true;
332 }
333
334 bool htable_del_(struct htable *ht, size_t h, const void *p)
335 {
336         struct htable_iter i;
337         void *c;
338
339         for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
340                 if (c == p) {
341                         htable_delval(ht, &i);
342                         return true;
343                 }
344         }
345         return false;
346 }
347
348 void htable_delval_(struct htable *ht, struct htable_iter *i)
349 {
350         assert(i->off < (size_t)1 << ht->bits);
351         assert(entry_is_valid(ht->table[i->off]));
352
353         ht->elems--;
354         ht->table[i->off] = HTABLE_DELETED;
355         ht->deleted++;
356 }
357
358 struct htable *htable_check(const struct htable *ht, const char *abortstr)
359 {
360         void *p;
361         struct htable_iter i;
362         size_t n = 0;
363
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;
368                 void *c;
369                 size_t h = ht->rehash(p, ht->priv);
370                 bool found = false;
371
372                 n++;
373
374                 /* Open-code htable_get to avoid CCAN_HTABLE_DEBUG */
375                 for (c = htable_firstval_(ht, &i2, h);
376                      c;
377                      c = htable_nextval_(ht, &i2, h)) {
378                         if (c == p) {
379                                 found = true;
380                                 break;
381                         }
382                 }
383
384                 if (!found) {
385                         if (abortstr) {
386                                 fprintf(stderr,
387                                         "%s: element %p in position %zu"
388                                         " cannot find itself\n",
389                                         abortstr, p, i.off);
390                                 abort();
391                         }
392                         return NULL;
393                 }
394         }
395         if (n != ht->elems) {
396                 if (abortstr) {
397                         fprintf(stderr,
398                                 "%s: found %zu elems, expected %zu\n",
399                                 abortstr, n, ht->elems);
400                         abort();
401                 }
402                 return NULL;
403         }
404
405         return (struct htable *)ht;
406 }