nghttp2_map.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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_HASHBITS 4
  32. void nghttp2_map_init(nghttp2_map *map, uint32_t seed, nghttp2_mem *mem) {
  33. map->mem = mem;
  34. map->hashbits = 0;
  35. map->table = NULL;
  36. map->seed = seed;
  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. int nghttp2_map_each(const nghttp2_map *map, int (*func)(void *data, void *ptr),
  46. void *ptr) {
  47. int rv;
  48. size_t i;
  49. nghttp2_map_bucket *bkt;
  50. size_t tablelen;
  51. if (map->size == 0) {
  52. return 0;
  53. }
  54. tablelen = 1u << map->hashbits;
  55. for (i = 0; i < tablelen; ++i) {
  56. bkt = &map->table[i];
  57. if (bkt->data == NULL) {
  58. continue;
  59. }
  60. rv = func(bkt->data, ptr);
  61. if (rv != 0) {
  62. return rv;
  63. }
  64. }
  65. return 0;
  66. }
  67. static size_t map_hash(const nghttp2_map *map, nghttp2_map_key_type key) {
  68. /* hasher from
  69. https://github.com/rust-lang/rustc-hash/blob/dc5c33f1283de2da64d8d7a06401d91aded03ad4/src/lib.rs
  70. We do not perform finalization here because we use top bits
  71. anyway. */
  72. uint32_t h = ((uint32_t)key + map->seed) * 0x93d765dd;
  73. return (size_t)((h * 2654435769u) >> (32 - map->hashbits));
  74. }
  75. static void map_bucket_swap(nghttp2_map_bucket *a, nghttp2_map_bucket *b) {
  76. nghttp2_map_bucket c = *a;
  77. *a = *b;
  78. *b = c;
  79. }
  80. #ifndef WIN32
  81. void nghttp2_map_print_distance(const nghttp2_map *map) {
  82. size_t i;
  83. size_t idx;
  84. nghttp2_map_bucket *bkt;
  85. size_t tablelen;
  86. if (map->size == 0) {
  87. return;
  88. }
  89. tablelen = 1u << map->hashbits;
  90. for (i = 0; i < tablelen; ++i) {
  91. bkt = &map->table[i];
  92. if (bkt->data == NULL) {
  93. fprintf(stderr, "@%zu <EMPTY>\n", i);
  94. continue;
  95. }
  96. idx = map_hash(map, bkt->key);
  97. fprintf(stderr, "@%zu hash=%zu key=%d base=%zu distance=%u\n", i,
  98. map_hash(map, bkt->key), bkt->key, idx, bkt->psl);
  99. }
  100. }
  101. #endif /* !defined(WIN32) */
  102. static int map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
  103. size_t idx = map_hash(map, key);
  104. nghttp2_map_bucket b = {
  105. .key = key,
  106. .data = data,
  107. };
  108. nghttp2_map_bucket *bkt;
  109. size_t mask = (1u << map->hashbits) - 1;
  110. for (;;) {
  111. bkt = &map->table[idx];
  112. if (bkt->data == NULL) {
  113. *bkt = b;
  114. ++map->size;
  115. return 0;
  116. }
  117. if (b.psl > bkt->psl) {
  118. map_bucket_swap(bkt, &b);
  119. } else if (bkt->key == key) {
  120. /* TODO This check is just a waste after first swap or if this
  121. function is called from map_resize. That said, there is no
  122. difference with or without this conditional in performance
  123. wise. */
  124. return NGHTTP2_ERR_INVALID_ARGUMENT;
  125. }
  126. ++b.psl;
  127. idx = (idx + 1) & mask;
  128. }
  129. }
  130. static int map_resize(nghttp2_map *map, size_t new_hashbits) {
  131. size_t i;
  132. nghttp2_map_bucket *bkt;
  133. size_t tablelen;
  134. int rv;
  135. nghttp2_map new_map = {
  136. .table = nghttp2_mem_calloc(map->mem, 1u << new_hashbits,
  137. sizeof(nghttp2_map_bucket)),
  138. .mem = map->mem,
  139. .seed = map->seed,
  140. .hashbits = new_hashbits,
  141. };
  142. (void)rv;
  143. if (new_map.table == NULL) {
  144. return NGHTTP2_ERR_NOMEM;
  145. }
  146. if (map->size) {
  147. tablelen = 1u << map->hashbits;
  148. for (i = 0; i < tablelen; ++i) {
  149. bkt = &map->table[i];
  150. if (bkt->data == NULL) {
  151. continue;
  152. }
  153. rv = map_insert(&new_map, bkt->key, bkt->data);
  154. assert(0 == rv);
  155. }
  156. }
  157. nghttp2_mem_free(map->mem, map->table);
  158. map->table = new_map.table;
  159. map->hashbits = new_hashbits;
  160. return 0;
  161. }
  162. int nghttp2_map_insert(nghttp2_map *map, nghttp2_map_key_type key, void *data) {
  163. int rv;
  164. assert(data);
  165. /* Load factor is 7/8 */
  166. /* Under the very initial condition, that is map->size == 0 and
  167. map->hashbits == 0, 8 > 7 still holds nicely. */
  168. if ((map->size + 1) * 8 > (1u << map->hashbits) * 7) {
  169. if (map->hashbits) {
  170. rv = map_resize(map, map->hashbits + 1);
  171. if (rv != 0) {
  172. return rv;
  173. }
  174. } else {
  175. rv = map_resize(map, NGHTTP2_INITIAL_HASHBITS);
  176. if (rv != 0) {
  177. return rv;
  178. }
  179. }
  180. }
  181. rv = map_insert(map, key, data);
  182. if (rv != 0) {
  183. return rv;
  184. }
  185. return 0;
  186. }
  187. void *nghttp2_map_find(const nghttp2_map *map, nghttp2_map_key_type key) {
  188. size_t idx;
  189. nghttp2_map_bucket *bkt;
  190. size_t psl = 0;
  191. size_t mask;
  192. if (map->size == 0) {
  193. return NULL;
  194. }
  195. idx = map_hash(map, key);
  196. mask = (1u << map->hashbits) - 1;
  197. for (;;) {
  198. bkt = &map->table[idx];
  199. if (bkt->data == NULL || psl > bkt->psl) {
  200. return NULL;
  201. }
  202. if (bkt->key == key) {
  203. return bkt->data;
  204. }
  205. ++psl;
  206. idx = (idx + 1) & mask;
  207. }
  208. }
  209. int nghttp2_map_remove(nghttp2_map *map, nghttp2_map_key_type key) {
  210. size_t idx;
  211. nghttp2_map_bucket *b, *bkt;
  212. size_t psl = 0;
  213. size_t mask;
  214. if (map->size == 0) {
  215. return NGHTTP2_ERR_INVALID_ARGUMENT;
  216. }
  217. idx = map_hash(map, key);
  218. mask = (1u << map->hashbits) - 1;
  219. for (;;) {
  220. bkt = &map->table[idx];
  221. if (bkt->data == NULL || psl > bkt->psl) {
  222. return NGHTTP2_ERR_INVALID_ARGUMENT;
  223. }
  224. if (bkt->key == key) {
  225. b = bkt;
  226. idx = (idx + 1) & mask;
  227. for (;;) {
  228. bkt = &map->table[idx];
  229. if (bkt->data == NULL || bkt->psl == 0) {
  230. b->data = NULL;
  231. break;
  232. }
  233. --bkt->psl;
  234. *b = *bkt;
  235. b = bkt;
  236. idx = (idx + 1) & mask;
  237. }
  238. --map->size;
  239. return 0;
  240. }
  241. ++psl;
  242. idx = (idx + 1) & mask;
  243. }
  244. }
  245. void nghttp2_map_clear(nghttp2_map *map) {
  246. if (map->size == 0) {
  247. return;
  248. }
  249. memset(map->table, 0, sizeof(*map->table) * (1u << map->hashbits));
  250. map->size = 0;
  251. }
  252. size_t nghttp2_map_size(const nghttp2_map *map) { return map->size; }