idr.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_next;
  19. };
  20. /*
  21. * The IDR API does not expose the tagging functionality of the radix tree
  22. * to users. Use tag 0 to track whether a node has free space below it.
  23. */
  24. #define IDR_FREE 0
  25. /* Set the IDR flag and the IDR_FREE tag */
  26. #define IDR_RT_MARKER ((__force gfp_t)(3 << __GFP_BITS_SHIFT))
  27. #define IDR_INIT \
  28. { \
  29. .idr_rt = RADIX_TREE_INIT(IDR_RT_MARKER) \
  30. }
  31. #define DEFINE_IDR(name) struct idr name = IDR_INIT
  32. /**
  33. * idr_get_cursor - Return the current position of the cyclic allocator
  34. * @idr: idr handle
  35. *
  36. * The value returned is the value that will be next returned from
  37. * idr_alloc_cyclic() if it is free (otherwise the search will start from
  38. * this position).
  39. */
  40. static inline unsigned int idr_get_cursor(const struct idr *idr)
  41. {
  42. return READ_ONCE(idr->idr_next);
  43. }
  44. /**
  45. * idr_set_cursor - Set the current position of the cyclic allocator
  46. * @idr: idr handle
  47. * @val: new position
  48. *
  49. * The next call to idr_alloc_cyclic() will return @val if it is free
  50. * (otherwise the search will start from this position).
  51. */
  52. static inline void idr_set_cursor(struct idr *idr, unsigned int val)
  53. {
  54. WRITE_ONCE(idr->idr_next, val);
  55. }
  56. /**
  57. * DOC: idr sync
  58. * idr synchronization (stolen from radix-tree.h)
  59. *
  60. * idr_find() is able to be called locklessly, using RCU. The caller must
  61. * ensure calls to this function are made within rcu_read_lock() regions.
  62. * Other readers (lock-free or otherwise) and modifications may be running
  63. * concurrently.
  64. *
  65. * It is still required that the caller manage the synchronization and
  66. * lifetimes of the items. So if RCU lock-free lookups are used, typically
  67. * this would mean that the items have their own locks, or are amenable to
  68. * lock-free access; and that the items are freed by RCU (or only freed after
  69. * having been deleted from the idr tree *and* a synchronize_rcu() grace
  70. * period).
  71. */
  72. void idr_preload(gfp_t gfp_mask);
  73. int idr_alloc(struct idr *, void *entry, int start, int end, gfp_t);
  74. int idr_alloc_cyclic(struct idr *, void *entry, int start, int end, gfp_t);
  75. int idr_for_each(const struct idr *,
  76. int (*fn)(int id, void *p, void *data), void *data);
  77. void *idr_get_next(struct idr *, int *nextid);
  78. void *idr_replace(struct idr *, void *, int id);
  79. void idr_destroy(struct idr *);
  80. static inline void idr_remove(struct idr *idr, int id)
  81. {
  82. radix_tree_delete(&idr->idr_rt, id);
  83. }
  84. static inline void idr_init(struct idr *idr)
  85. {
  86. INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER);
  87. idr->idr_next = 0;
  88. }
  89. static inline bool idr_is_empty(const struct idr *idr)
  90. {
  91. return radix_tree_empty(&idr->idr_rt) &&
  92. radix_tree_tagged(&idr->idr_rt, IDR_FREE);
  93. }
  94. /**
  95. * idr_preload_end - end preload section started with idr_preload()
  96. *
  97. * Each idr_preload() should be matched with an invocation of this
  98. * function. See idr_preload() for details.
  99. */
  100. static inline void idr_preload_end(void)
  101. {
  102. preempt_enable();
  103. }
  104. /**
  105. * idr_find - return pointer for given id
  106. * @idr: idr handle
  107. * @id: lookup key
  108. *
  109. * Return the pointer given the id it has been registered with. A %NULL
  110. * return indicates that @id is not valid or you passed %NULL in
  111. * idr_get_new().
  112. *
  113. * This function can be called under rcu_read_lock(), given that the leaf
  114. * pointers lifetimes are correctly managed.
  115. */
  116. static inline void *idr_find(const struct idr *idr, int id)
  117. {
  118. return radix_tree_lookup(&idr->idr_rt, id);
  119. }
  120. /**
  121. * idr_for_each_entry - iterate over an idr's elements of a given type
  122. * @idr: idr handle
  123. * @entry: the type * to use as cursor
  124. * @id: id entry's key
  125. *
  126. * @entry and @id do not need to be initialized before the loop, and
  127. * after normal terminatinon @entry is left with the value NULL. This
  128. * is convenient for a "not found" value.
  129. */
  130. #define idr_for_each_entry(idr, entry, id) \
  131. for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; ++id)
  132. /**
  133. * idr_for_each_entry_continue - continue iteration over an idr's elements of a given type
  134. * @idr: idr handle
  135. * @entry: the type * to use as cursor
  136. * @id: id entry's key
  137. *
  138. * Continue to iterate over list of given type, continuing after
  139. * the current position.
  140. */
  141. #define idr_for_each_entry_continue(idr, entry, id) \
  142. for ((entry) = idr_get_next((idr), &(id)); \
  143. entry; \
  144. ++id, (entry) = idr_get_next((idr), &(id)))
  145. /*
  146. * IDA - IDR based id allocator, use when translation from id to
  147. * pointer isn't necessary.
  148. */
  149. #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
  150. #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long))
  151. #define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
  152. struct ida_bitmap {
  153. unsigned long bitmap[IDA_BITMAP_LONGS];
  154. };
  155. DECLARE_PER_CPU(struct ida_bitmap *, ida_bitmap);
  156. struct ida {
  157. struct radix_tree_root ida_rt;
  158. };
  159. #define IDA_INIT { \
  160. .ida_rt = RADIX_TREE_INIT(IDR_RT_MARKER | GFP_NOWAIT), \
  161. }
  162. #define DEFINE_IDA(name) struct ida name = IDA_INIT
  163. int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
  164. int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
  165. void ida_remove(struct ida *ida, int id);
  166. void ida_destroy(struct ida *ida);
  167. int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
  168. gfp_t gfp_mask);
  169. void ida_simple_remove(struct ida *ida, unsigned int id);
  170. static inline void ida_init(struct ida *ida)
  171. {
  172. INIT_RADIX_TREE(&ida->ida_rt, IDR_RT_MARKER | GFP_NOWAIT);
  173. }
  174. /**
  175. * ida_get_new - allocate new ID
  176. * @ida: idr handle
  177. * @p_id: pointer to the allocated handle
  178. *
  179. * Simple wrapper around ida_get_new_above() w/ @starting_id of zero.
  180. */
  181. static inline int ida_get_new(struct ida *ida, int *p_id)
  182. {
  183. return ida_get_new_above(ida, 0, p_id);
  184. }
  185. static inline bool ida_is_empty(const struct ida *ida)
  186. {
  187. return radix_tree_empty(&ida->ida_rt);
  188. }
  189. #endif /* __IDR_H__ */