rdxtree.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Copyright (c) 2011-2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Upstream site with license notes :
  18. * http://git.sceen.net/rbraun/librbraun.git/
  19. */
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <limits.h>
  23. #include <stdbool.h>
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <kern/init.h>
  28. #include <kern/kmem.h>
  29. #include <kern/macros.h>
  30. #include <kern/rcu.h>
  31. #include <kern/rdxtree.h>
  32. #include <kern/work.h>
  33. // Mask applied on an entry to obtain its address.
  34. #define RDXTREE_ENTRY_ADDR_MASK (~0x3UL)
  35. // Global properties used to shape radix trees.
  36. #define RDXTREE_RADIX 6
  37. #define RDXTREE_RADIX_SIZE (1UL << RDXTREE_RADIX)
  38. #define RDXTREE_RADIX_MASK (RDXTREE_RADIX_SIZE - 1)
  39. #if RDXTREE_RADIX < 6
  40. typedef unsigned long rdxtree_bm_t;
  41. #define rdxtree_ffs __builtin_ffsl
  42. #elif RDXTREE_RADIX == 6
  43. typedef unsigned long long rdxtree_bm_t;
  44. #define rdxtree_ffs __builtin_ffsll
  45. #else
  46. #error "radix too high"
  47. #endif
  48. // Allocation bitmap size in bits.
  49. #define RDXTREE_BM_SIZE (sizeof (rdxtree_bm_t) * CHAR_BIT)
  50. /*
  51. * Empty/full allocation bitmap words.
  52. */
  53. #define RDXTREE_BM_EMPTY ((rdxtree_bm_t)0)
  54. #define RDXTREE_BM_FULL \
  55. ((~(rdxtree_bm_t)0) >> (RDXTREE_BM_SIZE - RDXTREE_RADIX_SIZE))
  56. /*
  57. * Radix tree node.
  58. *
  59. * The height of a tree is the number of nodes to traverse until stored
  60. * pointers are reached. A height of 0 means the entries of a node (or the
  61. * tree root) directly point to stored pointers.
  62. *
  63. * The index is valid if and only if the parent isn't NULL.
  64. *
  65. * Concerning the allocation bitmap, a bit is set when the node it denotes,
  66. * or one of its children, can be used to allocate an entry. Conversely, a bit
  67. * is clear when the matching node and all of its children have no free entry.
  68. *
  69. * In order to support safe lockless lookups, in particular during a resize,
  70. * each node includes the height of its subtree, which is invariant during
  71. * the entire node lifetime. Since the tree height does vary, it can't be
  72. * used to determine whether the tree root is a node or a stored pointer.
  73. * This implementation assumes that all nodes and stored pointers are at least
  74. * 4-byte aligned, and uses the least significant bit of entries to indicate
  75. * the pointer type. This bit is set for internal nodes, and clear for stored
  76. * pointers so that they can be accessed from slots without conversion.
  77. */
  78. struct rdxtree_node
  79. {
  80. union
  81. {
  82. struct
  83. {
  84. struct rdxtree_node *parent;
  85. uint16_t index;
  86. };
  87. // Deferred destruction when unlinked.
  88. struct work work;
  89. };
  90. uint16_t height;
  91. uint16_t nr_entries;
  92. rdxtree_bm_t alloc_bm;
  93. void *entries[RDXTREE_RADIX_SIZE];
  94. };
  95. static struct kmem_cache rdxtree_node_cache;
  96. static bool
  97. rdxtree_alignment_valid (const void *ptr)
  98. {
  99. return (((uintptr_t)ptr & ~RDXTREE_ENTRY_ADDR_MASK) == 0);
  100. }
  101. static inline void*
  102. rdxtree_entry_addr (void *entry)
  103. {
  104. return ((void *)((uintptr_t)entry & RDXTREE_ENTRY_ADDR_MASK));
  105. }
  106. static inline bool
  107. rdxtree_entry_is_node (const void *entry)
  108. {
  109. return (((uintptr_t)entry & 1) != 0);
  110. }
  111. static inline void*
  112. rdxtree_node_to_entry (struct rdxtree_node *node)
  113. {
  114. return ((void *)((uintptr_t)node | 1));
  115. }
  116. static void
  117. rdxtree_node_ctor (void *buf)
  118. {
  119. struct rdxtree_node *node = buf;
  120. node->nr_entries = 0;
  121. node->alloc_bm = RDXTREE_BM_FULL;
  122. memset (node->entries, 0, sizeof (node->entries));
  123. }
  124. static int
  125. rdxtree_node_create (struct rdxtree_node **nodep,
  126. uint16_t height, uint16_t flags)
  127. {
  128. struct rdxtree_node *node =
  129. kmem_cache_alloc2 (&rdxtree_node_cache,
  130. (flags & RDXTREE_ALLOC_SLEEP) ?
  131. KMEM_ALLOC_SLEEP : 0);
  132. if (! node)
  133. return (ENOMEM);
  134. assert (rdxtree_alignment_valid (node));
  135. node->parent = NULL;
  136. node->height = height;
  137. *nodep = node;
  138. return (0);
  139. }
  140. static void
  141. rdxtree_node_destroy (struct rdxtree_node *node)
  142. {
  143. // See rdxtree_shrink().
  144. if (node->nr_entries)
  145. {
  146. assert (node->entries[0]);
  147. for (uint32_t i = 0; i < node->nr_entries; ++i)
  148. node->entries[i] = NULL;
  149. node->nr_entries = 0;
  150. node->alloc_bm = RDXTREE_BM_FULL;
  151. }
  152. kmem_cache_free (&rdxtree_node_cache, node);
  153. }
  154. static void
  155. rdxtree_node_destroy_deferred (struct work *work)
  156. {
  157. rdxtree_node_destroy (structof (work, struct rdxtree_node, work));
  158. }
  159. static void
  160. rdxtree_node_schedule_destruction (struct rdxtree_node *node)
  161. {
  162. assert (!node->parent);
  163. work_init (&node->work, rdxtree_node_destroy_deferred);
  164. rcu_defer (&node->work);
  165. }
  166. static inline void
  167. rdxtree_node_link (struct rdxtree_node *node, struct rdxtree_node *parent,
  168. uint16_t index)
  169. {
  170. node->parent = parent;
  171. node->index = index;
  172. }
  173. static inline void
  174. rdxtree_node_unlink (struct rdxtree_node *node)
  175. {
  176. assert (node->parent);
  177. node->parent = NULL;
  178. }
  179. static inline bool
  180. rdxtree_node_full (struct rdxtree_node *node)
  181. {
  182. return (node->nr_entries == ARRAY_SIZE (node->entries));
  183. }
  184. static inline bool
  185. rdxtree_node_empty (struct rdxtree_node *node)
  186. {
  187. return (!node->nr_entries);
  188. }
  189. static inline void
  190. rdxtree_node_insert (struct rdxtree_node *node, uint16_t index, void *entry)
  191. {
  192. assert (index < ARRAY_SIZE (node->entries));
  193. assert (!node->entries[index]);
  194. ++node->nr_entries;
  195. rcu_store (&node->entries[index], entry);
  196. }
  197. static inline void
  198. rdxtree_node_insert_node (struct rdxtree_node *node, uint16_t index,
  199. struct rdxtree_node *child)
  200. {
  201. rdxtree_node_insert (node, index, rdxtree_node_to_entry (child));
  202. }
  203. static inline void
  204. rdxtree_node_remove (struct rdxtree_node *node, uint16_t index)
  205. {
  206. assert (index < ARRAY_SIZE (node->entries));
  207. assert (node->entries[index]);
  208. --node->nr_entries;
  209. rcu_store (&node->entries[index], NULL);
  210. }
  211. static inline void*
  212. rdxtree_node_find (struct rdxtree_node *node, uint16_t *indexp)
  213. {
  214. for (uint16_t index = *indexp; index < ARRAY_SIZE (node->entries); ++index)
  215. {
  216. void *ptr = rdxtree_entry_addr (rcu_load (&node->entries[index]));
  217. if (ptr)
  218. {
  219. *indexp = index;
  220. return (ptr);
  221. }
  222. }
  223. return (NULL);
  224. }
  225. static inline void
  226. rdxtree_node_bm_set (struct rdxtree_node *node, uint16_t index)
  227. {
  228. node->alloc_bm |= (rdxtree_bm_t)1 << index;
  229. }
  230. static inline void
  231. rdxtree_node_bm_clear (struct rdxtree_node *node, uint16_t index)
  232. {
  233. node->alloc_bm &= ~ ((rdxtree_bm_t)1 << index);
  234. }
  235. static inline bool
  236. rdxtree_node_bm_is_set (struct rdxtree_node *node, uint16_t index)
  237. {
  238. return (node->alloc_bm & ((rdxtree_bm_t)1 << index));
  239. }
  240. static inline bool
  241. rdxtree_node_bm_empty (struct rdxtree_node *node)
  242. {
  243. return (node->alloc_bm == RDXTREE_BM_EMPTY);
  244. }
  245. static inline uint16_t
  246. rdxtree_node_bm_first (struct rdxtree_node *node)
  247. {
  248. return (rdxtree_ffs (node->alloc_bm) - 1);
  249. }
  250. static inline rdxtree_key_t
  251. rdxtree_max_key (uint16_t height)
  252. {
  253. size_t shift = RDXTREE_RADIX * height;
  254. if (likely (shift < sizeof (rdxtree_key_t) * CHAR_BIT))
  255. return (((rdxtree_key_t)1 << shift) - 1);
  256. else
  257. return (~((rdxtree_key_t)0));
  258. }
  259. static inline bool
  260. rdxtree_key_alloc_enabled (const struct rdxtree *tree)
  261. {
  262. return (tree->flags & RDXTREE_KEY_ALLOC);
  263. }
  264. static void
  265. rdxtree_shrink (struct rdxtree *tree)
  266. {
  267. while (tree->height > 0)
  268. {
  269. struct rdxtree_node *node = rdxtree_entry_addr (tree->root);
  270. if (node->nr_entries != 1)
  271. break;
  272. void *entry = node->entries[0];
  273. if (! entry)
  274. break;
  275. else if (--tree->height > 0)
  276. rdxtree_node_unlink (rdxtree_entry_addr (entry));
  277. rcu_store (&tree->root, entry);
  278. /*
  279. * There is still one valid entry (the first one) in this node. It
  280. * must remain valid as long as read-side references can exist so
  281. * that concurrent lookups can find the rest of the tree. Therefore,
  282. * this entry isn't reset before node destruction.
  283. */
  284. rdxtree_node_schedule_destruction (node);
  285. }
  286. }
  287. static int
  288. rdxtree_grow (struct rdxtree *tree, rdxtree_key_t key)
  289. {
  290. uint16_t new_height = tree->height + 1;
  291. while (key > rdxtree_max_key (new_height))
  292. new_height++;
  293. if (!tree->root)
  294. {
  295. tree->height = new_height;
  296. return (0);
  297. }
  298. struct rdxtree_node *root = rdxtree_entry_addr (tree->root);
  299. do
  300. {
  301. struct rdxtree_node *node;
  302. int error = rdxtree_node_create (&node, tree->height, tree->flags);
  303. if (error)
  304. {
  305. rdxtree_shrink (tree);
  306. return (error);
  307. }
  308. else if (tree->height)
  309. {
  310. rdxtree_node_link (root, node, 0);
  311. if (rdxtree_key_alloc_enabled (tree) &&
  312. rdxtree_node_bm_empty (root))
  313. rdxtree_node_bm_clear (node, 0);
  314. }
  315. else if (rdxtree_key_alloc_enabled (tree))
  316. rdxtree_node_bm_clear (node, 0);
  317. rdxtree_node_insert (node, 0, tree->root);
  318. ++tree->height;
  319. rcu_store (&tree->root, rdxtree_node_to_entry (node));
  320. root = node;
  321. }
  322. while (new_height > tree->height);
  323. return (0);
  324. }
  325. static void
  326. rdxtree_cleanup (struct rdxtree *tree, struct rdxtree_node *node)
  327. {
  328. while (1)
  329. {
  330. if (likely (!rdxtree_node_empty (node)))
  331. {
  332. if (unlikely (!node->parent))
  333. rdxtree_shrink (tree);
  334. break;
  335. }
  336. if (!node->parent)
  337. {
  338. tree->height = 0;
  339. rcu_store (&tree->root, NULL);
  340. rdxtree_node_schedule_destruction (node);
  341. break;
  342. }
  343. struct rdxtree_node *prev = node;
  344. node = node->parent;
  345. rdxtree_node_unlink (prev);
  346. rdxtree_node_remove (node, prev->index);
  347. rdxtree_node_schedule_destruction (prev);
  348. }
  349. }
  350. static void
  351. rdxtree_insert_bm_clear (struct rdxtree_node *node, uint16_t index)
  352. {
  353. while (1)
  354. {
  355. rdxtree_node_bm_clear (node, index);
  356. if (!rdxtree_node_full (node) || !node->parent)
  357. break;
  358. index = node->index;
  359. node = node->parent;
  360. }
  361. }
  362. int
  363. rdxtree_insert_common (struct rdxtree *tree, rdxtree_key_t key,
  364. void *ptr, void ***slotp)
  365. {
  366. assert (ptr);
  367. assert (rdxtree_alignment_valid (ptr));
  368. if (unlikely (key > rdxtree_max_key (tree->height)))
  369. {
  370. int error = rdxtree_grow (tree, key);
  371. if (error)
  372. return (error);
  373. }
  374. uint16_t height = tree->height;
  375. if (unlikely (! height))
  376. {
  377. if (slotp)
  378. *slotp = &tree->root;
  379. if (tree->root)
  380. return (EBUSY);
  381. rcu_store (&tree->root, ptr);
  382. return (0);
  383. }
  384. struct rdxtree_node *node = rdxtree_entry_addr (tree->root),
  385. *prev = NULL;
  386. uint16_t index = 0, shift = (height - 1) * RDXTREE_RADIX;
  387. do
  388. {
  389. if (! node)
  390. {
  391. int error = rdxtree_node_create (&node, height - 1, tree->flags);
  392. if (error)
  393. {
  394. if (! prev)
  395. tree->height = 0;
  396. else
  397. rdxtree_cleanup (tree, prev);
  398. return (error);
  399. }
  400. if (! prev)
  401. rcu_store (&tree->root, rdxtree_node_to_entry (node));
  402. else
  403. {
  404. rdxtree_node_link (node, prev, index);
  405. rdxtree_node_insert_node (prev, index, node);
  406. }
  407. }
  408. prev = node;
  409. index = (uint16_t)(key >> shift) & RDXTREE_RADIX_MASK;
  410. node = rdxtree_entry_addr (prev->entries[index]);
  411. shift -= RDXTREE_RADIX;
  412. --height;
  413. }
  414. while (height > 0);
  415. if (slotp)
  416. *slotp = &prev->entries[index];
  417. if (unlikely (node))
  418. return (EBUSY);
  419. rdxtree_node_insert (prev, index, ptr);
  420. if (rdxtree_key_alloc_enabled (tree))
  421. rdxtree_insert_bm_clear (prev, index);
  422. return (0);
  423. }
  424. int
  425. rdxtree_insert_alloc_common (struct rdxtree *tree, void *ptr,
  426. rdxtree_key_t *keyp, void ***slotp)
  427. {
  428. rdxtree_key_t key;
  429. int error;
  430. assert (rdxtree_key_alloc_enabled (tree));
  431. assert (ptr);
  432. assert (rdxtree_alignment_valid (ptr));
  433. uint16_t height = tree->height;
  434. if (unlikely (! height))
  435. {
  436. if (!tree->root)
  437. {
  438. rcu_store (&tree->root, ptr);
  439. *keyp = 0;
  440. if (slotp != NULL)
  441. *slotp = &tree->root;
  442. return (0);
  443. }
  444. goto grow;
  445. }
  446. struct rdxtree_node *node = rdxtree_entry_addr (tree->root),
  447. *prev = NULL;
  448. uint16_t index = 0, shift = (height - 1) * RDXTREE_RADIX;
  449. key = 0;
  450. do
  451. {
  452. if (! node)
  453. {
  454. error = rdxtree_node_create (&node, height - 1, tree->flags);
  455. if (error)
  456. {
  457. rdxtree_cleanup (tree, prev);
  458. return (error);
  459. }
  460. rdxtree_node_link (node, prev, index);
  461. rdxtree_node_insert_node (prev, index, node);
  462. }
  463. prev = node;
  464. index = rdxtree_node_bm_first (node);
  465. if (index == UINT16_MAX)
  466. goto grow;
  467. key |= (rdxtree_key_t)index << shift;
  468. node = rdxtree_entry_addr (node->entries[index]);
  469. shift -= RDXTREE_RADIX;
  470. --height;
  471. }
  472. while (height > 0);
  473. rdxtree_node_insert (prev, index, ptr);
  474. rdxtree_insert_bm_clear (prev, index);
  475. if (slotp)
  476. *slotp = &prev->entries[index];
  477. goto out;
  478. grow:
  479. key = rdxtree_max_key (height) + 1;
  480. error = rdxtree_insert_common (tree, key, ptr, slotp);
  481. if (error)
  482. return (error);
  483. out:
  484. *keyp = key;
  485. return (0);
  486. }
  487. static void
  488. rdxtree_remove_bm_set (struct rdxtree_node *node, uint16_t index)
  489. {
  490. do
  491. {
  492. rdxtree_node_bm_set (node, index);
  493. if (!node->parent)
  494. break;
  495. index = node->index;
  496. node = node->parent;
  497. }
  498. while (!rdxtree_node_bm_is_set (node, index));
  499. }
  500. void*
  501. rdxtree_remove (struct rdxtree *tree, rdxtree_key_t key)
  502. {
  503. uint16_t height = tree->height;
  504. if (unlikely (key > rdxtree_max_key (height)))
  505. return (NULL);
  506. struct rdxtree_node *prev, *node = rdxtree_entry_addr (tree->root);
  507. if (unlikely (! height))
  508. {
  509. rcu_store (&tree->root, NULL);
  510. return (node);
  511. }
  512. uint16_t index, shift = (height - 1) * RDXTREE_RADIX;
  513. do
  514. {
  515. if (! node)
  516. return (NULL);
  517. prev = node;
  518. index = (uint16_t) (key >> shift) & RDXTREE_RADIX_MASK;
  519. node = rdxtree_entry_addr (node->entries[index]);
  520. shift -= RDXTREE_RADIX;
  521. --height;
  522. }
  523. while (height > 0);
  524. if (! node)
  525. return (NULL);
  526. if (rdxtree_key_alloc_enabled (tree))
  527. rdxtree_remove_bm_set (prev, index);
  528. rdxtree_node_remove (prev, index);
  529. rdxtree_cleanup (tree, prev);
  530. return (node);
  531. }
  532. void*
  533. rdxtree_lookup_common (const struct rdxtree *tree, rdxtree_key_t key,
  534. bool get_slot)
  535. {
  536. struct rdxtree_node *node;
  537. uint16_t height;
  538. void *entry = rcu_load (&tree->root);
  539. if (! entry)
  540. {
  541. node = NULL;
  542. height = 0;
  543. }
  544. else
  545. {
  546. node = rdxtree_entry_addr (entry);
  547. height = rdxtree_entry_is_node (entry) ? node->height + 1 : 0;
  548. }
  549. if (key > rdxtree_max_key (height))
  550. return (NULL);
  551. else if (! height)
  552. return (node && get_slot ? (void *)&tree->root : node);
  553. uint16_t index, shift = (height - 1) * RDXTREE_RADIX;
  554. struct rdxtree_node *prev;
  555. do
  556. {
  557. if (! node)
  558. return (NULL);
  559. prev = node;
  560. index = (uint16_t)(key >> shift) & RDXTREE_RADIX_MASK;
  561. entry = rcu_load (&node->entries[index]);
  562. node = rdxtree_entry_addr (entry);
  563. shift -= RDXTREE_RADIX;
  564. --height;
  565. }
  566. while (height > 0);
  567. return (node && get_slot ? (void *)&prev->entries[index] : node);
  568. }
  569. void*
  570. rdxtree_replace_slot (void **slot, void *ptr)
  571. {
  572. assert (ptr);
  573. assert (rdxtree_alignment_valid (ptr));
  574. void *old = *slot;
  575. assert (old);
  576. assert (rdxtree_alignment_valid (old));
  577. rcu_store (slot, ptr);
  578. return (old);
  579. }
  580. static void*
  581. rdxtree_walk_next (struct rdxtree *tree, struct rdxtree_iter *iter)
  582. {
  583. void *entry = rcu_load (&tree->root);
  584. if (! entry)
  585. return (NULL);
  586. else if (!rdxtree_entry_is_node (entry))
  587. {
  588. if (iter->key != (rdxtree_key_t)-1)
  589. return (NULL);
  590. else
  591. {
  592. iter->key = 0;
  593. return (rdxtree_entry_addr (entry));
  594. }
  595. }
  596. rdxtree_key_t key = iter->key + 1;
  597. if (!key && iter->node)
  598. return (NULL);
  599. struct rdxtree_node *root, *node, *prev;
  600. uint16_t height, shift, index, orig_index;
  601. root = rdxtree_entry_addr (entry);
  602. restart:
  603. node = root;
  604. height = root->height + 1;
  605. if (key > rdxtree_max_key (height))
  606. return (NULL);
  607. shift = (height - 1) * RDXTREE_RADIX;
  608. do
  609. {
  610. prev = node;
  611. index = (key >> shift) & RDXTREE_RADIX_MASK;
  612. orig_index = index;
  613. node = rdxtree_node_find (node, &index);
  614. if (! node)
  615. {
  616. shift += RDXTREE_RADIX;
  617. key = ((key >> shift) + 1) << shift;
  618. if (! key)
  619. return (NULL);
  620. goto restart;
  621. }
  622. if (orig_index != index)
  623. key = ((key >> shift) + (index - orig_index)) << shift;
  624. shift -= RDXTREE_RADIX;
  625. --height;
  626. }
  627. while (height > 0);
  628. iter->node = prev;
  629. iter->key = key;
  630. return (node);
  631. }
  632. void*
  633. rdxtree_walk (struct rdxtree *tree, struct rdxtree_iter *iter)
  634. {
  635. if (!iter->node)
  636. return (rdxtree_walk_next (tree, iter));
  637. uint16_t index = (iter->key + 1) & RDXTREE_RADIX_MASK;
  638. if (index)
  639. {
  640. uint16_t orig_index = index;
  641. void *ptr = rdxtree_node_find (iter->node, &index);
  642. if (ptr)
  643. {
  644. iter->key += (index - orig_index) + 1;
  645. return (ptr);
  646. }
  647. }
  648. return (rdxtree_walk_next (tree, iter));
  649. }
  650. void
  651. rdxtree_remove_all (struct rdxtree *tree)
  652. {
  653. if (!tree->height)
  654. {
  655. if (tree->root)
  656. rcu_store (&tree->root, NULL);
  657. return;
  658. }
  659. while (1)
  660. {
  661. struct rdxtree_iter iter;
  662. rdxtree_iter_init (&iter);
  663. rdxtree_walk_next (tree, &iter);
  664. if (!iter.node)
  665. break;
  666. struct rdxtree_node *node = iter.node,
  667. *parent = node->parent;
  668. if (! parent)
  669. rdxtree_init (tree, tree->flags);
  670. else
  671. {
  672. if (rdxtree_key_alloc_enabled (tree))
  673. rdxtree_remove_bm_set (parent, node->index);
  674. rdxtree_node_remove (parent, node->index);
  675. rdxtree_cleanup (tree, parent);
  676. node->parent = NULL;
  677. }
  678. rdxtree_node_schedule_destruction (node);
  679. }
  680. }
  681. static int __init
  682. rdxtree_setup (void)
  683. {
  684. kmem_cache_init (&rdxtree_node_cache, "rdxtree_node",
  685. sizeof (struct rdxtree_node), 0,
  686. rdxtree_node_ctor, KMEM_CACHE_PAGE_ONLY);
  687. return (0);
  688. }
  689. INIT_OP_DEFINE (rdxtree_setup,
  690. INIT_OP_DEP (kmem_bootstrap, true),
  691. INIT_OP_DEP (rcu_bootstrap, true));