/src/nghttp2/lib/nghttp2_map.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * nghttp2 - HTTP/2 C Library |
3 | | * |
4 | | * Copyright (c) 2017 ngtcp2 contributors |
5 | | * Copyright (c) 2012 nghttp2 contributors |
6 | | * |
7 | | * Permission is hereby granted, free of charge, to any person obtaining |
8 | | * a copy of this software and associated documentation files (the |
9 | | * "Software"), to deal in the Software without restriction, including |
10 | | * without limitation the rights to use, copy, modify, merge, publish, |
11 | | * distribute, sublicense, and/or sell copies of the Software, and to |
12 | | * permit persons to whom the Software is furnished to do so, subject to |
13 | | * the following conditions: |
14 | | * |
15 | | * The above copyright notice and this permission notice shall be |
16 | | * included in all copies or substantial portions of the Software. |
17 | | * |
18 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
19 | | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
20 | | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
21 | | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
22 | | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
23 | | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
24 | | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
25 | | */ |
26 | | #include "nghttp2_map.h" |
27 | | |
28 | | #include <string.h> |
29 | | #include <assert.h> |
30 | | #include <stdio.h> |
31 | | |
32 | | #include "nghttp2_helper.h" |
33 | | |
34 | 9.86k | #define NGHTTP2_INITIAL_HASHBITS 4 |
35 | | |
36 | 11.5k | void nghttp2_map_init(nghttp2_map *map, uint32_t seed, nghttp2_mem *mem) { |
37 | 11.5k | map->mem = mem; |
38 | 11.5k | map->hashbits = 0; |
39 | 11.5k | map->table = NULL; |
40 | 11.5k | map->seed = seed; |
41 | 11.5k | map->size = 0; |
42 | 11.5k | } |
43 | | |
44 | 11.5k | void nghttp2_map_free(nghttp2_map *map) { |
45 | 11.5k | if (!map) { |
46 | 0 | return; |
47 | 0 | } |
48 | | |
49 | 11.5k | nghttp2_mem_free(map->mem, map->table); |
50 | 11.5k | } |
51 | | |
52 | | int nghttp2_map_each(const nghttp2_map *map, int (*func)(void *data, void *ptr), |
53 | 14.2k | void *ptr) { |
54 | 14.2k | int rv; |
55 | 14.2k | size_t i; |
56 | 14.2k | nghttp2_map_bucket *bkt; |
57 | 14.2k | size_t tablelen; |
58 | | |
59 | 14.2k | if (map->size == 0) { |
60 | 6.90k | return 0; |
61 | 6.90k | } |
62 | | |
63 | 7.35k | tablelen = 1u << map->hashbits; |
64 | | |
65 | 129k | for (i = 0; i < tablelen; ++i) { |
66 | 121k | bkt = &map->table[i]; |
67 | | |
68 | 121k | if (bkt->data == NULL) { |
69 | 111k | continue; |
70 | 111k | } |
71 | | |
72 | 10.5k | rv = func(bkt->data, ptr); |
73 | 10.5k | if (rv != 0) { |
74 | 0 | return rv; |
75 | 0 | } |
76 | 10.5k | } |
77 | | |
78 | 7.35k | return 0; |
79 | 7.35k | } |
80 | | |
81 | 240k | static size_t map_hash(const nghttp2_map *map, nghttp2_map_key_type key) { |
82 | | /* hasher from |
83 | | https://github.com/rust-lang/rustc-hash/blob/dc5c33f1283de2da64d8d7a06401d91aded03ad4/src/lib.rs |
84 | | We do not perform finalization here because we use top bits |
85 | | anyway. */ |
86 | 240k | uint32_t h = ((uint32_t)key + map->seed) * 0x93d765dd; |
87 | 240k | return (size_t)((h * 2654435769u) >> (32 - map->hashbits)); |
88 | 240k | } |
89 | | |
90 | 9.96k | static void map_bucket_swap(nghttp2_map_bucket *a, nghttp2_map_bucket *b) { |
91 | 9.96k | nghttp2_map_bucket c = *a; |
92 | | |
93 | 9.96k | *a = *b; |
94 | 9.96k | *b = c; |
95 | 9.96k | } |
96 | | |
97 | | #ifndef WIN32 |
98 | 0 | void nghttp2_map_print_distance(const nghttp2_map *map) { |
99 | 0 | size_t i; |
100 | 0 | size_t idx; |
101 | 0 | nghttp2_map_bucket *bkt; |
102 | 0 | size_t tablelen; |
103 | |
|
104 | 0 | if (map->size == 0) { |
105 | 0 | return; |
106 | 0 | } |
107 | | |
108 | 0 | tablelen = 1u << map->hashbits; |
109 | |
|
110 | 0 | for (i = 0; i < tablelen; ++i) { |
111 | 0 | bkt = &map->table[i]; |
112 | |
|
113 | 0 | if (bkt->data == NULL) { |
114 | 0 | fprintf(stderr, "@%zu <EMPTY>\n", i); |
115 | 0 | continue; |
116 | 0 | } |
117 | | |
118 | 0 | idx = map_hash(map, bkt->key); |
119 | 0 | fprintf(stderr, "@%zu hash=%zu key=%d base=%zu distance=%u\n", i, |
120 | 0 | map_hash(map, bkt->key), bkt->key, idx, bkt->psl); |
121 | 0 | } |
122 | 0 | } |
123 | | #endif /* !defined(WIN32) */ |
124 | | |
125 | 46.0k | static int map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) { |
126 | 46.0k | size_t idx = map_hash(map, key); |
127 | 46.0k | nghttp2_map_bucket b = { |
128 | 46.0k | .key = key, |
129 | 46.0k | .data = data, |
130 | 46.0k | }; |
131 | 46.0k | nghttp2_map_bucket *bkt; |
132 | 46.0k | size_t mask = (1u << map->hashbits) - 1; |
133 | | |
134 | 73.1k | for (;;) { |
135 | 73.1k | bkt = &map->table[idx]; |
136 | | |
137 | 73.1k | if (bkt->data == NULL) { |
138 | 46.0k | *bkt = b; |
139 | 46.0k | ++map->size; |
140 | 46.0k | return 0; |
141 | 46.0k | } |
142 | | |
143 | 27.1k | if (b.psl > bkt->psl) { |
144 | 9.96k | map_bucket_swap(bkt, &b); |
145 | 17.1k | } else if (bkt->key == key) { |
146 | | /* TODO This check is just a waste after first swap or if this |
147 | | function is called from map_resize. That said, there is no |
148 | | difference with or without this conditional in performance |
149 | | wise. */ |
150 | 0 | return NGHTTP2_ERR_INVALID_ARGUMENT; |
151 | 0 | } |
152 | | |
153 | 27.1k | ++b.psl; |
154 | 27.1k | idx = (idx + 1) & mask; |
155 | 27.1k | } |
156 | 46.0k | } |
157 | | |
158 | 10.7k | static int map_resize(nghttp2_map *map, size_t new_hashbits) { |
159 | 10.7k | size_t i; |
160 | 10.7k | nghttp2_map_bucket *bkt; |
161 | 10.7k | size_t tablelen; |
162 | 10.7k | int rv; |
163 | 10.7k | nghttp2_map new_map = { |
164 | 10.7k | .table = nghttp2_mem_calloc(map->mem, 1u << new_hashbits, |
165 | 10.7k | sizeof(nghttp2_map_bucket)), |
166 | 10.7k | .mem = map->mem, |
167 | 10.7k | .seed = map->seed, |
168 | 10.7k | .hashbits = new_hashbits, |
169 | 10.7k | }; |
170 | 10.7k | (void)rv; |
171 | | |
172 | 10.7k | if (new_map.table == NULL) { |
173 | 0 | return NGHTTP2_ERR_NOMEM; |
174 | 0 | } |
175 | | |
176 | 10.7k | if (map->size) { |
177 | 926 | tablelen = 1u << map->hashbits; |
178 | | |
179 | 20.8k | for (i = 0; i < tablelen; ++i) { |
180 | 19.9k | bkt = &map->table[i]; |
181 | 19.9k | if (bkt->data == NULL) { |
182 | 2.48k | continue; |
183 | 2.48k | } |
184 | | |
185 | 17.4k | rv = map_insert(&new_map, bkt->key, bkt->data); |
186 | | |
187 | 17.4k | assert(0 == rv); |
188 | 17.4k | } |
189 | 926 | } |
190 | | |
191 | 10.7k | nghttp2_mem_free(map->mem, map->table); |
192 | 10.7k | map->table = new_map.table; |
193 | 10.7k | map->hashbits = new_hashbits; |
194 | | |
195 | 10.7k | return 0; |
196 | 10.7k | } |
197 | | |
198 | 28.6k | int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) { |
199 | 28.6k | int rv; |
200 | | |
201 | 28.6k | assert(data); |
202 | | |
203 | | /* Load factor is 7/8 */ |
204 | | /* Under the very initial condition, that is map->size == 0 and |
205 | | map->hashbits == 0, 8 > 7 still holds nicely. */ |
206 | 28.6k | if ((map->size + 1) * 8 > (1u << map->hashbits) * 7) { |
207 | 10.7k | if (map->hashbits) { |
208 | 926 | rv = map_resize(map, map->hashbits + 1); |
209 | 926 | if (rv != 0) { |
210 | 0 | return rv; |
211 | 0 | } |
212 | 9.86k | } else { |
213 | 9.86k | rv = map_resize(map, NGHTTP2_INITIAL_HASHBITS); |
214 | 9.86k | if (rv != 0) { |
215 | 0 | return rv; |
216 | 0 | } |
217 | 9.86k | } |
218 | 10.7k | } |
219 | | |
220 | 28.6k | rv = map_insert(map, key, data); |
221 | 28.6k | if (rv != 0) { |
222 | 0 | return rv; |
223 | 0 | } |
224 | | |
225 | 28.6k | return 0; |
226 | 28.6k | } |
227 | | |
228 | 218k | void *nghttp2_map_find(const nghttp2_map *map, nghttp2_map_key_type key) { |
229 | 218k | size_t idx; |
230 | 218k | nghttp2_map_bucket *bkt; |
231 | 218k | size_t psl = 0; |
232 | 218k | size_t mask; |
233 | | |
234 | 218k | if (map->size == 0) { |
235 | 45.6k | return NULL; |
236 | 45.6k | } |
237 | | |
238 | 173k | idx = map_hash(map, key); |
239 | 173k | mask = (1u << map->hashbits) - 1; |
240 | | |
241 | 243k | for (;;) { |
242 | 243k | bkt = &map->table[idx]; |
243 | | |
244 | 243k | if (bkt->data == NULL || psl > bkt->psl) { |
245 | 42.4k | return NULL; |
246 | 42.4k | } |
247 | | |
248 | 201k | if (bkt->key == key) { |
249 | 130k | return bkt->data; |
250 | 130k | } |
251 | | |
252 | 70.8k | ++psl; |
253 | 70.8k | idx = (idx + 1) & mask; |
254 | 70.8k | } |
255 | 173k | } |
256 | | |
257 | 21.4k | int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) { |
258 | 21.4k | size_t idx; |
259 | 21.4k | nghttp2_map_bucket *b, *bkt; |
260 | 21.4k | size_t psl = 0; |
261 | 21.4k | size_t mask; |
262 | | |
263 | 21.4k | if (map->size == 0) { |
264 | 0 | return NGHTTP2_ERR_INVALID_ARGUMENT; |
265 | 0 | } |
266 | | |
267 | 21.4k | idx = map_hash(map, key); |
268 | 21.4k | mask = (1u << map->hashbits) - 1; |
269 | | |
270 | 23.7k | for (;;) { |
271 | 23.7k | bkt = &map->table[idx]; |
272 | | |
273 | 23.7k | if (bkt->data == NULL || psl > bkt->psl) { |
274 | 0 | return NGHTTP2_ERR_INVALID_ARGUMENT; |
275 | 0 | } |
276 | | |
277 | 23.7k | if (bkt->key == key) { |
278 | 21.4k | b = bkt; |
279 | 21.4k | idx = (idx + 1) & mask; |
280 | | |
281 | 27.0k | for (;;) { |
282 | 27.0k | bkt = &map->table[idx]; |
283 | 27.0k | if (bkt->data == NULL || bkt->psl == 0) { |
284 | 21.4k | b->data = NULL; |
285 | 21.4k | break; |
286 | 21.4k | } |
287 | | |
288 | 5.51k | --bkt->psl; |
289 | 5.51k | *b = *bkt; |
290 | 5.51k | b = bkt; |
291 | | |
292 | 5.51k | idx = (idx + 1) & mask; |
293 | 5.51k | } |
294 | | |
295 | 21.4k | --map->size; |
296 | | |
297 | 21.4k | return 0; |
298 | 21.4k | } |
299 | | |
300 | 2.30k | ++psl; |
301 | 2.30k | idx = (idx + 1) & mask; |
302 | 2.30k | } |
303 | 21.4k | } |
304 | | |
305 | 0 | void nghttp2_map_clear(nghttp2_map *map) { |
306 | 0 | if (map->size == 0) { |
307 | 0 | return; |
308 | 0 | } |
309 | | |
310 | 0 | memset(map->table, 0, sizeof(*map->table) * (1u << map->hashbits)); |
311 | 0 | map->size = 0; |
312 | 0 | } |
313 | | |
314 | 58.4k | size_t nghttp2_map_size(const nghttp2_map *map) { return map->size; } |