initial commit with v2.6.9
[linux-2.6.9-moxart.git] / lib / idr.c
blob972eefcce2b3a9b3ed825bb1b852a92fd3260f38
1 /*
2 * 2002-10-18 written by Jim Houston jim.houston@ccur.com
3 * Copyright (C) 2002 by Concurrent Computer Corporation
4 * Distributed under the GNU GPL license version 2.
6 * Modified by George Anzinger to reuse immediately and to use
7 * find bit instructions. Also removed _irq on spinlocks.
9 * Small id to pointer translation service.
11 * It uses a radix tree like structure as a sparse array indexed
12 * by the id to obtain the pointer. The bitmap makes allocating
13 * a new id quick.
15 * You call it to allocate an id (an int) an associate with that id a
16 * pointer or what ever, we treat it as a (void *). You can pass this
17 * id to a user for him to pass back at a later time. You then pass
18 * that id to this code and it returns your pointer.
20 * You can release ids at any time. When all ids are released, most of
21 * the memory is returned (we keep IDR_FREE_MAX) in a local pool so we
22 * don't need to go to the memory "store" during an id allocate, just
23 * so you don't need to be too concerned about locking and conflicts
24 * with the slab allocator.
27 #ifndef TEST // to test in user space...
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #endif
32 #include <linux/string.h>
33 #include <linux/idr.h>
35 static kmem_cache_t *idr_layer_cache;
37 static struct idr_layer *alloc_layer(struct idr *idp)
39 struct idr_layer *p;
41 spin_lock(&idp->lock);
42 if (!(p = idp->id_free)) {
43 spin_unlock(&idp->lock);
44 return NULL;
46 idp->id_free = p->ary[0];
47 idp->id_free_cnt--;
48 p->ary[0] = NULL;
49 spin_unlock(&idp->lock);
50 return(p);
53 static void free_layer(struct idr *idp, struct idr_layer *p)
56 * Depends on the return element being zeroed.
58 spin_lock(&idp->lock);
59 p->ary[0] = idp->id_free;
60 idp->id_free = p;
61 idp->id_free_cnt++;
62 spin_unlock(&idp->lock);
65 /**
66 * idr_pre_get - reserver resources for idr allocation
67 * @idp: idr handle
68 * @gfp_mask: memory allocation flags
70 * This function should be called prior to locking and calling the
71 * following function. It preallocates enough memory to satisfy
72 * the worst possible allocation.
74 * If the system is REALLY out of memory this function returns 0,
75 * otherwise 1.
77 int idr_pre_get(struct idr *idp, unsigned gfp_mask)
79 while (idp->id_free_cnt < IDR_FREE_MAX) {
80 struct idr_layer *new;
81 new = kmem_cache_alloc(idr_layer_cache, gfp_mask);
82 if(new == NULL)
83 return (0);
84 free_layer(idp, new);
86 return 1;
88 EXPORT_SYMBOL(idr_pre_get);
90 static int sub_alloc(struct idr *idp, void *ptr, int *starting_id)
92 int n, m, sh;
93 struct idr_layer *p, *new;
94 struct idr_layer *pa[MAX_LEVEL];
95 int l, id;
96 long bm;
98 id = *starting_id;
99 p = idp->top;
100 l = idp->layers;
101 pa[l--] = NULL;
102 while (1) {
104 * We run around this while until we reach the leaf node...
106 n = (id >> (IDR_BITS*l)) & IDR_MASK;
107 bm = ~p->bitmap;
108 m = find_next_bit(&bm, IDR_SIZE, n);
109 if (m == IDR_SIZE) {
110 /* no space available go back to previous layer. */
111 l++;
112 id = (id | ((1 << (IDR_BITS*l))-1)) + 1;
113 if (!(p = pa[l])) {
114 *starting_id = id;
115 return -2;
117 continue;
119 if (m != n) {
120 sh = IDR_BITS*l;
121 id = ((id >> sh) ^ n ^ m) << sh;
123 if ((id >= MAX_ID_BIT) || (id < 0))
124 return -3;
125 if (l == 0)
126 break;
128 * Create the layer below if it is missing.
130 if (!p->ary[m]) {
131 if (!(new = alloc_layer(idp)))
132 return -1;
133 p->ary[m] = new;
134 p->count++;
136 pa[l--] = p;
137 p = p->ary[m];
140 * We have reached the leaf node, plant the
141 * users pointer and return the raw id.
143 p->ary[m] = (struct idr_layer *)ptr;
144 __set_bit(m, &p->bitmap);
145 p->count++;
147 * If this layer is full mark the bit in the layer above
148 * to show that this part of the radix tree is full.
149 * This may complete the layer above and require walking
150 * up the radix tree.
152 n = id;
153 while (p->bitmap == IDR_FULL) {
154 if (!(p = pa[++l]))
155 break;
156 n = n >> IDR_BITS;
157 __set_bit((n & IDR_MASK), &p->bitmap);
159 return(id);
162 static int idr_get_new_above_int(struct idr *idp, void *ptr, int starting_id)
164 struct idr_layer *p, *new;
165 int layers, v, id;
167 id = starting_id;
168 build_up:
169 p = idp->top;
170 layers = idp->layers;
171 if (unlikely(!p)) {
172 if (!(p = alloc_layer(idp)))
173 return -1;
174 layers = 1;
177 * Add a new layer to the top of the tree if the requested
178 * id is larger than the currently allocated space.
180 while ((layers < MAX_LEVEL) && (id >= (1 << (layers*IDR_BITS)))) {
181 layers++;
182 if (!p->count)
183 continue;
184 if (!(new = alloc_layer(idp))) {
186 * The allocation failed. If we built part of
187 * the structure tear it down.
189 for (new = p; p && p != idp->top; new = p) {
190 p = p->ary[0];
191 new->ary[0] = NULL;
192 new->bitmap = new->count = 0;
193 free_layer(idp, new);
195 return -1;
197 new->ary[0] = p;
198 new->count = 1;
199 if (p->bitmap == IDR_FULL)
200 __set_bit(0, &new->bitmap);
201 p = new;
203 idp->top = p;
204 idp->layers = layers;
205 v = sub_alloc(idp, ptr, &id);
206 if (v == -2)
207 goto build_up;
208 return(v);
212 * idr_get_new_above - allocate new idr entry above a start id
213 * @idp: idr handle
214 * @ptr: pointer you want associated with the ide
215 * @start_id: id to start search at
216 * @id: pointer to the allocated handle
218 * This is the allocate id function. It should be called with any
219 * required locks.
221 * If memory is required, it will return -EAGAIN, you should unlock
222 * and go back to the idr_pre_get() call. If the idr is full, it will
223 * return -ENOSPC.
225 * @id returns a value in the range 0 ... 0x7fffffff
227 int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
229 int rv;
230 rv = idr_get_new_above_int(idp, ptr, starting_id);
232 * This is a cheap hack until the IDR code can be fixed to
233 * return proper error values.
235 if (rv < 0) {
236 if (rv == -1)
237 return -EAGAIN;
238 else /* Will be -3 */
239 return -ENOSPC;
241 *id = rv;
242 return 0;
244 EXPORT_SYMBOL(idr_get_new_above);
247 * idr_get_new - allocate new idr entry
248 * @idp: idr handle
249 * @ptr: pointer you want associated with the ide
250 * @id: pointer to the allocated handle
252 * This is the allocate id function. It should be called with any
253 * required locks.
255 * If memory is required, it will return -EAGAIN, you should unlock
256 * and go back to the idr_pre_get() call. If the idr is full, it will
257 * return -ENOSPC.
259 * @id returns a value in the range 0 ... 0x7fffffff
261 int idr_get_new(struct idr *idp, void *ptr, int *id)
263 int rv;
264 rv = idr_get_new_above_int(idp, ptr, 0);
266 * This is a cheap hack until the IDR code can be fixed to
267 * return proper error values.
269 if (rv < 0) {
270 if (rv == -1)
271 return -EAGAIN;
272 else /* Will be -3 */
273 return -ENOSPC;
275 *id = rv;
276 return 0;
278 EXPORT_SYMBOL(idr_get_new);
280 static void sub_remove(struct idr *idp, int shift, int id)
282 struct idr_layer *p = idp->top;
283 struct idr_layer **pa[MAX_LEVEL];
284 struct idr_layer ***paa = &pa[0];
286 *paa = NULL;
287 *++paa = &idp->top;
289 while ((shift > 0) && p) {
290 int n = (id >> shift) & IDR_MASK;
291 __clear_bit(n, &p->bitmap);
292 *++paa = &p->ary[n];
293 p = p->ary[n];
294 shift -= IDR_BITS;
296 if (likely(p != NULL)){
297 int n = id & IDR_MASK;
298 __clear_bit(n, &p->bitmap);
299 p->ary[n] = NULL;
300 while(*paa && ! --((**paa)->count)){
301 free_layer(idp, **paa);
302 **paa-- = NULL;
304 if ( ! *paa )
305 idp->layers = 0;
310 * idr_remove - remove the given id and free it's slot
311 * idp: idr handle
312 * id: uniqueue key
314 void idr_remove(struct idr *idp, int id)
316 struct idr_layer *p;
318 /* Mask off upper bits we don't use for the search. */
319 id &= MAX_ID_MASK;
321 sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
322 if ( idp->top && idp->top->count == 1 &&
323 (idp->layers > 1) &&
324 idp->top->ary[0]){ // We can drop a layer
326 p = idp->top->ary[0];
327 idp->top->bitmap = idp->top->count = 0;
328 free_layer(idp, idp->top);
329 idp->top = p;
330 --idp->layers;
332 while (idp->id_free_cnt >= IDR_FREE_MAX) {
334 p = alloc_layer(idp);
335 kmem_cache_free(idr_layer_cache, p);
336 return;
339 EXPORT_SYMBOL(idr_remove);
342 * idr_find - return pointer for given id
343 * @idp: idr handle
344 * @id: lookup key
346 * Return the pointer given the id it has been registered with. A %NULL
347 * return indicates that @id is not valid or you passed %NULL in
348 * idr_get_new().
350 * The caller must serialize idr_find() vs idr_get_new() and idr_remove().
352 void *idr_find(struct idr *idp, int id)
354 int n;
355 struct idr_layer *p;
357 n = idp->layers * IDR_BITS;
358 p = idp->top;
360 /* Mask off upper bits we don't use for the search. */
361 id &= MAX_ID_MASK;
363 if (id >= (1 << n))
364 return NULL;
366 while (n > 0 && p) {
367 n -= IDR_BITS;
368 p = p->ary[(id >> n) & IDR_MASK];
370 return((void *)p);
372 EXPORT_SYMBOL(idr_find);
374 static void idr_cache_ctor(void * idr_layer,
375 kmem_cache_t *idr_layer_cache, unsigned long flags)
377 memset(idr_layer, 0, sizeof(struct idr_layer));
380 static int init_id_cache(void)
382 if (!idr_layer_cache)
383 idr_layer_cache = kmem_cache_create("idr_layer_cache",
384 sizeof(struct idr_layer), 0, 0, idr_cache_ctor, NULL);
385 return 0;
389 * idr_init - initialize idr handle
390 * @idp: idr handle
392 * This function is use to set up the handle (@idp) that you will pass
393 * to the rest of the functions.
395 void idr_init(struct idr *idp)
397 init_id_cache();
398 memset(idp, 0, sizeof(struct idr));
399 spin_lock_init(&idp->lock);
401 EXPORT_SYMBOL(idr_init);