rdxtree.h 6.1 KB

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