idr.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/types.h>
  14. #include <linux/bitops.h>
  15. #include <linux/init.h>
  16. #include <linux/rcupdate.h>
  17. /*
  18. * We want shallower trees and thus more bits covered at each layer. 8
  19. * bits gives us large enough first layer for most use cases and maximum
  20. * tree depth of 4. Each idr_layer is slightly larger than 2k on 64bit and
  21. * 1k on 32bit.
  22. */
  23. #define IDR_BITS 8
  24. #define IDR_SIZE (1 << IDR_BITS)
  25. #define IDR_MASK ((1 << IDR_BITS)-1)
  26. struct idr_layer {
  27. int prefix; /* the ID prefix of this idr_layer */
  28. int layer; /* distance from leaf */
  29. struct idr_layer __rcu *ary[1<<IDR_BITS];
  30. int count; /* When zero, we can release it */
  31. union {
  32. /* A zero bit means "space here" */
  33. DECLARE_BITMAP(bitmap, IDR_SIZE);
  34. struct rcu_head rcu_head;
  35. };
  36. };
  37. struct idr {
  38. struct idr_layer __rcu *hint; /* the last layer allocated from */
  39. struct idr_layer __rcu *top;
  40. int layers; /* only valid w/o concurrent changes */
  41. int cur; /* current pos for cyclic allocation */
  42. spinlock_t lock;
  43. int id_free_cnt;
  44. struct idr_layer *id_free;
  45. };
  46. #define IDR_INIT(name) \
  47. { \
  48. .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
  49. }
  50. #define DEFINE_IDR(name) struct idr name = IDR_INIT(name)
  51. /**
  52. * DOC: idr sync
  53. * idr synchronization (stolen from radix-tree.h)
  54. *
  55. * idr_find() is able to be called locklessly, using RCU. The caller must
  56. * ensure calls to this function are made within rcu_read_lock() regions.
  57. * Other readers (lock-free or otherwise) and modifications may be running
  58. * concurrently.
  59. *
  60. * It is still required that the caller manage the synchronization and
  61. * lifetimes of the items. So if RCU lock-free lookups are used, typically
  62. * this would mean that the items have their own locks, or are amenable to
  63. * lock-free access; and that the items are freed by RCU (or only freed after
  64. * having been deleted from the idr tree *and* a synchronize_rcu() grace
  65. * period).
  66. */
  67. /*
  68. * This is what we export.
  69. */
  70. void *idr_find_slowpath(struct idr *idp, int id);
  71. void idr_preload(gfp_t gfp_mask);
  72. int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask);
  73. int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask);
  74. int idr_for_each(struct idr *idp,
  75. int (*fn)(int id, void *p, void *data), void *data);
  76. void *idr_get_next(struct idr *idp, int *nextid);
  77. void *idr_replace(struct idr *idp, void *ptr, int id);
  78. void idr_remove(struct idr *idp, int id);
  79. void idr_destroy(struct idr *idp);
  80. void idr_init(struct idr *idp);
  81. bool idr_is_empty(struct idr *idp);
  82. /**
  83. * idr_preload_end - end preload section started with idr_preload()
  84. *
  85. * Each idr_preload() should be matched with an invocation of this
  86. * function. See idr_preload() for details.
  87. */
  88. static inline void idr_preload_end(void)
  89. {
  90. preempt_enable();
  91. }
  92. /**
  93. * idr_find - return pointer for given id
  94. * @idr: idr handle
  95. * @id: lookup key
  96. *
  97. * Return the pointer given the id it has been registered with. A %NULL
  98. * return indicates that @id is not valid or you passed %NULL in
  99. * idr_get_new().
  100. *
  101. * This function can be called under rcu_read_lock(), given that the leaf
  102. * pointers lifetimes are correctly managed.
  103. */
  104. static inline void *idr_find(struct idr *idr, int id)
  105. {
  106. struct idr_layer *hint = rcu_dereference_raw(idr->hint);
  107. if (hint && (id & ~IDR_MASK) == hint->prefix)
  108. return rcu_dereference_raw(hint->ary[id & IDR_MASK]);
  109. return idr_find_slowpath(idr, id);
  110. }
  111. /**
  112. * idr_for_each_entry - iterate over an idr's elements of a given type
  113. * @idp: idr handle
  114. * @entry: the type * to use as cursor
  115. * @id: id entry's key
  116. *
  117. * @entry and @id do not need to be initialized before the loop, and
  118. * after normal terminatinon @entry is left with the value NULL. This
  119. * is convenient for a "not found" value.
  120. */
  121. #define idr_for_each_entry(idp, entry, id) \
  122. for (id = 0; ((entry) = idr_get_next(idp, &(id))) != NULL; ++id)
  123. /*
  124. * IDA - IDR based id allocator, use when translation from id to
  125. * pointer isn't necessary.
  126. *
  127. * IDA_BITMAP_LONGS is calculated to be one less to accommodate
  128. * ida_bitmap->nr_busy so that the whole struct fits in 128 bytes.
  129. */
  130. #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
  131. #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long) - 1)
  132. #define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
  133. struct ida_bitmap {
  134. long nr_busy;
  135. unsigned long bitmap[IDA_BITMAP_LONGS];
  136. };
  137. struct ida {
  138. struct idr idr;
  139. struct ida_bitmap *free_bitmap;
  140. };
  141. #define IDA_INIT(name) { .idr = IDR_INIT((name).idr), .free_bitmap = NULL, }
  142. #define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
  143. int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
  144. int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
  145. void ida_remove(struct ida *ida, int id);
  146. void ida_destroy(struct ida *ida);
  147. void ida_init(struct ida *ida);
  148. int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
  149. gfp_t gfp_mask);
  150. void ida_simple_remove(struct ida *ida, unsigned int id);
  151. /**
  152. * ida_get_new - allocate new ID
  153. * @ida: idr handle
  154. * @p_id: pointer to the allocated handle
  155. *
  156. * Simple wrapper around ida_get_new_above() w/ @starting_id of zero.
  157. */
  158. static inline int ida_get_new(struct ida *ida, int *p_id)
  159. {
  160. return ida_get_new_above(ida, 0, p_id);
  161. }
  162. void __init idr_init_cache(void);
  163. #endif /* __IDR_H__ */