nghttp3_map.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2017 ngtcp2 contributors
  6. * Copyright (c) 2012 nghttp2 contributors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sublicense, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject to
  14. * the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #include "nghttp3_map.h"
  28. #include <string.h>
  29. #include <assert.h>
  30. #include <stdio.h>
  31. #include "nghttp3_conv.h"
  32. #define NGHTTP3_INITIAL_TABLE_LENBITS 4
  33. void nghttp3_map_init(nghttp3_map *map, const nghttp3_mem *mem) {
  34. map->mem = mem;
  35. map->hashbits = 0;
  36. map->table = NULL;
  37. map->size = 0;
  38. }
  39. void nghttp3_map_free(nghttp3_map *map) {
  40. if (!map) {
  41. return;
  42. }
  43. nghttp3_mem_free(map->mem, map->table);
  44. }
  45. int nghttp3_map_each(const nghttp3_map *map, int (*func)(void *data, void *ptr),
  46. void *ptr) {
  47. int rv;
  48. size_t i;
  49. nghttp3_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 hash(nghttp3_map_key_type key, size_t bits) {
  68. return (size_t)((key * 11400714819323198485llu) >> (64 - bits));
  69. }
  70. static void map_bucket_swap(nghttp3_map_bucket *a, nghttp3_map_bucket *b) {
  71. nghttp3_map_bucket c = *a;
  72. *a = *b;
  73. *b = c;
  74. }
  75. #ifndef WIN32
  76. void nghttp3_map_print_distance(const nghttp3_map *map) {
  77. size_t i;
  78. size_t idx;
  79. nghttp3_map_bucket *bkt;
  80. size_t tablelen;
  81. if (map->size == 0) {
  82. return;
  83. }
  84. tablelen = 1u << map->hashbits;
  85. for (i = 0; i < tablelen; ++i) {
  86. bkt = &map->table[i];
  87. if (bkt->data == NULL) {
  88. fprintf(stderr, "@%zu <EMPTY>\n", i);
  89. continue;
  90. }
  91. idx = hash(bkt->key, map->hashbits);
  92. fprintf(stderr, "@%zu hash=%zu key=%" PRIu64 " base=%zu distance=%u\n", i,
  93. hash(bkt->key, map->hashbits), bkt->key, idx, bkt->psl);
  94. }
  95. }
  96. #endif /* !defined(WIN32) */
  97. static int insert(nghttp3_map_bucket *table, size_t hashbits,
  98. nghttp3_map_key_type key, void *data) {
  99. size_t idx = hash(key, hashbits);
  100. nghttp3_map_bucket b = {0, key, data}, *bkt;
  101. size_t mask = (1u << hashbits) - 1;
  102. for (;;) {
  103. bkt = &table[idx];
  104. if (bkt->data == NULL) {
  105. *bkt = b;
  106. return 0;
  107. }
  108. if (b.psl > bkt->psl) {
  109. map_bucket_swap(bkt, &b);
  110. } else if (bkt->key == key) {
  111. /* TODO This check is just a waste after first swap or if this
  112. function is called from map_resize. That said, there is no
  113. difference with or without this conditional in performance
  114. wise. */
  115. return NGHTTP3_ERR_INVALID_ARGUMENT;
  116. }
  117. ++b.psl;
  118. idx = (idx + 1) & mask;
  119. }
  120. }
  121. static int map_resize(nghttp3_map *map, size_t new_hashbits) {
  122. size_t i;
  123. nghttp3_map_bucket *new_table;
  124. nghttp3_map_bucket *bkt;
  125. size_t tablelen;
  126. int rv;
  127. (void)rv;
  128. new_table = nghttp3_mem_calloc(map->mem, 1u << new_hashbits,
  129. sizeof(nghttp3_map_bucket));
  130. if (new_table == NULL) {
  131. return NGHTTP3_ERR_NOMEM;
  132. }
  133. if (map->size) {
  134. tablelen = 1u << map->hashbits;
  135. for (i = 0; i < tablelen; ++i) {
  136. bkt = &map->table[i];
  137. if (bkt->data == NULL) {
  138. continue;
  139. }
  140. rv = insert(new_table, new_hashbits, bkt->key, bkt->data);
  141. assert(0 == rv);
  142. }
  143. }
  144. nghttp3_mem_free(map->mem, map->table);
  145. map->hashbits = new_hashbits;
  146. map->table = new_table;
  147. return 0;
  148. }
  149. int nghttp3_map_insert(nghttp3_map *map, nghttp3_map_key_type key, void *data) {
  150. int rv;
  151. assert(data);
  152. /* Load factor is 0.75 */
  153. /* Under the very initial condition, that is map->size == 0 and
  154. map->hashbits == 0, 4 > 3 still holds nicely. */
  155. if ((map->size + 1) * 4 > (1u << map->hashbits) * 3) {
  156. if (map->hashbits) {
  157. rv = map_resize(map, map->hashbits + 1);
  158. if (rv != 0) {
  159. return rv;
  160. }
  161. } else {
  162. rv = map_resize(map, NGHTTP3_INITIAL_TABLE_LENBITS);
  163. if (rv != 0) {
  164. return rv;
  165. }
  166. }
  167. }
  168. rv = insert(map->table, map->hashbits, key, data);
  169. if (rv != 0) {
  170. return rv;
  171. }
  172. ++map->size;
  173. return 0;
  174. }
  175. void *nghttp3_map_find(const nghttp3_map *map, nghttp3_map_key_type key) {
  176. size_t idx;
  177. nghttp3_map_bucket *bkt;
  178. size_t psl = 0;
  179. size_t mask;
  180. if (map->size == 0) {
  181. return NULL;
  182. }
  183. idx = hash(key, map->hashbits);
  184. mask = (1u << map->hashbits) - 1;
  185. for (;;) {
  186. bkt = &map->table[idx];
  187. if (bkt->data == NULL || psl > bkt->psl) {
  188. return NULL;
  189. }
  190. if (bkt->key == key) {
  191. return bkt->data;
  192. }
  193. ++psl;
  194. idx = (idx + 1) & mask;
  195. }
  196. }
  197. int nghttp3_map_remove(nghttp3_map *map, nghttp3_map_key_type key) {
  198. size_t idx;
  199. nghttp3_map_bucket *b, *bkt;
  200. size_t psl = 0;
  201. size_t mask;
  202. if (map->size == 0) {
  203. return NGHTTP3_ERR_INVALID_ARGUMENT;
  204. }
  205. idx = hash(key, map->hashbits);
  206. mask = (1u << map->hashbits) - 1;
  207. for (;;) {
  208. bkt = &map->table[idx];
  209. if (bkt->data == NULL || psl > bkt->psl) {
  210. return NGHTTP3_ERR_INVALID_ARGUMENT;
  211. }
  212. if (bkt->key == key) {
  213. b = bkt;
  214. idx = (idx + 1) & mask;
  215. for (;;) {
  216. bkt = &map->table[idx];
  217. if (bkt->data == NULL || bkt->psl == 0) {
  218. b->data = NULL;
  219. break;
  220. }
  221. --bkt->psl;
  222. *b = *bkt;
  223. b = bkt;
  224. idx = (idx + 1) & mask;
  225. }
  226. --map->size;
  227. return 0;
  228. }
  229. ++psl;
  230. idx = (idx + 1) & mask;
  231. }
  232. }
  233. void nghttp3_map_clear(nghttp3_map *map) {
  234. if (map->size == 0) {
  235. return;
  236. }
  237. memset(map->table, 0, sizeof(*map->table) * (1u << map->hashbits));
  238. map->size = 0;
  239. }
  240. size_t nghttp3_map_size(const nghttp3_map *map) { return map->size; }