ngtcp2_ksl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2018 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef NGTCP2_KSL_H
  26. #define NGTCP2_KSL_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <stdlib.h>
  31. #include <ngtcp2/ngtcp2.h>
  32. #include "ngtcp2_objalloc.h"
  33. #define NGTCP2_KSL_DEGR 16
  34. /* NGTCP2_KSL_MAX_NBLK is the maximum number of nodes which a single
  35. block can contain. */
  36. #define NGTCP2_KSL_MAX_NBLK (2 * NGTCP2_KSL_DEGR - 1)
  37. /* NGTCP2_KSL_MIN_NBLK is the minimum number of nodes which a single
  38. block other than root must contain. */
  39. #define NGTCP2_KSL_MIN_NBLK (NGTCP2_KSL_DEGR - 1)
  40. /*
  41. * ngtcp2_ksl_key represents key in ngtcp2_ksl.
  42. */
  43. typedef void ngtcp2_ksl_key;
  44. typedef struct ngtcp2_ksl_node ngtcp2_ksl_node;
  45. typedef struct ngtcp2_ksl_blk ngtcp2_ksl_blk;
  46. /*
  47. * ngtcp2_ksl_node is a node which contains either ngtcp2_ksl_blk or
  48. * opaque data. If a node is an internal node, it contains
  49. * ngtcp2_ksl_blk. Otherwise, it has data. The key is stored at the
  50. * location starting at key.
  51. */
  52. struct ngtcp2_ksl_node {
  53. union {
  54. ngtcp2_ksl_blk *blk;
  55. void *data;
  56. };
  57. union {
  58. uint64_t align;
  59. /* key is a buffer to include key associated to this node.
  60. Because the length of key is unknown until ngtcp2_ksl_init is
  61. called, the actual buffer will be allocated after this
  62. field. */
  63. uint8_t key[1];
  64. };
  65. };
  66. /*
  67. * ngtcp2_ksl_blk contains ngtcp2_ksl_node objects.
  68. */
  69. struct ngtcp2_ksl_blk {
  70. union {
  71. struct {
  72. /* next points to the next block if leaf field is nonzero. */
  73. ngtcp2_ksl_blk *next;
  74. /* prev points to the previous block if leaf field is
  75. nonzero. */
  76. ngtcp2_ksl_blk *prev;
  77. /* n is the number of nodes this object contains in nodes. */
  78. uint32_t n;
  79. /* leaf is nonzero if this block contains leaf nodes. */
  80. uint32_t leaf;
  81. union {
  82. uint64_t align;
  83. /* nodes is a buffer to contain NGTCP2_KSL_MAX_NBLK
  84. ngtcp2_ksl_node objects. Because ngtcp2_ksl_node object is
  85. allocated along with the additional variable length key
  86. storage, the size of buffer is unknown until ngtcp2_ksl_init is
  87. called. */
  88. uint8_t nodes[1];
  89. };
  90. };
  91. ngtcp2_opl_entry oplent;
  92. };
  93. };
  94. ngtcp2_objalloc_decl(ksl_blk, ngtcp2_ksl_blk, oplent)
  95. /*
  96. * ngtcp2_ksl_compar is a function type which returns nonzero if key
  97. * |lhs| should be placed before |rhs|. It returns 0 otherwise.
  98. */
  99. typedef int (*ngtcp2_ksl_compar)(const ngtcp2_ksl_key *lhs,
  100. const ngtcp2_ksl_key *rhs);
  101. typedef struct ngtcp2_ksl ngtcp2_ksl;
  102. /*
  103. * ngtcp2_ksl_search is a function to search for the first element in
  104. * |blk|->nodes which is not ordered before |key|. It returns the
  105. * index of such element. It returns |blk|->n if there is no such
  106. * element.
  107. */
  108. typedef size_t (*ngtcp2_ksl_search)(const ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk,
  109. const ngtcp2_ksl_key *key);
  110. /*
  111. * ngtcp2_ksl_search_def is a macro to implement ngtcp2_ksl_search
  112. * with COMPAR which is supposed to be ngtcp2_ksl_compar.
  113. */
  114. #define ngtcp2_ksl_search_def(NAME, COMPAR) \
  115. static size_t ksl_##NAME##_search( \
  116. const ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk, const ngtcp2_ksl_key *key) { \
  117. size_t i; \
  118. ngtcp2_ksl_node *node; \
  119. \
  120. for (i = 0, node = (ngtcp2_ksl_node *)(void *)blk->nodes; \
  121. i < blk->n && COMPAR((ngtcp2_ksl_key *)node->key, key); ++i, \
  122. node = (ngtcp2_ksl_node *)(void *)((uint8_t *)node + ksl->nodelen)) \
  123. ; \
  124. \
  125. return i; \
  126. }
  127. typedef struct ngtcp2_ksl_it ngtcp2_ksl_it;
  128. /*
  129. * ngtcp2_ksl_it is a bidirectional iterator to iterate nodes.
  130. */
  131. struct ngtcp2_ksl_it {
  132. const ngtcp2_ksl *ksl;
  133. ngtcp2_ksl_blk *blk;
  134. size_t i;
  135. };
  136. /*
  137. * ngtcp2_ksl is a deterministic paged skip list.
  138. */
  139. struct ngtcp2_ksl {
  140. ngtcp2_objalloc blkalloc;
  141. /* head points to the root block. */
  142. ngtcp2_ksl_blk *head;
  143. /* front points to the first leaf block. */
  144. ngtcp2_ksl_blk *front;
  145. /* back points to the last leaf block. */
  146. ngtcp2_ksl_blk *back;
  147. ngtcp2_ksl_compar compar;
  148. ngtcp2_ksl_search search;
  149. /* n is the number of elements stored. */
  150. size_t n;
  151. /* keylen is the size of key */
  152. size_t keylen;
  153. /* nodelen is the actual size of ngtcp2_ksl_node including key
  154. storage. */
  155. size_t nodelen;
  156. };
  157. /*
  158. * ngtcp2_ksl_init initializes |ksl|. |compar| specifies compare
  159. * function. |search| is a search function which must use |compar|.
  160. * |keylen| is the length of key and must be at least
  161. * sizeof(uint64_t).
  162. */
  163. void ngtcp2_ksl_init(ngtcp2_ksl *ksl, ngtcp2_ksl_compar compar,
  164. ngtcp2_ksl_search search, size_t keylen,
  165. const ngtcp2_mem *mem);
  166. /*
  167. * ngtcp2_ksl_free frees resources allocated for |ksl|. If |ksl| is
  168. * NULL, this function does nothing. It does not free the memory
  169. * region pointed by |ksl| itself.
  170. */
  171. void ngtcp2_ksl_free(ngtcp2_ksl *ksl);
  172. /*
  173. * ngtcp2_ksl_insert inserts |key| with its associated |data|. On
  174. * successful insertion, the iterator points to the inserted node is
  175. * stored in |*it| if |it| is not NULL.
  176. *
  177. * This function returns 0 if it succeeds, or one of the following
  178. * negative error codes:
  179. *
  180. * NGTCP2_ERR_NOMEM
  181. * Out of memory.
  182. * NGTCP2_ERR_INVALID_ARGUMENT
  183. * |key| already exists.
  184. */
  185. int ngtcp2_ksl_insert(ngtcp2_ksl *ksl, ngtcp2_ksl_it *it,
  186. const ngtcp2_ksl_key *key, void *data);
  187. /*
  188. * ngtcp2_ksl_remove removes the |key| from |ksl|.
  189. *
  190. * This function assigns the iterator to |*it|, which points to the
  191. * node which is located at the right next of the removed node if |it|
  192. * is not NULL. If |key| is not found, no deletion takes place and
  193. * the return value of ngtcp2_ksl_end(ksl) is assigned to |*it| if
  194. * |it| is not NULL.
  195. *
  196. * This function returns 0 if it succeeds, or one of the following
  197. * negative error codes:
  198. *
  199. * NGTCP2_ERR_INVALID_ARGUMENT
  200. * |key| does not exist.
  201. */
  202. int ngtcp2_ksl_remove(ngtcp2_ksl *ksl, ngtcp2_ksl_it *it,
  203. const ngtcp2_ksl_key *key);
  204. /*
  205. * ngtcp2_ksl_remove_hint removes the |key| from |ksl|. |hint| must
  206. * point to the same node denoted by |key|. |hint| is used to remove
  207. * a node efficiently in some cases. Other than that, it behaves
  208. * exactly like ngtcp2_ksl_remove. |it| and |hint| can point to the
  209. * same object.
  210. */
  211. int ngtcp2_ksl_remove_hint(ngtcp2_ksl *ksl, ngtcp2_ksl_it *it,
  212. const ngtcp2_ksl_it *hint,
  213. const ngtcp2_ksl_key *key);
  214. /*
  215. * ngtcp2_ksl_lower_bound returns the iterator which points to the
  216. * first node which has the key which is equal to |key| or the last
  217. * node which satisfies !compar(&node->key, key). If there is no such
  218. * node, it returns the iterator which satisfies ngtcp2_ksl_it_end(it)
  219. * != 0.
  220. */
  221. ngtcp2_ksl_it ngtcp2_ksl_lower_bound(const ngtcp2_ksl *ksl,
  222. const ngtcp2_ksl_key *key);
  223. /*
  224. * ngtcp2_ksl_lower_bound_search works like ngtcp2_ksl_lower_bound,
  225. * but it takes custom function |search| to do lower bound search.
  226. */
  227. ngtcp2_ksl_it ngtcp2_ksl_lower_bound_search(const ngtcp2_ksl *ksl,
  228. const ngtcp2_ksl_key *key,
  229. ngtcp2_ksl_search search);
  230. /*
  231. * ngtcp2_ksl_update_key replaces the key of nodes which has |old_key|
  232. * with |new_key|. |new_key| must be strictly greater than the
  233. * previous node and strictly smaller than the next node.
  234. */
  235. void ngtcp2_ksl_update_key(ngtcp2_ksl *ksl, const ngtcp2_ksl_key *old_key,
  236. const ngtcp2_ksl_key *new_key);
  237. /*
  238. * ngtcp2_ksl_begin returns the iterator which points to the first
  239. * node. If there is no node in |ksl|, it returns the iterator which
  240. * satisfies both ngtcp2_ksl_it_begin(it) != 0 and
  241. * ngtcp2_ksl_it_end(it) != 0.
  242. */
  243. ngtcp2_ksl_it ngtcp2_ksl_begin(const ngtcp2_ksl *ksl);
  244. /*
  245. * ngtcp2_ksl_end returns the iterator which points to the node
  246. * following the last node. The returned object satisfies
  247. * ngtcp2_ksl_it_end(). If there is no node in |ksl|, it returns the
  248. * iterator which satisfies ngtcp2_ksl_it_begin(it) != 0 and
  249. * ngtcp2_ksl_it_end(it) != 0.
  250. */
  251. ngtcp2_ksl_it ngtcp2_ksl_end(const ngtcp2_ksl *ksl);
  252. /*
  253. * ngtcp2_ksl_len returns the number of elements stored in |ksl|.
  254. */
  255. size_t ngtcp2_ksl_len(const ngtcp2_ksl *ksl);
  256. /*
  257. * ngtcp2_ksl_clear removes all elements stored in |ksl|.
  258. */
  259. void ngtcp2_ksl_clear(ngtcp2_ksl *ksl);
  260. /*
  261. * ngtcp2_ksl_nth_node returns the |n|th node under |blk|.
  262. */
  263. #define ngtcp2_ksl_nth_node(KSL, BLK, N) \
  264. ((ngtcp2_ksl_node *)(void *)((BLK)->nodes + (KSL)->nodelen * (N)))
  265. #ifndef WIN32
  266. /*
  267. * ngtcp2_ksl_print prints its internal state in stderr. It assumes
  268. * that the key is of type int64_t. This function should be used for
  269. * the debugging purpose only.
  270. */
  271. void ngtcp2_ksl_print(const ngtcp2_ksl *ksl);
  272. #endif /* !defined(WIN32) */
  273. /*
  274. * ngtcp2_ksl_it_init initializes |it|.
  275. */
  276. void ngtcp2_ksl_it_init(ngtcp2_ksl_it *it, const ngtcp2_ksl *ksl,
  277. ngtcp2_ksl_blk *blk, size_t i);
  278. /*
  279. * ngtcp2_ksl_it_get returns the data associated to the node which
  280. * |it| points to. It is undefined to call this function when
  281. * ngtcp2_ksl_it_end(it) returns nonzero.
  282. */
  283. #define ngtcp2_ksl_it_get(IT) \
  284. ngtcp2_ksl_nth_node((IT)->ksl, (IT)->blk, (IT)->i)->data
  285. /*
  286. * ngtcp2_ksl_it_next advances the iterator by one. It is undefined
  287. * if this function is called when ngtcp2_ksl_it_end(it) returns
  288. * nonzero.
  289. */
  290. #define ngtcp2_ksl_it_next(IT) \
  291. (++(IT)->i == (IT)->blk->n && (IT)->blk->next \
  292. ? ((IT)->blk = (IT)->blk->next, (IT)->i = 0) \
  293. : 0)
  294. /*
  295. * ngtcp2_ksl_it_prev moves backward the iterator by one. It is
  296. * undefined if this function is called when ngtcp2_ksl_it_begin(it)
  297. * returns nonzero.
  298. */
  299. void ngtcp2_ksl_it_prev(ngtcp2_ksl_it *it);
  300. /*
  301. * ngtcp2_ksl_it_end returns nonzero if |it| points to the one beyond
  302. * the last node.
  303. */
  304. #define ngtcp2_ksl_it_end(IT) \
  305. ((IT)->blk->n == (IT)->i && (IT)->blk->next == NULL)
  306. /*
  307. * ngtcp2_ksl_it_begin returns nonzero if |it| points to the first
  308. * node. |it| might satisfy both ngtcp2_ksl_it_begin(it) != 0 and
  309. * ngtcp2_ksl_it_end(it) != 0 if the skip list has no node.
  310. */
  311. int ngtcp2_ksl_it_begin(const ngtcp2_ksl_it *it);
  312. /*
  313. * ngtcp2_ksl_key returns the key of the node which |it| points to.
  314. * It is undefined to call this function when ngtcp2_ksl_it_end(it)
  315. * returns nonzero.
  316. */
  317. #define ngtcp2_ksl_it_key(IT) \
  318. ((ngtcp2_ksl_key *)ngtcp2_ksl_nth_node((IT)->ksl, (IT)->blk, (IT)->i)->key)
  319. /*
  320. * ngtcp2_ksl_range_compar is an implementation of ngtcp2_ksl_compar.
  321. * |lhs| and |rhs| must point to ngtcp2_range object, and the function
  322. * returns nonzero if ((const ngtcp2_range *)lhs)->begin < ((const
  323. * ngtcp2_range *)rhs)->begin.
  324. */
  325. int ngtcp2_ksl_range_compar(const ngtcp2_ksl_key *lhs,
  326. const ngtcp2_ksl_key *rhs);
  327. /*
  328. * ngtcp2_ksl_range_search is an implementation of ngtcp2_ksl_search
  329. * that uses ngtcp2_ksl_range_compar.
  330. */
  331. size_t ngtcp2_ksl_range_search(const ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk,
  332. const ngtcp2_ksl_key *key);
  333. /*
  334. * ngtcp2_ksl_range_exclusive_compar is an implementation of
  335. * ngtcp2_ksl_compar. |lhs| and |rhs| must point to ngtcp2_range
  336. * object, and the function returns nonzero if ((const ngtcp2_range
  337. * *)lhs)->begin < ((const ngtcp2_range *)rhs)->begin, and the 2
  338. * ranges do not intersect.
  339. */
  340. int ngtcp2_ksl_range_exclusive_compar(const ngtcp2_ksl_key *lhs,
  341. const ngtcp2_ksl_key *rhs);
  342. /*
  343. * ngtcp2_ksl_range_exclusive_search is an implementation of
  344. * ngtcp2_ksl_search that uses ngtcp2_ksl_range_exclusive_compar.
  345. */
  346. size_t ngtcp2_ksl_range_exclusive_search(const ngtcp2_ksl *ksl,
  347. ngtcp2_ksl_blk *blk,
  348. const ngtcp2_ksl_key *key);
  349. /*
  350. * ngtcp2_ksl_uint64_less is an implementation of ngtcp2_ksl_compar.
  351. * |lhs| and |rhs| must point to uint64_t objects, and the function
  352. * returns nonzero if *(uint64_t *)|lhs| < *(uint64_t *)|rhs|.
  353. */
  354. int ngtcp2_ksl_uint64_less(const ngtcp2_ksl_key *lhs,
  355. const ngtcp2_ksl_key *rhs);
  356. /*
  357. * ngtcp2_ksl_uint64_less_search is an implementation of
  358. * ngtcp2_ksl_search that uses ngtcp2_ksl_uint64_less.
  359. */
  360. size_t ngtcp2_ksl_uint64_less_search(const ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk,
  361. const ngtcp2_ksl_key *key);
  362. /*
  363. * ngtcp2_ksl_int64_greater is an implementation of ngtcp2_ksl_compar.
  364. * |lhs| and |rhs| must point to int64_t objects, and the function
  365. * returns nonzero if *(int64_t *)|lhs| > *(int64_t *)|rhs|.
  366. */
  367. int ngtcp2_ksl_int64_greater(const ngtcp2_ksl_key *lhs,
  368. const ngtcp2_ksl_key *rhs);
  369. /*
  370. * ngtcp2_ksl_int64_greater_search is an implementation of
  371. * ngtcp2_ksl_search that uses ngtcp2_ksl_int64_greater.
  372. */
  373. size_t ngtcp2_ksl_int64_greater_search(const ngtcp2_ksl *ksl,
  374. ngtcp2_ksl_blk *blk,
  375. const ngtcp2_ksl_key *key);
  376. #endif /* !defined(NGTCP2_KSL_H) */