rdxtree.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. *
  21. * Radix tree.
  22. *
  23. * In addition to the standard insertion operation, this implementation can
  24. * allocate keys for the caller at insertion time. It also allows lookups to
  25. * occur concurrently with updates using RCU.
  26. */
  27. #ifndef KERN_RDXTREE_H
  28. #define KERN_RDXTREE_H
  29. #include <assert.h>
  30. #include <stdbool.h>
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <kern/init.h>
  34. #include <kern/rcu.h>
  35. typedef uint64_t rdxtree_key_t;
  36. // Radix tree initialization flags.
  37. #define RDXTREE_KEY_ALLOC 0x1 // Enable key allocation.
  38. #define RDXTREE_ALLOC_SLEEP 0x2 // Allocations may sleep.
  39. // User-defined bit.
  40. #define RDXTREE_XBIT 0x02
  41. // Static tree initializer.
  42. #define RDXTREE_INITIALIZER { 0, 0, NULL }
  43. // Radix tree.
  44. struct rdxtree
  45. {
  46. uint16_t height;
  47. uint16_t flags;
  48. void *root;
  49. };
  50. /*
  51. * Radix tree iterator.
  52. *
  53. * The node member refers to the node containing the current pointer, if any.
  54. * The key member refers to the current pointer, and is valid if and only if
  55. * rdxtree_walk() has been called at least once on the iterator.
  56. */
  57. struct rdxtree_iter
  58. {
  59. void *node;
  60. rdxtree_key_t key;
  61. int index;
  62. };
  63. // Initialize an iterator.
  64. static inline void
  65. rdxtree_iter_init (struct rdxtree_iter *iter)
  66. {
  67. iter->node = NULL;
  68. iter->key = (rdxtree_key_t)-1;
  69. }
  70. int rdxtree_insert_common (struct rdxtree *tree, rdxtree_key_t key,
  71. void *ptr, void ***slotp);
  72. int rdxtree_insert_alloc_common (struct rdxtree *tree, void *ptr,
  73. rdxtree_key_t *keyp, void ***slotp);
  74. void* rdxtree_lookup_common (const struct rdxtree *tree, rdxtree_key_t key,
  75. bool get_slot, void **node, int *idxp);
  76. void* rdxtree_walk (struct rdxtree *tree, struct rdxtree_iter *iter);
  77. // Initialize a tree.
  78. static inline void
  79. rdxtree_init (struct rdxtree *tree, uint16_t flags)
  80. {
  81. assert ((flags & ~(RDXTREE_KEY_ALLOC | RDXTREE_ALLOC_SLEEP)) == 0);
  82. tree->height = 0;
  83. tree->flags = flags;
  84. tree->root = NULL;
  85. }
  86. /*
  87. * Insert a pointer in a tree.
  88. *
  89. * The ptr parameter must not be NULL.
  90. */
  91. static inline int
  92. rdxtree_insert (struct rdxtree *tree, rdxtree_key_t key, void *ptr)
  93. {
  94. return (rdxtree_insert_common (tree, key, ptr, NULL));
  95. }
  96. /*
  97. * Insert a pointer in a tree and obtain its slot.
  98. *
  99. * The ptr and slotp parameters must not be NULL. If successful, the slot of
  100. * the newly inserted pointer is stored at the address pointed to by the slotp
  101. * parameter.
  102. */
  103. static inline int
  104. rdxtree_insert_slot (struct rdxtree *tree, rdxtree_key_t key,
  105. void *ptr, void ***slotp)
  106. {
  107. return (rdxtree_insert_common (tree, key, ptr, slotp));
  108. }
  109. /*
  110. * Insert a pointer in a tree, for which a new key is allocated.
  111. *
  112. * The ptr and keyp parameters must not be NULL. The newly allocated key is
  113. * stored at the address pointed to by the keyp parameter.
  114. */
  115. static inline int
  116. rdxtree_insert_alloc (struct rdxtree *tree, void *ptr, rdxtree_key_t *keyp)
  117. {
  118. return (rdxtree_insert_alloc_common (tree, ptr, keyp, NULL));
  119. }
  120. /*
  121. * Insert a pointer in a tree, for which a new key is allocated, and obtain
  122. * its slot.
  123. *
  124. * The ptr, keyp and slotp parameters must not be NULL. The newly allocated
  125. * key is stored at the address pointed to by the keyp parameter while the
  126. * slot of the inserted pointer is stored at the address pointed to by the
  127. * slotp parameter.
  128. */
  129. static inline int
  130. rdxtree_insert_alloc_slot (struct rdxtree *tree, void *ptr,
  131. rdxtree_key_t *keyp, void ***slotp)
  132. {
  133. return (rdxtree_insert_alloc_common (tree, ptr, keyp, slotp));
  134. }
  135. /*
  136. * Remove a pointer from a tree.
  137. *
  138. * The matching pointer is returned if successful, NULL otherwise.
  139. */
  140. void* rdxtree_remove (struct rdxtree *tree, rdxtree_key_t key);
  141. /*
  142. * Look up a pointer in a tree.
  143. *
  144. * The matching pointer is returned if successful, NULL otherwise.
  145. */
  146. static inline void*
  147. rdxtree_lookup (const struct rdxtree *tree, rdxtree_key_t key)
  148. {
  149. void *node;
  150. int idx;
  151. return (rdxtree_lookup_common (tree, key, false, &node, &idx));
  152. }
  153. /*
  154. * Look up a slot in a tree.
  155. *
  156. * A slot is a pointer to a stored pointer in a tree. It can be used as
  157. * a placeholder for fast replacements to avoid multiple lookups on the same
  158. * key.
  159. *
  160. * A slot for the matching pointer is returned if successful, NULL otherwise.
  161. *
  162. * See rdxtree_replace_slot().
  163. */
  164. static inline void**
  165. rdxtree_lookup_slot (const struct rdxtree *tree, rdxtree_key_t key)
  166. {
  167. void *node;
  168. int idx;
  169. return (rdxtree_lookup_common (tree, key, true, &node, &idx));
  170. }
  171. static inline void*
  172. rdxtree_load_slot (void **slot)
  173. {
  174. return (rcu_load (slot));
  175. }
  176. /*
  177. * Replace a pointer in a tree.
  178. *
  179. * The ptr parameter must not be NULL. The previous pointer is returned.
  180. *
  181. * See rdxtree_lookup_slot().
  182. */
  183. void* rdxtree_replace_slot (void **slot, void *ptr);
  184. // Remove a node and index from a tree.
  185. void rdxtree_remove_node_idx (struct rdxtree *tree, void *node, int idx);
  186. /*
  187. * Forge a loop to process all pointers of a tree.
  188. *
  189. * It is not safe to modify a tree from such a loop.
  190. */
  191. #define rdxtree_for_each(tree, iter, ptr) \
  192. for (rdxtree_iter_init (iter), ptr = rdxtree_walk (tree, iter); \
  193. ptr; ptr = rdxtree_walk (tree, iter))
  194. // Return the key of the current pointer from an iterator.
  195. static inline rdxtree_key_t
  196. rdxtree_iter_key (const struct rdxtree_iter *iter)
  197. {
  198. return (iter->key);
  199. }
  200. /*
  201. * Remove all pointers from a tree.
  202. *
  203. * The common way to destroy a tree and its pointers is to loop over all
  204. * the pointers using rdxtree_for_each(), freeing them, then call this
  205. * function.
  206. */
  207. void rdxtree_remove_all (struct rdxtree *tree);
  208. /*
  209. * This init operation provides :
  210. * - module fully initialized
  211. */
  212. INIT_OP_DECLARE (rdxtree_setup);
  213. #endif