1
2
3
4
5
6
7
8
9
10
11
12#ifndef __IDR_H__
13#define __IDR_H__
14
15#include <linux/radix-tree.h>
16#include <linux/gfp.h>
17#include <linux/percpu.h>
18
19struct idr {
20 struct radix_tree_root idr_rt;
21 unsigned int idr_next;
22};
23
24
25
26
27
28#define IDR_FREE 0
29
30
31#define IDR_RT_MARKER ((__force gfp_t)(3 << __GFP_BITS_SHIFT))
32
33#define IDR_INIT \
34{ \
35 .idr_rt = RADIX_TREE_INIT(IDR_RT_MARKER) \
36}
37#define DEFINE_IDR(name) struct idr name = IDR_INIT
38
39
40
41
42
43
44
45
46
47static inline unsigned int idr_get_cursor(const struct idr *idr)
48{
49 return READ_ONCE(idr->idr_next);
50}
51
52
53
54
55
56
57
58
59
60static inline void idr_set_cursor(struct idr *idr, unsigned int val)
61{
62 WRITE_ONCE(idr->idr_next, val);
63}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82void idr_preload(gfp_t gfp_mask);
83
84int idr_alloc_cmn(struct idr *idr, void *ptr, unsigned long *index,
85 unsigned long start, unsigned long end, gfp_t gfp,
86 bool ext);
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107static inline int idr_alloc(struct idr *idr, void *ptr,
108 int start, int end, gfp_t gfp)
109{
110 unsigned long id;
111 int ret;
112
113 if (WARN_ON_ONCE(start < 0))
114 return -EINVAL;
115
116 ret = idr_alloc_cmn(idr, ptr, &id, start, end, gfp, false);
117
118 if (ret)
119 return ret;
120
121 return id;
122}
123
124static inline int idr_alloc_ext(struct idr *idr, void *ptr,
125 unsigned long *index,
126 unsigned long start,
127 unsigned long end,
128 gfp_t gfp)
129{
130 return idr_alloc_cmn(idr, ptr, index, start, end, gfp, true);
131}
132
133int idr_alloc_cyclic(struct idr *, void *entry, int start, int end, gfp_t);
134int idr_for_each(const struct idr *,
135 int (*fn)(int id, void *p, void *data), void *data);
136void *idr_get_next(struct idr *, int *nextid);
137void *idr_get_next_ext(struct idr *idr, unsigned long *nextid);
138void *idr_replace(struct idr *, void *, int id);
139void *idr_replace_ext(struct idr *idr, void *ptr, unsigned long id);
140void idr_destroy(struct idr *);
141
142static inline void *idr_remove_ext(struct idr *idr, unsigned long id)
143{
144 return radix_tree_delete_item(&idr->idr_rt, id, NULL);
145}
146
147static inline void *idr_remove(struct idr *idr, int id)
148{
149 return idr_remove_ext(idr, id);
150}
151
152static inline void idr_init(struct idr *idr)
153{
154 INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER);
155 idr->idr_next = 0;
156}
157
158static inline bool idr_is_empty(const struct idr *idr)
159{
160 return radix_tree_empty(&idr->idr_rt) &&
161 radix_tree_tagged(&idr->idr_rt, IDR_FREE);
162}
163
164
165
166
167
168
169
170static inline void idr_preload_end(void)
171{
172 preempt_enable();
173}
174
175
176
177
178
179
180
181
182
183
184
185
186
187static inline void *idr_find_ext(const struct idr *idr, unsigned long id)
188{
189 return radix_tree_lookup(&idr->idr_rt, id);
190}
191
192static inline void *idr_find(const struct idr *idr, int id)
193{
194 return idr_find_ext(idr, id);
195}
196
197
198
199
200
201
202
203
204
205
206
207#define idr_for_each_entry(idr, entry, id) \
208 for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; ++id)
209#define idr_for_each_entry_ext(idr, entry, id) \
210 for (id = 0; ((entry) = idr_get_next_ext(idr, &(id))) != NULL; ++id)
211
212
213
214
215
216
217
218
219
220
221#define idr_for_each_entry_continue(idr, entry, id) \
222 for ((entry) = idr_get_next((idr), &(id)); \
223 entry; \
224 ++id, (entry) = idr_get_next((idr), &(id)))
225
226
227
228
229
230#define IDA_CHUNK_SIZE 128
231#define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long))
232#define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
233
234struct ida_bitmap {
235 unsigned long bitmap[IDA_BITMAP_LONGS];
236};
237
238DECLARE_PER_CPU(struct ida_bitmap *, ida_bitmap);
239
240struct ida {
241 struct radix_tree_root ida_rt;
242};
243
244#define IDA_INIT { \
245 .ida_rt = RADIX_TREE_INIT(IDR_RT_MARKER | GFP_NOWAIT), \
246}
247#define DEFINE_IDA(name) struct ida name = IDA_INIT
248
249int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
250int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
251void ida_remove(struct ida *ida, int id);
252void ida_destroy(struct ida *ida);
253
254int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
255 gfp_t gfp_mask);
256void ida_simple_remove(struct ida *ida, unsigned int id);
257
258static inline void ida_init(struct ida *ida)
259{
260 INIT_RADIX_TREE(&ida->ida_rt, IDR_RT_MARKER | GFP_NOWAIT);
261}
262
263
264
265
266
267
268
269
270static inline int ida_get_new(struct ida *ida, int *p_id)
271{
272 return ida_get_new_above(ida, 0, p_id);
273}
274
275static inline bool ida_is_empty(const struct ida *ida)
276{
277 return radix_tree_empty(&ida->ida_rt);
278}
279#endif
280