idr.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * include/linux/idr.h
  3. *
  4. * 2002-10-18 written by Jim Houston jim.houston@ccur.com
  5. * Copyright (C) 2002 by Concurrent Computer Corporation
  6. * Distributed under the GNU GPL license version 2.
  7. *
  8. * Small id to pointer translation service avoiding fixed sized
  9. * tables.
  10. */
  11. #ifndef __IDR_H__
  12. #define __IDR_H__
  13. #include <linux/radix-tree.h>
  14. #include <linux/gfp.h>
  15. #include <linux/percpu.h>
  16. struct idr {
  17. struct radix_tree_root idr_rt;
  18. unsigned int idr_base;
  19. unsigned int idr_next;
  20. };
  21. /*
  22. * The IDR API does not expose the tagging functionality of the radix tree
  23. * to users. Use tag 0 to track whether a node has free space below it.
  24. */
  25. #define IDR_FREE 0
  26. /* Set the IDR flag and the IDR_FREE tag */
  27. #define IDR_RT_MARKER (ROOT_IS_IDR | (__force gfp_t) \
  28. (1 << (ROOT_TAG_SHIFT + IDR_FREE)))
  29. #define IDR_INIT_BASE(name, base) { \
  30. .idr_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER), \
  31. .idr_base = (base), \
  32. .idr_next = 0, \
  33. }
  34. /**
  35. * IDR_INIT() - Initialise an IDR.
  36. * @name: Name of IDR.
  37. *
  38. * A freshly-initialised IDR contains no IDs.
  39. */
  40. #define IDR_INIT(name) IDR_INIT_BASE(name, 0)
  41. /**
  42. * DEFINE_IDR() - Define a statically-allocated IDR.
  43. * @name: Name of IDR.
  44. *
  45. * An IDR defined using this macro is ready for use with no additional
  46. * initialisation required. It contains no IDs.
  47. */
  48. #define DEFINE_IDR(name) struct idr name = IDR_INIT(name)
  49. /**
  50. * idr_get_cursor - Return the current position of the cyclic allocator
  51. * @idr: idr handle
  52. *
  53. * The value returned is the value that will be next returned from
  54. * idr_alloc_cyclic() if it is free (otherwise the search will start from
  55. * this position).
  56. */
  57. static inline unsigned int idr_get_cursor(const struct idr *idr)
  58. {
  59. return READ_ONCE(idr->idr_next);
  60. }
  61. /**
  62. * idr_set_cursor - Set the current position of the cyclic allocator
  63. * @idr: idr handle
  64. * @val: new position
  65. *
  66. * The next call to idr_alloc_cyclic() will return @val if it is free
  67. * (otherwise the search will start from this position).
  68. */
  69. static inline void idr_set_cursor(struct idr *idr, unsigned int val)
  70. {
  71. WRITE_ONCE(idr->idr_next, val);
  72. }
  73. /**
  74. * DOC: idr sync
  75. * idr synchronization (stolen from radix-tree.h)
  76. *
  77. * idr_find() is able to be called locklessly, using RCU. The caller must
  78. * ensure calls to this function are made within rcu_read_lock() regions.
  79. * Other readers (lock-free or otherwise) and modifications may be running
  80. * concurrently.
  81. *
  82. * It is still required that the caller manage the synchronization and
  83. * lifetimes of the items. So if RCU lock-free lookups are used, typically
  84. * this would mean that the items have their own locks, or are amenable to
  85. * lock-free access; and that the items are freed by RCU (or only freed after
  86. * having been deleted from the idr tree *and* a synchronize_rcu() grace
  87. * period).
  88. */
  89. void idr_preload(gfp_t gfp_mask);
  90. int idr_alloc(struct idr *, void *ptr, int start, int end, gfp_t);
  91. int __must_check idr_alloc_u32(struct idr *, void *ptr, u32 *id,
  92. unsigned long max, gfp_t);
  93. int idr_alloc_cyclic(struct idr *, void *ptr, int start, int end, gfp_t);
  94. void *idr_remove(struct idr *, unsigned long id);
  95. void *idr_find(const struct idr *, unsigned long id);
  96. int idr_for_each(const struct idr *,
  97. int (*fn)(int id, void *p, void *data), void *data);
  98. void *idr_get_next(struct idr *, int *nextid);
  99. void *idr_get_next_ul(struct idr *, unsigned long *nextid);
  100. void *idr_replace(struct idr *, void *, unsigned long id);
  101. void idr_destroy(struct idr *);
  102. /**
  103. * idr_init_base() - Initialise an IDR.
  104. * @idr: IDR handle.
  105. * @base: The base value for the IDR.
  106. *
  107. * This variation of idr_init() creates an IDR which will allocate IDs
  108. * starting at %base.
  109. */
  110. static inline void idr_init_base(struct idr *idr, int base)
  111. {
  112. INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER);
  113. idr->idr_base = base;
  114. idr->idr_next = 0;
  115. }
  116. /**
  117. * idr_init() - Initialise an IDR.
  118. * @idr: IDR handle.
  119. *
  120. * Initialise a dynamically allocated IDR. To initialise a
  121. * statically allocated IDR, use DEFINE_IDR().
  122. */
  123. static inline void idr_init(struct idr *idr)
  124. {
  125. idr_init_base(idr, 0);
  126. }
  127. /**
  128. * idr_is_empty() - Are there any IDs allocated?
  129. * @idr: IDR handle.
  130. *
  131. * Return: %true if any IDs have been allocated from this IDR.
  132. */
  133. static inline bool idr_is_empty(const struct idr *idr)
  134. {
  135. return radix_tree_empty(&idr->idr_rt) &&
  136. radix_tree_tagged(&idr->idr_rt, IDR_FREE);
  137. }
  138. /**
  139. * idr_preload_end - end preload section started with idr_preload()
  140. *
  141. * Each idr_preload() should be matched with an invocation of this
  142. * function. See idr_preload() for details.
  143. */
  144. static inline void idr_preload_end(void)
  145. {
  146. preempt_enable();
  147. }
  148. /**
  149. * idr_for_each_entry() - Iterate over an IDR's elements of a given type.
  150. * @idr: IDR handle.
  151. * @entry: The type * to use as cursor
  152. * @id: Entry ID.
  153. *
  154. * @entry and @id do not need to be initialized before the loop, and
  155. * after normal termination @entry is left with the value NULL. This
  156. * is convenient for a "not found" value.
  157. */
  158. #define idr_for_each_entry(idr, entry, id) \
  159. for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; ++id)
  160. /**
  161. * idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type.
  162. * @idr: IDR handle.
  163. * @entry: The type * to use as cursor.
  164. * @id: Entry ID.
  165. *
  166. * @entry and @id do not need to be initialized before the loop, and
  167. * after normal termination @entry is left with the value NULL. This
  168. * is convenient for a "not found" value.
  169. */
  170. #define idr_for_each_entry_ul(idr, entry, id) \
  171. for (id = 0; ((entry) = idr_get_next_ul(idr, &(id))) != NULL; ++id)
  172. /**
  173. * idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type
  174. * @idr: IDR handle.
  175. * @entry: The type * to use as a cursor.
  176. * @id: Entry ID.
  177. *
  178. * Continue to iterate over entries, continuing after the current position.
  179. */
  180. #define idr_for_each_entry_continue(idr, entry, id) \
  181. for ((entry) = idr_get_next((idr), &(id)); \
  182. entry; \
  183. ++id, (entry) = idr_get_next((idr), &(id)))
  184. /*
  185. * IDA - IDR based id allocator, use when translation from id to
  186. * pointer isn't necessary.
  187. */
  188. #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
  189. #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long))
  190. #define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
  191. struct ida_bitmap {
  192. unsigned long bitmap[IDA_BITMAP_LONGS];
  193. };
  194. DECLARE_PER_CPU(struct ida_bitmap *, ida_bitmap);
  195. struct ida {
  196. struct radix_tree_root ida_rt;
  197. };
  198. #define IDA_INIT(name) { \
  199. .ida_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER | GFP_NOWAIT), \
  200. }
  201. #define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
  202. int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
  203. int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
  204. void ida_remove(struct ida *ida, int id);
  205. void ida_destroy(struct ida *ida);
  206. int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
  207. gfp_t gfp_mask);
  208. void ida_simple_remove(struct ida *ida, unsigned int id);
  209. static inline void ida_init(struct ida *ida)
  210. {
  211. INIT_RADIX_TREE(&ida->ida_rt, IDR_RT_MARKER | GFP_NOWAIT);
  212. }
  213. /**
  214. * ida_get_new - allocate new ID
  215. * @ida: idr handle
  216. * @p_id: pointer to the allocated handle
  217. *
  218. * Simple wrapper around ida_get_new_above() w/ @starting_id of zero.
  219. */
  220. static inline int ida_get_new(struct ida *ida, int *p_id)
  221. {
  222. return ida_get_new_above(ida, 0, p_id);
  223. }
  224. static inline bool ida_is_empty(const struct ida *ida)
  225. {
  226. return radix_tree_empty(&ida->ida_rt);
  227. }
  228. #endif /* __IDR_H__ */