nghttp2_map.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. #include <string.h>
  28. #include <assert.h>
  29. #include <stdio.h>
  30. #include "nghttp2_helper.h"
  31. #define NGHTTP2_INITIAL_TABLE_LENBITS 4
  32. void nghttp2_map_init(nghttp2_map *map, nghttp2_mem *mem) {
  33. map->mem = mem;
  34. map->tablelen = 0;
  35. map->tablelenbits = 0;
  36. map->table = NULL;
  37. map->size = 0;
  38. }
  39. void nghttp2_map_free(nghttp2_map *map) {
  40. if (!map) {
  41. return;
  42. }
  43. nghttp2_mem_free(map->mem, map->table);
  44. }
  45. void nghttp2_map_each_free(nghttp2_map *map, int (*func)(void *data, void *ptr),
  46. void *ptr) {
  47. uint32_t i;
  48. nghttp2_map_bucket *bkt;
  49. for (i = 0; i < map->tablelen; ++i) {
  50. bkt = &map->table[i];
  51. if (bkt->data == NULL) {
  52. continue;
  53. }
  54. func(bkt->data, ptr);
  55. }
  56. }
  57. int nghttp2_map_each(nghttp2_map *map, int (*func)(void *data, void *ptr),
  58. void *ptr) {
  59. int rv;
  60. uint32_t i;
  61. nghttp2_map_bucket *bkt;
  62. if (map->size == 0) {
  63. return 0;
  64. }
  65. for (i = 0; i < map->tablelen; ++i) {
  66. bkt = &map->table[i];
  67. if (bkt->data == NULL) {
  68. continue;
  69. }
  70. rv = func(bkt->data, ptr);
  71. if (rv != 0) {
  72. return rv;
  73. }
  74. }
  75. return 0;
  76. }
  77. static uint32_t hash(nghttp2_map_key_type key) {
  78. return (uint32_t)key * 2654435769u;
  79. }
  80. static size_t h2idx(uint32_t hash, uint32_t bits) {
  81. return hash >> (32 - bits);
  82. }
  83. static size_t distance(uint32_t tablelen, uint32_t tablelenbits,
  84. nghttp2_map_bucket *bkt, size_t idx) {
  85. return (idx - h2idx(bkt->hash, tablelenbits)) & (tablelen - 1);
  86. }
  87. static void map_bucket_swap(nghttp2_map_bucket *bkt, uint32_t *phash,
  88. nghttp2_map_key_type *pkey, void **pdata) {
  89. uint32_t h = bkt->hash;
  90. nghttp2_map_key_type key = bkt->key;
  91. void *data = bkt->data;
  92. bkt->hash = *phash;
  93. bkt->key = *pkey;
  94. bkt->data = *pdata;
  95. *phash = h;
  96. *pkey = key;
  97. *pdata = data;
  98. }
  99. static void map_bucket_set_data(nghttp2_map_bucket *bkt, uint32_t hash,
  100. nghttp2_map_key_type key, void *data) {
  101. bkt->hash = hash;
  102. bkt->key = key;
  103. bkt->data = data;
  104. }
  105. #ifndef WIN32
  106. void nghttp2_map_print_distance(nghttp2_map *map) {
  107. uint32_t i;
  108. size_t idx;
  109. nghttp2_map_bucket *bkt;
  110. for (i = 0; i < map->tablelen; ++i) {
  111. bkt = &map->table[i];
  112. if (bkt->data == NULL) {
  113. fprintf(stderr, "@%u <EMPTY>\n", i);
  114. continue;
  115. }
  116. idx = h2idx(bkt->hash, map->tablelenbits);
  117. fprintf(stderr, "@%u hash=%08x key=%d base=%zu distance=%zu\n", i,
  118. bkt->hash, bkt->key, idx,
  119. distance(map->tablelen, map->tablelenbits, bkt, idx));
  120. }
  121. }
  122. #endif /* !WIN32 */
  123. static int insert(nghttp2_map_bucket *table, uint32_t tablelen,
  124. uint32_t tablelenbits, uint32_t hash,
  125. nghttp2_map_key_type key, void *data) {
  126. size_t idx = h2idx(hash, tablelenbits);
  127. size_t d = 0, dd;
  128. nghttp2_map_bucket *bkt;
  129. for (;;) {
  130. bkt = &table[idx];
  131. if (bkt->data == NULL) {
  132. map_bucket_set_data(bkt, hash, key, data);
  133. return 0;
  134. }
  135. dd = distance(tablelen, tablelenbits, bkt, idx);
  136. if (d > dd) {
  137. map_bucket_swap(bkt, &hash, &key, &data);
  138. d = dd;
  139. } else if (bkt->key == key) {
  140. /* TODO This check is just a waste after first swap or if this
  141. function is called from map_resize. That said, there is no
  142. difference with or without this conditional in performance
  143. wise. */
  144. return NGHTTP2_ERR_INVALID_ARGUMENT;
  145. }
  146. ++d;
  147. idx = (idx + 1) & (tablelen - 1);
  148. }
  149. }
  150. /* new_tablelen must be power of 2 and new_tablelen == (1 <<
  151. new_tablelenbits) must hold. */
  152. static int map_resize(nghttp2_map *map, uint32_t new_tablelen,
  153. uint32_t new_tablelenbits) {
  154. uint32_t i;
  155. nghttp2_map_bucket *new_table;
  156. nghttp2_map_bucket *bkt;
  157. int rv;
  158. (void)rv;
  159. new_table =
  160. nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_bucket));
  161. if (new_table == NULL) {
  162. return NGHTTP2_ERR_NOMEM;
  163. }
  164. for (i = 0; i < map->tablelen; ++i) {
  165. bkt = &map->table[i];
  166. if (bkt->data == NULL) {
  167. continue;
  168. }
  169. rv = insert(new_table, new_tablelen, new_tablelenbits, bkt->hash, bkt->key,
  170. bkt->data);
  171. assert(0 == rv);
  172. }
  173. nghttp2_mem_free(map->mem, map->table);
  174. map->tablelen = new_tablelen;
  175. map->tablelenbits = new_tablelenbits;
  176. map->table = new_table;
  177. return 0;
  178. }
  179. int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
  180. int rv;
  181. assert(data);
  182. /* Load factor is 0.75 */
  183. if ((map->size + 1) * 4 > map->tablelen * 3) {
  184. if (map->tablelen) {
  185. rv = map_resize(map, map->tablelen * 2, map->tablelenbits + 1);
  186. if (rv != 0) {
  187. return rv;
  188. }
  189. } else {
  190. rv = map_resize(map, 1 << NGHTTP2_INITIAL_TABLE_LENBITS,
  191. NGHTTP2_INITIAL_TABLE_LENBITS);
  192. if (rv != 0) {
  193. return rv;
  194. }
  195. }
  196. }
  197. rv = insert(map->table, map->tablelen, map->tablelenbits, hash(key), key,
  198. data);
  199. if (rv != 0) {
  200. return rv;
  201. }
  202. ++map->size;
  203. return 0;
  204. }
  205. void *nghttp2_map_find(nghttp2_map *map, nghttp2_map_key_type key) {
  206. uint32_t h;
  207. size_t idx;
  208. nghttp2_map_bucket *bkt;
  209. size_t d = 0;
  210. if (map->size == 0) {
  211. return NULL;
  212. }
  213. h = hash(key);
  214. idx = h2idx(h, map->tablelenbits);
  215. for (;;) {
  216. bkt = &map->table[idx];
  217. if (bkt->data == NULL ||
  218. d > distance(map->tablelen, map->tablelenbits, bkt, idx)) {
  219. return NULL;
  220. }
  221. if (bkt->key == key) {
  222. return bkt->data;
  223. }
  224. ++d;
  225. idx = (idx + 1) & (map->tablelen - 1);
  226. }
  227. }
  228. int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) {
  229. uint32_t h;
  230. size_t idx, didx;
  231. nghttp2_map_bucket *bkt;
  232. size_t d = 0;
  233. if (map->size == 0) {
  234. return NGHTTP2_ERR_INVALID_ARGUMENT;
  235. }
  236. h = hash(key);
  237. idx = h2idx(h, map->tablelenbits);
  238. for (;;) {
  239. bkt = &map->table[idx];
  240. if (bkt->data == NULL ||
  241. d > distance(map->tablelen, map->tablelenbits, bkt, idx)) {
  242. return NGHTTP2_ERR_INVALID_ARGUMENT;
  243. }
  244. if (bkt->key == key) {
  245. map_bucket_set_data(bkt, 0, 0, NULL);
  246. didx = idx;
  247. idx = (idx + 1) & (map->tablelen - 1);
  248. for (;;) {
  249. bkt = &map->table[idx];
  250. if (bkt->data == NULL ||
  251. distance(map->tablelen, map->tablelenbits, bkt, idx) == 0) {
  252. break;
  253. }
  254. map->table[didx] = *bkt;
  255. map_bucket_set_data(bkt, 0, 0, NULL);
  256. didx = idx;
  257. idx = (idx + 1) & (map->tablelen - 1);
  258. }
  259. --map->size;
  260. return 0;
  261. }
  262. ++d;
  263. idx = (idx + 1) & (map->tablelen - 1);
  264. }
  265. }
  266. void nghttp2_map_clear(nghttp2_map *map) {
  267. if (map->tablelen == 0) {
  268. return;
  269. }
  270. memset(map->table, 0, sizeof(*map->table) * map->tablelen);
  271. map->size = 0;
  272. }
  273. size_t nghttp2_map_size(nghttp2_map *map) { return map->size; }