2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
25 #include "nghttp2_map.h"
29 #define INITIAL_TABLE_LENGTH 256
31 int nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) {
33 map->tablelen = INITIAL_TABLE_LENGTH;
35 nghttp2_mem_calloc(mem, map->tablelen, sizeof(nghttp2_map_entry *));
36 if (map->table == NULL) {
37 return NGHTTP2_ERR_NOMEM;
45 void nghttp2_map_free(nghttp2_map *map) {
46 nghttp2_mem_free(map->mem, map->table);
49 void nghttp2_map_each_free(nghttp2_map *map,
50 int (*func)(nghttp2_map_entry *entry, void *ptr),
53 for (i = 0; i < map->tablelen; ++i) {
54 nghttp2_map_entry *entry;
55 for (entry = map->table[i]; entry;) {
56 nghttp2_map_entry *next = entry->next;
64 int nghttp2_map_each(nghttp2_map *map,
65 int (*func)(nghttp2_map_entry *entry, void *ptr),
69 for (i = 0; i < map->tablelen; ++i) {
70 nghttp2_map_entry *entry;
71 for (entry = map->table[i]; entry; entry = entry->next) {
72 rv = func(entry, ptr);
81 void nghttp2_map_entry_init(nghttp2_map_entry *entry, key_type key) {
86 /* Same hash function in android HashMap source code. */
87 /* The |mod| must be power of 2 */
88 static int32_t hash(int32_t h, size_t mod) {
89 h ^= (h >> 20) ^ (h >> 12);
90 h ^= (h >> 7) ^ (h >> 4);
94 static int insert(nghttp2_map_entry **table, size_t tablelen,
95 nghttp2_map_entry *entry) {
96 int32_t h = hash(entry->key, tablelen);
97 if (table[h] == NULL) {
100 nghttp2_map_entry *p;
101 /* We won't allow duplicated key, so check it out. */
102 for (p = table[h]; p; p = p->next) {
103 if (p->key == entry->key) {
104 return NGHTTP2_ERR_INVALID_ARGUMENT;
107 entry->next = table[h];
113 /* new_tablelen must be power of 2 */
114 static int resize(nghttp2_map *map, size_t new_tablelen) {
116 nghttp2_map_entry **new_table;
119 nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_entry *));
120 if (new_table == NULL) {
121 return NGHTTP2_ERR_NOMEM;
124 for (i = 0; i < map->tablelen; ++i) {
125 nghttp2_map_entry *entry;
126 for (entry = map->table[i]; entry;) {
127 nghttp2_map_entry *next = entry->next;
129 /* This function must succeed */
130 insert(new_table, new_tablelen, entry);
134 nghttp2_mem_free(map->mem, map->table);
135 map->tablelen = new_tablelen;
136 map->table = new_table;
141 int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_entry *new_entry) {
143 /* Load factor is 0.75 */
144 if ((map->size + 1) * 4 > map->tablelen * 3) {
145 rv = resize(map, map->tablelen * 2);
150 rv = insert(map->table, map->tablelen, new_entry);
158 nghttp2_map_entry *nghttp2_map_find(nghttp2_map *map, key_type key) {
160 nghttp2_map_entry *entry;
161 h = hash(key, map->tablelen);
162 for (entry = map->table[h]; entry; entry = entry->next) {
163 if (entry->key == key) {
170 int nghttp2_map_remove(nghttp2_map *map, key_type key) {
172 nghttp2_map_entry *entry, *prev;
173 h = hash(key, map->tablelen);
175 for (entry = map->table[h]; entry; entry = entry->next) {
176 if (entry->key == key) {
178 map->table[h] = entry->next;
180 prev->next = entry->next;
187 return NGHTTP2_ERR_INVALID_ARGUMENT;
190 size_t nghttp2_map_size(nghttp2_map *map) { return map->size; }