idr.h 5.2 KB

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