• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 #include "nghttp2_helper.h"
32 
33 #define INITIAL_TABLE_LENGTH 256
34 
nghttp2_map_init(nghttp2_map * map,nghttp2_mem * mem)35 int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) {
36   map->mem = mem;
37   map->tablelen = INITIAL_TABLE_LENGTH;
38   map->table =
39       nghttp2_mem_calloc(mem, map->tablelen, sizeof(nghttp2_map_bucket));
40   if (map->table == NULL) {
41     return NGHTTP2_ERR_NOMEM;
42   }
43 
44   map->size = 0;
45 
46   return 0;
47 }
48 
nghttp2_map_free(nghttp2_map * map)49 void nghttp2_map_free(nghttp2_map *map) {
50   size_t i;
51   nghttp2_map_bucket *bkt;
52 
53   if (!map) {
54     return;
55   }
56 
57   for (i = 0; i < map->tablelen; ++i) {
58     bkt = &map->table[i];
59     if (bkt->ksl) {
60       nghttp2_ksl_free(bkt->ksl);
61       nghttp2_mem_free(map->mem, bkt->ksl);
62     }
63   }
64 
65   nghttp2_mem_free(map->mem, map->table);
66 }
67 
nghttp2_map_each_free(nghttp2_map * map,int (* func)(nghttp2_map_entry * entry,void * ptr),void * ptr)68 void nghttp2_map_each_free(nghttp2_map *map,
69                            int (*func)(nghttp2_map_entry *entry, void *ptr),
70                            void *ptr) {
71   uint32_t i;
72   nghttp2_map_bucket *bkt;
73   nghttp2_ksl_it it;
74 
75   for (i = 0; i < map->tablelen; ++i) {
76     bkt = &map->table[i];
77 
78     if (bkt->ptr) {
79       func(bkt->ptr, ptr);
80       bkt->ptr = NULL;
81       assert(bkt->ksl == NULL || nghttp2_ksl_len(bkt->ksl) == 0);
82       continue;
83     }
84 
85     if (bkt->ksl) {
86       for (it = nghttp2_ksl_begin(bkt->ksl); !nghttp2_ksl_it_end(&it);
87            nghttp2_ksl_it_next(&it)) {
88         func(nghttp2_ksl_it_get(&it), ptr);
89       }
90 
91       nghttp2_ksl_free(bkt->ksl);
92       nghttp2_mem_free(map->mem, bkt->ksl);
93       bkt->ksl = NULL;
94     }
95   }
96 }
97 
nghttp2_map_each(nghttp2_map * map,int (* func)(nghttp2_map_entry * entry,void * ptr),void * ptr)98 int nghttp2_map_each(nghttp2_map *map,
99                      int (*func)(nghttp2_map_entry *entry, void *ptr),
100                      void *ptr) {
101   int rv;
102   uint32_t i;
103   nghttp2_map_bucket *bkt;
104   nghttp2_ksl_it it;
105 
106   for (i = 0; i < map->tablelen; ++i) {
107     bkt = &map->table[i];
108 
109     if (bkt->ptr) {
110       rv = func(bkt->ptr, ptr);
111       if (rv != 0) {
112         return rv;
113       }
114       assert(bkt->ksl == NULL || nghttp2_ksl_len(bkt->ksl) == 0);
115       continue;
116     }
117 
118     if (bkt->ksl) {
119       for (it = nghttp2_ksl_begin(bkt->ksl); !nghttp2_ksl_it_end(&it);
120            nghttp2_ksl_it_next(&it)) {
121         rv = func(nghttp2_ksl_it_get(&it), ptr);
122         if (rv != 0) {
123           return rv;
124         }
125       }
126     }
127   }
128   return 0;
129 }
130 
nghttp2_map_entry_init(nghttp2_map_entry * entry,key_type key)131 void nghttp2_map_entry_init(nghttp2_map_entry *entry, key_type key) {
132   entry->key = key;
133   entry->next = NULL;
134 }
135 
136 /* FNV1a hash */
hash(key_type key,uint32_t mod)137 static uint32_t hash(key_type key, uint32_t mod) {
138   uint8_t *p, *end;
139   uint32_t h = 0x811C9DC5u;
140 
141   p = (uint8_t *)&key;
142   end = p + sizeof(key_type);
143 
144   for (; p != end;) {
145     h ^= *p++;
146     h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
147   }
148 
149   return h & (mod - 1);
150 }
151 
less(const nghttp2_ksl_key * lhs,const nghttp2_ksl_key * rhs)152 static int less(const nghttp2_ksl_key *lhs, const nghttp2_ksl_key *rhs) {
153   return *(key_type *)lhs < *(key_type *)rhs;
154 }
155 
map_insert(nghttp2_map * map,nghttp2_map_bucket * table,uint32_t tablelen,nghttp2_map_entry * entry)156 static int map_insert(nghttp2_map *map, nghttp2_map_bucket *table,
157                       uint32_t tablelen, nghttp2_map_entry *entry) {
158   uint32_t h = hash(entry->key, tablelen);
159   nghttp2_map_bucket *bkt = &table[h];
160   nghttp2_mem *mem = map->mem;
161   int rv;
162 
163   if (bkt->ptr == NULL &&
164       (bkt->ksl == NULL || nghttp2_ksl_len(bkt->ksl) == 0)) {
165     bkt->ptr = entry;
166     return 0;
167   }
168 
169   if (!bkt->ksl) {
170     bkt->ksl = nghttp2_mem_malloc(mem, sizeof(*bkt->ksl));
171     if (bkt->ksl == NULL) {
172       return NGHTTP2_ERR_NOMEM;
173     }
174     nghttp2_ksl_init(bkt->ksl, less, sizeof(key_type), mem);
175   }
176 
177   if (bkt->ptr) {
178     rv = nghttp2_ksl_insert(bkt->ksl, NULL, &bkt->ptr->key, bkt->ptr);
179     if (rv != 0) {
180       return rv;
181     }
182 
183     bkt->ptr = NULL;
184   }
185 
186   return nghttp2_ksl_insert(bkt->ksl, NULL, &entry->key, entry);
187 }
188 
189 /* new_tablelen must be power of 2 */
map_resize(nghttp2_map * map,uint32_t new_tablelen)190 static int map_resize(nghttp2_map *map, uint32_t new_tablelen) {
191   uint32_t i;
192   nghttp2_map_bucket *new_table;
193   nghttp2_map_bucket *bkt;
194   nghttp2_ksl_it it;
195   int rv;
196 
197   new_table =
198       nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_bucket));
199   if (new_table == NULL) {
200     return NGHTTP2_ERR_NOMEM;
201   }
202 
203   for (i = 0; i < map->tablelen; ++i) {
204     bkt = &map->table[i];
205 
206     if (bkt->ptr) {
207       rv = map_insert(map, new_table, new_tablelen, bkt->ptr);
208       if (rv != 0) {
209         goto fail;
210       }
211       assert(bkt->ksl == NULL || nghttp2_ksl_len(bkt->ksl) == 0);
212       continue;
213     }
214 
215     if (bkt->ksl) {
216       for (it = nghttp2_ksl_begin(bkt->ksl); !nghttp2_ksl_it_end(&it);
217            nghttp2_ksl_it_next(&it)) {
218         rv = map_insert(map, new_table, new_tablelen, nghttp2_ksl_it_get(&it));
219         if (rv != 0) {
220           goto fail;
221         }
222       }
223     }
224   }
225 
226   for (i = 0; i < map->tablelen; ++i) {
227     bkt = &map->table[i];
228     if (bkt->ksl) {
229       nghttp2_ksl_free(bkt->ksl);
230       nghttp2_mem_free(map->mem, bkt->ksl);
231     }
232   }
233 
234   nghttp2_mem_free(map->mem, map->table);
235   map->tablelen = new_tablelen;
236   map->table = new_table;
237 
238   return 0;
239 
240 fail:
241   for (i = 0; i < new_tablelen; ++i) {
242     bkt = &new_table[i];
243     if (bkt->ksl) {
244       nghttp2_ksl_free(bkt->ksl);
245       nghttp2_mem_free(map->mem, bkt->ksl);
246     }
247   }
248 
249   return rv;
250 }
251 
nghttp2_map_insert(nghttp2_map * map,nghttp2_map_entry * new_entry)252 int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *new_entry) {
253   int rv;
254 
255   /* Load factor is 0.75 */
256   if ((map->size + 1) * 4 > map->tablelen * 3) {
257     rv = map_resize(map, map->tablelen * 2);
258     if (rv != 0) {
259       return rv;
260     }
261   }
262   rv = map_insert(map, map->table, map->tablelen, new_entry);
263   if (rv != 0) {
264     return rv;
265   }
266   ++map->size;
267   return 0;
268 }
269 
nghttp2_map_find(nghttp2_map * map,key_type key)270 nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key) {
271   nghttp2_map_bucket *bkt = &map->table[hash(key, map->tablelen)];
272   nghttp2_ksl_it it;
273 
274   if (bkt->ptr) {
275     if (bkt->ptr->key == key) {
276       return bkt->ptr;
277     }
278     return NULL;
279   }
280 
281   if (bkt->ksl) {
282     it = nghttp2_ksl_lower_bound(bkt->ksl, &key);
283     if (nghttp2_ksl_it_end(&it) ||
284         *(key_type *)nghttp2_ksl_it_key(&it) != key) {
285       return NULL;
286     }
287     return nghttp2_ksl_it_get(&it);
288   }
289 
290   return NULL;
291 }
292 
nghttp2_map_remove(nghttp2_map * map,key_type key)293 int nghttp2_map_remove(nghttp2_map *map, key_type key) {
294   nghttp2_map_bucket *bkt = &map->table[hash(key, map->tablelen)];
295   int rv;
296 
297   if (bkt->ptr) {
298     if (bkt->ptr->key == key) {
299       bkt->ptr = NULL;
300       --map->size;
301       return 0;
302     }
303     return NGHTTP2_ERR_INVALID_ARGUMENT;
304   }
305 
306   if (bkt->ksl) {
307     rv = nghttp2_ksl_remove(bkt->ksl, NULL, &key);
308     if (rv != 0) {
309       return rv;
310     }
311     --map->size;
312     return 0;
313   }
314 
315   return NGHTTP2_ERR_INVALID_ARGUMENT;
316 }
317 
nghttp2_map_clear(nghttp2_map * map)318 void nghttp2_map_clear(nghttp2_map *map) {
319   uint32_t i;
320   nghttp2_map_bucket *bkt;
321 
322   for (i = 0; i < map->tablelen; ++i) {
323     bkt = &map->table[i];
324     bkt->ptr = NULL;
325     if (bkt->ksl) {
326       nghttp2_ksl_free(bkt->ksl);
327       nghttp2_mem_free(map->mem, bkt->ksl);
328       bkt->ksl = NULL;
329     }
330   }
331 
332   map->size = 0;
333 }
334 
nghttp2_map_size(nghttp2_map * map)335 size_t nghttp2_map_size(nghttp2_map *map) { return map->size; }
336