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