2 Unix SMB/CIFS implementation.
4 very efficient functions to manage mapping a id (such as a fnum) to
5 a pointer. This is used for fnum and search id allocation.
7 Copyright (C) Andrew Tridgell 2004
9 This code is derived from lib/idr.c in the 2.6 Linux kernel, which was
10 written by Jim Houston jim.houston@ccur.com, and is
11 Copyright (C) 2002 by Concurrent Computer Corporation
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 see the section marked "public interface" below for documentation
41 #define IDR_FULL 0xfffffffful
43 #define TOP_LEVEL_FULL (IDR_FULL >> 30)
45 #define IDR_SIZE (1 << IDR_BITS)
46 #define IDR_MASK ((1 << IDR_BITS)-1)
47 #define MAX_ID_SHIFT (sizeof(int)*8 - 1)
48 #define MAX_ID_BIT (1U << MAX_ID_SHIFT)
49 #define MAX_ID_MASK (MAX_ID_BIT - 1)
50 #define MAX_LEVEL (MAX_ID_SHIFT + IDR_BITS - 1) / IDR_BITS
51 #define IDR_FREE_MAX MAX_LEVEL + MAX_LEVEL
53 #define set_bit(bit, v) (v) |= (1U<<(bit))
54 #define clear_bit(bit, v) (v) &= ~(1U<<(bit))
55 #define test_bit(bit, v) ((v) & (1U<<(bit)))
59 struct idr_layer *ary[IDR_SIZE];
64 struct idr_layer *top;
65 struct idr_layer *id_free;
70 static struct idr_layer *alloc_layer(struct idr_context *idp)
74 if (!(p = idp->id_free))
76 idp->id_free = p->ary[0];
82 static int find_next_bit(uint32_t bm, int maxid, int n)
84 while (n<maxid && !test_bit(n, bm)) n++;
88 static void free_layer(struct idr_context *idp, struct idr_layer *p)
90 p->ary[0] = idp->id_free;
95 static int idr_pre_get(struct idr_context *idp)
97 while (idp->id_free_cnt < IDR_FREE_MAX) {
98 struct idr_layer *pn = talloc_zero(idp, struct idr_layer);
106 static int sub_alloc(struct idr_context *idp, void *ptr, int *starting_id)
109 struct idr_layer *p, *pn;
110 struct idr_layer *pa[MAX_LEVEL+1];
111 unsigned int l, id, oid;
114 memset(pa, 0, sizeof(pa));
123 * We run around this while until we reach the leaf node...
125 n = (id >> (IDR_BITS*l)) & IDR_MASK;
127 m = find_next_bit(bm, IDR_SIZE, n);
129 /* no space available go back to previous layer. */
132 id = (id | ((1 << (IDR_BITS*l))-1)) + 1;
134 /* if already at the top layer, we need to grow */
140 /* If we need to go up one layer, continue the
141 * loop; otherwise, restart from the top.
143 sh = IDR_BITS * (l + 1);
144 if (oid >> sh == id >> sh)
151 id = ((id >> sh) ^ n ^ m) << sh;
153 if ((id >= MAX_ID_BIT) || (id < 0))
158 * Create the layer below if it is missing.
161 if (!(pn = alloc_layer(idp)))
170 * We have reached the leaf node, plant the
171 * users pointer and return the raw id.
173 p->ary[m] = (struct idr_layer *)ptr;
174 set_bit(m, p->bitmap);
177 * If this layer is full mark the bit in the layer above
178 * to show that this part of the radix tree is full.
179 * This may complete the layer above and require walking
183 while (p->bitmap == IDR_FULL) {
184 if (l >= MAX_LEVEL) {
192 set_bit((n & IDR_MASK), p->bitmap);
197 static int idr_get_new_above_int(struct idr_context *idp, void *ptr, int starting_id)
199 struct idr_layer *p, *pn;
207 layers = idp->layers;
209 if (!(p = alloc_layer(idp)))
214 * Add a new layer to the top of the tree if the requested
215 * id is larger than the currently allocated space.
217 while ((layers < MAX_LEVEL) && (id >= (1 << (layers*IDR_BITS)))) {
221 if (!(pn = alloc_layer(idp))) {
223 * The allocation failed. If we built part of
224 * the structure tear it down.
226 for (pn = p; p && p != idp->top; pn = p) {
229 pn->bitmap = pn->count = 0;
236 if (p->bitmap == IDR_FULL)
237 set_bit(0, pn->bitmap);
241 idp->layers = layers;
242 v = sub_alloc(idp, ptr, &id);
248 static int sub_remove(struct idr_context *idp, int shift, int id)
250 struct idr_layer *p = idp->top;
251 struct idr_layer **pa[1+MAX_LEVEL];
252 struct idr_layer ***paa = &pa[0];
258 while ((shift > 0) && p) {
259 n = (id >> shift) & IDR_MASK;
260 clear_bit(n, p->bitmap);
266 if (p != NULL && test_bit(n, p->bitmap)) {
267 clear_bit(n, p->bitmap);
269 while(*paa && ! --((**paa)->count)){
270 free_layer(idp, **paa);
280 static void *_idr_find(struct idr_context *idp, int id)
285 n = idp->layers * IDR_BITS;
288 * This tests to see if bits outside the current tree are
289 * present. If so, tain't one of ours!
291 if (n + IDR_BITS < 31 &&
292 ((id & ~(~0U << MAX_ID_SHIFT)) >> (n + IDR_BITS))) {
296 /* Mask off upper bits we don't use for the search. */
299 while (n >= IDR_BITS && p) {
301 p = p->ary[(id >> n) & IDR_MASK];
306 static int _idr_remove(struct idr_context *idp, int id)
310 /* Mask off upper bits we don't use for the search. */
313 if (sub_remove(idp, (idp->layers - 1) * IDR_BITS, id) == -1) {
317 if ( idp->top && idp->top->count == 1 &&
320 /* We can drop a layer */
321 p = idp->top->ary[0];
322 idp->top->bitmap = idp->top->count = 0;
323 free_layer(idp, idp->top);
327 while (idp->id_free_cnt >= IDR_FREE_MAX) {
328 p = alloc_layer(idp);
334 /************************************************************************
335 this is the public interface
336 **************************************************************************/
339 initialise a idr tree. The context return value must be passed to
340 all subsequent idr calls. To destroy the idr tree use talloc_free()
343 _PUBLIC_ struct idr_context *idr_init(TALLOC_CTX *mem_ctx)
345 return talloc_zero(mem_ctx, struct idr_context);
349 allocate the next available id, and assign 'ptr' into its slot.
350 you can retrieve later this pointer using idr_find()
352 _PUBLIC_ int idr_get_new(struct idr_context *idp, void *ptr, int limit)
354 int ret = idr_get_new_above_int(idp, ptr, 0);
356 idr_remove(idp, ret);
363 allocate a new id, giving the first available value greater than or
364 equal to the given starting id
366 _PUBLIC_ int idr_get_new_above(struct idr_context *idp, void *ptr, int starting_id, int limit)
368 int ret = idr_get_new_above_int(idp, ptr, starting_id);
370 idr_remove(idp, ret);
377 find a pointer value previously set with idr_get_new given an id
379 _PUBLIC_ void *idr_find(struct idr_context *idp, int id)
381 return _idr_find(idp, id);
385 remove an id from the idr tree
387 _PUBLIC_ int idr_remove(struct idr_context *idp, int id)
390 ret = _idr_remove((struct idr_context *)idp, id);
392 DEBUG(0,("WARNING: attempt to remove unset id %d in idtree\n", id));