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
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>
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)
41 spin_lock(&idp->lock);
42 if ((p = idp->id_free)) {
43 idp->id_free = p->ary[0];
47 spin_unlock(&idp->lock);
51 /* only called when idp->lock is held */
52 static void __free_layer(struct idr *idp, struct idr_layer *p)
54 p->ary[0] = idp->id_free;
59 static void free_layer(struct idr *idp, struct idr_layer *p)
62 * Depends on the return element being zeroed.
64 spin_lock(&idp->lock);
66 spin_unlock(&idp->lock);
70 * idr_pre_get - reserver resources for idr allocation
72 * @gfp_mask: memory allocation flags
74 * This function should be called prior to locking and calling the
75 * following function. It preallocates enough memory to satisfy
76 * the worst possible allocation.
78 * If the system is REALLY out of memory this function returns 0,
81 int idr_pre_get(struct idr *idp, gfp_t gfp_mask)
83 while (idp->id_free_cnt < IDR_FREE_MAX) {
84 struct idr_layer *new;
85 new = kmem_cache_alloc(idr_layer_cache, gfp_mask);
92 EXPORT_SYMBOL(idr_pre_get);
94 static int sub_alloc(struct idr *idp, void *ptr, int *starting_id)
97 struct idr_layer *p, *new;
98 struct idr_layer *pa[MAX_LEVEL];
108 * We run around this while until we reach the leaf node...
110 n = (id >> (IDR_BITS*l)) & IDR_MASK;
112 m = find_next_bit(&bm, IDR_SIZE, n);
114 /* no space available go back to previous layer. */
116 id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
125 id = ((id >> sh) ^ n ^ m) << sh;
127 if ((id >= MAX_ID_BIT) || (id < 0))
132 * Create the layer below if it is missing.
135 if (!(new = alloc_layer(idp)))
144 * We have reached the leaf node, plant the
145 * users pointer and return the raw id.
147 p->ary[m] = (struct idr_layer *)ptr;
148 __set_bit(m, &p->bitmap);
151 * If this layer is full mark the bit in the layer above
152 * to show that this part of the radix tree is full.
153 * This may complete the layer above and require walking
157 while (p->bitmap == IDR_FULL) {
161 __set_bit((n & IDR_MASK), &p->bitmap);
166 static int idr_get_new_above_int(struct idr *idp, void *ptr, int starting_id)
168 struct idr_layer *p, *new;
174 layers = idp->layers;
176 if (!(p = alloc_layer(idp)))
181 * Add a new layer to the top of the tree if the requested
182 * id is larger than the currently allocated space.
184 while ((layers < (MAX_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) {
188 if (!(new = alloc_layer(idp))) {
190 * The allocation failed. If we built part of
191 * the structure tear it down.
193 spin_lock(&idp->lock);
194 for (new = p; p && p != idp->top; new = p) {
197 new->bitmap = new->count = 0;
198 __free_layer(idp, new);
200 spin_unlock(&idp->lock);
205 if (p->bitmap == IDR_FULL)
206 __set_bit(0, &new->bitmap);
210 idp->layers = layers;
211 v = sub_alloc(idp, ptr, &id);
218 * idr_get_new_above - allocate new idr entry above or equal to a start id
220 * @ptr: pointer you want associated with the ide
221 * @start_id: id to start search at
222 * @id: pointer to the allocated handle
224 * This is the allocate id function. It should be called with any
227 * If memory is required, it will return -EAGAIN, you should unlock
228 * and go back to the idr_pre_get() call. If the idr is full, it will
231 * @id returns a value in the range 0 ... 0x7fffffff
233 int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
237 rv = idr_get_new_above_int(idp, ptr, starting_id);
239 * This is a cheap hack until the IDR code can be fixed to
240 * return proper error values.
245 else /* Will be -3 */
251 EXPORT_SYMBOL(idr_get_new_above);
254 * idr_get_new - allocate new idr entry
256 * @ptr: pointer you want associated with the ide
257 * @id: pointer to the allocated handle
259 * This is the allocate id function. It should be called with any
262 * If memory is required, it will return -EAGAIN, you should unlock
263 * and go back to the idr_pre_get() call. If the idr is full, it will
266 * @id returns a value in the range 0 ... 0x7fffffff
268 int idr_get_new(struct idr *idp, void *ptr, int *id)
272 rv = idr_get_new_above_int(idp, ptr, 0);
274 * This is a cheap hack until the IDR code can be fixed to
275 * return proper error values.
280 else /* Will be -3 */
286 EXPORT_SYMBOL(idr_get_new);
288 static void idr_remove_warning(int id)
290 printk("idr_remove called for id=%d which is not allocated.\n", id);
294 static void sub_remove(struct idr *idp, int shift, int id)
296 struct idr_layer *p = idp->top;
297 struct idr_layer **pa[MAX_LEVEL];
298 struct idr_layer ***paa = &pa[0];
304 while ((shift > 0) && p) {
305 n = (id >> shift) & IDR_MASK;
306 __clear_bit(n, &p->bitmap);
312 if (likely(p != NULL && test_bit(n, &p->bitmap))){
313 __clear_bit(n, &p->bitmap);
315 while(*paa && ! --((**paa)->count)){
316 free_layer(idp, **paa);
322 idr_remove_warning(id);
326 * idr_remove - remove the given id and free it's slot
330 void idr_remove(struct idr *idp, int id)
334 /* Mask off upper bits we don't use for the search. */
337 sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
338 if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
339 idp->top->ary[0]) { // We can drop a layer
341 p = idp->top->ary[0];
342 idp->top->bitmap = idp->top->count = 0;
343 free_layer(idp, idp->top);
347 while (idp->id_free_cnt >= IDR_FREE_MAX) {
348 p = alloc_layer(idp);
349 kmem_cache_free(idr_layer_cache, p);
353 EXPORT_SYMBOL(idr_remove);
356 * idr_destroy - release all cached layers within an idr tree
359 void idr_destroy(struct idr *idp)
361 while (idp->id_free_cnt) {
362 struct idr_layer *p = alloc_layer(idp);
363 kmem_cache_free(idr_layer_cache, p);
366 EXPORT_SYMBOL(idr_destroy);
369 * idr_find - return pointer for given id
373 * Return the pointer given the id it has been registered with. A %NULL
374 * return indicates that @id is not valid or you passed %NULL in
377 * The caller must serialize idr_find() vs idr_get_new() and idr_remove().
379 void *idr_find(struct idr *idp, int id)
384 n = idp->layers * IDR_BITS;
387 /* Mask off upper bits we don't use for the search. */
395 p = p->ary[(id >> n) & IDR_MASK];
399 EXPORT_SYMBOL(idr_find);
401 static void idr_cache_ctor(void * idr_layer, kmem_cache_t *idr_layer_cache,
404 memset(idr_layer, 0, sizeof(struct idr_layer));
407 static int init_id_cache(void)
409 if (!idr_layer_cache)
410 idr_layer_cache = kmem_cache_create("idr_layer_cache",
411 sizeof(struct idr_layer), 0, 0, idr_cache_ctor, NULL);
416 * idr_init - initialize idr handle
419 * This function is use to set up the handle (@idp) that you will pass
420 * to the rest of the functions.
422 void idr_init(struct idr *idp)
425 memset(idp, 0, sizeof(struct idr));
426 spin_lock_init(&idp->lock);
428 EXPORT_SYMBOL(idr_init);