]> git.ozlabs.org Git - ccan/blob - ccan/idtree/idtree.c
tal: allow notifiers on NULL.
[ccan] / ccan / idtree / idtree.c
1 /*
2    Based on SAMBA 7ce1356c9f571c55af70bd6b966fe50898c1582d.
3
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.
6
7    Copyright (C) Andrew Tridgell 2004
8
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
12
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.
17
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.
22
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/>.
25 */
26
27 #include <ccan/idtree/idtree.h>
28 #include <ccan/tal/tal.h>
29 #include <stdint.h>
30 #include <string.h>
31
32 #define IDTREE_BITS 5
33 #define IDTREE_FULL 0xfffffffful
34 #if 0 /* unused */
35 #define TOP_LEVEL_FULL (IDTREE_FULL >> 30)
36 #endif
37 #define IDTREE_SIZE (1 << IDTREE_BITS)
38 #define IDTREE_MASK ((1 << IDTREE_BITS)-1)
39 #define MAX_ID_SHIFT (sizeof(int)*8 - 1)
40 #define MAX_ID_BIT (1U << MAX_ID_SHIFT)
41 #define MAX_ID_MASK (MAX_ID_BIT - 1)
42 #define MAX_LEVEL (MAX_ID_SHIFT + IDTREE_BITS - 1) / IDTREE_BITS
43 #define IDTREE_FREE_MAX MAX_LEVEL + MAX_LEVEL
44
45 #define set_bit(bit, v) (v) |= (1<<(bit))
46 #define clear_bit(bit, v) (v) &= ~(1<<(bit))
47 #define test_bit(bit, v) ((v) & (1<<(bit)))
48
49 struct idtree_layer {
50         uint32_t                 bitmap;
51         struct idtree_layer     *ary[IDTREE_SIZE];
52         int                      count;
53 };
54
55 struct idtree {
56         struct idtree_layer *top;
57         struct idtree_layer *id_free;
58         int               layers;
59         int               id_free_cnt;
60 };
61
62 static struct idtree_layer *alloc_layer(struct idtree *idp)
63 {
64         struct idtree_layer *p;
65
66         if (!(p = idp->id_free))
67                 return NULL;
68         idp->id_free = p->ary[0];
69         idp->id_free_cnt--;
70         p->ary[0] = NULL;
71         return p;
72 }
73
74 static int find_next_bit(uint32_t bm, int maxid, int n)
75 {
76         while (n<maxid && !test_bit(n, bm)) n++;
77         return n;
78 }
79
80 static void free_layer(struct idtree *idp, struct idtree_layer *p)
81 {
82         p->ary[0] = idp->id_free;
83         idp->id_free = p;
84         idp->id_free_cnt++;
85 }
86
87 static int idtree_pre_get(struct idtree *idp)
88 {
89         while (idp->id_free_cnt < IDTREE_FREE_MAX) {
90                 struct idtree_layer *pn = talz(idp, struct idtree_layer);
91                 if(pn == NULL)
92                         return (0);
93                 free_layer(idp, pn);
94         }
95         return 1;
96 }
97
98 static int sub_alloc(struct idtree *idp, const void *ptr, int *starting_id)
99 {
100         int n, m, sh;
101         struct idtree_layer *p, *pn;
102         struct idtree_layer *pa[MAX_LEVEL+1];
103         unsigned int l;
104         int id, oid;
105         uint32_t bm;
106
107         memset(pa, 0, sizeof(pa));
108
109         id = *starting_id;
110 restart:
111         p = idp->top;
112         l = idp->layers;
113         pa[l--] = NULL;
114         while (1) {
115                 /*
116                  * We run around this while until we reach the leaf node...
117                  */
118                 n = (id >> (IDTREE_BITS*l)) & IDTREE_MASK;
119                 bm = ~p->bitmap;
120                 m = find_next_bit(bm, IDTREE_SIZE, n);
121                 if (m == IDTREE_SIZE) {
122                         /* no space available go back to previous layer. */
123                         l++;
124                         oid = id;
125                         id = (id | ((1 << (IDTREE_BITS*l))-1)) + 1;
126
127                         /* if already at the top layer, we need to grow */
128                         if (!(p = pa[l])) {
129                                 *starting_id = id;
130                                 return -2;
131                         }
132
133                         /* If we need to go up one layer, continue the
134                          * loop; otherwise, restart from the top.
135                          */
136                         sh = IDTREE_BITS * (l + 1);
137                         if (oid >> sh == id >> sh)
138                                 continue;
139                         else
140                                 goto restart;
141                 }
142                 if (m != n) {
143                         sh = IDTREE_BITS*l;
144                         id = ((id >> sh) ^ n ^ m) << sh;
145                 }
146                 if ((id >= MAX_ID_BIT) || (id < 0))
147                         return -1;
148                 if (l == 0)
149                         break;
150                 /*
151                  * Create the layer below if it is missing.
152                  */
153                 if (!p->ary[m]) {
154                         if (!(pn = alloc_layer(idp)))
155                                 return -1;
156                         p->ary[m] = pn;
157                         p->count++;
158                 }
159                 pa[l--] = p;
160                 p = p->ary[m];
161         }
162         /*
163          * We have reached the leaf node, plant the
164          * users pointer and return the raw id.
165          */
166         p->ary[m] = (struct idtree_layer *)ptr;
167         set_bit(m, p->bitmap);
168         p->count++;
169         /*
170          * If this layer is full mark the bit in the layer above
171          * to show that this part of the radix tree is full.
172          * This may complete the layer above and require walking
173          * up the radix tree.
174          */
175         n = id;
176         while (p->bitmap == IDTREE_FULL) {
177                 if (!(p = pa[++l]))
178                         break;
179                 n = n >> IDTREE_BITS;
180                 set_bit((n & IDTREE_MASK), p->bitmap);
181         }
182         return(id);
183 }
184
185 static int idtree_get_new_above_int(struct idtree *idp,
186                                     const void *ptr, int starting_id)
187 {
188         struct idtree_layer *p, *pn;
189         int layers, v, id;
190
191         idtree_pre_get(idp);
192
193         id = starting_id;
194 build_up:
195         p = idp->top;
196         layers = idp->layers;
197         if (!p) {
198                 if (!(p = alloc_layer(idp)))
199                         return -1;
200                 layers = 1;
201         }
202         /*
203          * Add a new layer to the top of the tree if the requested
204          * id is larger than the currently allocated space.
205          */
206         while ((layers < MAX_LEVEL) && (id >= (1 << (layers*IDTREE_BITS)))) {
207                 layers++;
208                 if (!p->count)
209                         continue;
210                 if (!(pn = alloc_layer(idp))) {
211                         /*
212                          * The allocation failed.  If we built part of
213                          * the structure tear it down.
214                          */
215                         for (pn = p; p && p != idp->top; pn = p) {
216                                 p = p->ary[0];
217                                 pn->ary[0] = NULL;
218                                 pn->bitmap = pn->count = 0;
219                                 free_layer(idp, pn);
220                         }
221                         return -1;
222                 }
223                 pn->ary[0] = p;
224                 pn->count = 1;
225                 if (p->bitmap == IDTREE_FULL)
226                         set_bit(0, pn->bitmap);
227                 p = pn;
228         }
229         idp->top = p;
230         idp->layers = layers;
231         v = sub_alloc(idp, ptr, &id);
232         if (v == -2)
233                 goto build_up;
234         return(v);
235 }
236
237 static int sub_remove(struct idtree *idp, int shift, int id)
238 {
239         struct idtree_layer *p = idp->top;
240         struct idtree_layer **pa[1+MAX_LEVEL];
241         struct idtree_layer ***paa = &pa[0];
242         int n;
243
244         *paa = NULL;
245         *++paa = &idp->top;
246
247         while ((shift > 0) && p) {
248                 n = (id >> shift) & IDTREE_MASK;
249                 clear_bit(n, p->bitmap);
250                 *++paa = &p->ary[n];
251                 p = p->ary[n];
252                 shift -= IDTREE_BITS;
253         }
254         n = id & IDTREE_MASK;
255         if (p != NULL && test_bit(n, p->bitmap)) {
256                 clear_bit(n, p->bitmap);
257                 p->ary[n] = NULL;
258                 while(*paa && ! --((**paa)->count)){
259                         free_layer(idp, **paa);
260                         **paa-- = NULL;
261                 }
262                 if ( ! *paa )
263                         idp->layers = 0;
264                 return 0;
265         }
266         return -1;
267 }
268
269 void *idtree_lookup(const struct idtree *idp, int id)
270 {
271         int n;
272         struct idtree_layer *p;
273
274         n = idp->layers * IDTREE_BITS;
275         p = idp->top;
276         /*
277          * This tests to see if bits outside the current tree are
278          * present.  If so, tain't one of ours!
279          */
280         if (n + IDTREE_BITS < 31 &&
281             (id & ~(~0U << MAX_ID_SHIFT)) >> (n + IDTREE_BITS))
282              return NULL;
283
284         /* Mask off upper bits we don't use for the search. */
285         id &= MAX_ID_MASK;
286
287         while (n >= IDTREE_BITS && p) {
288                 n -= IDTREE_BITS;
289                 p = p->ary[(id >> n) & IDTREE_MASK];
290         }
291         return((void *)p);
292 }
293
294 bool idtree_remove(struct idtree *idp, int id)
295 {
296         struct idtree_layer *p;
297
298         /* Mask off upper bits we don't use for the search. */
299         id &= MAX_ID_MASK;
300
301         if (sub_remove(idp, (idp->layers - 1) * IDTREE_BITS, id) == -1) {
302                 return false;
303         }
304
305         if ( idp->top && idp->top->count == 1 &&
306              (idp->layers > 1) &&
307              idp->top->ary[0]) {
308                 /* We can drop a layer */
309                 p = idp->top->ary[0];
310                 idp->top->bitmap = idp->top->count = 0;
311                 free_layer(idp, idp->top);
312                 idp->top = p;
313                 --idp->layers;
314         }
315         while (idp->id_free_cnt >= IDTREE_FREE_MAX) {
316                 p = alloc_layer(idp);
317                 tal_free(p);
318         }
319         return true;
320 }
321
322 struct idtree *idtree_new(void *mem_ctx)
323 {
324         return talz(mem_ctx, struct idtree);
325 }
326
327 int idtree_add(struct idtree *idp, const void *ptr, int limit)
328 {
329         int ret = idtree_get_new_above_int(idp, ptr, 0);
330         if (ret > limit) {
331                 idtree_remove(idp, ret);
332                 return -1;
333         }
334         return ret;
335 }
336
337 int idtree_add_above(struct idtree *idp, const void *ptr,
338                      int starting_id, int limit)
339 {
340         int ret = idtree_get_new_above_int(idp, ptr, starting_id);
341         if (ret > limit) {
342                 idtree_remove(idp, ret);
343                 return -1;
344         }
345         return ret;
346 }