backupkey: Handle more clearly the case where we find the secret, but it has no value
[Samba.git] / lib / util / idtree.c
blob0056c0961aa1857a151b2779c4fa37f0162f65dd
1 /*
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
31 /**
32 * @file
35 #include <talloc.h>
36 #include "replace.h"
37 #include "debug.h"
38 #include "idtree.h"
40 #define IDR_BITS 5
41 #define IDR_FULL 0xfffffffful
42 #if 0 /* unused */
43 #define TOP_LEVEL_FULL (IDR_FULL >> 30)
44 #endif
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) |= (1<<(bit))
54 #define clear_bit(bit, v) (v) &= ~(1<<(bit))
55 #define test_bit(bit, v) ((v) & (1<<(bit)))
57 struct idr_layer {
58 uint32_t bitmap;
59 struct idr_layer *ary[IDR_SIZE];
60 int count;
63 struct idr_context {
64 struct idr_layer *top;
65 struct idr_layer *id_free;
66 int layers;
67 int id_free_cnt;
70 static struct idr_layer *alloc_layer(struct idr_context *idp)
72 struct idr_layer *p;
74 if (!(p = idp->id_free))
75 return NULL;
76 idp->id_free = p->ary[0];
77 idp->id_free_cnt--;
78 p->ary[0] = NULL;
79 return p;
82 static int find_next_bit(uint32_t bm, int maxid, int n)
84 while (n<maxid && !test_bit(n, bm)) n++;
85 return n;
88 static void free_layer(struct idr_context *idp, struct idr_layer *p)
90 p->ary[0] = idp->id_free;
91 idp->id_free = p;
92 idp->id_free_cnt++;
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);
99 if(pn == NULL)
100 return (0);
101 free_layer(idp, pn);
103 return 1;
106 static int sub_alloc(struct idr_context *idp, void *ptr, int *starting_id)
108 int n, m, sh;
109 struct idr_layer *p, *pn;
110 struct idr_layer *pa[MAX_LEVEL+1];
111 unsigned int l, id, oid;
112 uint32_t bm;
114 memset(pa, 0, sizeof(pa));
116 id = *starting_id;
117 restart:
118 p = idp->top;
119 l = idp->layers;
120 pa[l--] = NULL;
121 while (1) {
123 * We run around this while until we reach the leaf node...
125 n = (id >> (IDR_BITS*l)) & IDR_MASK;
126 bm = ~p->bitmap;
127 m = find_next_bit(bm, IDR_SIZE, n);
128 if (m == IDR_SIZE) {
129 /* no space available go back to previous layer. */
130 l++;
131 oid = id;
132 id = (id | ((1 << (IDR_BITS*l))-1)) + 1;
134 /* if already at the top layer, we need to grow */
135 if (!(p = pa[l])) {
136 *starting_id = id;
137 return -2;
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)
145 continue;
146 else
147 goto restart;
149 if (m != n) {
150 sh = IDR_BITS*l;
151 id = ((id >> sh) ^ n ^ m) << sh;
153 if ((id >= MAX_ID_BIT) || (id < 0))
154 return -1;
155 if (l == 0)
156 break;
158 * Create the layer below if it is missing.
160 if (!p->ary[m]) {
161 if (!(pn = alloc_layer(idp)))
162 return -1;
163 p->ary[m] = pn;
164 p->count++;
166 pa[l--] = p;
167 p = p->ary[m];
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);
175 p->count++;
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
180 * up the radix tree.
182 n = id;
183 while (p->bitmap == IDR_FULL) {
184 if (!(p = pa[++l]))
185 break;
186 n = n >> IDR_BITS;
187 set_bit((n & IDR_MASK), p->bitmap);
189 return(id);
192 static int idr_get_new_above_int(struct idr_context *idp, void *ptr, int starting_id)
194 struct idr_layer *p, *pn;
195 int layers, v, id;
197 idr_pre_get(idp);
199 id = starting_id;
200 build_up:
201 p = idp->top;
202 layers = idp->layers;
203 if (!p) {
204 if (!(p = alloc_layer(idp)))
205 return -1;
206 layers = 1;
209 * Add a new layer to the top of the tree if the requested
210 * id is larger than the currently allocated space.
212 while ((layers < MAX_LEVEL) && (id >= (1 << (layers*IDR_BITS)))) {
213 layers++;
214 if (!p->count)
215 continue;
216 if (!(pn = alloc_layer(idp))) {
218 * The allocation failed. If we built part of
219 * the structure tear it down.
221 for (pn = p; p && p != idp->top; pn = p) {
222 p = p->ary[0];
223 pn->ary[0] = NULL;
224 pn->bitmap = pn->count = 0;
225 free_layer(idp, pn);
227 return -1;
229 pn->ary[0] = p;
230 pn->count = 1;
231 if (p->bitmap == IDR_FULL)
232 set_bit(0, pn->bitmap);
233 p = pn;
235 idp->top = p;
236 idp->layers = layers;
237 v = sub_alloc(idp, ptr, &id);
238 if (v == -2)
239 goto build_up;
240 return(v);
243 static int sub_remove(struct idr_context *idp, int shift, int id)
245 struct idr_layer *p = idp->top;
246 struct idr_layer **pa[1+MAX_LEVEL];
247 struct idr_layer ***paa = &pa[0];
248 int n;
250 *paa = NULL;
251 *++paa = &idp->top;
253 while ((shift > 0) && p) {
254 n = (id >> shift) & IDR_MASK;
255 clear_bit(n, p->bitmap);
256 *++paa = &p->ary[n];
257 p = p->ary[n];
258 shift -= IDR_BITS;
260 n = id & IDR_MASK;
261 if (p != NULL && test_bit(n, p->bitmap)) {
262 clear_bit(n, p->bitmap);
263 p->ary[n] = NULL;
264 while(*paa && ! --((**paa)->count)){
265 free_layer(idp, **paa);
266 **paa-- = NULL;
268 if ( ! *paa )
269 idp->layers = 0;
270 return 0;
272 return -1;
275 static void *_idr_find(struct idr_context *idp, int id)
277 int n;
278 struct idr_layer *p;
280 n = idp->layers * IDR_BITS;
281 p = idp->top;
283 * This tests to see if bits outside the current tree are
284 * present. If so, tain't one of ours!
286 if (n + IDR_BITS < 31 &&
287 ((id & ~(~0 << MAX_ID_SHIFT)) >> (n + IDR_BITS))) {
288 return NULL;
291 /* Mask off upper bits we don't use for the search. */
292 id &= MAX_ID_MASK;
294 while (n >= IDR_BITS && p) {
295 n -= IDR_BITS;
296 p = p->ary[(id >> n) & IDR_MASK];
298 return((void *)p);
301 static int _idr_remove(struct idr_context *idp, int id)
303 struct idr_layer *p;
305 /* Mask off upper bits we don't use for the search. */
306 id &= MAX_ID_MASK;
308 if (sub_remove(idp, (idp->layers - 1) * IDR_BITS, id) == -1) {
309 return -1;
312 if ( idp->top && idp->top->count == 1 &&
313 (idp->layers > 1) &&
314 idp->top->ary[0]) {
315 /* We can drop a layer */
316 p = idp->top->ary[0];
317 idp->top->bitmap = idp->top->count = 0;
318 free_layer(idp, idp->top);
319 idp->top = p;
320 --idp->layers;
322 while (idp->id_free_cnt >= IDR_FREE_MAX) {
323 p = alloc_layer(idp);
324 talloc_free(p);
326 return 0;
329 /************************************************************************
330 this is the public interface
331 **************************************************************************/
334 initialise a idr tree. The context return value must be passed to
335 all subsequent idr calls. To destroy the idr tree use talloc_free()
336 on this context
338 _PUBLIC_ struct idr_context *idr_init(TALLOC_CTX *mem_ctx)
340 return talloc_zero(mem_ctx, struct idr_context);
344 allocate the next available id, and assign 'ptr' into its slot.
345 you can retrieve later this pointer using idr_find()
347 _PUBLIC_ int idr_get_new(struct idr_context *idp, void *ptr, int limit)
349 int ret = idr_get_new_above_int(idp, ptr, 0);
350 if (ret > limit) {
351 idr_remove(idp, ret);
352 return -1;
354 return ret;
358 allocate a new id, giving the first available value greater than or
359 equal to the given starting id
361 _PUBLIC_ int idr_get_new_above(struct idr_context *idp, void *ptr, int starting_id, int limit)
363 int ret = idr_get_new_above_int(idp, ptr, starting_id);
364 if (ret > limit) {
365 idr_remove(idp, ret);
366 return -1;
368 return ret;
372 find a pointer value previously set with idr_get_new given an id
374 _PUBLIC_ void *idr_find(struct idr_context *idp, int id)
376 return _idr_find(idp, id);
380 remove an id from the idr tree
382 _PUBLIC_ int idr_remove(struct idr_context *idp, int id)
384 int ret;
385 ret = _idr_remove((struct idr_context *)idp, id);
386 if (ret != 0) {
387 DEBUG(0,("WARNING: attempt to remove unset id %d in idtree\n", id));
389 return ret;