nghttp3_ksl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2018 ngtcp2 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. #ifndef NGHTTP3_KSL_H
  27. #define NGHTTP3_KSL_H
  28. #ifdef HAVE_CONFIG_H
  29. # include <config.h>
  30. #endif /* defined(HAVE_CONFIG_H) */
  31. #include <stdlib.h>
  32. #include <nghttp3/nghttp3.h>
  33. #include "nghttp3_objalloc.h"
  34. #define NGHTTP3_KSL_DEGR 16
  35. /* NGHTTP3_KSL_MAX_NBLK is the maximum number of nodes which a single
  36. block can contain. */
  37. #define NGHTTP3_KSL_MAX_NBLK (2 * NGHTTP3_KSL_DEGR - 1)
  38. /* NGHTTP3_KSL_MIN_NBLK is the minimum number of nodes which a single
  39. block other than root must contain. */
  40. #define NGHTTP3_KSL_MIN_NBLK (NGHTTP3_KSL_DEGR - 1)
  41. /*
  42. * nghttp3_ksl_key represents key in nghttp3_ksl.
  43. */
  44. typedef void nghttp3_ksl_key;
  45. typedef struct nghttp3_ksl_node nghttp3_ksl_node;
  46. typedef struct nghttp3_ksl_blk nghttp3_ksl_blk;
  47. /*
  48. * nghttp3_ksl_node is a node which contains either nghttp3_ksl_blk or
  49. * opaque data. If a node is an internal node, it contains
  50. * nghttp3_ksl_blk. Otherwise, it has data. The key is stored at the
  51. * location starting at key.
  52. */
  53. struct nghttp3_ksl_node {
  54. union {
  55. nghttp3_ksl_blk *blk;
  56. void *data;
  57. };
  58. union {
  59. uint64_t align;
  60. /* key is a buffer to include key associated to this node.
  61. Because the length of key is unknown until nghttp3_ksl_init is
  62. called, the actual buffer will be allocated after this
  63. field. */
  64. uint8_t key[1];
  65. };
  66. };
  67. /*
  68. * nghttp3_ksl_blk contains nghttp3_ksl_node objects.
  69. */
  70. struct nghttp3_ksl_blk {
  71. union {
  72. struct {
  73. /* next points to the next block if leaf field is nonzero. */
  74. nghttp3_ksl_blk *next;
  75. /* prev points to the previous block if leaf field is
  76. nonzero. */
  77. nghttp3_ksl_blk *prev;
  78. /* n is the number of nodes this object contains in nodes. */
  79. uint32_t n;
  80. /* leaf is nonzero if this block contains leaf nodes. */
  81. uint32_t leaf;
  82. union {
  83. uint64_t align;
  84. /* nodes is a buffer to contain NGHTTP3_KSL_MAX_NBLK
  85. nghttp3_ksl_node objects. Because nghttp3_ksl_node object
  86. is allocated along with the additional variable length key
  87. storage, the size of buffer is unknown until
  88. nghttp3_ksl_init is called. */
  89. uint8_t nodes[1];
  90. };
  91. };
  92. nghttp3_opl_entry oplent;
  93. };
  94. };
  95. nghttp3_objalloc_decl(ksl_blk, nghttp3_ksl_blk, oplent)
  96. /*
  97. * nghttp3_ksl_compar is a function type which returns nonzero if key
  98. * |lhs| should be placed before |rhs|. It returns 0 otherwise.
  99. */
  100. typedef int (*nghttp3_ksl_compar)(const nghttp3_ksl_key *lhs,
  101. const nghttp3_ksl_key *rhs);
  102. typedef struct nghttp3_ksl nghttp3_ksl;
  103. /*
  104. * nghttp3_ksl_search is a function to search for the first element in
  105. * |blk|->nodes which is not ordered before |key|. It returns the
  106. * index of such element. It returns |blk|->n if there is no such
  107. * element.
  108. */
  109. typedef size_t (*nghttp3_ksl_search)(const nghttp3_ksl *ksl,
  110. nghttp3_ksl_blk *blk,
  111. const nghttp3_ksl_key *key);
  112. /*
  113. * nghttp3_ksl_search_def is a macro to implement nghttp3_ksl_search
  114. * with COMPAR which is supposed to be nghttp3_ksl_compar.
  115. */
  116. #define nghttp3_ksl_search_def(NAME, COMPAR) \
  117. static size_t ksl_##NAME##_search(const nghttp3_ksl *ksl, \
  118. nghttp3_ksl_blk *blk, \
  119. const nghttp3_ksl_key *key) { \
  120. size_t i; \
  121. nghttp3_ksl_node *node; \
  122. \
  123. for (i = 0, node = (nghttp3_ksl_node *)(void *)blk->nodes; \
  124. i < blk->n && COMPAR((nghttp3_ksl_key *)node->key, key); ++i, \
  125. node = (nghttp3_ksl_node *)(void *)((uint8_t *)node + ksl->nodelen)) \
  126. ; \
  127. \
  128. return i; \
  129. }
  130. typedef struct nghttp3_ksl_it nghttp3_ksl_it;
  131. /*
  132. * nghttp3_ksl_it is a bidirectional iterator to iterate nodes.
  133. */
  134. struct nghttp3_ksl_it {
  135. const nghttp3_ksl *ksl;
  136. nghttp3_ksl_blk *blk;
  137. size_t i;
  138. };
  139. /*
  140. * nghttp3_ksl is a deterministic paged skip list.
  141. */
  142. struct nghttp3_ksl {
  143. nghttp3_objalloc blkalloc;
  144. /* head points to the root block. */
  145. nghttp3_ksl_blk *head;
  146. /* front points to the first leaf block. */
  147. nghttp3_ksl_blk *front;
  148. /* back points to the last leaf block. */
  149. nghttp3_ksl_blk *back;
  150. nghttp3_ksl_compar compar;
  151. nghttp3_ksl_search search;
  152. /* n is the number of elements stored. */
  153. size_t n;
  154. /* keylen is the size of key */
  155. size_t keylen;
  156. /* nodelen is the actual size of nghttp3_ksl_node including key
  157. storage. */
  158. size_t nodelen;
  159. };
  160. /*
  161. * nghttp3_ksl_init initializes |ksl|. |compar| specifies compare
  162. * function. |search| is a search function which must use |compar|.
  163. * |keylen| is the length of key and must be at least
  164. * sizeof(uint64_t).
  165. */
  166. void nghttp3_ksl_init(nghttp3_ksl *ksl, nghttp3_ksl_compar compar,
  167. nghttp3_ksl_search search, size_t keylen,
  168. const nghttp3_mem *mem);
  169. /*
  170. * nghttp3_ksl_free frees resources allocated for |ksl|. If |ksl| is
  171. * NULL, this function does nothing. It does not free the memory
  172. * region pointed by |ksl| itself.
  173. */
  174. void nghttp3_ksl_free(nghttp3_ksl *ksl);
  175. /*
  176. * nghttp3_ksl_insert inserts |key| with its associated |data|. On
  177. * successful insertion, the iterator points to the inserted node is
  178. * stored in |*it| if |it| is not NULL.
  179. *
  180. * This function returns 0 if it succeeds, or one of the following
  181. * negative error codes:
  182. *
  183. * NGHTTP3_ERR_NOMEM
  184. * Out of memory.
  185. * NGHTTP3_ERR_INVALID_ARGUMENT
  186. * |key| already exists.
  187. */
  188. int nghttp3_ksl_insert(nghttp3_ksl *ksl, nghttp3_ksl_it *it,
  189. const nghttp3_ksl_key *key, void *data);
  190. /*
  191. * nghttp3_ksl_remove removes the |key| from |ksl|.
  192. *
  193. * This function assigns the iterator to |*it|, which points to the
  194. * node which is located at the right next of the removed node if |it|
  195. * is not NULL. If |key| is not found, no deletion takes place and
  196. * the return value of nghttp3_ksl_end(ksl) is assigned to |*it| if
  197. * |it| is not NULL.
  198. *
  199. * This function returns 0 if it succeeds, or one of the following
  200. * negative error codes:
  201. *
  202. * NGHTTP3_ERR_INVALID_ARGUMENT
  203. * |key| does not exist.
  204. */
  205. int nghttp3_ksl_remove(nghttp3_ksl *ksl, nghttp3_ksl_it *it,
  206. const nghttp3_ksl_key *key);
  207. /*
  208. * nghttp3_ksl_remove_hint removes the |key| from |ksl|. |hint| must
  209. * point to the same node denoted by |key|. |hint| is used to remove
  210. * a node efficiently in some cases. Other than that, it behaves
  211. * exactly like nghttp3_ksl_remove. |it| and |hint| can point to the
  212. * same object.
  213. */
  214. int nghttp3_ksl_remove_hint(nghttp3_ksl *ksl, nghttp3_ksl_it *it,
  215. const nghttp3_ksl_it *hint,
  216. const nghttp3_ksl_key *key);
  217. /*
  218. * nghttp3_ksl_lower_bound returns the iterator which points to the
  219. * first node which has the key which is equal to |key| or the last
  220. * node which satisfies !compar(&node->key, key). If there is no such
  221. * node, it returns the iterator which satisfies
  222. * nghttp3_ksl_it_end(it) != 0.
  223. */
  224. nghttp3_ksl_it nghttp3_ksl_lower_bound(const nghttp3_ksl *ksl,
  225. const nghttp3_ksl_key *key);
  226. /*
  227. * nghttp3_ksl_lower_bound_search works like nghttp3_ksl_lower_bound,
  228. * but it takes custom function |search| to do lower bound search.
  229. */
  230. nghttp3_ksl_it nghttp3_ksl_lower_bound_search(const nghttp3_ksl *ksl,
  231. const nghttp3_ksl_key *key,
  232. nghttp3_ksl_search search);
  233. /*
  234. * nghttp3_ksl_update_key replaces the key of nodes which has
  235. * |old_key| with |new_key|. |new_key| must be strictly greater than
  236. * the previous node and strictly smaller than the next node.
  237. */
  238. void nghttp3_ksl_update_key(nghttp3_ksl *ksl, const nghttp3_ksl_key *old_key,
  239. const nghttp3_ksl_key *new_key);
  240. /*
  241. * nghttp3_ksl_begin returns the iterator which points to the first
  242. * node. If there is no node in |ksl|, it returns the iterator which
  243. * satisfies both nghttp3_ksl_it_begin(it) != 0 and
  244. * nghttp3_ksl_it_end(it) != 0.
  245. */
  246. nghttp3_ksl_it nghttp3_ksl_begin(const nghttp3_ksl *ksl);
  247. /*
  248. * nghttp3_ksl_end returns the iterator which points to the node
  249. * following the last node. The returned object satisfies
  250. * nghttp3_ksl_it_end(). If there is no node in |ksl|, it returns the
  251. * iterator which satisfies nghttp3_ksl_it_begin(it) != 0 and
  252. * nghttp3_ksl_it_end(it) != 0.
  253. */
  254. nghttp3_ksl_it nghttp3_ksl_end(const nghttp3_ksl *ksl);
  255. /*
  256. * nghttp3_ksl_len returns the number of elements stored in |ksl|.
  257. */
  258. size_t nghttp3_ksl_len(const nghttp3_ksl *ksl);
  259. /*
  260. * nghttp3_ksl_clear removes all elements stored in |ksl|.
  261. */
  262. void nghttp3_ksl_clear(nghttp3_ksl *ksl);
  263. /*
  264. * nghttp3_ksl_nth_node returns the |n|th node under |blk|.
  265. */
  266. #define nghttp3_ksl_nth_node(KSL, BLK, N) \
  267. ((nghttp3_ksl_node *)(void *)((BLK)->nodes + (KSL)->nodelen * (N)))
  268. #ifndef WIN32
  269. /*
  270. * nghttp3_ksl_print prints its internal state in stderr. It assumes
  271. * that the key is of type int64_t. This function should be used for
  272. * the debugging purpose only.
  273. */
  274. void nghttp3_ksl_print(const nghttp3_ksl *ksl);
  275. #endif /* !defined(WIN32) */
  276. /*
  277. * nghttp3_ksl_it_init initializes |it|.
  278. */
  279. void nghttp3_ksl_it_init(nghttp3_ksl_it *it, const nghttp3_ksl *ksl,
  280. nghttp3_ksl_blk *blk, size_t i);
  281. /*
  282. * nghttp3_ksl_it_get returns the data associated to the node which
  283. * |it| points to. It is undefined to call this function when
  284. * nghttp3_ksl_it_end(it) returns nonzero.
  285. */
  286. #define nghttp3_ksl_it_get(IT) \
  287. nghttp3_ksl_nth_node((IT)->ksl, (IT)->blk, (IT)->i)->data
  288. /*
  289. * nghttp3_ksl_it_next advances the iterator by one. It is undefined
  290. * if this function is called when nghttp3_ksl_it_end(it) returns
  291. * nonzero.
  292. */
  293. #define nghttp3_ksl_it_next(IT) \
  294. (++(IT)->i == (IT)->blk->n && (IT)->blk->next \
  295. ? ((IT)->blk = (IT)->blk->next, (IT)->i = 0) \
  296. : 0)
  297. /*
  298. * nghttp3_ksl_it_prev moves backward the iterator by one. It is
  299. * undefined if this function is called when nghttp3_ksl_it_begin(it)
  300. * returns nonzero.
  301. */
  302. void nghttp3_ksl_it_prev(nghttp3_ksl_it *it);
  303. /*
  304. * nghttp3_ksl_it_end returns nonzero if |it| points to the one beyond
  305. * the last node.
  306. */
  307. #define nghttp3_ksl_it_end(IT) \
  308. ((IT)->blk->n == (IT)->i && (IT)->blk->next == NULL)
  309. /*
  310. * nghttp3_ksl_it_begin returns nonzero if |it| points to the first
  311. * node. |it| might satisfy both nghttp3_ksl_it_begin(it) != 0 and
  312. * nghttp3_ksl_it_end(it) != 0 if the skip list has no node.
  313. */
  314. int nghttp3_ksl_it_begin(const nghttp3_ksl_it *it);
  315. /*
  316. * nghttp3_ksl_key returns the key of the node which |it| points to.
  317. * It is undefined to call this function when nghttp3_ksl_it_end(it)
  318. * returns nonzero.
  319. */
  320. #define nghttp3_ksl_it_key(IT) \
  321. ((nghttp3_ksl_key *)nghttp3_ksl_nth_node((IT)->ksl, (IT)->blk, (IT)->i)->key)
  322. /*
  323. * nghttp3_ksl_range_compar is an implementation of
  324. * nghttp3_ksl_compar. |lhs| and |rhs| must point to nghttp3_range
  325. * object, and the function returns nonzero if ((const nghttp3_range
  326. * *)lhs)->begin < ((const nghttp3_range *)rhs)->begin.
  327. */
  328. int nghttp3_ksl_range_compar(const nghttp3_ksl_key *lhs,
  329. const nghttp3_ksl_key *rhs);
  330. /*
  331. * nghttp3_ksl_range_search is an implementation of nghttp3_ksl_search
  332. * that uses nghttp3_ksl_range_compar.
  333. */
  334. size_t nghttp3_ksl_range_search(const nghttp3_ksl *ksl, nghttp3_ksl_blk *blk,
  335. const nghttp3_ksl_key *key);
  336. /*
  337. * nghttp3_ksl_range_exclusive_compar is an implementation of
  338. * nghttp3_ksl_compar. |lhs| and |rhs| must point to nghttp3_range
  339. * object, and the function returns nonzero if ((const nghttp3_range
  340. * *)lhs)->begin < ((const nghttp3_range *)rhs)->begin, and the 2
  341. * ranges do not intersect.
  342. */
  343. int nghttp3_ksl_range_exclusive_compar(const nghttp3_ksl_key *lhs,
  344. const nghttp3_ksl_key *rhs);
  345. /*
  346. * nghttp3_ksl_range_exclusive_search is an implementation of
  347. * nghttp3_ksl_search that uses nghttp3_ksl_range_exclusive_compar.
  348. */
  349. size_t nghttp3_ksl_range_exclusive_search(const nghttp3_ksl *ksl,
  350. nghttp3_ksl_blk *blk,
  351. const nghttp3_ksl_key *key);
  352. /*
  353. * nghttp3_ksl_uint64_less is an implementation of nghttp3_ksl_compar.
  354. * |lhs| and |rhs| must point to uint64_t objects, and the function
  355. * returns nonzero if *(uint64_t *)|lhs| < *(uint64_t *)|rhs|.
  356. */
  357. int nghttp3_ksl_uint64_less(const nghttp3_ksl_key *lhs,
  358. const nghttp3_ksl_key *rhs);
  359. /*
  360. * nghttp3_ksl_uint64_less_search is an implementation of
  361. * nghttp3_ksl_search that uses nghttp3_ksl_uint64_less.
  362. */
  363. size_t nghttp3_ksl_uint64_less_search(const nghttp3_ksl *ksl,
  364. nghttp3_ksl_blk *blk,
  365. const nghttp3_ksl_key *key);
  366. /*
  367. * nghttp3_ksl_int64_greater is an implementation of
  368. * nghttp3_ksl_compar. |lhs| and |rhs| must point to int64_t objects,
  369. * and the function returns nonzero if *(int64_t *)|lhs| > *(int64_t
  370. * *)|rhs|.
  371. */
  372. int nghttp3_ksl_int64_greater(const nghttp3_ksl_key *lhs,
  373. const nghttp3_ksl_key *rhs);
  374. /*
  375. * nghttp3_ksl_int64_greater_search is an implementation of
  376. * nghttp3_ksl_search that uses nghttp3_ksl_int64_greater.
  377. */
  378. size_t nghttp3_ksl_int64_greater_search(const nghttp3_ksl *ksl,
  379. nghttp3_ksl_blk *blk,
  380. const nghttp3_ksl_key *key);
  381. #endif /* !defined(NGHTTP3_KSL_H) */