vserver 1.9.3
[linux-2.6.git] / lib / idr.c
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.
5  *
6  * Modified by George Anzinger to reuse immediately and to use
7  * find bit instructions.  Also removed _irq on spinlocks.
8  *
9  * Small id to pointer translation service.  
10  *
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.  
14  *
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.
19
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.
25  */
26
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>
34
35 static kmem_cache_t *idr_layer_cache;
36
37 static struct idr_layer *alloc_layer(struct idr *idp)
38 {
39         struct idr_layer *p;
40
41         spin_lock(&idp->lock);
42         if (!(p = idp->id_free)) {
43                 spin_unlock(&idp->lock);
44                 return NULL;
45         }
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);
51 }
52
53 static void free_layer(struct idr *idp, struct idr_layer *p)
54 {
55         /*
56          * Depends on the return element being zeroed.
57          */
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);
63 }
64
65 /**
66  * idr_pre_get - reserver resources for idr allocation
67  * @idp:        idr handle
68  * @gfp_mask:   memory allocation flags
69  *
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.
73  *
74  * If the system is REALLY out of memory this function returns 0,
75  * otherwise 1.
76  */
77 int idr_pre_get(struct idr *idp, unsigned gfp_mask)
78 {
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);
85         }
86         return 1;
87 }
88 EXPORT_SYMBOL(idr_pre_get);
89
90 static int sub_alloc(struct idr *idp, void *ptr, int *starting_id)
91 {
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;
97
98         id = *starting_id;
99         p = idp->top;
100         l = idp->layers;
101         pa[l--] = NULL;
102         while (1) {
103                 /*
104                  * We run around this while until we reach the leaf node...
105                  */
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;
116                         }
117                         continue;
118                 }
119                 if (m != n) {
120                         sh = IDR_BITS*l;
121                         id = ((id >> sh) ^ n ^ m) << sh;
122                 }
123                 if ((id >= MAX_ID_BIT) || (id < 0))
124                         return -3;
125                 if (l == 0)
126                         break;
127                 /*
128                  * Create the layer below if it is missing.
129                  */
130                 if (!p->ary[m]) {
131                         if (!(new = alloc_layer(idp)))
132                                 return -1;
133                         p->ary[m] = new;
134                         p->count++;
135                 }
136                 pa[l--] = p;
137                 p = p->ary[m];
138         }
139         /*
140          * We have reached the leaf node, plant the
141          * users pointer and return the raw id.
142          */
143         p->ary[m] = (struct idr_layer *)ptr;
144         __set_bit(m, &p->bitmap);
145         p->count++;
146         /*
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.
151          */
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);
158         }
159         return(id);
160 }
161
162 static int idr_get_new_above_int(struct idr *idp, void *ptr, int starting_id)
163 {
164         struct idr_layer *p, *new;
165         int layers, v, id;
166         
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;
175         }
176         /*
177          * Add a new layer to the top of the tree if the requested
178          * id is larger than the currently allocated space.
179          */
180         while ((layers < MAX_LEVEL) && (id >= (1 << (layers*IDR_BITS)))) {
181                 layers++;
182                 if (!p->count)
183                         continue;
184                 if (!(new = alloc_layer(idp))) {
185                         /*
186                          * The allocation failed.  If we built part of
187                          * the structure tear it down.
188                          */
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);
194                         }
195                         return -1;
196                 }
197                 new->ary[0] = p;
198                 new->count = 1;
199                 if (p->bitmap == IDR_FULL)
200                         __set_bit(0, &new->bitmap);
201                 p = new;
202         }
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);
209 }
210
211 /**
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
217  *
218  * This is the allocate id function.  It should be called with any
219  * required locks.
220  *
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.
224  *
225  * @id returns a value in the range 0 ... 0x7fffffff
226  */
227 int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
228 {
229         int rv;
230         rv = idr_get_new_above_int(idp, ptr, starting_id);
231         /*
232          * This is a cheap hack until the IDR code can be fixed to
233          * return proper error values.
234          */
235         if (rv < 0) {
236                 if (rv == -1)
237                         return -EAGAIN;
238                 else /* Will be -3 */
239                         return -ENOSPC;
240         }
241         *id = rv;
242         return 0;
243 }
244 EXPORT_SYMBOL(idr_get_new_above);
245
246 /**
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
251  *
252  * This is the allocate id function.  It should be called with any
253  * required locks.
254  *
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.
258  *
259  * @id returns a value in the range 0 ... 0x7fffffff
260  */
261 int idr_get_new(struct idr *idp, void *ptr, int *id)
262 {
263         int rv;
264         rv = idr_get_new_above_int(idp, ptr, 0);
265         /*
266          * This is a cheap hack until the IDR code can be fixed to
267          * return proper error values.
268          */
269         if (rv < 0) {
270                 if (rv == -1)
271                         return -EAGAIN;
272                 else /* Will be -3 */
273                         return -ENOSPC;
274         }
275         *id = rv;
276         return 0;
277 }
278 EXPORT_SYMBOL(idr_get_new);
279
280 static void sub_remove(struct idr *idp, int shift, int id)
281 {
282         struct idr_layer *p = idp->top;
283         struct idr_layer **pa[MAX_LEVEL];
284         struct idr_layer ***paa = &pa[0];
285
286         *paa = NULL;
287         *++paa = &idp->top;
288
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;
295         }
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;
303                 }
304                 if ( ! *paa )
305                         idp->layers = 0;
306         }
307 }
308
309 /**
310  * idr_remove - remove the given id and free it's slot
311  * idp: idr handle
312  * id: uniqueue key
313  */
314 void idr_remove(struct idr *idp, int id)
315 {
316         struct idr_layer *p;
317
318         /* Mask off upper bits we don't use for the search. */
319         id &= MAX_ID_MASK;
320
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
325
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;
331         }
332         while (idp->id_free_cnt >= IDR_FREE_MAX) {
333                 
334                 p = alloc_layer(idp);
335                 kmem_cache_free(idr_layer_cache, p);
336                 return;
337         }
338 }
339 EXPORT_SYMBOL(idr_remove);
340
341 /**
342  * idr_find - return pointer for given id
343  * @idp: idr handle
344  * @id: lookup key
345  *
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().
349  *
350  * The caller must serialize idr_find() vs idr_get_new() and idr_remove().
351  */
352 void *idr_find(struct idr *idp, int id)
353 {
354         int n;
355         struct idr_layer *p;
356
357         n = idp->layers * IDR_BITS;
358         p = idp->top;
359
360         /* Mask off upper bits we don't use for the search. */
361         id &= MAX_ID_MASK;
362
363         if (id >= (1 << n))
364                 return NULL;
365
366         while (n > 0 && p) {
367                 n -= IDR_BITS;
368                 p = p->ary[(id >> n) & IDR_MASK];
369         }
370         return((void *)p);
371 }
372 EXPORT_SYMBOL(idr_find);
373
374 static void idr_cache_ctor(void * idr_layer, 
375                            kmem_cache_t *idr_layer_cache, unsigned long flags)
376 {
377         memset(idr_layer, 0, sizeof(struct idr_layer));
378 }
379
380 static  int init_id_cache(void)
381 {
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;
386 }
387
388 /**
389  * idr_init - initialize idr handle
390  * @idp:        idr handle
391  *
392  * This function is use to set up the handle (@idp) that you will pass
393  * to the rest of the functions.
394  */
395 void idr_init(struct idr *idp)
396 {
397         init_id_cache();
398         memset(idp, 0, sizeof(struct idr));
399         spin_lock_init(&idp->lock);
400 }
401 EXPORT_SYMBOL(idr_init);