/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.15k | #define NGHTTP2_INITIAL_HASHBITS 4 |
35 | | |
36 | 11.7k | void nghttp2_map_init(nghttp2_map *map, uint32_t seed, nghttp2_mem *mem) { |
37 | 11.7k | map->mem = mem; |
38 | 11.7k | map->hashbits = 0; |
39 | 11.7k | map->table = NULL; |
40 | 11.7k | map->seed = seed; |
41 | 11.7k | map->size = 0; |
42 | 11.7k | } |
43 | | |
44 | 11.7k | void nghttp2_map_free(nghttp2_map *map) { |
45 | 11.7k | if (!map) { |
46 | 0 | return; |
47 | 0 | } |
48 | | |
49 | 11.7k | nghttp2_mem_free(map->mem, map->table); |
50 | 11.7k | } |
51 | | |
52 | | int nghttp2_map_each(const nghttp2_map *map, int (*func)(void *data, void *ptr), |
53 | 18.0k | void *ptr) { |
54 | 18.0k | int rv; |
55 | 18.0k | size_t i; |
56 | 18.0k | nghttp2_map_bucket *bkt; |
57 | 18.0k | size_t tablelen; |
58 | | |
59 | 18.0k | if (map->size == 0) { |
60 | 5.30k | return 0; |
61 | 5.30k | } |
62 | | |
63 | 12.7k | tablelen = 1u << map->hashbits; |
64 | | |
65 | 216k | for (i = 0; i < tablelen; ++i) { |
66 | 203k | bkt = &map->table[i]; |
67 | | |
68 | 203k | if (bkt->data == NULL) { |
69 | 188k | continue; |
70 | 188k | } |
71 | | |
72 | 14.5k | rv = func(bkt->data, ptr); |
73 | 14.5k | if (rv != 0) { |
74 | 1 | return rv; |
75 | 1 | } |
76 | 14.5k | } |
77 | | |
78 | 12.7k | return 0; |
79 | 12.7k | } |
80 | | |
81 | 73.0k | 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 | 73.0k | uint32_t h = ((uint32_t)key + map->seed) * 0x93d765dd; |
87 | 73.0k | return (size_t)((h * 2654435769u) >> (32 - map->hashbits)); |
88 | 73.0k | } |
89 | | |
90 | 102 | static void map_bucket_swap(nghttp2_map_bucket *a, nghttp2_map_bucket *b) { |
91 | 102 | nghttp2_map_bucket c = *a; |
92 | | |
93 | 102 | *a = *b; |
94 | 102 | *b = c; |
95 | 102 | } |
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 | 10.3k | static int map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) { |
126 | 10.3k | size_t idx = map_hash(map, key); |
127 | 10.3k | nghttp2_map_bucket b = { |
128 | 10.3k | .key = key, |
129 | 10.3k | .data = data, |
130 | 10.3k | }; |
131 | 10.3k | nghttp2_map_bucket *bkt; |
132 | 10.3k | size_t mask = (1u << map->hashbits) - 1; |
133 | | |
134 | 10.8k | for (;;) { |
135 | 10.8k | bkt = &map->table[idx]; |
136 | | |
137 | 10.8k | if (bkt->data == NULL) { |
138 | 10.3k | *bkt = b; |
139 | 10.3k | ++map->size; |
140 | 10.3k | return 0; |
141 | 10.3k | } |
142 | | |
143 | 550 | if (b.psl > bkt->psl) { |
144 | 102 | map_bucket_swap(bkt, &b); |
145 | 448 | } 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 | 550 | ++b.psl; |
154 | 550 | idx = (idx + 1) & mask; |
155 | 550 | } |
156 | 10.3k | } |
157 | | |
158 | 9.15k | static int map_resize(nghttp2_map *map, size_t new_hashbits) { |
159 | 9.15k | size_t i; |
160 | 9.15k | nghttp2_map_bucket *bkt; |
161 | 9.15k | size_t tablelen; |
162 | 9.15k | int rv; |
163 | 9.15k | nghttp2_map new_map = { |
164 | 9.15k | .table = nghttp2_mem_calloc(map->mem, 1u << new_hashbits, |
165 | 9.15k | sizeof(nghttp2_map_bucket)), |
166 | 9.15k | .mem = map->mem, |
167 | 9.15k | .seed = map->seed, |
168 | 9.15k | .hashbits = new_hashbits, |
169 | 9.15k | }; |
170 | 9.15k | (void)rv; |
171 | | |
172 | 9.15k | if (new_map.table == NULL) { |
173 | 0 | return NGHTTP2_ERR_NOMEM; |
174 | 0 | } |
175 | | |
176 | 9.15k | if (map->size) { |
177 | 0 | tablelen = 1u << map->hashbits; |
178 | |
|
179 | 0 | for (i = 0; i < tablelen; ++i) { |
180 | 0 | bkt = &map->table[i]; |
181 | 0 | if (bkt->data == NULL) { |
182 | 0 | continue; |
183 | 0 | } |
184 | | |
185 | 0 | rv = map_insert(&new_map, bkt->key, bkt->data); |
186 | |
|
187 | 0 | assert(0 == rv); |
188 | 0 | } |
189 | 0 | } |
190 | | |
191 | 9.15k | nghttp2_mem_free(map->mem, map->table); |
192 | 9.15k | map->table = new_map.table; |
193 | 9.15k | map->hashbits = new_hashbits; |
194 | | |
195 | 9.15k | return 0; |
196 | 9.15k | } |
197 | | |
198 | 10.3k | int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) { |
199 | 10.3k | int rv; |
200 | | |
201 | 10.3k | 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 | 10.3k | if ((map->size + 1) * 8 > (1u << map->hashbits) * 7) { |
207 | 9.15k | if (map->hashbits) { |
208 | 0 | rv = map_resize(map, map->hashbits + 1); |
209 | 0 | if (rv != 0) { |
210 | 0 | return rv; |
211 | 0 | } |
212 | 9.15k | } else { |
213 | 9.15k | rv = map_resize(map, NGHTTP2_INITIAL_HASHBITS); |
214 | 9.15k | if (rv != 0) { |
215 | 0 | return rv; |
216 | 0 | } |
217 | 9.15k | } |
218 | 9.15k | } |
219 | | |
220 | 10.3k | rv = map_insert(map, key, data); |
221 | 10.3k | if (rv != 0) { |
222 | 0 | return rv; |
223 | 0 | } |
224 | | |
225 | 10.3k | return 0; |
226 | 10.3k | } |
227 | | |
228 | 162k | void *nghttp2_map_find(const nghttp2_map *map, nghttp2_map_key_type key) { |
229 | 162k | size_t idx; |
230 | 162k | nghttp2_map_bucket *bkt; |
231 | 162k | size_t psl = 0; |
232 | 162k | size_t mask; |
233 | | |
234 | 162k | if (map->size == 0) { |
235 | 100k | return NULL; |
236 | 100k | } |
237 | | |
238 | 62.6k | idx = map_hash(map, key); |
239 | 62.6k | mask = (1u << map->hashbits) - 1; |
240 | | |
241 | 68.7k | for (;;) { |
242 | 68.7k | bkt = &map->table[idx]; |
243 | | |
244 | 68.7k | if (bkt->data == NULL || psl > bkt->psl) { |
245 | 28.4k | return NULL; |
246 | 28.4k | } |
247 | | |
248 | 40.2k | if (bkt->key == key) { |
249 | 34.1k | return bkt->data; |
250 | 34.1k | } |
251 | | |
252 | 6.06k | ++psl; |
253 | 6.06k | idx = (idx + 1) & mask; |
254 | 6.06k | } |
255 | 62.6k | } |
256 | | |
257 | 66 | int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) { |
258 | 66 | size_t idx; |
259 | 66 | nghttp2_map_bucket *b, *bkt; |
260 | 66 | size_t psl = 0; |
261 | 66 | size_t mask; |
262 | | |
263 | 66 | if (map->size == 0) { |
264 | 0 | return NGHTTP2_ERR_INVALID_ARGUMENT; |
265 | 0 | } |
266 | | |
267 | 66 | idx = map_hash(map, key); |
268 | 66 | mask = (1u << map->hashbits) - 1; |
269 | | |
270 | 108 | for (;;) { |
271 | 108 | bkt = &map->table[idx]; |
272 | | |
273 | 108 | if (bkt->data == NULL || psl > bkt->psl) { |
274 | 0 | return NGHTTP2_ERR_INVALID_ARGUMENT; |
275 | 0 | } |
276 | | |
277 | 108 | if (bkt->key == key) { |
278 | 66 | b = bkt; |
279 | 66 | idx = (idx + 1) & mask; |
280 | | |
281 | 95 | for (;;) { |
282 | 95 | bkt = &map->table[idx]; |
283 | 95 | if (bkt->data == NULL || bkt->psl == 0) { |
284 | 66 | b->data = NULL; |
285 | 66 | break; |
286 | 66 | } |
287 | | |
288 | 29 | --bkt->psl; |
289 | 29 | *b = *bkt; |
290 | 29 | b = bkt; |
291 | | |
292 | 29 | idx = (idx + 1) & mask; |
293 | 29 | } |
294 | | |
295 | 66 | --map->size; |
296 | | |
297 | 66 | return 0; |
298 | 66 | } |
299 | | |
300 | 42 | ++psl; |
301 | 42 | idx = (idx + 1) & mask; |
302 | 42 | } |
303 | 66 | } |
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 | 53.3k | size_t nghttp2_map_size(const nghttp2_map *map) { return map->size; } |