generic-radix-tree.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #ifndef _LINUX_GENERIC_RADIX_TREE_H
  2. #define _LINUX_GENERIC_RADIX_TREE_H
  3. /**
  4. * DOC: Generic radix trees/sparse arrays
  5. *
  6. * Very simple and minimalistic, supporting arbitrary size entries up to
  7. * PAGE_SIZE.
  8. *
  9. * A genradix is defined with the type it will store, like so:
  10. *
  11. * static GENRADIX(struct foo) foo_genradix;
  12. *
  13. * The main operations are:
  14. *
  15. * - genradix_init(radix) - initialize an empty genradix
  16. *
  17. * - genradix_free(radix) - free all memory owned by the genradix and
  18. * reinitialize it
  19. *
  20. * - genradix_ptr(radix, idx) - gets a pointer to the entry at idx, returning
  21. * NULL if that entry does not exist
  22. *
  23. * - genradix_ptr_alloc(radix, idx, gfp) - gets a pointer to an entry,
  24. * allocating it if necessary
  25. *
  26. * - genradix_for_each(radix, iter, p) - iterate over each entry in a genradix
  27. *
  28. * The radix tree allocates one page of entries at a time, so entries may exist
  29. * that were never explicitly allocated - they will be initialized to all
  30. * zeroes.
  31. *
  32. * Internally, a genradix is just a radix tree of pages, and indexing works in
  33. * terms of byte offsets. The wrappers in this header file use sizeof on the
  34. * type the radix contains to calculate a byte offset from the index - see
  35. * __idx_to_offset.
  36. */
  37. #include <asm/page.h>
  38. #include <linux/bug.h>
  39. #include <linux/limits.h>
  40. #include <linux/log2.h>
  41. #include <linux/math.h>
  42. #include <linux/types.h>
  43. struct genradix_root;
  44. struct __genradix {
  45. struct genradix_root *root;
  46. };
  47. /*
  48. * NOTE: currently, sizeof(_type) must not be larger than PAGE_SIZE:
  49. */
  50. #define __GENRADIX_INITIALIZER \
  51. { \
  52. .tree = { \
  53. .root = NULL, \
  54. } \
  55. }
  56. /*
  57. * We use a 0 size array to stash the type we're storing without taking any
  58. * space at runtime - then the various accessor macros can use typeof() to get
  59. * to it for casts/sizeof - we also force the alignment so that storing a type
  60. * with a ridiculous alignment doesn't blow up the alignment or size of the
  61. * genradix.
  62. */
  63. #define GENRADIX(_type) \
  64. struct { \
  65. struct __genradix tree; \
  66. _type type[0] __aligned(1); \
  67. }
  68. #define DEFINE_GENRADIX(_name, _type) \
  69. GENRADIX(_type) _name = __GENRADIX_INITIALIZER
  70. /**
  71. * genradix_init - initialize a genradix
  72. * @_radix: genradix to initialize
  73. *
  74. * Does not fail
  75. */
  76. #define genradix_init(_radix) \
  77. do { \
  78. *(_radix) = (typeof(*_radix)) __GENRADIX_INITIALIZER; \
  79. } while (0)
  80. void __genradix_free(struct __genradix *);
  81. /**
  82. * genradix_free: free all memory owned by a genradix
  83. * @_radix: the genradix to free
  84. *
  85. * After freeing, @_radix will be reinitialized and empty
  86. */
  87. #define genradix_free(_radix) __genradix_free(&(_radix)->tree)
  88. static inline size_t __idx_to_offset(size_t idx, size_t obj_size)
  89. {
  90. if (__builtin_constant_p(obj_size))
  91. BUILD_BUG_ON(obj_size > PAGE_SIZE);
  92. else
  93. BUG_ON(obj_size > PAGE_SIZE);
  94. if (!is_power_of_2(obj_size)) {
  95. size_t objs_per_page = PAGE_SIZE / obj_size;
  96. return (idx / objs_per_page) * PAGE_SIZE +
  97. (idx % objs_per_page) * obj_size;
  98. } else {
  99. return idx * obj_size;
  100. }
  101. }
  102. #define __genradix_cast(_radix) (typeof((_radix)->type[0]) *)
  103. #define __genradix_obj_size(_radix) sizeof((_radix)->type[0])
  104. #define __genradix_idx_to_offset(_radix, _idx) \
  105. __idx_to_offset(_idx, __genradix_obj_size(_radix))
  106. void *__genradix_ptr(struct __genradix *, size_t);
  107. /**
  108. * genradix_ptr - get a pointer to a genradix entry
  109. * @_radix: genradix to access
  110. * @_idx: index to fetch
  111. *
  112. * Returns a pointer to entry at @_idx, or NULL if that entry does not exist.
  113. */
  114. #define genradix_ptr(_radix, _idx) \
  115. (__genradix_cast(_radix) \
  116. __genradix_ptr(&(_radix)->tree, \
  117. __genradix_idx_to_offset(_radix, _idx)))
  118. void *__genradix_ptr_alloc(struct __genradix *, size_t, gfp_t);
  119. /**
  120. * genradix_ptr_alloc - get a pointer to a genradix entry, allocating it
  121. * if necessary
  122. * @_radix: genradix to access
  123. * @_idx: index to fetch
  124. * @_gfp: gfp mask
  125. *
  126. * Returns a pointer to entry at @_idx, or NULL on allocation failure
  127. */
  128. #define genradix_ptr_alloc(_radix, _idx, _gfp) \
  129. (__genradix_cast(_radix) \
  130. __genradix_ptr_alloc(&(_radix)->tree, \
  131. __genradix_idx_to_offset(_radix, _idx), \
  132. _gfp))
  133. struct genradix_iter {
  134. size_t offset;
  135. size_t pos;
  136. };
  137. /**
  138. * genradix_iter_init - initialize a genradix_iter
  139. * @_radix: genradix that will be iterated over
  140. * @_idx: index to start iterating from
  141. */
  142. #define genradix_iter_init(_radix, _idx) \
  143. ((struct genradix_iter) { \
  144. .pos = (_idx), \
  145. .offset = __genradix_idx_to_offset((_radix), (_idx)),\
  146. })
  147. void *__genradix_iter_peek(struct genradix_iter *, struct __genradix *, size_t);
  148. /**
  149. * genradix_iter_peek - get first entry at or above iterator's current
  150. * position
  151. * @_iter: a genradix_iter
  152. * @_radix: genradix being iterated over
  153. *
  154. * If no more entries exist at or above @_iter's current position, returns NULL
  155. */
  156. #define genradix_iter_peek(_iter, _radix) \
  157. (__genradix_cast(_radix) \
  158. __genradix_iter_peek(_iter, &(_radix)->tree, \
  159. PAGE_SIZE / __genradix_obj_size(_radix)))
  160. static inline void __genradix_iter_advance(struct genradix_iter *iter,
  161. size_t obj_size)
  162. {
  163. if (iter->offset + obj_size < iter->offset) {
  164. iter->offset = SIZE_MAX;
  165. iter->pos = SIZE_MAX;
  166. return;
  167. }
  168. iter->offset += obj_size;
  169. if (!is_power_of_2(obj_size) &&
  170. (iter->offset & (PAGE_SIZE - 1)) + obj_size > PAGE_SIZE)
  171. iter->offset = round_up(iter->offset, PAGE_SIZE);
  172. iter->pos++;
  173. }
  174. #define genradix_iter_advance(_iter, _radix) \
  175. __genradix_iter_advance(_iter, __genradix_obj_size(_radix))
  176. #define genradix_for_each_from(_radix, _iter, _p, _start) \
  177. for (_iter = genradix_iter_init(_radix, _start); \
  178. (_p = genradix_iter_peek(&_iter, _radix)) != NULL; \
  179. genradix_iter_advance(&_iter, _radix))
  180. /**
  181. * genradix_for_each - iterate over entry in a genradix
  182. * @_radix: genradix to iterate over
  183. * @_iter: a genradix_iter to track current position
  184. * @_p: pointer to genradix entry type
  185. *
  186. * On every iteration, @_p will point to the current entry, and @_iter.pos
  187. * will be the current entry's index.
  188. */
  189. #define genradix_for_each(_radix, _iter, _p) \
  190. genradix_for_each_from(_radix, _iter, _p, 0)
  191. int __genradix_prealloc(struct __genradix *, size_t, gfp_t);
  192. /**
  193. * genradix_prealloc - preallocate entries in a generic radix tree
  194. * @_radix: genradix to preallocate
  195. * @_nr: number of entries to preallocate
  196. * @_gfp: gfp mask
  197. *
  198. * Returns 0 on success, -ENOMEM on failure
  199. */
  200. #define genradix_prealloc(_radix, _nr, _gfp) \
  201. __genradix_prealloc(&(_radix)->tree, \
  202. __genradix_idx_to_offset(_radix, _nr + 1),\
  203. _gfp)
  204. #endif /* _LINUX_GENERIC_RADIX_TREE_H */