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 kmem_cache_t
*idr_layer_cache
;
38 static struct idr_layer
*alloc_layer(struct idr
*idp
)
42 spin_lock(&idp
->lock
);
43 if ((p
= idp
->id_free
)) {
44 idp
->id_free
= p
->ary
[0];
48 spin_unlock(&idp
->lock
);
52 /* only called when idp->lock is held */
53 static void __free_layer(struct idr
*idp
, struct idr_layer
*p
)
55 p
->ary
[0] = idp
->id_free
;
60 static void free_layer(struct idr
*idp
, struct idr_layer
*p
)
63 * Depends on the return element being zeroed.
65 spin_lock(&idp
->lock
);
67 spin_unlock(&idp
->lock
);
71 * idr_pre_get - reserver resources for idr allocation
73 * @gfp_mask: memory allocation flags
75 * This function should be called prior to locking and calling the
76 * following function. It preallocates enough memory to satisfy
77 * the worst possible allocation.
79 * If the system is REALLY out of memory this function returns 0,
82 int idr_pre_get(struct idr
*idp
, gfp_t gfp_mask
)
84 while (idp
->id_free_cnt
< IDR_FREE_MAX
) {
85 struct idr_layer
*new;
86 new = kmem_cache_alloc(idr_layer_cache
, gfp_mask
);
93 EXPORT_SYMBOL(idr_pre_get
);
95 static int sub_alloc(struct idr
*idp
, void *ptr
, int *starting_id
)
98 struct idr_layer
*p
, *new;
99 struct idr_layer
*pa
[MAX_LEVEL
];
109 * We run around this while until we reach the leaf node...
111 n
= (id
>> (IDR_BITS
*l
)) & IDR_MASK
;
113 m
= find_next_bit(&bm
, IDR_SIZE
, n
);
115 /* no space available go back to previous layer. */
117 id
= (id
| ((1 << (IDR_BITS
* l
)) - 1)) + 1;
126 id
= ((id
>> sh
) ^ n
^ m
) << sh
;
128 if ((id
>= MAX_ID_BIT
) || (id
< 0))
133 * Create the layer below if it is missing.
136 if (!(new = alloc_layer(idp
)))
145 * We have reached the leaf node, plant the
146 * users pointer and return the raw id.
148 p
->ary
[m
] = (struct idr_layer
*)ptr
;
149 __set_bit(m
, &p
->bitmap
);
152 * If this layer is full mark the bit in the layer above
153 * to show that this part of the radix tree is full.
154 * This may complete the layer above and require walking
158 while (p
->bitmap
== IDR_FULL
) {
162 __set_bit((n
& IDR_MASK
), &p
->bitmap
);
167 static int idr_get_new_above_int(struct idr
*idp
, void *ptr
, int starting_id
)
169 struct idr_layer
*p
, *new;
175 layers
= idp
->layers
;
177 if (!(p
= alloc_layer(idp
)))
182 * Add a new layer to the top of the tree if the requested
183 * id is larger than the currently allocated space.
185 while ((layers
< (MAX_LEVEL
- 1)) && (id
>= (1 << (layers
*IDR_BITS
)))) {
189 if (!(new = alloc_layer(idp
))) {
191 * The allocation failed. If we built part of
192 * the structure tear it down.
194 spin_lock(&idp
->lock
);
195 for (new = p
; p
&& p
!= idp
->top
; new = p
) {
198 new->bitmap
= new->count
= 0;
199 __free_layer(idp
, new);
201 spin_unlock(&idp
->lock
);
206 if (p
->bitmap
== IDR_FULL
)
207 __set_bit(0, &new->bitmap
);
211 idp
->layers
= layers
;
212 v
= sub_alloc(idp
, ptr
, &id
);
219 * idr_get_new_above - allocate new idr entry above or equal to a start id
221 * @ptr: pointer you want associated with the ide
222 * @start_id: id to start search at
223 * @id: pointer to the allocated handle
225 * This is the allocate id function. It should be called with any
228 * If memory is required, it will return -EAGAIN, you should unlock
229 * and go back to the idr_pre_get() call. If the idr is full, it will
232 * @id returns a value in the range 0 ... 0x7fffffff
234 int idr_get_new_above(struct idr
*idp
, void *ptr
, int starting_id
, int *id
)
238 rv
= idr_get_new_above_int(idp
, ptr
, starting_id
);
240 * This is a cheap hack until the IDR code can be fixed to
241 * return proper error values.
246 else /* Will be -3 */
252 EXPORT_SYMBOL(idr_get_new_above
);
255 * idr_get_new - allocate new idr entry
257 * @ptr: pointer you want associated with the ide
258 * @id: pointer to the allocated handle
260 * This is the allocate id function. It should be called with any
263 * If memory is required, it will return -EAGAIN, you should unlock
264 * and go back to the idr_pre_get() call. If the idr is full, it will
267 * @id returns a value in the range 0 ... 0x7fffffff
269 int idr_get_new(struct idr
*idp
, void *ptr
, int *id
)
273 rv
= idr_get_new_above_int(idp
, ptr
, 0);
275 * This is a cheap hack until the IDR code can be fixed to
276 * return proper error values.
281 else /* Will be -3 */
287 EXPORT_SYMBOL(idr_get_new
);
289 static void idr_remove_warning(int id
)
291 printk("idr_remove called for id=%d which is not allocated.\n", id
);
295 static void sub_remove(struct idr
*idp
, int shift
, int id
)
297 struct idr_layer
*p
= idp
->top
;
298 struct idr_layer
**pa
[MAX_LEVEL
];
299 struct idr_layer
***paa
= &pa
[0];
305 while ((shift
> 0) && p
) {
306 n
= (id
>> shift
) & IDR_MASK
;
307 __clear_bit(n
, &p
->bitmap
);
313 if (likely(p
!= NULL
&& test_bit(n
, &p
->bitmap
))){
314 __clear_bit(n
, &p
->bitmap
);
316 while(*paa
&& ! --((**paa
)->count
)){
317 free_layer(idp
, **paa
);
323 idr_remove_warning(id
);
327 * idr_remove - remove the given id and free it's slot
331 void idr_remove(struct idr
*idp
, int id
)
335 /* Mask off upper bits we don't use for the search. */
338 sub_remove(idp
, (idp
->layers
- 1) * IDR_BITS
, id
);
339 if (idp
->top
&& idp
->top
->count
== 1 && (idp
->layers
> 1) &&
340 idp
->top
->ary
[0]) { // We can drop a layer
342 p
= idp
->top
->ary
[0];
343 idp
->top
->bitmap
= idp
->top
->count
= 0;
344 free_layer(idp
, idp
->top
);
348 while (idp
->id_free_cnt
>= IDR_FREE_MAX
) {
349 p
= alloc_layer(idp
);
350 kmem_cache_free(idr_layer_cache
, p
);
354 EXPORT_SYMBOL(idr_remove
);
357 * idr_destroy - release all cached layers within an idr tree
360 void idr_destroy(struct idr
*idp
)
362 while (idp
->id_free_cnt
) {
363 struct idr_layer
*p
= alloc_layer(idp
);
364 kmem_cache_free(idr_layer_cache
, p
);
367 EXPORT_SYMBOL(idr_destroy
);
370 * idr_find - return pointer for given id
374 * Return the pointer given the id it has been registered with. A %NULL
375 * return indicates that @id is not valid or you passed %NULL in
378 * The caller must serialize idr_find() vs idr_get_new() and idr_remove().
380 void *idr_find(struct idr
*idp
, int id
)
385 n
= idp
->layers
* IDR_BITS
;
388 /* Mask off upper bits we don't use for the search. */
396 p
= p
->ary
[(id
>> n
) & IDR_MASK
];
400 EXPORT_SYMBOL(idr_find
);
403 * idr_replace - replace pointer for given id
405 * @ptr: pointer you want associated with the id
408 * Replace the pointer registered with an id and return the old value.
409 * A -ENOENT return indicates that @id was not found.
410 * A -EINVAL return indicates that @id was not within valid constraints.
412 * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove().
414 void *idr_replace(struct idr
*idp
, void *ptr
, int id
)
417 struct idr_layer
*p
, *old_p
;
419 n
= idp
->layers
* IDR_BITS
;
425 return ERR_PTR(-EINVAL
);
428 while ((n
> 0) && p
) {
429 p
= p
->ary
[(id
>> n
) & IDR_MASK
];
434 if (unlikely(p
== NULL
|| !test_bit(n
, &p
->bitmap
)))
435 return ERR_PTR(-ENOENT
);
442 EXPORT_SYMBOL(idr_replace
);
444 static void idr_cache_ctor(void * idr_layer
, kmem_cache_t
*idr_layer_cache
,
447 memset(idr_layer
, 0, sizeof(struct idr_layer
));
450 static int init_id_cache(void)
452 if (!idr_layer_cache
)
453 idr_layer_cache
= kmem_cache_create("idr_layer_cache",
454 sizeof(struct idr_layer
), 0, 0, idr_cache_ctor
, NULL
);
459 * idr_init - initialize idr handle
462 * This function is use to set up the handle (@idp) that you will pass
463 * to the rest of the functions.
465 void idr_init(struct idr
*idp
)
468 memset(idp
, 0, sizeof(struct idr
));
469 spin_lock_init(&idp
->lock
);
471 EXPORT_SYMBOL(idr_init
);