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 int layer;
33 struct idr_layer __rcu *ary[1<<IDR_BITS];
34 int count;
35 union {
36
37 DECLARE_BITMAP(bitmap, IDR_SIZE);
38 struct rcu_head rcu_head;
39 };
40};
41
42struct idr {
43 struct idr_layer __rcu *hint;
44 struct idr_layer __rcu *top;
45 int layers;
46 int cur;
47 spinlock_t lock;
48 int id_free_cnt;
49 struct idr_layer *id_free;
50};
51
52#define IDR_INIT(name) \
53{ \
54 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
55}
56#define DEFINE_IDR(name) struct idr name = IDR_INIT(name)
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79void *idr_find_slowpath(struct idr *idp, int id);
80void idr_preload(gfp_t gfp_mask);
81int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask);
82int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask);
83int idr_for_each(struct idr *idp,
84 int (*fn)(int id, void *p, void *data), void *data);
85void *idr_get_next(struct idr *idp, int *nextid);
86void *idr_replace(struct idr *idp, void *ptr, int id);
87void idr_remove(struct idr *idp, int id);
88void idr_destroy(struct idr *idp);
89void idr_init(struct idr *idp);
90bool idr_is_empty(struct idr *idp);
91
92
93
94
95
96
97
98static inline void idr_preload_end(void)
99{
100 preempt_enable();
101}
102
103
104
105
106
107
108
109
110
111
112
113
114
115static inline void *idr_find(struct idr *idr, int id)
116{
117 struct idr_layer *hint = rcu_dereference_raw(idr->hint);
118
119 if (hint && (id & ~IDR_MASK) == hint->prefix)
120 return rcu_dereference_raw(hint->ary[id & IDR_MASK]);
121
122 return idr_find_slowpath(idr, id);
123}
124
125
126
127
128
129
130
131
132
133
134
135#define idr_for_each_entry(idp, entry, id) \
136 for (id = 0; ((entry) = idr_get_next(idp, &(id))) != NULL; ++id)
137
138
139
140
141
142
143
144
145#define IDA_CHUNK_SIZE 128
146#define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long) - 1)
147#define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
148
149struct ida_bitmap {
150 long nr_busy;
151 unsigned long bitmap[IDA_BITMAP_LONGS];
152};
153
154struct ida {
155 struct idr idr;
156 struct ida_bitmap *free_bitmap;
157};
158
159#define IDA_INIT(name) { .idr = IDR_INIT((name).idr), .free_bitmap = NULL, }
160#define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
161
162int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
163int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
164void ida_remove(struct ida *ida, int id);
165void ida_destroy(struct ida *ida);
166void ida_init(struct ida *ida);
167
168int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
169 gfp_t gfp_mask);
170void ida_simple_remove(struct ida *ida, unsigned int id);
171
172
173
174
175
176
177
178
179static inline int ida_get_new(struct ida *ida, int *p_id)
180{
181 return ida_get_new_above(ida, 0, p_id);
182}
183
184void __init idr_init_cache(void);
185
186#endif
187