idr.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. # define IDR_FULL 0xfffffffful
  20. /* We can only use two of the bits in the top level because there is
  21. only one possible bit in the top level (5 bits * 7 levels = 35
  22. bits, but you only use 31 bits in the id). */
  23. # define TOP_LEVEL_FULL (IDR_FULL >> 30)
  24. #elif BITS_PER_LONG == 64
  25. # define IDR_BITS 6
  26. # define IDR_FULL 0xfffffffffffffffful
  27. /* We can only use two of the bits in the top level because there is
  28. only one possible bit in the top level (6 bits * 6 levels = 36
  29. bits, but you only use 31 bits in the id). */
  30. # define TOP_LEVEL_FULL (IDR_FULL >> 62)
  31. #else
  32. # error "BITS_PER_LONG is not 32 or 64"
  33. #endif
  34. #define IDR_SIZE (1 << IDR_BITS)
  35. #define IDR_MASK ((1 << IDR_BITS)-1)
  36. #define MAX_ID_SHIFT (sizeof(int)*8 - 1)
  37. #define MAX_ID_BIT (1U << MAX_ID_SHIFT)
  38. #define MAX_ID_MASK (MAX_ID_BIT - 1)
  39. /* Leave the possibility of an incomplete final layer */
  40. #define MAX_LEVEL (MAX_ID_SHIFT + IDR_BITS - 1) / IDR_BITS
  41. /* Number of id_layer structs to leave in free list */
  42. #define IDR_FREE_MAX MAX_LEVEL + MAX_LEVEL
  43. struct idr_layer {
  44. unsigned long bitmap; /* A zero bit means "space here" */
  45. struct idr_layer __rcu *ary[1<<IDR_BITS];
  46. int count; /* When zero, we can release it */
  47. int layer; /* distance from leaf */
  48. struct rcu_head rcu_head;
  49. };
  50. struct idr {
  51. struct idr_layer __rcu *top;
  52. struct idr_layer *id_free;
  53. int layers; /* only valid without concurrent changes */
  54. int id_free_cnt;
  55. spinlock_t lock;
  56. };
  57. #define IDR_INIT(name) \
  58. { \
  59. .top = NULL, \
  60. .id_free = NULL, \
  61. .layers = 0, \
  62. .id_free_cnt = 0, \
  63. .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
  64. }
  65. #define DEFINE_IDR(name) struct idr name = IDR_INIT(name)
  66. /* Actions to be taken after a call to _idr_sub_alloc */
  67. #define IDR_NEED_TO_GROW -2
  68. #define IDR_NOMORE_SPACE -3
  69. #define _idr_rc_to_errno(rc) ((rc) == -1 ? -EAGAIN : -ENOSPC)
  70. /**
  71. * DOC: idr sync
  72. * idr synchronization (stolen from radix-tree.h)
  73. *
  74. * idr_find() is able to be called locklessly, using RCU. The caller must
  75. * ensure calls to this function are made within rcu_read_lock() regions.
  76. * Other readers (lock-free or otherwise) and modifications may be running
  77. * concurrently.
  78. *
  79. * It is still required that the caller manage the synchronization and
  80. * lifetimes of the items. So if RCU lock-free lookups are used, typically
  81. * this would mean that the items have their own locks, or are amenable to
  82. * lock-free access; and that the items are freed by RCU (or only freed after
  83. * having been deleted from the idr tree *and* a synchronize_rcu() grace
  84. * period).
  85. */
  86. /*
  87. * This is what we export.
  88. */
  89. void *idr_find(struct idr *idp, int id);
  90. int idr_pre_get(struct idr *idp, gfp_t gfp_mask);
  91. int idr_get_new(struct idr *idp, void *ptr, int *id);
  92. int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id);
  93. int idr_for_each(struct idr *idp,
  94. int (*fn)(int id, void *p, void *data), void *data);
  95. void *idr_get_next(struct idr *idp, int *nextid);
  96. void *idr_replace(struct idr *idp, void *ptr, int id);
  97. void idr_remove(struct idr *idp, int id);
  98. void idr_remove_all(struct idr *idp);
  99. void idr_destroy(struct idr *idp);
  100. void idr_init(struct idr *idp);
  101. /*
  102. * IDA - IDR based id allocator, use when translation from id to
  103. * pointer isn't necessary.
  104. *
  105. * IDA_BITMAP_LONGS is calculated to be one less to accommodate
  106. * ida_bitmap->nr_busy so that the whole struct fits in 128 bytes.
  107. */
  108. #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
  109. #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long) - 1)
  110. #define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
  111. struct ida_bitmap {
  112. long nr_busy;
  113. unsigned long bitmap[IDA_BITMAP_LONGS];
  114. };
  115. struct ida {
  116. struct idr idr;
  117. struct ida_bitmap *free_bitmap;
  118. };
  119. #define IDA_INIT(name) { .idr = IDR_INIT(name), .free_bitmap = NULL, }
  120. #define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
  121. int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
  122. int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
  123. int ida_get_new(struct ida *ida, int *p_id);
  124. void ida_remove(struct ida *ida, int id);
  125. void ida_destroy(struct ida *ida);
  126. void ida_init(struct ida *ida);
  127. int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
  128. gfp_t gfp_mask);
  129. void ida_simple_remove(struct ida *ida, unsigned int id);
  130. void __init idr_init_cache(void);
  131. /**
  132. * idr_for_each_entry - iterate over an idr's elements of a given type
  133. * @idp: idr handle
  134. * @entry: the type * to use as cursor
  135. * @id: id entry's key
  136. */
  137. #define idr_for_each_entry(idp, entry, id) \
  138. for (id = 0, entry = (typeof(entry))idr_get_next((idp), &(id)); \
  139. entry != NULL; \
  140. ++id, entry = (typeof(entry))idr_get_next((idp), &(id)))
  141. #endif /* __IDR_H__ */