]> git.ozlabs.org Git - ccan/blob - ccan/htable/htable.c
2028d336aec20bff59201c13074602dadd144ec7
[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 static inline size_t ht_max(const struct htable *ht)
82 {
83         return ((size_t)3 << ht->bits) / 4;
84 }
85
86 static inline size_t ht_max_with_deleted(const struct htable *ht)
87 {
88         return ((size_t)9 << ht->bits) / 10;
89 }
90
91 bool htable_init_sized(struct htable *ht,
92                        size_t (*rehash)(const void *, void *),
93                        void *priv, size_t expect)
94 {
95         htable_init(ht, rehash, priv);
96
97         /* Don't go insane with sizing. */
98         for (ht->bits = 1; ((size_t)3 << ht->bits) / 4 < expect; ht->bits++) {
99                 if (ht->bits == 30)
100                         break;
101         }
102
103         ht->table = htable_alloc(ht, sizeof(size_t) << ht->bits);
104         if (!ht->table) {
105                 ht->table = &ht->perfect_bit;
106                 return false;
107         }
108         (void)htable_debug(ht, HTABLE_LOC);
109         return true;
110 }
111         
112 void htable_clear(struct htable *ht)
113 {
114         if (ht->table != &ht->perfect_bit)
115                 htable_free(ht, (void *)ht->table);
116         htable_init(ht, ht->rehash, ht->priv);
117 }
118
119 bool htable_copy_(struct htable *dst, const struct htable *src)
120 {
121         uintptr_t *htable = htable_alloc(dst, sizeof(size_t) << src->bits);
122
123         if (!htable)
124                 return false;
125
126         *dst = *src;
127         dst->table = htable;
128         memcpy(dst->table, src->table, sizeof(size_t) << src->bits);
129         return true;
130 }
131
132 static size_t hash_bucket(const struct htable *ht, size_t h)
133 {
134         return h & ((1 << ht->bits)-1);
135 }
136
137 static void *htable_val(const struct htable *ht,
138                         struct htable_iter *i, size_t hash, uintptr_t perfect)
139 {
140         uintptr_t h2 = get_hash_ptr_bits(ht, hash) | perfect;
141
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]);
146                 }
147                 i->off = (i->off + 1) & ((1 << ht->bits)-1);
148                 h2 &= ~perfect;
149         }
150         return NULL;
151 }
152
153 void *htable_firstval_(const struct htable *ht,
154                        struct htable_iter *i, size_t hash)
155 {
156         i->off = hash_bucket(ht, hash);
157         return htable_val(ht, i, hash, ht->perfect_bit);
158 }
159
160 void *htable_nextval_(const struct htable *ht,
161                       struct htable_iter *i, size_t hash)
162 {
163         i->off = (i->off + 1) & ((1 << ht->bits)-1);
164         return htable_val(ht, i, hash, 0);
165 }
166
167 void *htable_first_(const struct htable *ht, struct htable_iter *i)
168 {
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]);
172         }
173         return NULL;
174 }
175
176 void *htable_next_(const struct htable *ht, struct htable_iter *i)
177 {
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]);
181         }
182         return NULL;
183 }
184
185 void *htable_prev_(const struct htable *ht, struct htable_iter *i)
186 {
187         for (;;) {
188                 if (!i->off)
189                         return NULL;
190                 i->off --;
191                 if (entry_is_valid(ht->table[i->off]))
192                         return get_raw_ptr(ht, ht->table[i->off]);
193         }
194 }
195
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)
198 {
199         size_t i;
200         uintptr_t perfect = ht->perfect_bit;
201
202         i = hash_bucket(ht, h);
203
204         while (entry_is_valid(ht->table[i])) {
205                 perfect = 0;
206                 i = (i + 1) & ((1 << ht->bits)-1);
207         }
208         ht->table[i] = make_hval(ht, new, get_hash_ptr_bits(ht, h)|perfect);
209 }
210
211 static COLD bool double_table(struct htable *ht)
212 {
213         unsigned int i;
214         size_t oldnum = (size_t)1 << ht->bits;
215         uintptr_t *oldtable, e;
216
217         oldtable = ht->table;
218         ht->table = htable_alloc(ht, sizeof(size_t) << (ht->bits+1));
219         if (!ht->table) {
220                 ht->table = oldtable;
221                 return false;
222         }
223         ht->bits++;
224
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;
230                                 break;
231                         }
232                 }
233         }
234
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));
240                         }
241                 }
242                 htable_free(ht, oldtable);
243         }
244         ht->deleted = 0;
245
246         (void)htable_debug(ht, HTABLE_LOC);
247         return true;
248 }
249
250 static COLD void rehash_table(struct htable *ht)
251 {
252         size_t start, i;
253         uintptr_t e;
254
255         /* Beware wrap cases: we need to start from first empty bucket. */
256         for (start = 0; ht->table[start]; start++);
257
258         for (i = 0; i < (size_t)1 << ht->bits; i++) {
259                 size_t h = (i + start) & ((1 << ht->bits)-1);
260                 e = ht->table[h];
261                 if (!e)
262                         continue;
263                 if (e == HTABLE_DELETED)
264                         ht->table[h] = 0;
265                 else if (!(e & ht->perfect_bit)) {
266                         void *p = get_raw_ptr(ht, e);
267                         ht->table[h] = 0;
268                         ht_add(ht, p, ht->rehash(p, ht->priv));
269                 }
270         }
271         ht->deleted = 0;
272         (void)htable_debug(ht, HTABLE_LOC);
273 }
274
275 /* We stole some bits, now we need to put them back... */
276 static COLD void update_common(struct htable *ht, const void *p)
277 {
278         unsigned int i;
279         uintptr_t maskdiff, bitsdiff;
280
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
285                  * valid pointer. */
286                 for (i = sizeof(uintptr_t)*CHAR_BIT - 1; i > 0; i--) {
287                         if ((uintptr_t)p & ((uintptr_t)1 << i))
288                                 break;
289                 }
290
291                 ht->common_mask = ~((uintptr_t)1 << i);
292                 ht->common_bits = ((uintptr_t)p & ht->common_mask);
293                 ht->perfect_bit = 1;
294                 (void)htable_debug(ht, HTABLE_LOC);
295                 return;
296         }
297
298         /* Find bits which are unequal to old common set. */
299         maskdiff = ht->common_bits ^ ((uintptr_t)p & ht->common_mask);
300
301         /* These are the bits which go there in existing entries. */
302         bitsdiff = ht->common_bits & maskdiff;
303
304         for (i = 0; i < (size_t)1 << ht->bits; i++) {
305                 if (!entry_is_valid(ht->table[i]))
306                         continue;
307                 /* Clear the bits no longer in the mask, set them as
308                  * expected. */
309                 ht->table[i] &= ~maskdiff;
310                 ht->table[i] |= bitsdiff;
311         }
312
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);
318 }
319
320 bool htable_add_(struct htable *ht, size_t hash, const void *p)
321 {
322         if (ht->elems+1 > ht_max(ht) && !double_table(ht))
323                 return false;
324         if (ht->elems+1 + ht->deleted > ht_max_with_deleted(ht))
325                 rehash_table(ht);
326         assert(p);
327         if (((uintptr_t)p & ht->common_mask) != ht->common_bits)
328                 update_common(ht, p);
329
330         ht_add(ht, p, hash);
331         ht->elems++;
332         return true;
333 }
334
335 bool htable_del_(struct htable *ht, size_t h, const void *p)
336 {
337         struct htable_iter i;
338         void *c;
339
340         for (c = htable_firstval(ht,&i,h); c; c = htable_nextval(ht,&i,h)) {
341                 if (c == p) {
342                         htable_delval(ht, &i);
343                         return true;
344                 }
345         }
346         return false;
347 }
348
349 void htable_delval_(struct htable *ht, struct htable_iter *i)
350 {
351         assert(i->off < (size_t)1 << ht->bits);
352         assert(entry_is_valid(ht->table[i->off]));
353
354         ht->elems--;
355         ht->table[i->off] = HTABLE_DELETED;
356         ht->deleted++;
357 }
358
359 struct htable *htable_check(const struct htable *ht, const char *abortstr)
360 {
361         void *p;
362         struct htable_iter i;
363         size_t n = 0;
364
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;
369                 void *c;
370                 size_t h = ht->rehash(p, ht->priv);
371                 bool found = false;
372
373                 n++;
374
375                 /* Open-code htable_get to avoid CCAN_HTABLE_DEBUG */
376                 for (c = htable_firstval_(ht, &i2, h);
377                      c;
378                      c = htable_nextval_(ht, &i2, h)) {
379                         if (c == p) {
380                                 found = true;
381                                 break;
382                         }
383                 }
384
385                 if (!found) {
386                         if (abortstr) {
387                                 fprintf(stderr,
388                                         "%s: element %p in position %zu"
389                                         " cannot find itself\n",
390                                         abortstr, p, i.off);
391                                 abort();
392                         }
393                         return NULL;
394                 }
395         }
396         if (n != ht->elems) {
397                 if (abortstr) {
398                         fprintf(stderr,
399                                 "%s: found %zu elems, expected %zu\n",
400                                 abortstr, n, ht->elems);
401                         abort();
402                 }
403                 return NULL;
404         }
405
406         return (struct htable *)ht;
407 }