29 #ifndef TEST // to test in user space...
30 #include <linux/slab.h>
32 #include <linux/export.h>
35 #include <linux/string.h>
42 static struct idr_layer *get_from_free_list(
struct idr *idp)
53 spin_unlock_irqrestore(&idp->
lock, flags);
65 static inline void free_layer(
struct idr_layer *p)
71 static void __move_to_free_list(
struct idr *idp,
struct idr_layer *p)
78 static void move_to_free_list(
struct idr *idp,
struct idr_layer *p)
86 __move_to_free_list(idp, p);
87 spin_unlock_irqrestore(&idp->
lock, flags);
90 static void idr_mark_full(
struct idr_layer **pa,
int id)
102 while (p->
bitmap == IDR_FULL) {
127 new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
130 move_to_free_list(idp,
new);
136 static int sub_alloc(
struct idr *idp,
int *starting_id,
struct idr_layer **pa)
159 id = (
id | ((1 << (IDR_BITS *
l)) - 1)) + 1;
162 if (
id >= 1 << (idp->
layers * IDR_BITS)) {
172 sh = IDR_BITS * (l + 1);
173 if (oid >> sh ==
id >> sh)
180 id = ((
id >> sh) ^ n ^ m) << sh;
190 new = get_from_free_list(idp);
205 static int idr_get_empty_slot(
struct idr *idp,
int starting_id,
217 if (!(p = get_from_free_list(idp)))
226 while ((layers < (
MAX_IDR_LEVEL - 1)) && (
id >= (1 << (layers*IDR_BITS)))) {
236 if (!(
new = get_from_free_list(idp))) {
242 for (
new = p; p && p != idp->
top;
new =
p) {
245 new->bitmap =
new->count = 0;
246 __move_to_free_list(idp,
new);
248 spin_unlock_irqrestore(&idp->
lock, flags);
253 new->layer = layers-1;
254 if (p->
bitmap == IDR_FULL)
260 v = sub_alloc(idp, &
id, pa);
266 static int idr_get_new_above_int(
struct idr *idp,
void *
ptr,
int starting_id)
271 id = idr_get_empty_slot(idp, starting_id, pa);
280 idr_mark_full(pa,
id);
308 rv = idr_get_new_above_int(idp, ptr, starting_id);
338 rv = idr_get_new_above_int(idp, ptr, 0);
350 static void idr_remove_warning(
int id)
353 "idr_remove called for id=%d which is not allocated.\n",
id);
357 static void sub_remove(
struct idr *idp,
int shift,
int id)
368 while ((shift > 0) && p) {
380 while(*paa && ! --((**paa)->count)){
391 idr_remove_warning(
id);
407 sub_remove(idp, (idp->
layers - 1) * IDR_BITS,
id);
408 if (idp->
top && idp->
top->count == 1 && (idp->
layers > 1) &&
417 p = idp->
top->ary[0];
424 p = get_from_free_list(idp);
457 n = idp->
layers * IDR_BITS;
464 while (n > IDR_BITS && p) {
467 p = p->
ary[(
id >>
n) & IDR_MASK];
473 while (n < fls(
id ^ bt_mask)) {
491 struct idr_layer *p = get_from_free_list(idp);
517 n = (p->
layer+1) * IDR_BITS;
554 int (*
fn)(
int id,
void *p,
void *
data),
void *data)
561 n = idp->
layers * IDR_BITS;
574 error =
fn(
id, (
void *)p, data);
580 while (n < fls(
id)) {
613 n = (p->
layer + 1) * IDR_BITS;
629 while (n < fls(
id)) {
660 n = (p->
layer+1) * IDR_BITS;
668 while ((n > 0) &&
p) {
669 p = p->
ary[(
id >>
n) & IDR_MASK];
727 spin_unlock_irqrestore(&ida->
idr.lock, flags);
759 free_bitmap(ida, bitmap);
792 t = idr_get_empty_slot(&ida->
idr, idr_id, pa);
804 bitmap = (
void *)pa[0]->ary[idr_id & IDR_MASK];
809 spin_unlock_irqrestore(&ida->
idr.lock, flags);
835 idr_mark_full(pa, idr_id);
881 int shift = (ida->
idr.layers - 1) * IDR_BITS;
888 while ((shift > 0) &&
p) {
889 n = (idr_id >> shift) & IDR_MASK;
901 bitmap = (
void *)p->
ary[n];
910 free_bitmap(ida, bitmap);
917 "ida_remove called for id=%d which is not allocated.\n",
id);
975 spin_unlock_irqrestore(&simple_ida_lock, flags);
996 spin_unlock_irqrestore(&simple_ida_lock, flags);
1009 memset(ida, 0,
sizeof(
struct ida));