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