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 spin_unlock(&idp
->lock
);
46 idp
->id_free
= p
->ary
[0];
49 spin_unlock(&idp
->lock
);
53 static void free_layer(struct idr
*idp
, struct idr_layer
*p
)
56 * Depends on the return element being zeroed.
58 spin_lock(&idp
->lock
);
59 p
->ary
[0] = idp
->id_free
;
62 spin_unlock(&idp
->lock
);
66 * idr_pre_get - reserver resources for idr allocation
68 * @gfp_mask: memory allocation flags
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.
74 * If the system is REALLY out of memory this function returns 0,
77 int idr_pre_get(struct idr
*idp
, unsigned gfp_mask
)
79 while (idp
->id_free_cnt
< IDR_FREE_MAX
) {
80 struct idr_layer
*new;
81 new = kmem_cache_alloc(idr_layer_cache
, gfp_mask
);
88 EXPORT_SYMBOL(idr_pre_get
);
90 static int sub_alloc(struct idr
*idp
, void *ptr
, int *starting_id
)
93 struct idr_layer
*p
, *new;
94 struct idr_layer
*pa
[MAX_LEVEL
];
104 * We run around this while until we reach the leaf node...
106 n
= (id
>> (IDR_BITS
*l
)) & IDR_MASK
;
108 m
= find_next_bit(&bm
, IDR_SIZE
, n
);
110 /* no space available go back to previous layer. */
112 id
= (id
| ((1 << (IDR_BITS
*l
))-1)) + 1;
121 id
= ((id
>> sh
) ^ n
^ m
) << sh
;
123 if ((id
>= MAX_ID_BIT
) || (id
< 0))
128 * Create the layer below if it is missing.
131 if (!(new = alloc_layer(idp
)))
140 * We have reached the leaf node, plant the
141 * users pointer and return the raw id.
143 p
->ary
[m
] = (struct idr_layer
*)ptr
;
144 __set_bit(m
, &p
->bitmap
);
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
153 while (p
->bitmap
== IDR_FULL
) {
157 __set_bit((n
& IDR_MASK
), &p
->bitmap
);
162 static int idr_get_new_above_int(struct idr
*idp
, void *ptr
, int starting_id
)
164 struct idr_layer
*p
, *new;
170 layers
= idp
->layers
;
172 if (!(p
= alloc_layer(idp
)))
177 * Add a new layer to the top of the tree if the requested
178 * id is larger than the currently allocated space.
180 while ((layers
< MAX_LEVEL
) && (id
>= (1 << (layers
*IDR_BITS
)))) {
184 if (!(new = alloc_layer(idp
))) {
186 * The allocation failed. If we built part of
187 * the structure tear it down.
189 for (new = p
; p
&& p
!= idp
->top
; new = p
) {
192 new->bitmap
= new->count
= 0;
193 free_layer(idp
, new);
199 if (p
->bitmap
== IDR_FULL
)
200 __set_bit(0, &new->bitmap
);
204 idp
->layers
= layers
;
205 v
= sub_alloc(idp
, ptr
, &id
);
212 * idr_get_new_above - allocate new idr entry above a start id
214 * @ptr: pointer you want associated with the ide
215 * @start_id: id to start search at
216 * @id: pointer to the allocated handle
218 * This is the allocate id function. It should be called with any
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
225 * @id returns a value in the range 0 ... 0x7fffffff
227 int idr_get_new_above(struct idr
*idp
, void *ptr
, int starting_id
, int *id
)
230 rv
= idr_get_new_above_int(idp
, ptr
, starting_id
);
232 * This is a cheap hack until the IDR code can be fixed to
233 * return proper error values.
238 else /* Will be -3 */
244 EXPORT_SYMBOL(idr_get_new_above
);
247 * idr_get_new - allocate new idr entry
249 * @ptr: pointer you want associated with the ide
250 * @id: pointer to the allocated handle
252 * This is the allocate id function. It should be called with any
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
259 * @id returns a value in the range 0 ... 0x7fffffff
261 int idr_get_new(struct idr
*idp
, void *ptr
, int *id
)
264 rv
= idr_get_new_above_int(idp
, ptr
, 0);
266 * This is a cheap hack until the IDR code can be fixed to
267 * return proper error values.
272 else /* Will be -3 */
278 EXPORT_SYMBOL(idr_get_new
);
280 static void sub_remove(struct idr
*idp
, int shift
, int id
)
282 struct idr_layer
*p
= idp
->top
;
283 struct idr_layer
**pa
[MAX_LEVEL
];
284 struct idr_layer
***paa
= &pa
[0];
289 while ((shift
> 0) && p
) {
290 int n
= (id
>> shift
) & IDR_MASK
;
291 __clear_bit(n
, &p
->bitmap
);
296 if (likely(p
!= NULL
)){
297 int n
= id
& IDR_MASK
;
298 __clear_bit(n
, &p
->bitmap
);
300 while(*paa
&& ! --((**paa
)->count
)){
301 free_layer(idp
, **paa
);
310 * idr_remove - remove the given id and free it's slot
314 void idr_remove(struct idr
*idp
, int id
)
318 /* Mask off upper bits we don't use for the search. */
321 sub_remove(idp
, (idp
->layers
- 1) * IDR_BITS
, id
);
322 if ( idp
->top
&& idp
->top
->count
== 1 &&
324 idp
->top
->ary
[0]){ // We can drop a layer
326 p
= idp
->top
->ary
[0];
327 idp
->top
->bitmap
= idp
->top
->count
= 0;
328 free_layer(idp
, idp
->top
);
332 while (idp
->id_free_cnt
>= IDR_FREE_MAX
) {
334 p
= alloc_layer(idp
);
335 kmem_cache_free(idr_layer_cache
, p
);
339 EXPORT_SYMBOL(idr_remove
);
342 * idr_find - return pointer for given id
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
350 * The caller must serialize idr_find() vs idr_get_new() and idr_remove().
352 void *idr_find(struct idr
*idp
, int id
)
357 n
= idp
->layers
* IDR_BITS
;
360 /* Mask off upper bits we don't use for the search. */
368 p
= p
->ary
[(id
>> n
) & IDR_MASK
];
372 EXPORT_SYMBOL(idr_find
);
374 static void idr_cache_ctor(void * idr_layer
,
375 kmem_cache_t
*idr_layer_cache
, unsigned long flags
)
377 memset(idr_layer
, 0, sizeof(struct idr_layer
));
380 static int init_id_cache(void)
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
);
389 * idr_init - initialize idr handle
392 * This function is use to set up the handle (@idp) that you will pass
393 * to the rest of the functions.
395 void idr_init(struct idr
*idp
)
398 memset(idp
, 0, sizeof(struct idr
));
399 spin_lock_init(&idp
->lock
);
401 EXPORT_SYMBOL(idr_init
);