blob: de19030a999bada012fc63c2ed53a1b3807eb245 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
5 *
6 * Modified by George Anzinger to reuse immediately and to use
7 * find bit instructions. Also removed _irq on spinlocks.
8 *
Jesper Juhle15ae2d2005-10-30 15:02:14 -08009 * Small id to pointer translation service.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
Jesper Juhle15ae2d2005-10-30 15:02:14 -080011 * It uses a radix tree like structure as a sparse array indexed
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * by the id to obtain the pointer. The bitmap makes allocating
Jesper Juhle15ae2d2005-10-30 15:02:14 -080013 * a new id quick.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
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.
19
Jesper Juhle15ae2d2005-10-30 15:02:14 -080020 * You can release ids at any time. When all ids are released, most of
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * the memory is returned (we keep IDR_FREE_MAX) in a local pool so we
Jesper Juhle15ae2d2005-10-30 15:02:14 -080022 * don't need to go to the memory "store" during an id allocate, just
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * so you don't need to be too concerned about locking and conflicts
24 * with the slab allocator.
25 */
26
27#ifndef TEST // to test in user space...
28#include <linux/slab.h>
29#include <linux/init.h>
30#include <linux/module.h>
31#endif
32#include <linux/string.h>
33#include <linux/idr.h>
34
35static kmem_cache_t *idr_layer_cache;
36
37static struct idr_layer *alloc_layer(struct idr *idp)
38{
39 struct idr_layer *p;
40
41 spin_lock(&idp->lock);
42 if ((p = idp->id_free)) {
43 idp->id_free = p->ary[0];
44 idp->id_free_cnt--;
45 p->ary[0] = NULL;
46 }
47 spin_unlock(&idp->lock);
48 return(p);
49}
50
Sonny Rao1eec0052006-06-25 05:49:34 -070051/* only called when idp->lock is held */
52static void __free_layer(struct idr *idp, struct idr_layer *p)
53{
54 p->ary[0] = idp->id_free;
55 idp->id_free = p;
56 idp->id_free_cnt++;
57}
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void free_layer(struct idr *idp, struct idr_layer *p)
60{
61 /*
62 * Depends on the return element being zeroed.
63 */
64 spin_lock(&idp->lock);
Sonny Rao1eec0052006-06-25 05:49:34 -070065 __free_layer(idp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 spin_unlock(&idp->lock);
67}
68
69/**
70 * idr_pre_get - reserver resources for idr allocation
71 * @idp: idr handle
72 * @gfp_mask: memory allocation flags
73 *
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.
77 *
78 * If the system is REALLY out of memory this function returns 0,
79 * otherwise 1.
80 */
Al Virofd4f2df2005-10-21 03:18:50 -040081int idr_pre_get(struct idr *idp, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 while (idp->id_free_cnt < IDR_FREE_MAX) {
84 struct idr_layer *new;
85 new = kmem_cache_alloc(idr_layer_cache, gfp_mask);
Jesper Juhle15ae2d2005-10-30 15:02:14 -080086 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return (0);
88 free_layer(idp, new);
89 }
90 return 1;
91}
92EXPORT_SYMBOL(idr_pre_get);
93
94static int sub_alloc(struct idr *idp, void *ptr, int *starting_id)
95{
96 int n, m, sh;
97 struct idr_layer *p, *new;
98 struct idr_layer *pa[MAX_LEVEL];
99 int l, id;
100 long bm;
101
102 id = *starting_id;
103 p = idp->top;
104 l = idp->layers;
105 pa[l--] = NULL;
106 while (1) {
107 /*
108 * We run around this while until we reach the leaf node...
109 */
110 n = (id >> (IDR_BITS*l)) & IDR_MASK;
111 bm = ~p->bitmap;
112 m = find_next_bit(&bm, IDR_SIZE, n);
113 if (m == IDR_SIZE) {
114 /* no space available go back to previous layer. */
115 l++;
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800116 id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (!(p = pa[l])) {
118 *starting_id = id;
119 return -2;
120 }
121 continue;
122 }
123 if (m != n) {
124 sh = IDR_BITS*l;
125 id = ((id >> sh) ^ n ^ m) << sh;
126 }
127 if ((id >= MAX_ID_BIT) || (id < 0))
128 return -3;
129 if (l == 0)
130 break;
131 /*
132 * Create the layer below if it is missing.
133 */
134 if (!p->ary[m]) {
135 if (!(new = alloc_layer(idp)))
136 return -1;
137 p->ary[m] = new;
138 p->count++;
139 }
140 pa[l--] = p;
141 p = p->ary[m];
142 }
143 /*
144 * We have reached the leaf node, plant the
145 * users pointer and return the raw id.
146 */
147 p->ary[m] = (struct idr_layer *)ptr;
148 __set_bit(m, &p->bitmap);
149 p->count++;
150 /*
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
154 * up the radix tree.
155 */
156 n = id;
157 while (p->bitmap == IDR_FULL) {
158 if (!(p = pa[++l]))
159 break;
160 n = n >> IDR_BITS;
161 __set_bit((n & IDR_MASK), &p->bitmap);
162 }
163 return(id);
164}
165
166static int idr_get_new_above_int(struct idr *idp, void *ptr, int starting_id)
167{
168 struct idr_layer *p, *new;
169 int layers, v, id;
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 id = starting_id;
172build_up:
173 p = idp->top;
174 layers = idp->layers;
175 if (unlikely(!p)) {
176 if (!(p = alloc_layer(idp)))
177 return -1;
178 layers = 1;
179 }
180 /*
181 * Add a new layer to the top of the tree if the requested
182 * id is larger than the currently allocated space.
183 */
Zaur Kambarov589777e2005-06-21 17:14:31 -0700184 while ((layers < (MAX_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 layers++;
186 if (!p->count)
187 continue;
188 if (!(new = alloc_layer(idp))) {
189 /*
190 * The allocation failed. If we built part of
191 * the structure tear it down.
192 */
Sonny Rao1eec0052006-06-25 05:49:34 -0700193 spin_lock(&idp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 for (new = p; p && p != idp->top; new = p) {
195 p = p->ary[0];
196 new->ary[0] = NULL;
197 new->bitmap = new->count = 0;
Sonny Rao1eec0052006-06-25 05:49:34 -0700198 __free_layer(idp, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Sonny Rao1eec0052006-06-25 05:49:34 -0700200 spin_unlock(&idp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return -1;
202 }
203 new->ary[0] = p;
204 new->count = 1;
205 if (p->bitmap == IDR_FULL)
206 __set_bit(0, &new->bitmap);
207 p = new;
208 }
209 idp->top = p;
210 idp->layers = layers;
211 v = sub_alloc(idp, ptr, &id);
212 if (v == -2)
213 goto build_up;
214 return(v);
215}
216
217/**
John McCutchan7c657f22005-08-26 14:02:04 -0400218 * idr_get_new_above - allocate new idr entry above or equal to a start id
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * @idp: idr handle
220 * @ptr: pointer you want associated with the ide
221 * @start_id: id to start search at
222 * @id: pointer to the allocated handle
223 *
224 * This is the allocate id function. It should be called with any
225 * required locks.
226 *
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
229 * return -ENOSPC.
230 *
231 * @id returns a value in the range 0 ... 0x7fffffff
232 */
233int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
234{
235 int rv;
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 rv = idr_get_new_above_int(idp, ptr, starting_id);
238 /*
239 * This is a cheap hack until the IDR code can be fixed to
240 * return proper error values.
241 */
242 if (rv < 0) {
243 if (rv == -1)
244 return -EAGAIN;
245 else /* Will be -3 */
246 return -ENOSPC;
247 }
248 *id = rv;
249 return 0;
250}
251EXPORT_SYMBOL(idr_get_new_above);
252
253/**
254 * idr_get_new - allocate new idr entry
255 * @idp: idr handle
256 * @ptr: pointer you want associated with the ide
257 * @id: pointer to the allocated handle
258 *
259 * This is the allocate id function. It should be called with any
260 * required locks.
261 *
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
264 * return -ENOSPC.
265 *
266 * @id returns a value in the range 0 ... 0x7fffffff
267 */
268int idr_get_new(struct idr *idp, void *ptr, int *id)
269{
270 int rv;
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 rv = idr_get_new_above_int(idp, ptr, 0);
273 /*
274 * This is a cheap hack until the IDR code can be fixed to
275 * return proper error values.
276 */
277 if (rv < 0) {
278 if (rv == -1)
279 return -EAGAIN;
280 else /* Will be -3 */
281 return -ENOSPC;
282 }
283 *id = rv;
284 return 0;
285}
286EXPORT_SYMBOL(idr_get_new);
287
288static void idr_remove_warning(int id)
289{
290 printk("idr_remove called for id=%d which is not allocated.\n", id);
291 dump_stack();
292}
293
294static void sub_remove(struct idr *idp, int shift, int id)
295{
296 struct idr_layer *p = idp->top;
297 struct idr_layer **pa[MAX_LEVEL];
298 struct idr_layer ***paa = &pa[0];
299 int n;
300
301 *paa = NULL;
302 *++paa = &idp->top;
303
304 while ((shift > 0) && p) {
305 n = (id >> shift) & IDR_MASK;
306 __clear_bit(n, &p->bitmap);
307 *++paa = &p->ary[n];
308 p = p->ary[n];
309 shift -= IDR_BITS;
310 }
311 n = id & IDR_MASK;
312 if (likely(p != NULL && test_bit(n, &p->bitmap))){
313 __clear_bit(n, &p->bitmap);
314 p->ary[n] = NULL;
315 while(*paa && ! --((**paa)->count)){
316 free_layer(idp, **paa);
317 **paa-- = NULL;
318 }
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800319 if (!*paa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 idp->layers = 0;
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800321 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 idr_remove_warning(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/**
326 * idr_remove - remove the given id and free it's slot
327 * idp: idr handle
328 * id: uniqueue key
329 */
330void idr_remove(struct idr *idp, int id)
331{
332 struct idr_layer *p;
333
334 /* Mask off upper bits we don't use for the search. */
335 id &= MAX_ID_MASK;
336
337 sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800338 if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
339 idp->top->ary[0]) { // We can drop a layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 p = idp->top->ary[0];
342 idp->top->bitmap = idp->top->count = 0;
343 free_layer(idp, idp->top);
344 idp->top = p;
345 --idp->layers;
346 }
347 while (idp->id_free_cnt >= IDR_FREE_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 p = alloc_layer(idp);
349 kmem_cache_free(idr_layer_cache, p);
350 return;
351 }
352}
353EXPORT_SYMBOL(idr_remove);
354
355/**
Andrew Morton8d3b3592005-10-23 12:57:18 -0700356 * idr_destroy - release all cached layers within an idr tree
357 * idp: idr handle
358 */
359void idr_destroy(struct idr *idp)
360{
361 while (idp->id_free_cnt) {
362 struct idr_layer *p = alloc_layer(idp);
363 kmem_cache_free(idr_layer_cache, p);
364 }
365}
366EXPORT_SYMBOL(idr_destroy);
367
368/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * idr_find - return pointer for given id
370 * @idp: idr handle
371 * @id: lookup key
372 *
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
375 * idr_get_new().
376 *
377 * The caller must serialize idr_find() vs idr_get_new() and idr_remove().
378 */
379void *idr_find(struct idr *idp, int id)
380{
381 int n;
382 struct idr_layer *p;
383
384 n = idp->layers * IDR_BITS;
385 p = idp->top;
386
387 /* Mask off upper bits we don't use for the search. */
388 id &= MAX_ID_MASK;
389
390 if (id >= (1 << n))
391 return NULL;
392
393 while (n > 0 && p) {
394 n -= IDR_BITS;
395 p = p->ary[(id >> n) & IDR_MASK];
396 }
397 return((void *)p);
398}
399EXPORT_SYMBOL(idr_find);
400
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800401static void idr_cache_ctor(void * idr_layer, kmem_cache_t *idr_layer_cache,
402 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 memset(idr_layer, 0, sizeof(struct idr_layer));
405}
406
407static int init_id_cache(void)
408{
409 if (!idr_layer_cache)
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800410 idr_layer_cache = kmem_cache_create("idr_layer_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 sizeof(struct idr_layer), 0, 0, idr_cache_ctor, NULL);
412 return 0;
413}
414
415/**
416 * idr_init - initialize idr handle
417 * @idp: idr handle
418 *
419 * This function is use to set up the handle (@idp) that you will pass
420 * to the rest of the functions.
421 */
422void idr_init(struct idr *idp)
423{
424 init_id_cache();
425 memset(idp, 0, sizeof(struct idr));
426 spin_lock_init(&idp->lock);
427}
428EXPORT_SYMBOL(idr_init);