2 * Copyright (c) 2005-2012 The DragonFly Project.
3 * Copyright (c) 2013 François Tigeot
4 * Copyright (c) 2013 Matthew Dillon
7 * This code is derived from software contributed to The DragonFly Project
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
20 * 3. Neither the name of The DragonFly Project nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific, prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * cc -I. -DUSERLAND_TEST libkern/linux_idr.c -o /tmp/idr -g
46 #define _KERNEL_STRUCTURES
55 #include <sys/errno.h>
58 #define MALLOC_DEFINE(a, b, c)
59 #define lwkt_gettoken(x)
60 #define lwkt_reltoken(x)
61 #define kmalloc(bytes, zone, flags) calloc(bytes, 1)
62 #define lwkt_token_init(a, b)
63 #define lwkt_token_uninit(a)
64 #define kfree(ptr, flags) free(ptr)
66 #define panic(str, ...) assert(0)
67 #define min(a, b) (((a) < (b)) ? (a) : (b))
68 #define max(a, b) (((a) > (b)) ? (a) : (b))
71 main(int ac, char **av)
75 intptr_t generation = 0x10000000;
83 while (fgets(buf, sizeof(buf), stdin) != NULL) {
84 if (sscanf(buf, "a %d", &id) == 1) {
86 if (idr_pre_get(&idr, 0) == 0) {
87 fprintf(stderr, "pre_get failed\n");
90 error = idr_get_new_above(&idr,
96 fprintf(stderr, "get_new err %d\n",
100 printf("allocated %d value %08x\n",
101 id, (int)generation);
105 } else if (sscanf(buf, "f %d", &id) == 1) {
106 idr_remove(&idr, id);
107 } else if (sscanf(buf, "g %d", &id) == 1) {
108 void *res = idr_find(&idr, id);
109 printf("find %d res %p\n", id, res);
120 #include <sys/kernel.h>
121 #include <sys/libkern.h>
122 #include <sys/malloc.h>
123 #include <sys/param.h>
124 #include <sys/systm.h>
125 #include <sys/spinlock2.h>
126 #include <sys/limits.h>
130 /* Must be 2^n - 1 */
131 #define IDR_DEFAULT_SIZE 255
133 MALLOC_DEFINE(M_IDR, "idr", "Integer ID management");
135 static void idr_grow(struct idr *idp, int want);
136 static void idr_reserve(struct idr *idp, int id, int incr);
137 static int idr_find_free(struct idr *idp, int want, int lim);
140 * Number of nodes in right subtree, including the root.
143 right_subtree_size(int n)
145 return (n ^ (n | (n + 1)));
152 right_ancestor(int n)
154 return (n | (n + 1));
163 return ((n & (n + 1)) - 1);
167 idrfixup(struct idr *idp, int id)
169 if (id < idp->idr_freeindex) {
170 idp->idr_freeindex = id;
172 while (idp->idr_lastindex >= 0 &&
173 idp->idr_nodes[idp->idr_lastindex].data == NULL
175 --idp->idr_lastindex;
179 static __inline struct idr_node *
180 idr_get_node(struct idr *idp, int id)
182 struct idr_node *idrnp;
183 if (id < 0 || id >= idp->idr_count)
185 idrnp = &idp->idr_nodes[id];
186 if (idrnp->allocated == 0)
192 idr_reserve(struct idr *idp, int id, int incr)
195 idp->idr_nodes[id].allocated += incr;
196 KKASSERT(idp->idr_nodes[id].allocated >= 0);
197 id = left_ancestor(id);
202 idr_find_free(struct idr *idp, int want, int lim)
204 int id, rsum, rsize, node;
207 * Search for a free descriptor starting at the higher
208 * of want or fd_freefile. If that fails, consider
209 * expanding the ofile array.
211 * NOTE! the 'allocated' field is a cumulative recursive allocation
212 * count. If we happen to see a value of 0 then we can shortcut
213 * our search. Otherwise we run through through the tree going
214 * down branches we know have free descriptor(s) until we hit a
215 * leaf node. The leaf node will be free but will not necessarily
216 * have an allocated field of 0.
219 /* move up the tree looking for a subtree with a free node */
220 for (id = max(want, idp->idr_freeindex); id < min(idp->idr_count, lim);
221 id = right_ancestor(id)) {
222 if (idp->idr_nodes[id].allocated == 0)
225 rsize = right_subtree_size(id);
226 if (idp->idr_nodes[id].allocated == rsize)
227 continue; /* right subtree full */
230 * Free fd is in the right subtree of the tree rooted at fd.
231 * Call that subtree R. Look for the smallest (leftmost)
232 * subtree of R with an unallocated fd: continue moving
233 * down the left branch until encountering a full left
234 * subtree, then move to the right.
236 for (rsum = 0, rsize /= 2; rsize > 0; rsize /= 2) {
238 rsum += idp->idr_nodes[node].allocated;
239 if (idp->idr_nodes[id].allocated == rsum + rsize) {
240 id = node; /* move to the right */
241 if (idp->idr_nodes[node].allocated == 0)
252 * Blocking pre-get support, allows callers to use idr_pre_get() in
253 * combination with idr_get_new_above() such that idr_get_new_above()
254 * can be called safely with a spinlock held.
256 * Returns 0 on failure, 1 on success.
258 * Caller must hold a blockable lock.
261 idr_pre_get(struct idr *idp, __unused unsigned gfp_mask)
263 int want = idp->idr_maxwant;
265 int result = 1; /* success */
268 KKASSERT(mycpu->gd_spinlocks == 0);
269 lwkt_gettoken(&idp->idr_token);
272 * Grow if necessary (or if forced by the loop)
274 if (want >= idp->idr_count)
278 * Check if a spot is available, break and return 0 if true,
279 * unless the available spot is beyond our limit. It is
280 * possible to exceed the limit due to the way array growth
283 * XXX we assume that the caller uses a consistent <sid> such
284 * that the idr_maxwant field is correct, otherwise we
285 * may believe that a slot is available but the caller then
286 * fails in idr_get_new_above() and loops.
288 id = idr_find_free(idp, idp->idr_maxwant, lim);
291 result = 0; /* failure */
296 * Return ENOSPC if our limit has been reached, otherwise
297 * loop and force growth.
299 if (idp->idr_count >= lim) {
300 result = 0; /* failure */
303 want = idp->idr_count;
305 lwkt_reltoken(&idp->idr_token);
310 * Allocate an integer. If -EAGAIN is returned the caller should loop
311 * and call idr_pre_get() with no locks held, and then retry the call
312 * to idr_get_new_above().
314 * Can be safely called with spinlocks held.
317 idr_get_new_above(struct idr *idp, void *ptr, int sid, int *id)
321 KKASSERT(ptr != NULL);
324 * NOTE! Because the idp is initialized with a non-zero count,
325 * sid might be < idp->idr_count but idr_maxwant might not
326 * yet be initialized. So check both cases.
328 lwkt_gettoken(&idp->idr_token);
329 if (sid >= idp->idr_count || idp->idr_maxwant < sid) {
330 idp->idr_maxwant = max(idp->idr_maxwant, sid);
331 lwkt_reltoken(&idp->idr_token);
335 resid = idr_find_free(idp, sid, INT_MAX);
337 lwkt_reltoken(&idp->idr_token);
341 if (resid >= idp->idr_count)
342 panic("idr_get_new_above(): illegal resid %d", resid);
343 if (resid > idp->idr_lastindex)
344 idp->idr_lastindex = resid;
345 if (sid <= idp->idr_freeindex)
346 idp->idr_freeindex = resid;
348 idr_reserve(idp, resid, 1);
349 idp->idr_nodes[resid].data = ptr;
351 lwkt_reltoken(&idp->idr_token);
356 idr_get_new(struct idr *idp, void *ptr, int *id)
358 return idr_get_new_above(idp, ptr, 0, id);
362 * Grow the file table so it can hold through descriptor (want).
364 * Caller must hold a blockable lock.
367 idr_grow(struct idr *idp, int want)
369 struct idr_node *oldnodes, *newnodes;
372 /* We want 2^n - 1 descriptors */
376 } while (nf <= want);
379 printf("idr_grow: %d -> %d\n", idp->idr_count, nf);
382 /* Allocate a new zero'ed node array */
383 newnodes = kmalloc(nf * sizeof(struct idr_node),
384 M_IDR, M_ZERO | M_WAITOK);
386 /* We might race another grow */
387 if (nf <= idp->idr_count) {
388 kfree(newnodes, M_IDR);
393 * Copy existing nodes to the beginning of the new array
395 oldnodes = idp->idr_nodes;
397 bcopy(oldnodes, newnodes,
398 idp->idr_count * sizeof(struct idr_node));
400 idp->idr_nodes = newnodes;
404 kfree(oldnodes, M_IDR);
410 idr_remove(struct idr *idp, int id)
414 lwkt_gettoken(&idp->idr_token);
415 if (id < 0 || id >= idp->idr_count) {
416 lwkt_reltoken(&idp->idr_token);
419 if ((ptr = idp->idr_nodes[id].data) == NULL) {
420 lwkt_reltoken(&idp->idr_token);
423 idp->idr_nodes[id].data = NULL;
424 idr_reserve(idp, id, -1);
426 lwkt_reltoken(&idp->idr_token);
430 * Remove all int allocations, leave array intact.
432 * Caller must hold a blockable lock (or be in a context where holding
433 * the spinlock is not relevant).
436 idr_remove_all(struct idr *idp)
438 lwkt_gettoken(&idp->idr_token);
439 bzero(idp->idr_nodes, idp->idr_count * sizeof(struct idr_node));
440 idp->idr_lastindex = -1;
441 idp->idr_freeindex = 0;
442 idp->idr_nexpands = 0;
443 idp->idr_maxwant = 0;
444 lwkt_reltoken(&idp->idr_token);
448 idr_destroy(struct idr *idp)
450 lwkt_token_uninit(&idp->idr_token);
451 if (idp->idr_nodes) {
452 kfree(idp->idr_nodes, M_IDR);
453 idp->idr_nodes = NULL;
455 bzero(idp, sizeof(*idp));
459 idr_find(struct idr *idp, int id)
463 if (id < 0 || id >= idp->idr_count) {
465 } else if (idp->idr_nodes[id].allocated == 0) {
468 ret = idp->idr_nodes[id].data;
474 idr_for_each(struct idr *idp, int (*fn)(int id, void *p, void *data),
478 struct idr_node *nodes;
480 nodes = idp->idr_nodes;
481 for (i = 0; i < idp->idr_count; i++) {
482 if (nodes[i].data != NULL && nodes[i].allocated > 0) {
483 error = fn(i, nodes[i].data, data);
492 idr_replace(struct idr *idp, void *ptr, int id)
494 struct idr_node *idrnp;
497 lwkt_gettoken(&idp->idr_token);
498 idrnp = idr_get_node(idp, id);
499 if (idrnp == NULL || ptr == NULL) {
505 lwkt_reltoken(&idp->idr_token);
510 idr_init(struct idr *idp)
512 bzero(idp, sizeof(struct idr));
513 idp->idr_nodes = kmalloc(IDR_DEFAULT_SIZE * sizeof(struct idr_node),
514 M_IDR, M_WAITOK | M_ZERO);
515 idp->idr_count = IDR_DEFAULT_SIZE;
516 idp->idr_lastindex = -1;
517 idp->idr_maxwant = 0;
518 lwkt_token_init(&idp->idr_token, "idr token");