nghttp2_map.c 6.6 KB

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