nghttp2_map.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. void nghttp2_map_print_distance(nghttp2_map *map) {
  106. uint32_t i;
  107. size_t idx;
  108. nghttp2_map_bucket *bkt;
  109. for (i = 0; i < map->tablelen; ++i) {
  110. bkt = &map->table[i];
  111. if (bkt->data == NULL) {
  112. fprintf(stderr, "@%u <EMPTY>\n", i);
  113. continue;
  114. }
  115. idx = h2idx(bkt->hash, map->tablelenbits);
  116. fprintf(stderr, "@%u hash=%08x key=%d base=%zu distance=%zu\n", i,
  117. bkt->hash, bkt->key, idx,
  118. distance(map->tablelen, map->tablelenbits, bkt, idx));
  119. }
  120. }
  121. static int insert(nghttp2_map_bucket *table, uint32_t tablelen,
  122. uint32_t tablelenbits, uint32_t hash,
  123. nghttp2_map_key_type key, void *data) {
  124. size_t idx = h2idx(hash, tablelenbits);
  125. size_t d = 0, dd;
  126. nghttp2_map_bucket *bkt;
  127. for (;;) {
  128. bkt = &table[idx];
  129. if (bkt->data == NULL) {
  130. map_bucket_set_data(bkt, hash, key, data);
  131. return 0;
  132. }
  133. dd = distance(tablelen, tablelenbits, bkt, idx);
  134. if (d > dd) {
  135. map_bucket_swap(bkt, &hash, &key, &data);
  136. d = dd;
  137. } else if (bkt->key == key) {
  138. /* TODO This check is just a waste after first swap or if this
  139. function is called from map_resize. That said, there is no
  140. difference with or without this conditional in performance
  141. wise. */
  142. return NGHTTP2_ERR_INVALID_ARGUMENT;
  143. }
  144. ++d;
  145. idx = (idx + 1) & (tablelen - 1);
  146. }
  147. }
  148. /* new_tablelen must be power of 2 and new_tablelen == (1 <<
  149. new_tablelenbits) must hold. */
  150. static int map_resize(nghttp2_map *map, uint32_t new_tablelen,
  151. uint32_t new_tablelenbits) {
  152. uint32_t i;
  153. nghttp2_map_bucket *new_table;
  154. nghttp2_map_bucket *bkt;
  155. int rv;
  156. (void)rv;
  157. new_table =
  158. nghttp2_mem_calloc(map->mem, new_tablelen, sizeof(nghttp2_map_bucket));
  159. if (new_table == NULL) {
  160. return NGHTTP2_ERR_NOMEM;
  161. }
  162. for (i = 0; i < map->tablelen; ++i) {
  163. bkt = &map->table[i];
  164. if (bkt->data == NULL) {
  165. continue;
  166. }
  167. rv = insert(new_table, new_tablelen, new_tablelenbits, bkt->hash, bkt->key,
  168. bkt->data);
  169. assert(0 == rv);
  170. }
  171. nghttp2_mem_free(map->mem, map->table);
  172. map->tablelen = new_tablelen;
  173. map->tablelenbits = new_tablelenbits;
  174. map->table = new_table;
  175. return 0;
  176. }
  177. int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
  178. int rv;
  179. assert(data);
  180. /* Load factor is 0.75 */
  181. if ((map->size + 1) * 4 > map->tablelen * 3) {
  182. if (map->tablelen) {
  183. rv = map_resize(map, map->tablelen * 2, map->tablelenbits + 1);
  184. if (rv != 0) {
  185. return rv;
  186. }
  187. } else {
  188. rv = map_resize(map, 1 << NGHTTP2_INITIAL_TABLE_LENBITS,
  189. NGHTTP2_INITIAL_TABLE_LENBITS);
  190. if (rv != 0) {
  191. return rv;
  192. }
  193. }
  194. }
  195. rv = insert(map->table, map->tablelen, map->tablelenbits, hash(key), key,
  196. data);
  197. if (rv != 0) {
  198. return rv;
  199. }
  200. ++map->size;
  201. return 0;
  202. }
  203. void *nghttp2_map_find(nghttp2_map *map, nghttp2_map_key_type key) {
  204. uint32_t h;
  205. size_t idx;
  206. nghttp2_map_bucket *bkt;
  207. size_t d = 0;
  208. if (map->size == 0) {
  209. return NULL;
  210. }
  211. h = hash(key);
  212. idx = h2idx(h, map->tablelenbits);
  213. for (;;) {
  214. bkt = &map->table[idx];
  215. if (bkt->data == NULL ||
  216. d > distance(map->tablelen, map->tablelenbits, bkt, idx)) {
  217. return NULL;
  218. }
  219. if (bkt->key == key) {
  220. return bkt->data;
  221. }
  222. ++d;
  223. idx = (idx + 1) & (map->tablelen - 1);
  224. }
  225. }
  226. int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) {
  227. uint32_t h;
  228. size_t idx, didx;
  229. nghttp2_map_bucket *bkt;
  230. size_t d = 0;
  231. if (map->size == 0) {
  232. return NGHTTP2_ERR_INVALID_ARGUMENT;
  233. }
  234. h = hash(key);
  235. idx = h2idx(h, map->tablelenbits);
  236. for (;;) {
  237. bkt = &map->table[idx];
  238. if (bkt->data == NULL ||
  239. d > distance(map->tablelen, map->tablelenbits, bkt, idx)) {
  240. return NGHTTP2_ERR_INVALID_ARGUMENT;
  241. }
  242. if (bkt->key == key) {
  243. map_bucket_set_data(bkt, 0, 0, NULL);
  244. didx = idx;
  245. idx = (idx + 1) & (map->tablelen - 1);
  246. for (;;) {
  247. bkt = &map->table[idx];
  248. if (bkt->data == NULL ||
  249. distance(map->tablelen, map->tablelenbits, bkt, idx) == 0) {
  250. break;
  251. }
  252. map->table[didx] = *bkt;
  253. map_bucket_set_data(bkt, 0, 0, NULL);
  254. didx = idx;
  255. idx = (idx + 1) & (map->tablelen - 1);
  256. }
  257. --map->size;
  258. return 0;
  259. }
  260. ++d;
  261. idx = (idx + 1) & (map->tablelen - 1);
  262. }
  263. }
  264. void nghttp2_map_clear(nghttp2_map *map) {
  265. if (map->tablelen == 0) {
  266. return;
  267. }
  268. memset(map->table, 0, sizeof(*map->table) * map->tablelen);
  269. map->size = 0;
  270. }
  271. size_t nghttp2_map_size(nghttp2_map *map) { return map->size; }