ngtcp2_ksl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. #include "ngtcp2_ksl.h"
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include <stdio.h>
  30. #include "ngtcp2_macro.h"
  31. #include "ngtcp2_mem.h"
  32. #include "ngtcp2_range.h"
  33. static ngtcp2_ksl_blk null_blk = {{{NULL, NULL, 0, 0, {0}}}};
  34. ngtcp2_objalloc_def(ksl_blk, ngtcp2_ksl_blk, oplent)
  35. static size_t ksl_nodelen(size_t keylen) {
  36. assert(keylen >= sizeof(uint64_t));
  37. return (sizeof(ngtcp2_ksl_node) + keylen - sizeof(uint64_t) + 0x7u) &
  38. ~(uintptr_t)0x7u;
  39. }
  40. static size_t ksl_blklen(size_t nodelen) {
  41. return sizeof(ngtcp2_ksl_blk) + nodelen * NGTCP2_KSL_MAX_NBLK -
  42. sizeof(uint64_t);
  43. }
  44. /*
  45. * ksl_node_set_key sets |key| to |node|.
  46. */
  47. static void ksl_node_set_key(ngtcp2_ksl *ksl, ngtcp2_ksl_node *node,
  48. const void *key) {
  49. memcpy(node->key, key, ksl->keylen);
  50. }
  51. void ngtcp2_ksl_init(ngtcp2_ksl *ksl, ngtcp2_ksl_compar compar,
  52. ngtcp2_ksl_search search, size_t keylen,
  53. const ngtcp2_mem *mem) {
  54. size_t nodelen = ksl_nodelen(keylen);
  55. ngtcp2_objalloc_init(&ksl->blkalloc,
  56. (ksl_blklen(nodelen) + 0xfu) & ~(uintptr_t)0xfu, mem);
  57. ksl->head = NULL;
  58. ksl->front = ksl->back = NULL;
  59. ksl->compar = compar;
  60. ksl->search = search;
  61. ksl->n = 0;
  62. ksl->keylen = keylen;
  63. ksl->nodelen = nodelen;
  64. }
  65. static ngtcp2_ksl_blk *ksl_blk_objalloc_new(ngtcp2_ksl *ksl) {
  66. return ngtcp2_objalloc_ksl_blk_len_get(&ksl->blkalloc,
  67. ksl_blklen(ksl->nodelen));
  68. }
  69. static void ksl_blk_objalloc_del(ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk) {
  70. ngtcp2_objalloc_ksl_blk_release(&ksl->blkalloc, blk);
  71. }
  72. static int ksl_head_init(ngtcp2_ksl *ksl) {
  73. ngtcp2_ksl_blk *head = ksl_blk_objalloc_new(ksl);
  74. if (!head) {
  75. return NGTCP2_ERR_NOMEM;
  76. }
  77. head->next = head->prev = NULL;
  78. head->n = 0;
  79. head->leaf = 1;
  80. ksl->head = head;
  81. ksl->front = ksl->back = head;
  82. return 0;
  83. }
  84. #ifdef NOMEMPOOL
  85. /*
  86. * ksl_free_blk frees |blk| recursively.
  87. */
  88. static void ksl_free_blk(ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk) {
  89. size_t i;
  90. if (!blk->leaf) {
  91. for (i = 0; i < blk->n; ++i) {
  92. ksl_free_blk(ksl, ngtcp2_ksl_nth_node(ksl, blk, i)->blk);
  93. }
  94. }
  95. ksl_blk_objalloc_del(ksl, blk);
  96. }
  97. #endif /* defined(NOMEMPOOL) */
  98. void ngtcp2_ksl_free(ngtcp2_ksl *ksl) {
  99. if (!ksl || !ksl->head) {
  100. return;
  101. }
  102. #ifdef NOMEMPOOL
  103. ksl_free_blk(ksl, ksl->head);
  104. #endif /* defined(NOMEMPOOL) */
  105. ngtcp2_objalloc_free(&ksl->blkalloc);
  106. }
  107. /*
  108. * ksl_split_blk splits |blk| into 2 ngtcp2_ksl_blk objects. The new
  109. * ngtcp2_ksl_blk is always the "right" block.
  110. *
  111. * It returns the pointer to the ngtcp2_ksl_blk created which is the
  112. * located at the right of |blk|, or NULL which indicates out of
  113. * memory error.
  114. */
  115. static ngtcp2_ksl_blk *ksl_split_blk(ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk) {
  116. ngtcp2_ksl_blk *rblk;
  117. rblk = ksl_blk_objalloc_new(ksl);
  118. if (rblk == NULL) {
  119. return NULL;
  120. }
  121. rblk->next = blk->next;
  122. blk->next = rblk;
  123. if (rblk->next) {
  124. rblk->next->prev = rblk;
  125. } else if (ksl->back == blk) {
  126. ksl->back = rblk;
  127. }
  128. rblk->prev = blk;
  129. rblk->leaf = blk->leaf;
  130. rblk->n = blk->n / 2;
  131. blk->n -= rblk->n;
  132. memcpy(rblk->nodes, blk->nodes + ksl->nodelen * blk->n,
  133. ksl->nodelen * rblk->n);
  134. assert(blk->n >= NGTCP2_KSL_MIN_NBLK);
  135. assert(rblk->n >= NGTCP2_KSL_MIN_NBLK);
  136. return rblk;
  137. }
  138. /*
  139. * ksl_split_node splits a node included in |blk| at the position |i|
  140. * into 2 adjacent nodes. The new node is always inserted at the
  141. * position |i+1|.
  142. *
  143. * It returns 0 if it succeeds, or one of the following negative error
  144. * codes:
  145. *
  146. * NGTCP2_ERR_NOMEM
  147. * Out of memory.
  148. */
  149. static int ksl_split_node(ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk, size_t i) {
  150. ngtcp2_ksl_node *node;
  151. ngtcp2_ksl_blk *lblk = ngtcp2_ksl_nth_node(ksl, blk, i)->blk, *rblk;
  152. rblk = ksl_split_blk(ksl, lblk);
  153. if (rblk == NULL) {
  154. return NGTCP2_ERR_NOMEM;
  155. }
  156. memmove(blk->nodes + (i + 2) * ksl->nodelen,
  157. blk->nodes + (i + 1) * ksl->nodelen,
  158. ksl->nodelen * (blk->n - (i + 1)));
  159. node = ngtcp2_ksl_nth_node(ksl, blk, i + 1);
  160. node->blk = rblk;
  161. ++blk->n;
  162. ksl_node_set_key(ksl, node, ngtcp2_ksl_nth_node(ksl, rblk, rblk->n - 1)->key);
  163. node = ngtcp2_ksl_nth_node(ksl, blk, i);
  164. ksl_node_set_key(ksl, node, ngtcp2_ksl_nth_node(ksl, lblk, lblk->n - 1)->key);
  165. return 0;
  166. }
  167. /*
  168. * ksl_split_head splits a head (root) block. It increases the height
  169. * of skip list by 1.
  170. *
  171. * It returns 0 if it succeeds, or one of the following negative error
  172. * codes:
  173. *
  174. * NGTCP2_ERR_NOMEM
  175. * Out of memory.
  176. */
  177. static int ksl_split_head(ngtcp2_ksl *ksl) {
  178. ngtcp2_ksl_blk *rblk = NULL, *lblk, *nhead = NULL;
  179. ngtcp2_ksl_node *node;
  180. rblk = ksl_split_blk(ksl, ksl->head);
  181. if (rblk == NULL) {
  182. return NGTCP2_ERR_NOMEM;
  183. }
  184. lblk = ksl->head;
  185. nhead = ksl_blk_objalloc_new(ksl);
  186. if (nhead == NULL) {
  187. ksl_blk_objalloc_del(ksl, rblk);
  188. return NGTCP2_ERR_NOMEM;
  189. }
  190. nhead->next = nhead->prev = NULL;
  191. nhead->n = 2;
  192. nhead->leaf = 0;
  193. node = ngtcp2_ksl_nth_node(ksl, nhead, 0);
  194. ksl_node_set_key(ksl, node, ngtcp2_ksl_nth_node(ksl, lblk, lblk->n - 1)->key);
  195. node->blk = lblk;
  196. node = ngtcp2_ksl_nth_node(ksl, nhead, 1);
  197. ksl_node_set_key(ksl, node, ngtcp2_ksl_nth_node(ksl, rblk, rblk->n - 1)->key);
  198. node->blk = rblk;
  199. ksl->head = nhead;
  200. return 0;
  201. }
  202. /*
  203. * ksl_insert_node inserts a node whose key is |key| with the
  204. * associated |data| at the index of |i|. This function assumes that
  205. * the number of nodes contained by |blk| is strictly less than
  206. * NGTCP2_KSL_MAX_NBLK.
  207. */
  208. static void ksl_insert_node(ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk, size_t i,
  209. const ngtcp2_ksl_key *key, void *data) {
  210. ngtcp2_ksl_node *node;
  211. assert(blk->n < NGTCP2_KSL_MAX_NBLK);
  212. memmove(blk->nodes + (i + 1) * ksl->nodelen, blk->nodes + i * ksl->nodelen,
  213. ksl->nodelen * (blk->n - i));
  214. node = ngtcp2_ksl_nth_node(ksl, blk, i);
  215. ksl_node_set_key(ksl, node, key);
  216. node->data = data;
  217. ++blk->n;
  218. }
  219. int ngtcp2_ksl_insert(ngtcp2_ksl *ksl, ngtcp2_ksl_it *it,
  220. const ngtcp2_ksl_key *key, void *data) {
  221. ngtcp2_ksl_blk *blk;
  222. ngtcp2_ksl_node *node;
  223. size_t i;
  224. int rv;
  225. if (!ksl->head) {
  226. rv = ksl_head_init(ksl);
  227. if (rv != 0) {
  228. return rv;
  229. }
  230. }
  231. if (ksl->head->n == NGTCP2_KSL_MAX_NBLK) {
  232. rv = ksl_split_head(ksl);
  233. if (rv != 0) {
  234. return rv;
  235. }
  236. }
  237. blk = ksl->head;
  238. for (;;) {
  239. i = ksl->search(ksl, blk, key);
  240. if (blk->leaf) {
  241. if (i < blk->n &&
  242. !ksl->compar(key, ngtcp2_ksl_nth_node(ksl, blk, i)->key)) {
  243. if (it) {
  244. *it = ngtcp2_ksl_end(ksl);
  245. }
  246. return NGTCP2_ERR_INVALID_ARGUMENT;
  247. }
  248. ksl_insert_node(ksl, blk, i, key, data);
  249. ++ksl->n;
  250. if (it) {
  251. ngtcp2_ksl_it_init(it, ksl, blk, i);
  252. }
  253. return 0;
  254. }
  255. if (i == blk->n) {
  256. /* This insertion extends the largest key in this subtree. */
  257. for (; !blk->leaf;) {
  258. node = ngtcp2_ksl_nth_node(ksl, blk, blk->n - 1);
  259. if (node->blk->n == NGTCP2_KSL_MAX_NBLK) {
  260. rv = ksl_split_node(ksl, blk, blk->n - 1);
  261. if (rv != 0) {
  262. return rv;
  263. }
  264. node = ngtcp2_ksl_nth_node(ksl, blk, blk->n - 1);
  265. }
  266. ksl_node_set_key(ksl, node, key);
  267. blk = node->blk;
  268. }
  269. ksl_insert_node(ksl, blk, blk->n, key, data);
  270. ++ksl->n;
  271. if (it) {
  272. ngtcp2_ksl_it_init(it, ksl, blk, blk->n - 1);
  273. }
  274. return 0;
  275. }
  276. node = ngtcp2_ksl_nth_node(ksl, blk, i);
  277. if (node->blk->n == NGTCP2_KSL_MAX_NBLK) {
  278. rv = ksl_split_node(ksl, blk, i);
  279. if (rv != 0) {
  280. return rv;
  281. }
  282. if (ksl->compar((ngtcp2_ksl_key *)node->key, key)) {
  283. node = ngtcp2_ksl_nth_node(ksl, blk, i + 1);
  284. if (ksl->compar((ngtcp2_ksl_key *)node->key, key)) {
  285. ksl_node_set_key(ksl, node, key);
  286. }
  287. }
  288. }
  289. blk = node->blk;
  290. }
  291. }
  292. /*
  293. * ksl_remove_node removes the node included in |blk| at the index of
  294. * |i|.
  295. */
  296. static void ksl_remove_node(ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk, size_t i) {
  297. memmove(blk->nodes + i * ksl->nodelen, blk->nodes + (i + 1) * ksl->nodelen,
  298. ksl->nodelen * (blk->n - (i + 1)));
  299. --blk->n;
  300. }
  301. /*
  302. * ksl_merge_node merges 2 nodes which are the nodes at the index of
  303. * |i| and |i + 1|.
  304. *
  305. * If |blk| is the head (root) block and it contains just 2 nodes
  306. * before merging nodes, the merged block becomes head block, which
  307. * decreases the height of |ksl| by 1.
  308. *
  309. * This function returns the pointer to the merged block.
  310. */
  311. static ngtcp2_ksl_blk *ksl_merge_node(ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk,
  312. size_t i) {
  313. ngtcp2_ksl_node *lnode;
  314. ngtcp2_ksl_blk *lblk, *rblk;
  315. assert(i + 1 < blk->n);
  316. lnode = ngtcp2_ksl_nth_node(ksl, blk, i);
  317. lblk = lnode->blk;
  318. rblk = ngtcp2_ksl_nth_node(ksl, blk, i + 1)->blk;
  319. assert(lblk->n + rblk->n < NGTCP2_KSL_MAX_NBLK);
  320. memcpy(lblk->nodes + ksl->nodelen * lblk->n, rblk->nodes,
  321. ksl->nodelen * rblk->n);
  322. lblk->n += rblk->n;
  323. lblk->next = rblk->next;
  324. if (lblk->next) {
  325. lblk->next->prev = lblk;
  326. } else if (ksl->back == rblk) {
  327. ksl->back = lblk;
  328. }
  329. ksl_blk_objalloc_del(ksl, rblk);
  330. if (ksl->head == blk && blk->n == 2) {
  331. ksl_blk_objalloc_del(ksl, ksl->head);
  332. ksl->head = lblk;
  333. } else {
  334. ksl_remove_node(ksl, blk, i + 1);
  335. ksl_node_set_key(ksl, lnode,
  336. ngtcp2_ksl_nth_node(ksl, lblk, lblk->n - 1)->key);
  337. }
  338. return lblk;
  339. }
  340. /*
  341. * ksl_shift_left moves the first nodes in blk->nodes[i]->blk->nodes
  342. * to blk->nodes[i - 1]->blk->nodes in a manner that they have the
  343. * same amount of nodes as much as possible.
  344. */
  345. static void ksl_shift_left(ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk, size_t i) {
  346. ngtcp2_ksl_node *lnode, *rnode;
  347. ngtcp2_ksl_blk *lblk, *rblk;
  348. size_t n;
  349. assert(i > 0);
  350. lnode = ngtcp2_ksl_nth_node(ksl, blk, i - 1);
  351. rnode = ngtcp2_ksl_nth_node(ksl, blk, i);
  352. lblk = lnode->blk;
  353. rblk = rnode->blk;
  354. assert(lblk->n < NGTCP2_KSL_MAX_NBLK);
  355. assert(rblk->n > NGTCP2_KSL_MIN_NBLK);
  356. n = (lblk->n + rblk->n + 1) / 2 - lblk->n;
  357. assert(n > 0);
  358. assert(lblk->n <= NGTCP2_KSL_MAX_NBLK - n);
  359. assert(rblk->n >= NGTCP2_KSL_MIN_NBLK + n);
  360. memcpy(lblk->nodes + ksl->nodelen * lblk->n, rblk->nodes, ksl->nodelen * n);
  361. lblk->n += (uint32_t)n;
  362. rblk->n -= (uint32_t)n;
  363. ksl_node_set_key(ksl, lnode,
  364. ngtcp2_ksl_nth_node(ksl, lblk, lblk->n - 1)->key);
  365. memmove(rblk->nodes, rblk->nodes + ksl->nodelen * n, ksl->nodelen * rblk->n);
  366. }
  367. /*
  368. * ksl_shift_right moves the last nodes in blk->nodes[i]->blk->nodes
  369. * to blk->nodes[i + 1]->blk->nodes in a manner that they have the
  370. * same amount of nodes as much as possible.
  371. */
  372. static void ksl_shift_right(ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk, size_t i) {
  373. ngtcp2_ksl_node *lnode, *rnode;
  374. ngtcp2_ksl_blk *lblk, *rblk;
  375. size_t n;
  376. assert(i < blk->n - 1);
  377. lnode = ngtcp2_ksl_nth_node(ksl, blk, i);
  378. rnode = ngtcp2_ksl_nth_node(ksl, blk, i + 1);
  379. lblk = lnode->blk;
  380. rblk = rnode->blk;
  381. assert(lblk->n > NGTCP2_KSL_MIN_NBLK);
  382. assert(rblk->n < NGTCP2_KSL_MAX_NBLK);
  383. n = (lblk->n + rblk->n + 1) / 2 - rblk->n;
  384. assert(n > 0);
  385. assert(lblk->n >= NGTCP2_KSL_MIN_NBLK + n);
  386. assert(rblk->n <= NGTCP2_KSL_MAX_NBLK - n);
  387. memmove(rblk->nodes + ksl->nodelen * n, rblk->nodes, ksl->nodelen * rblk->n);
  388. rblk->n += (uint32_t)n;
  389. lblk->n -= (uint32_t)n;
  390. memcpy(rblk->nodes, lblk->nodes + ksl->nodelen * lblk->n, ksl->nodelen * n);
  391. ksl_node_set_key(ksl, lnode,
  392. ngtcp2_ksl_nth_node(ksl, lblk, lblk->n - 1)->key);
  393. }
  394. /*
  395. * key_equal returns nonzero if |lhs| and |rhs| are equal using the
  396. * function |compar|.
  397. */
  398. static int key_equal(ngtcp2_ksl_compar compar, const ngtcp2_ksl_key *lhs,
  399. const ngtcp2_ksl_key *rhs) {
  400. return !compar(lhs, rhs) && !compar(rhs, lhs);
  401. }
  402. int ngtcp2_ksl_remove_hint(ngtcp2_ksl *ksl, ngtcp2_ksl_it *it,
  403. const ngtcp2_ksl_it *hint,
  404. const ngtcp2_ksl_key *key) {
  405. ngtcp2_ksl_blk *blk = hint->blk;
  406. assert(ksl->head);
  407. if (blk->n <= NGTCP2_KSL_MIN_NBLK) {
  408. return ngtcp2_ksl_remove(ksl, it, key);
  409. }
  410. ksl_remove_node(ksl, blk, hint->i);
  411. --ksl->n;
  412. if (it) {
  413. if (hint->i == blk->n && blk->next) {
  414. ngtcp2_ksl_it_init(it, ksl, blk->next, 0);
  415. } else {
  416. ngtcp2_ksl_it_init(it, ksl, blk, hint->i);
  417. }
  418. }
  419. return 0;
  420. }
  421. int ngtcp2_ksl_remove(ngtcp2_ksl *ksl, ngtcp2_ksl_it *it,
  422. const ngtcp2_ksl_key *key) {
  423. ngtcp2_ksl_blk *blk = ksl->head;
  424. ngtcp2_ksl_node *node;
  425. size_t i;
  426. if (!blk) {
  427. return NGTCP2_ERR_INVALID_ARGUMENT;
  428. }
  429. if (!blk->leaf && blk->n == 2 &&
  430. ngtcp2_ksl_nth_node(ksl, blk, 0)->blk->n == NGTCP2_KSL_MIN_NBLK &&
  431. ngtcp2_ksl_nth_node(ksl, blk, 1)->blk->n == NGTCP2_KSL_MIN_NBLK) {
  432. blk = ksl_merge_node(ksl, blk, 0);
  433. }
  434. for (;;) {
  435. i = ksl->search(ksl, blk, key);
  436. if (i == blk->n) {
  437. if (it) {
  438. *it = ngtcp2_ksl_end(ksl);
  439. }
  440. return NGTCP2_ERR_INVALID_ARGUMENT;
  441. }
  442. if (blk->leaf) {
  443. if (ksl->compar(key, ngtcp2_ksl_nth_node(ksl, blk, i)->key)) {
  444. if (it) {
  445. *it = ngtcp2_ksl_end(ksl);
  446. }
  447. return NGTCP2_ERR_INVALID_ARGUMENT;
  448. }
  449. ksl_remove_node(ksl, blk, i);
  450. --ksl->n;
  451. if (it) {
  452. if (blk->n == i && blk->next) {
  453. ngtcp2_ksl_it_init(it, ksl, blk->next, 0);
  454. } else {
  455. ngtcp2_ksl_it_init(it, ksl, blk, i);
  456. }
  457. }
  458. return 0;
  459. }
  460. node = ngtcp2_ksl_nth_node(ksl, blk, i);
  461. if (node->blk->n > NGTCP2_KSL_MIN_NBLK) {
  462. blk = node->blk;
  463. continue;
  464. }
  465. assert(node->blk->n == NGTCP2_KSL_MIN_NBLK);
  466. if (i + 1 < blk->n &&
  467. ngtcp2_ksl_nth_node(ksl, blk, i + 1)->blk->n > NGTCP2_KSL_MIN_NBLK) {
  468. ksl_shift_left(ksl, blk, i + 1);
  469. blk = node->blk;
  470. continue;
  471. }
  472. if (i > 0 &&
  473. ngtcp2_ksl_nth_node(ksl, blk, i - 1)->blk->n > NGTCP2_KSL_MIN_NBLK) {
  474. ksl_shift_right(ksl, blk, i - 1);
  475. blk = node->blk;
  476. continue;
  477. }
  478. if (i + 1 < blk->n) {
  479. blk = ksl_merge_node(ksl, blk, i);
  480. continue;
  481. }
  482. assert(i > 0);
  483. blk = ksl_merge_node(ksl, blk, i - 1);
  484. }
  485. }
  486. ngtcp2_ksl_it ngtcp2_ksl_lower_bound(const ngtcp2_ksl *ksl,
  487. const ngtcp2_ksl_key *key) {
  488. return ngtcp2_ksl_lower_bound_search(ksl, key, ksl->search);
  489. }
  490. ngtcp2_ksl_it ngtcp2_ksl_lower_bound_search(const ngtcp2_ksl *ksl,
  491. const ngtcp2_ksl_key *key,
  492. ngtcp2_ksl_search search) {
  493. ngtcp2_ksl_blk *blk = ksl->head;
  494. ngtcp2_ksl_it it;
  495. size_t i;
  496. if (!blk) {
  497. ngtcp2_ksl_it_init(&it, ksl, &null_blk, 0);
  498. return it;
  499. }
  500. for (;;) {
  501. i = search(ksl, blk, key);
  502. if (blk->leaf) {
  503. if (i == blk->n && blk->next) {
  504. blk = blk->next;
  505. i = 0;
  506. }
  507. ngtcp2_ksl_it_init(&it, ksl, blk, i);
  508. return it;
  509. }
  510. if (i == blk->n) {
  511. /* This happens if descendant has smaller key. Fast forward to
  512. find last node in this subtree. */
  513. for (; !blk->leaf; blk = ngtcp2_ksl_nth_node(ksl, blk, blk->n - 1)->blk)
  514. ;
  515. if (blk->next) {
  516. blk = blk->next;
  517. i = 0;
  518. } else {
  519. i = blk->n;
  520. }
  521. ngtcp2_ksl_it_init(&it, ksl, blk, i);
  522. return it;
  523. }
  524. blk = ngtcp2_ksl_nth_node(ksl, blk, i)->blk;
  525. }
  526. }
  527. void ngtcp2_ksl_update_key(ngtcp2_ksl *ksl, const ngtcp2_ksl_key *old_key,
  528. const ngtcp2_ksl_key *new_key) {
  529. ngtcp2_ksl_blk *blk = ksl->head;
  530. ngtcp2_ksl_node *node;
  531. size_t i;
  532. assert(ksl->head);
  533. for (;;) {
  534. i = ksl->search(ksl, blk, old_key);
  535. assert(i < blk->n);
  536. node = ngtcp2_ksl_nth_node(ksl, blk, i);
  537. if (blk->leaf) {
  538. assert(key_equal(ksl->compar, (ngtcp2_ksl_key *)node->key, old_key));
  539. ksl_node_set_key(ksl, node, new_key);
  540. return;
  541. }
  542. if (key_equal(ksl->compar, (ngtcp2_ksl_key *)node->key, old_key) ||
  543. ksl->compar((ngtcp2_ksl_key *)node->key, new_key)) {
  544. ksl_node_set_key(ksl, node, new_key);
  545. }
  546. blk = node->blk;
  547. }
  548. }
  549. size_t ngtcp2_ksl_len(const ngtcp2_ksl *ksl) { return ksl->n; }
  550. void ngtcp2_ksl_clear(ngtcp2_ksl *ksl) {
  551. if (!ksl->head) {
  552. return;
  553. }
  554. #ifdef NOMEMPOOL
  555. ksl_free_blk(ksl, ksl->head);
  556. #endif /* defined(NOMEMPOOL) */
  557. ksl->front = ksl->back = ksl->head = NULL;
  558. ksl->n = 0;
  559. ngtcp2_objalloc_clear(&ksl->blkalloc);
  560. }
  561. #ifndef WIN32
  562. static void ksl_print(const ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk,
  563. size_t level) {
  564. size_t i;
  565. ngtcp2_ksl_node *node;
  566. fprintf(stderr, "LV=%zu n=%u\n", level, blk->n);
  567. if (blk->leaf) {
  568. for (i = 0; i < blk->n; ++i) {
  569. node = ngtcp2_ksl_nth_node(ksl, blk, i);
  570. fprintf(stderr, " %" PRId64, *(int64_t *)(void *)node->key);
  571. }
  572. fprintf(stderr, "\n");
  573. return;
  574. }
  575. for (i = 0; i < blk->n; ++i) {
  576. ksl_print(ksl, ngtcp2_ksl_nth_node(ksl, blk, i)->blk, level + 1);
  577. }
  578. }
  579. void ngtcp2_ksl_print(const ngtcp2_ksl *ksl) {
  580. if (!ksl->head) {
  581. return;
  582. }
  583. ksl_print(ksl, ksl->head, 0);
  584. }
  585. #endif /* !defined(WIN32) */
  586. ngtcp2_ksl_it ngtcp2_ksl_begin(const ngtcp2_ksl *ksl) {
  587. ngtcp2_ksl_it it;
  588. if (ksl->head) {
  589. ngtcp2_ksl_it_init(&it, ksl, ksl->front, 0);
  590. } else {
  591. ngtcp2_ksl_it_init(&it, ksl, &null_blk, 0);
  592. }
  593. return it;
  594. }
  595. ngtcp2_ksl_it ngtcp2_ksl_end(const ngtcp2_ksl *ksl) {
  596. ngtcp2_ksl_it it;
  597. if (ksl->head) {
  598. ngtcp2_ksl_it_init(&it, ksl, ksl->back, ksl->back->n);
  599. } else {
  600. ngtcp2_ksl_it_init(&it, ksl, &null_blk, 0);
  601. }
  602. return it;
  603. }
  604. void ngtcp2_ksl_it_init(ngtcp2_ksl_it *it, const ngtcp2_ksl *ksl,
  605. ngtcp2_ksl_blk *blk, size_t i) {
  606. it->ksl = ksl;
  607. it->blk = blk;
  608. it->i = i;
  609. }
  610. void ngtcp2_ksl_it_prev(ngtcp2_ksl_it *it) {
  611. assert(!ngtcp2_ksl_it_begin(it));
  612. if (it->i == 0) {
  613. it->blk = it->blk->prev;
  614. it->i = it->blk->n - 1;
  615. } else {
  616. --it->i;
  617. }
  618. }
  619. int ngtcp2_ksl_it_begin(const ngtcp2_ksl_it *it) {
  620. return it->i == 0 && it->blk->prev == NULL;
  621. }
  622. int ngtcp2_ksl_range_compar(const ngtcp2_ksl_key *lhs,
  623. const ngtcp2_ksl_key *rhs) {
  624. const ngtcp2_range *a = lhs, *b = rhs;
  625. return a->begin < b->begin;
  626. }
  627. ngtcp2_ksl_search_def(range, ngtcp2_ksl_range_compar)
  628. size_t ngtcp2_ksl_range_search(const ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk,
  629. const ngtcp2_ksl_key *key) {
  630. return ksl_range_search(ksl, blk, key);
  631. }
  632. int ngtcp2_ksl_range_exclusive_compar(const ngtcp2_ksl_key *lhs,
  633. const ngtcp2_ksl_key *rhs) {
  634. const ngtcp2_range *a = lhs, *b = rhs;
  635. return a->begin < b->begin && !(ngtcp2_max_uint64(a->begin, b->begin) <
  636. ngtcp2_min_uint64(a->end, b->end));
  637. }
  638. ngtcp2_ksl_search_def(range_exclusive, ngtcp2_ksl_range_exclusive_compar)
  639. size_t ngtcp2_ksl_range_exclusive_search(const ngtcp2_ksl *ksl,
  640. ngtcp2_ksl_blk *blk,
  641. const ngtcp2_ksl_key *key) {
  642. return ksl_range_exclusive_search(ksl, blk, key);
  643. }
  644. int ngtcp2_ksl_uint64_less(const ngtcp2_ksl_key *lhs,
  645. const ngtcp2_ksl_key *rhs) {
  646. return *(uint64_t *)lhs < *(uint64_t *)rhs;
  647. }
  648. ngtcp2_ksl_search_def(uint64_less, ngtcp2_ksl_uint64_less)
  649. size_t ngtcp2_ksl_uint64_less_search(const ngtcp2_ksl *ksl, ngtcp2_ksl_blk *blk,
  650. const ngtcp2_ksl_key *key) {
  651. return ksl_uint64_less_search(ksl, blk, key);
  652. }
  653. int ngtcp2_ksl_int64_greater(const ngtcp2_ksl_key *lhs,
  654. const ngtcp2_ksl_key *rhs) {
  655. return *(int64_t *)lhs > *(int64_t *)rhs;
  656. }
  657. ngtcp2_ksl_search_def(int64_greater, ngtcp2_ksl_int64_greater)
  658. size_t ngtcp2_ksl_int64_greater_search(const ngtcp2_ksl *ksl,
  659. ngtcp2_ksl_blk *blk,
  660. const ngtcp2_ksl_key *key) {
  661. return ksl_int64_greater_search(ksl, blk, key);
  662. }