1
2
3
4
5
6
7
8
9
10
11
12#ifndef __IDR_H__
13#define __IDR_H__
14
15#include <linux/types.h>
16#include <linux/bitops.h>
17#include <linux/init.h>
18#include <linux/rcupdate.h>
19
20
21
22
23
24
25
26#define IDR_BITS 8
27#define IDR_SIZE (1 << IDR_BITS)
28#define IDR_MASK ((1 << IDR_BITS)-1)
29
30struct idr_layer {
31 int prefix;
32 DECLARE_BITMAP(bitmap, IDR_SIZE);
33 struct idr_layer __rcu *ary[1<<IDR_BITS];
34 int count;
35 int layer;
36 struct rcu_head rcu_head;
37};
38
39struct idr {
40 struct idr_layer __rcu *hint;
41 struct idr_layer __rcu *top;
42 struct idr_layer *id_free;
43 int layers;
44 int id_free_cnt;
45 int cur;
46 spinlock_t lock;
47};
48
49#define IDR_INIT(name) \
50{ \
51 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
52}
53#define DEFINE_IDR(name) struct idr name = IDR_INIT(name)
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76void *idr_find_slowpath(struct idr *idp, int id);
77void idr_preload(gfp_t gfp_mask);
78int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask);
79int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask);
80int idr_for_each(struct idr *idp,
81 int (*fn)(int id, void *p, void *data), void *data);
82void *idr_get_next(struct idr *idp, int *nextid);
83void *idr_replace(struct idr *idp, void *ptr, int id);
84void idr_remove(struct idr *idp, int id);
85void idr_destroy(struct idr *idp);
86void idr_init(struct idr *idp);
87bool idr_is_empty(struct idr *idp);
88
89
90
91
92
93
94
95static inline void idr_preload_end(void)
96{
97 preempt_enable();
98}
99
100
101
102
103
104
105
106
107
108
109
110
111
112static inline void *idr_find(struct idr *idr, int id)
113{
114 struct idr_layer *hint = rcu_dereference_raw(idr->hint);
115
116 if (hint && (id & ~IDR_MASK) == hint->prefix)
117 return rcu_dereference_raw(hint->ary[id & IDR_MASK]);
118
119 return idr_find_slowpath(idr, id);
120}
121
122
123
124
125
126
127
128
129
130
131
132#define idr_for_each_entry(idp, entry, id) \
133 for (id = 0; ((entry) = idr_get_next(idp, &(id))) != NULL; ++id)
134
135
136
137
138
139int __idr_pre_get(struct idr *idp, gfp_t gfp_mask);
140int __idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id);
141void __idr_remove_all(struct idr *idp);
142
143
144
145
146
147
148
149
150
151static inline int __deprecated idr_pre_get(struct idr *idp, gfp_t gfp_mask)
152{
153 return __idr_pre_get(idp, gfp_mask);
154}
155
156
157
158
159
160
161
162
163
164
165
166static inline int __deprecated idr_get_new_above(struct idr *idp, void *ptr,
167 int starting_id, int *id)
168{
169 return __idr_get_new_above(idp, ptr, starting_id, id);
170}
171
172
173
174
175
176
177
178
179
180
181static inline int __deprecated idr_get_new(struct idr *idp, void *ptr, int *id)
182{
183 return __idr_get_new_above(idp, ptr, 0, id);
184}
185
186
187
188
189
190
191
192
193static inline void __deprecated idr_remove_all(struct idr *idp)
194{
195 __idr_remove_all(idp);
196}
197
198
199
200
201
202
203
204
205#define IDA_CHUNK_SIZE 128
206#define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long) - 1)
207#define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
208
209struct ida_bitmap {
210 long nr_busy;
211 unsigned long bitmap[IDA_BITMAP_LONGS];
212};
213
214struct ida {
215 struct idr idr;
216 struct ida_bitmap *free_bitmap;
217};
218
219#define IDA_INIT(name) { .idr = IDR_INIT((name).idr), .free_bitmap = NULL, }
220#define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
221
222int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
223int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
224void ida_remove(struct ida *ida, int id);
225void ida_destroy(struct ida *ida);
226void ida_init(struct ida *ida);
227
228int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
229 gfp_t gfp_mask);
230void ida_simple_remove(struct ida *ida, unsigned int id);
231
232
233
234
235
236
237
238
239static inline int ida_get_new(struct ida *ida, int *p_id)
240{
241 return ida_get_new_above(ida, 0, p_id);
242}
243
244void __init idr_init_cache(void);
245
246#endif
247