1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "nghttp2_map.h"
26 
27 #include <string.h>
28 
29 #define INITIAL_TABLE_LENGTH 256
30 
nghttp2_map_init(nghttp2_map * map,nghttp2_mem * mem)31 int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem)
32 {
33     map->mem = mem;
34     map->tablelen = INITIAL_TABLE_LENGTH;
35     map->table =
36         nghttp2_mem_calloc(mem, map->tablelen, sizeof(nghttp2_map_entry *));
37     if (map->table == NULL) {
38         return NGHTTP2_ERR_NOMEM;
39     }
40 
41     map->size = 0;
42 
43     return 0;
44 }
45 
nghttp2_map_free(nghttp2_map * map)46 void nghttp2_map_free(nghttp2_map *map)
47 {
48     nghttp2_mem_free(map->mem, map->table);
49 }
50 
nghttp2_map_each_free(nghttp2_map * map,int (* func)(nghttp2_map_entry * entry,void * ptr),void * ptr)51 void nghttp2_map_each_free(nghttp2_map *map,
52                            int (*func)(nghttp2_map_entry *entry, void *ptr),
53                            void *ptr)
54 {
55     uint32_t i;
56     for (i = 0; i < map->tablelen; ++i) {
57         nghttp2_map_entry *entry;
58         for (entry = map->table[i]; entry;) {
59             nghttp2_map_entry *next = entry->next;
60             func(entry, ptr);
61             entry = next;
62         }
63         map->table[i] = NULL;
64     }
65 }
66 
nghttp2_map_each(nghttp2_map * map,int (* func)(nghttp2_map_entry * entry,void * ptr),void * ptr)67 int nghttp2_map_each(nghttp2_map *map,
68                      int (*func)(nghttp2_map_entry *entry, void *ptr),
69                      void *ptr)
70 {
71     int rv;
72     uint32_t i;
73     for (i = 0; i < map->tablelen; ++i) {
74         nghttp2_map_entry *entry;
75         for (entry = map->table[i]; entry; entry = entry->next) {
76             rv = func(entry, ptr);
77             if (rv != 0) {
78                 return rv;
79             }
80         }
81     }
82     return 0;
83 }
84 
nghttp2_map_entry_init(nghttp2_map_entry * entry,key_type key)85 void nghttp2_map_entry_init(nghttp2_map_entry *entry, key_type key)
86 {
87     entry->key = key;
88     entry->next = NULL;
89 }
90 
91 /* Same hash function in android HashMap source code. */
92 /* The |mod| must be power of 2 */
hash(int32_t key,uint32_t mod)93 static uint32_t hash(int32_t key, uint32_t mod)
94 {
95     uint32_t h = (uint32_t)key;
96     h ^= (h >> 20) ^ (h >> 12);
97     h ^= (h >> 7) ^ (h >> 4);
98     return h & (mod - 1);
99 }
100 
insert(nghttp2_map_entry ** table,uint32_t tablelen,nghttp2_map_entry * entry)101 static int insert(nghttp2_map_entry **table, uint32_t tablelen,
102                   nghttp2_map_entry *entry)
103 {
104     uint32_t h = hash(entry->key, tablelen);
105     if (table[h] == NULL) {
106         table[h] = entry;
107     } else {
108         nghttp2_map_entry *p;
109         /* We won't allow duplicated key, so check it out. */
110         for (p = table[h]; p; p = p->next) {
111             if (p->key == entry->key) {
112                 return NGHTTP2_ERR_INVALID_ARGUMENT;
113             }
114         }
115         entry->next = table[h];
116         table[h] = entry;
117     }
118     return 0;
119 }
120 
121 /* new_tablelen must be power of 2 */
resize(nghttp2_map * map,uint32_t new_tablelen)122 static int resize(nghttp2_map *map, uint32_t new_tablelen)
123 {
124     uint32_t i;
125     nghttp2_map_entry **new_table;
126 
127     new_table =
128         nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_entry *));
129     if (new_table == NULL) {
130         return NGHTTP2_ERR_NOMEM;
131     }
132 
133     for (i = 0; i < map->tablelen; ++i) {
134         nghttp2_map_entry *entry;
135         for (entry = map->table[i]; entry;) {
136             nghttp2_map_entry *next = entry->next;
137             entry->next = NULL;
138             /* This function must succeed */
139             insert(new_table, new_tablelen, entry);
140             entry = next;
141         }
142     }
143     nghttp2_mem_free(map->mem, map->table);
144     map->tablelen = new_tablelen;
145     map->table = new_table;
146 
147     return 0;
148 }
149 
nghttp2_map_insert(nghttp2_map * map,nghttp2_map_entry * new_entry)150 int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *new_entry)
151 {
152     int rv;
153     /* Load factor is 0.75 */
154     if ((map->size + 1) * 4 > map->tablelen * 3) {
155         rv = resize(map, map->tablelen * 2);
156         if (rv != 0) {
157             return rv;
158         }
159     }
160     rv = insert(map->table, map->tablelen, new_entry);
161     if (rv != 0) {
162         return rv;
163     }
164     ++map->size;
165     return 0;
166 }
167 
nghttp2_map_find(nghttp2_map * map,key_type key)168 nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key)
169 {
170     uint32_t h;
171     nghttp2_map_entry *entry;
172     h = hash(key, map->tablelen);
173     for (entry = map->table[h]; entry; entry = entry->next) {
174         if (entry->key == key) {
175             return entry;
176         }
177     }
178     return NULL;
179 }
180 
nghttp2_map_remove(nghttp2_map * map,key_type key)181 int nghttp2_map_remove(nghttp2_map *map, key_type key)
182 {
183     uint32_t h;
184     nghttp2_map_entry **dst;
185 
186     h = hash(key, map->tablelen);
187 
188     for (dst = &map->table[h]; *dst; dst = &(*dst)->next) {
189         if ((*dst)->key != key) {
190             continue;
191         }
192 
193         *dst = (*dst)->next;
194         --map->size;
195         return 0;
196     }
197     return NGHTTP2_ERR_INVALID_ARGUMENT;
198 }
199 
nghttp2_map_size(nghttp2_map * map)200 size_t nghttp2_map_size(nghttp2_map *map)
201 {
202     return map->size;
203 }
204