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/err.h>
33 #include <linux/string.h>
34 #include <linux/idr.h>
36 static struct kmem_cache *idr_layer_cache;
38 static struct idr_layer *get_from_free_list(struct idr *idp)
43 spin_lock_irqsave(&idp->lock, flags);
44 if ((p = idp->id_free)) {
45 idp->id_free = p->ary[0];
49 spin_unlock_irqrestore(&idp->lock, flags);
53 /* only called when idp->lock is held */
54 static void __move_to_free_list(struct idr *idp, struct idr_layer *p)
56 p->ary[0] = idp->id_free;
61 static void move_to_free_list(struct idr *idp, struct idr_layer *p)
66 * Depends on the return element being zeroed.
68 spin_lock_irqsave(&idp->lock, flags);
69 __move_to_free_list(idp, p);
70 spin_unlock_irqrestore(&idp->lock, flags);
73 static void idr_mark_full(struct idr_layer **pa, int id)
75 struct idr_layer *p = pa[0];
78 __set_bit(id & IDR_MASK, &p->bitmap);
80 * If this layer is full mark the bit in the layer above to
81 * show that this part of the radix tree is full. This may
82 * complete the layer above and require walking up the radix
85 while (p->bitmap == IDR_FULL) {
89 __set_bit((id & IDR_MASK), &p->bitmap);
94 * idr_pre_get - reserver resources for idr allocation
96 * @gfp_mask: memory allocation flags
98 * This function should be called prior to locking and calling the
99 * following function. It preallocates enough memory to satisfy
100 * the worst possible allocation.
102 * If the system is REALLY out of memory this function returns 0,
105 int idr_pre_get(struct idr *idp, gfp_t gfp_mask)
107 while (idp->id_free_cnt < IDR_FREE_MAX) {
108 struct idr_layer *new;
109 new = kmem_cache_alloc(idr_layer_cache, gfp_mask);
112 move_to_free_list(idp, new);
116 EXPORT_SYMBOL(idr_pre_get);
118 static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa)
121 struct idr_layer *p, *new;
132 * We run around this while until we reach the leaf node...
134 n = (id >> (IDR_BITS*l)) & IDR_MASK;
136 m = find_next_bit(&bm, IDR_SIZE, n);
138 /* no space available go back to previous layer. */
141 id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
143 /* if already at the top layer, we need to grow */
146 return IDR_NEED_TO_GROW;
149 /* If we need to go up one layer, continue the
150 * loop; otherwise, restart from the top.
152 sh = IDR_BITS * (l + 1);
153 if (oid >> sh == id >> sh)
160 id = ((id >> sh) ^ n ^ m) << sh;
162 if ((id >= MAX_ID_BIT) || (id < 0))
163 return IDR_NOMORE_SPACE;
167 * Create the layer below if it is missing.
170 new = get_from_free_list(idp);
184 static int idr_get_empty_slot(struct idr *idp, int starting_id,
185 struct idr_layer **pa)
187 struct idr_layer *p, *new;
194 layers = idp->layers;
196 if (!(p = get_from_free_list(idp)))
201 * Add a new layer to the top of the tree if the requested
202 * id is larger than the currently allocated space.
204 while ((layers < (MAX_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) {
208 if (!(new = get_from_free_list(idp))) {
210 * The allocation failed. If we built part of
211 * the structure tear it down.
213 spin_lock_irqsave(&idp->lock, flags);
214 for (new = p; p && p != idp->top; new = p) {
217 new->bitmap = new->count = 0;
218 __move_to_free_list(idp, new);
220 spin_unlock_irqrestore(&idp->lock, flags);
225 if (p->bitmap == IDR_FULL)
226 __set_bit(0, &new->bitmap);
230 idp->layers = layers;
231 v = sub_alloc(idp, &id, pa);
232 if (v == IDR_NEED_TO_GROW)
237 static int idr_get_new_above_int(struct idr *idp, void *ptr, int starting_id)
239 struct idr_layer *pa[MAX_LEVEL];
242 id = idr_get_empty_slot(idp, starting_id, pa);
245 * Successfully found an empty slot. Install the user
246 * pointer and mark the slot full.
248 pa[0]->ary[id & IDR_MASK] = (struct idr_layer *)ptr;
250 idr_mark_full(pa, id);
257 * idr_get_new_above - allocate new idr entry above or equal to a start id
259 * @ptr: pointer you want associated with the ide
260 * @start_id: id to start search at
261 * @id: pointer to the allocated handle
263 * This is the allocate id function. It should be called with any
266 * If memory is required, it will return -EAGAIN, you should unlock
267 * and go back to the idr_pre_get() call. If the idr is full, it will
270 * @id returns a value in the range 0 ... 0x7fffffff
272 int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
276 rv = idr_get_new_above_int(idp, ptr, starting_id);
278 * This is a cheap hack until the IDR code can be fixed to
279 * return proper error values.
282 return _idr_rc_to_errno(rv);
286 EXPORT_SYMBOL(idr_get_new_above);
289 * idr_get_new - allocate new idr entry
291 * @ptr: pointer you want associated with the ide
292 * @id: pointer to the allocated handle
294 * This is the allocate id function. It should be called with any
297 * If memory is required, it will return -EAGAIN, you should unlock
298 * and go back to the idr_pre_get() call. If the idr is full, it will
301 * @id returns a value in the range 0 ... 0x7fffffff
303 int idr_get_new(struct idr *idp, void *ptr, int *id)
307 rv = idr_get_new_above_int(idp, ptr, 0);
309 * This is a cheap hack until the IDR code can be fixed to
310 * return proper error values.
313 return _idr_rc_to_errno(rv);
317 EXPORT_SYMBOL(idr_get_new);
319 static void idr_remove_warning(int id)
322 "idr_remove called for id=%d which is not allocated.\n", id);
326 static void sub_remove(struct idr *idp, int shift, int id)
328 struct idr_layer *p = idp->top;
329 struct idr_layer **pa[MAX_LEVEL];
330 struct idr_layer ***paa = &pa[0];
336 while ((shift > 0) && p) {
337 n = (id >> shift) & IDR_MASK;
338 __clear_bit(n, &p->bitmap);
344 if (likely(p != NULL && test_bit(n, &p->bitmap))){
345 __clear_bit(n, &p->bitmap);
347 while(*paa && ! --((**paa)->count)){
348 move_to_free_list(idp, **paa);
354 idr_remove_warning(id);
358 * idr_remove - remove the given id and free it's slot
362 void idr_remove(struct idr *idp, int id)
366 /* Mask off upper bits we don't use for the search. */
369 sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
370 if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
371 idp->top->ary[0]) { // We can drop a layer
373 p = idp->top->ary[0];
374 idp->top->bitmap = idp->top->count = 0;
375 move_to_free_list(idp, idp->top);
379 while (idp->id_free_cnt >= IDR_FREE_MAX) {
380 p = get_from_free_list(idp);
381 kmem_cache_free(idr_layer_cache, p);
385 EXPORT_SYMBOL(idr_remove);
388 * idr_remove_all - remove all ids from the given idr tree
391 * idr_destroy() only frees up unused, cached idp_layers, but this
392 * function will remove all id mappings and leave all idp_layers
395 * A typical clean-up sequence for objects stored in an idr tree, will
396 * use idr_for_each() to free all objects, if necessay, then
397 * idr_remove_all() to remove all ids, and idr_destroy() to free
398 * up the cached idr_layers.
400 void idr_remove_all(struct idr *idp)
404 struct idr_layer *pa[MAX_LEVEL];
405 struct idr_layer **paa = &pa[0];
407 n = idp->layers * IDR_BITS;
413 while (n > IDR_BITS && p) {
416 p = p->ary[(id >> n) & IDR_MASK];
420 while (n < fls(id)) {
422 memset(p, 0, sizeof *p);
423 move_to_free_list(idp, p);
432 EXPORT_SYMBOL(idr_remove_all);
435 * idr_destroy - release all cached layers within an idr tree
438 void idr_destroy(struct idr *idp)
440 while (idp->id_free_cnt) {
441 struct idr_layer *p = get_from_free_list(idp);
442 kmem_cache_free(idr_layer_cache, p);
445 EXPORT_SYMBOL(idr_destroy);
448 * idr_find - return pointer for given id
452 * Return the pointer given the id it has been registered with. A %NULL
453 * return indicates that @id is not valid or you passed %NULL in
456 * The caller must serialize idr_find() vs idr_get_new() and idr_remove().
458 void *idr_find(struct idr *idp, int id)
463 n = idp->layers * IDR_BITS;
466 /* Mask off upper bits we don't use for the search. */
474 p = p->ary[(id >> n) & IDR_MASK];
478 EXPORT_SYMBOL(idr_find);
481 * idr_for_each - iterate through all stored pointers
483 * @fn: function to be called for each pointer
484 * @data: data passed back to callback function
486 * Iterate over the pointers registered with the given idr. The
487 * callback function will be called for each pointer currently
488 * registered, passing the id, the pointer and the data pointer passed
489 * to this function. It is not safe to modify the idr tree while in
490 * the callback, so functions such as idr_get_new and idr_remove are
493 * We check the return of @fn each time. If it returns anything other
494 * than 0, we break out and return that value.
496 * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove().
498 int idr_for_each(struct idr *idp,
499 int (*fn)(int id, void *p, void *data), void *data)
501 int n, id, max, error = 0;
503 struct idr_layer *pa[MAX_LEVEL];
504 struct idr_layer **paa = &pa[0];
506 n = idp->layers * IDR_BITS;
515 p = p->ary[(id >> n) & IDR_MASK];
519 error = fn(id, (void *)p, data);
525 while (n < fls(id)) {
533 EXPORT_SYMBOL(idr_for_each);
536 * idr_replace - replace pointer for given id
538 * @ptr: pointer you want associated with the id
541 * Replace the pointer registered with an id and return the old value.
542 * A -ENOENT return indicates that @id was not found.
543 * A -EINVAL return indicates that @id was not within valid constraints.
545 * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove().
547 void *idr_replace(struct idr *idp, void *ptr, int id)
550 struct idr_layer *p, *old_p;
552 n = idp->layers * IDR_BITS;
558 return ERR_PTR(-EINVAL);
561 while ((n > 0) && p) {
562 p = p->ary[(id >> n) & IDR_MASK];
567 if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
568 return ERR_PTR(-ENOENT);
575 EXPORT_SYMBOL(idr_replace);
577 static void idr_cache_ctor(struct kmem_cache *idr_layer_cache, void *idr_layer)
579 memset(idr_layer, 0, sizeof(struct idr_layer));
582 void __init idr_init_cache(void)
584 idr_layer_cache = kmem_cache_create("idr_layer_cache",
585 sizeof(struct idr_layer), 0, SLAB_PANIC,
590 * idr_init - initialize idr handle
593 * This function is use to set up the handle (@idp) that you will pass
594 * to the rest of the functions.
596 void idr_init(struct idr *idp)
598 memset(idp, 0, sizeof(struct idr));
599 spin_lock_init(&idp->lock);
601 EXPORT_SYMBOL(idr_init);
605 * IDA - IDR based ID allocator
607 * this is id allocator without id -> pointer translation. Memory
608 * usage is much lower than full blown idr because each id only
609 * occupies a bit. ida uses a custom leaf node which contains
610 * IDA_BITMAP_BITS slots.
612 * 2007-04-25 written by Tejun Heo <htejun@gmail.com>
615 static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
619 if (!ida->free_bitmap) {
620 spin_lock_irqsave(&ida->idr.lock, flags);
621 if (!ida->free_bitmap) {
622 ida->free_bitmap = bitmap;
625 spin_unlock_irqrestore(&ida->idr.lock, flags);
632 * ida_pre_get - reserve resources for ida allocation
634 * @gfp_mask: memory allocation flag
636 * This function should be called prior to locking and calling the
637 * following function. It preallocates enough memory to satisfy the
638 * worst possible allocation.
640 * If the system is REALLY out of memory this function returns 0,
643 int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
645 /* allocate idr_layers */
646 if (!idr_pre_get(&ida->idr, gfp_mask))
649 /* allocate free_bitmap */
650 if (!ida->free_bitmap) {
651 struct ida_bitmap *bitmap;
653 bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
657 free_bitmap(ida, bitmap);
662 EXPORT_SYMBOL(ida_pre_get);
665 * ida_get_new_above - allocate new ID above or equal to a start id
667 * @staring_id: id to start search at
668 * @p_id: pointer to the allocated handle
670 * Allocate new ID above or equal to @ida. It should be called with
671 * any required locks.
673 * If memory is required, it will return -EAGAIN, you should unlock
674 * and go back to the ida_pre_get() call. If the ida is full, it will
677 * @p_id returns a value in the range 0 ... 0x7fffffff.
679 int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
681 struct idr_layer *pa[MAX_LEVEL];
682 struct ida_bitmap *bitmap;
684 int idr_id = starting_id / IDA_BITMAP_BITS;
685 int offset = starting_id % IDA_BITMAP_BITS;
689 /* get vacant slot */
690 t = idr_get_empty_slot(&ida->idr, idr_id, pa);
692 return _idr_rc_to_errno(t);
694 if (t * IDA_BITMAP_BITS >= MAX_ID_BIT)
701 /* if bitmap isn't there, create a new one */
702 bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
704 spin_lock_irqsave(&ida->idr.lock, flags);
705 bitmap = ida->free_bitmap;
706 ida->free_bitmap = NULL;
707 spin_unlock_irqrestore(&ida->idr.lock, flags);
712 memset(bitmap, 0, sizeof(struct ida_bitmap));
713 pa[0]->ary[idr_id & IDR_MASK] = (void *)bitmap;
717 /* lookup for empty slot */
718 t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
719 if (t == IDA_BITMAP_BITS) {
720 /* no empty slot after offset, continue to the next chunk */
726 id = idr_id * IDA_BITMAP_BITS + t;
727 if (id >= MAX_ID_BIT)
730 __set_bit(t, bitmap->bitmap);
731 if (++bitmap->nr_busy == IDA_BITMAP_BITS)
732 idr_mark_full(pa, idr_id);
736 /* Each leaf node can handle nearly a thousand slots and the
737 * whole idea of ida is to have small memory foot print.
738 * Throw away extra resources one by one after each successful
741 if (ida->idr.id_free_cnt || ida->free_bitmap) {
742 struct idr_layer *p = get_from_free_list(&ida->idr);
744 kmem_cache_free(idr_layer_cache, p);
749 EXPORT_SYMBOL(ida_get_new_above);
752 * ida_get_new - allocate new ID
754 * @p_id: pointer to the allocated handle
756 * Allocate new ID. It should be called with any required locks.
758 * If memory is required, it will return -EAGAIN, you should unlock
759 * and go back to the idr_pre_get() call. If the idr is full, it will
762 * @id returns a value in the range 0 ... 0x7fffffff.
764 int ida_get_new(struct ida *ida, int *p_id)
766 return ida_get_new_above(ida, 0, p_id);
768 EXPORT_SYMBOL(ida_get_new);
771 * ida_remove - remove the given ID
775 void ida_remove(struct ida *ida, int id)
777 struct idr_layer *p = ida->idr.top;
778 int shift = (ida->idr.layers - 1) * IDR_BITS;
779 int idr_id = id / IDA_BITMAP_BITS;
780 int offset = id % IDA_BITMAP_BITS;
782 struct ida_bitmap *bitmap;
784 /* clear full bits while looking up the leaf idr_layer */
785 while ((shift > 0) && p) {
786 n = (idr_id >> shift) & IDR_MASK;
787 __clear_bit(n, &p->bitmap);
795 n = idr_id & IDR_MASK;
796 __clear_bit(n, &p->bitmap);
798 bitmap = (void *)p->ary[n];
799 if (!test_bit(offset, bitmap->bitmap))
802 /* update bitmap and remove it if empty */
803 __clear_bit(offset, bitmap->bitmap);
804 if (--bitmap->nr_busy == 0) {
805 __set_bit(n, &p->bitmap); /* to please idr_remove() */
806 idr_remove(&ida->idr, idr_id);
807 free_bitmap(ida, bitmap);
814 "ida_remove called for id=%d which is not allocated.\n", id);
816 EXPORT_SYMBOL(ida_remove);
819 * ida_destroy - release all cached layers within an ida tree
822 void ida_destroy(struct ida *ida)
824 idr_destroy(&ida->idr);
825 kfree(ida->free_bitmap);
827 EXPORT_SYMBOL(ida_destroy);
830 * ida_init - initialize ida handle
833 * This function is use to set up the handle (@ida) that you will pass
834 * to the rest of the functions.
836 void ida_init(struct ida *ida)
838 memset(ida, 0, sizeof(struct ida));
842 EXPORT_SYMBOL(ida_init);