| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
#ifndef TEST |
|---|
| 100 |
#include <linux/slab.h> |
|---|
| 101 |
#include <linux/init.h> |
|---|
| 102 |
#include <linux/module.h> |
|---|
| 103 |
#endif |
|---|
| 104 |
#include <linux/string.h> |
|---|
| 105 |
#include <linux/idr.h> |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
static kmem_cache_t *idr_layer_cache; |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
static struct idr_layer *alloc_layer(struct idr *idp) |
|---|
| 113 |
{ |
|---|
| 114 |
struct idr_layer *p; |
|---|
| 115 |
|
|---|
| 116 |
spin_lock(&idp->lock); |
|---|
| 117 |
if (!(p = idp->id_free)) |
|---|
| 118 |
BUG(); |
|---|
| 119 |
idp->id_free = p->ary[0]; |
|---|
| 120 |
idp->id_free_cnt--; |
|---|
| 121 |
p->ary[0] = 0; |
|---|
| 122 |
spin_unlock(&idp->lock); |
|---|
| 123 |
return(p); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
static void free_layer(struct idr *idp, struct idr_layer *p) |
|---|
| 127 |
{ |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
spin_lock(&idp->lock); |
|---|
| 132 |
p->ary[0] = idp->id_free; |
|---|
| 133 |
idp->id_free = p; |
|---|
| 134 |
idp->id_free_cnt++; |
|---|
| 135 |
spin_unlock(&idp->lock); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
int idr_pre_get(struct idr *idp, unsigned gfp_mask) |
|---|
| 139 |
{ |
|---|
| 140 |
while (idp->id_free_cnt < IDR_FREE_MAX) { |
|---|
| 141 |
struct idr_layer *new; |
|---|
| 142 |
new = kmem_cache_alloc(idr_layer_cache, gfp_mask); |
|---|
| 143 |
if(new == NULL) |
|---|
| 144 |
return (0); |
|---|
| 145 |
free_layer(idp, new); |
|---|
| 146 |
} |
|---|
| 147 |
return 1; |
|---|
| 148 |
} |
|---|
| 149 |
EXPORT_SYMBOL(idr_pre_get); |
|---|
| 150 |
|
|---|
| 151 |
static int sub_alloc(struct idr *idp, void *ptr, int *starting_id) |
|---|
| 152 |
{ |
|---|
| 153 |
int n, m, sh; |
|---|
| 154 |
struct idr_layer *p, *new; |
|---|
| 155 |
struct idr_layer *pa[MAX_LEVEL]; |
|---|
| 156 |
int l, id; |
|---|
| 157 |
long bm; |
|---|
| 158 |
|
|---|
| 159 |
id = *starting_id; |
|---|
| 160 |
p = idp->top; |
|---|
| 161 |
l = idp->layers; |
|---|
| 162 |
pa[l--] = NULL; |
|---|
| 163 |
while (1) { |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
n = (id >> (IDR_BITS*l)) & IDR_MASK; |
|---|
| 168 |
bm = ~p->bitmap; |
|---|
| 169 |
m = find_next_bit(&bm, IDR_SIZE, n); |
|---|
| 170 |
if (m == IDR_SIZE) { |
|---|
| 171 |
|
|---|
| 172 |
l++; |
|---|
| 173 |
id = (id | ((1 << (IDR_BITS*l))-1)) + 1; |
|---|
| 174 |
if (!(p = pa[l])) { |
|---|
| 175 |
*starting_id = id; |
|---|
| 176 |
return -2; |
|---|
| 177 |
} |
|---|
| 178 |
continue; |
|---|
| 179 |
} |
|---|
| 180 |
if (m != n) { |
|---|
| 181 |
sh = IDR_BITS*l; |
|---|
| 182 |
id = ((id >> sh) ^ n ^ m) << sh; |
|---|
| 183 |
} |
|---|
| 184 |
if (id >= MAX_ID_BIT) |
|---|
| 185 |
return -1; |
|---|
| 186 |
if (l == 0) |
|---|
| 187 |
break; |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
if (!p->ary[m]) { |
|---|
| 192 |
if (!(new = alloc_layer(idp))) |
|---|
| 193 |
return -1; |
|---|
| 194 |
p->ary[m] = new; |
|---|
| 195 |
p->count++; |
|---|
| 196 |
} |
|---|
| 197 |
pa[l--] = p; |
|---|
| 198 |
p = p->ary[m]; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
p->ary[m] = (struct idr_layer *)ptr; |
|---|
| 205 |
__set_bit(m, &p->bitmap); |
|---|
| 206 |
p->count++; |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
n = id; |
|---|
| 214 |
while (p->bitmap == IDR_FULL) { |
|---|
| 215 |
if (!(p = pa[++l])) |
|---|
| 216 |
break; |
|---|
| 217 |
n = n >> IDR_BITS; |
|---|
| 218 |
__set_bit((n & IDR_MASK), &p->bitmap); |
|---|
| 219 |
} |
|---|
| 220 |
return(id); |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
int idr_get_new_above(struct idr *idp, void *ptr, int starting_id) |
|---|
| 224 |
{ |
|---|
| 225 |
struct idr_layer *p, *new; |
|---|
| 226 |
int layers, v, id; |
|---|
| 227 |
|
|---|
| 228 |
id = starting_id; |
|---|
| 229 |
build_up: |
|---|
| 230 |
p = idp->top; |
|---|
| 231 |
layers = idp->layers; |
|---|
| 232 |
if (unlikely(!p)) { |
|---|
| 233 |
if (!(p = alloc_layer(idp))) |
|---|
| 234 |
return -1; |
|---|
| 235 |
layers = 1; |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
while (id >= (1 << (layers*IDR_BITS))) { |
|---|
| 242 |
layers++; |
|---|
| 243 |
if (!p->count) |
|---|
| 244 |
continue; |
|---|
| 245 |
if (!(new = alloc_layer(idp))) { |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
for (new = p; p && p != idp->top; new = p) { |
|---|
| 251 |
p = p->ary[0]; |
|---|
| 252 |
new->ary[0] = 0; |
|---|
| 253 |
new->bitmap = new->count = 0; |
|---|
| 254 |
free_layer(idp, new); |
|---|
| 255 |
} |
|---|
| 256 |
return -1; |
|---|
| 257 |
} |
|---|
| 258 |
new->ary[0] = p; |
|---|
| 259 |
new->count = 1; |
|---|
| 260 |
if (p->bitmap == IDR_FULL) |
|---|
| 261 |
__set_bit(0, &new->bitmap); |
|---|
| 262 |
p = new; |
|---|
| 263 |
} |
|---|
| 264 |
idp->top = p; |
|---|
| 265 |
idp->layers = layers; |
|---|
| 266 |
v = sub_alloc(idp, ptr, &id); |
|---|
| 267 |
if (v == -2) |
|---|
| 268 |
goto build_up; |
|---|
| 269 |
if ( likely(v >= 0 )) { |
|---|
| 270 |
idp->count++; |
|---|
| 271 |
v += (idp->count << MAX_ID_SHIFT); |
|---|
| 272 |
if ( unlikely( v == -1 )) |
|---|
| 273 |
v += (1L << MAX_ID_SHIFT); |
|---|
| 274 |
} |
|---|
| 275 |
return(v); |
|---|
| 276 |
} |
|---|
| 277 |
EXPORT_SYMBOL(idr_get_new_above); |
|---|
| 278 |
|
|---|
| 279 |
int idr_get_new(struct idr *idp, void *ptr) |
|---|
| 280 |
{ |
|---|
| 281 |
return idr_get_new_above(idp, ptr, 0); |
|---|
| 282 |
} |
|---|
| 283 |
EXPORT_SYMBOL(idr_get_new); |
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
static void sub_remove(struct idr *idp, int shift, int id) |
|---|
| 287 |
{ |
|---|
| 288 |
struct idr_layer *p = idp->top; |
|---|
| 289 |
struct idr_layer **pa[MAX_LEVEL]; |
|---|
| 290 |
struct idr_layer ***paa = &pa[0]; |
|---|
| 291 |
|
|---|
| 292 |
*paa = NULL; |
|---|
| 293 |
*++paa = &idp->top; |
|---|
| 294 |
|
|---|
| 295 |
while ((shift > 0) && p) { |
|---|
| 296 |
int n = (id >> shift) & IDR_MASK; |
|---|
| 297 |
__clear_bit(n, &p->bitmap); |
|---|
| 298 |
*++paa = &p->ary[n]; |
|---|
| 299 |
p = p->ary[n]; |
|---|
| 300 |
shift -= IDR_BITS; |
|---|
| 301 |
} |
|---|
| 302 |
if (likely(p != NULL)){ |
|---|
| 303 |
int n = id & IDR_MASK; |
|---|
| 304 |
__clear_bit(n, &p->bitmap); |
|---|
| 305 |
p->ary[n] = NULL; |
|---|
| 306 |
while(*paa && ! --((**paa)->count)){ |
|---|
| 307 |
free_layer(idp, **paa); |
|---|
| 308 |
**paa-- = NULL; |
|---|
| 309 |
} |
|---|
| 310 |
if ( ! *paa ) |
|---|
| 311 |
idp->layers = 0; |
|---|
| 312 |
} |
|---|
| 313 |
} |
|---|
| 314 |
void idr_remove(struct idr *idp, int id) |
|---|
| 315 |
{ |
|---|
| 316 |
struct idr_layer *p; |
|---|
| 317 |
|
|---|
| 318 |
sub_remove(idp, (idp->layers - 1) * IDR_BITS, id); |
|---|
| 319 |
if ( idp->top && idp->top->count == 1 && |
|---|
| 320 |
(idp->layers > 1) && |
|---|
| 321 |
idp->top->ary[0]){ |
|---|
| 322 |
|
|---|
| 323 |
p = idp->top->ary[0]; |
|---|
| 324 |
idp->top->bitmap = idp->top->count = 0; |
|---|
| 325 |
free_layer(idp, idp->top); |
|---|
| 326 |
idp->top = p; |
|---|
| 327 |
--idp->layers; |
|---|
| 328 |
} |
|---|
| 329 |
while (idp->id_free_cnt >= IDR_FREE_MAX) { |
|---|
| 330 |
|
|---|
| 331 |
p = alloc_layer(idp); |
|---|
| 332 |
kmem_cache_free(idr_layer_cache, p); |
|---|
| 333 |
return; |
|---|
| 334 |
} |
|---|
| 335 |
} |
|---|
| 336 |
EXPORT_SYMBOL(idr_remove); |
|---|
| 337 |
|
|---|
| 338 |
void *idr_find(struct idr *idp, int id) |
|---|
| 339 |
{ |
|---|
| 340 |
int n; |
|---|
| 341 |
struct idr_layer *p; |
|---|
| 342 |
|
|---|
| 343 |
n = idp->layers * IDR_BITS; |
|---|
| 344 |
p = idp->top; |
|---|
| 345 |
#if 0 |
|---|
| 346 |
|
|---|
| 347 |
* This tests to see if bits outside the current tree are |
|---|
| 348 |
* present. If so, tain't one of ours! |
|---|
| 349 |
*/ |
|---|
| 350 |
if ( unlikely( (id & ~(~0 << MAX_ID_SHIFT)) >> (n + IDR_BITS))) |
|---|
| 351 |
return NULL; |
|---|
| 352 |
#endif |
|---|
| 353 |
while (n > 0 && p) { |
|---|
| 354 |
n -= IDR_BITS; |
|---|
| 355 |
p = p->ary[(id >> n) & IDR_MASK]; |
|---|
| 356 |
} |
|---|
| 357 |
return((void *)p); |
|---|
| 358 |
} |
|---|
| 359 |
EXPORT_SYMBOL(idr_find); |
|---|
| 360 |
|
|---|
| 361 |
static void idr_cache_ctor(void * idr_layer, |
|---|
| 362 |
kmem_cache_t *idr_layer_cache, unsigned long flags) |
|---|
| 363 |
{ |
|---|
| 364 |
memset(idr_layer, 0, sizeof(struct idr_layer)); |
|---|
| 365 |
} |
|---|
| 366 |
|
|---|
| 367 |
static int init_id_cache(void) |
|---|
| 368 |
{ |
|---|
| 369 |
if (!idr_layer_cache) |
|---|
| 370 |
idr_layer_cache = kmem_cache_create("idr_layer_cache", |
|---|
| 371 |
sizeof(struct idr_layer), 0, 0, idr_cache_ctor, 0); |
|---|
| 372 |
return 0; |
|---|
| 373 |
} |
|---|
| 374 |
|
|---|
| 375 |
void idr_init(struct idr *idp) |
|---|
| 376 |
{ |
|---|
| 377 |
init_id_cache(); |
|---|
| 378 |
memset(idp, 0, sizeof(struct idr)); |
|---|
| 379 |
spin_lock_init(&idp->lock); |
|---|
| 380 |
} |
|---|
| 381 |
EXPORT_SYMBOL(idr_init); |
|---|
| 382 |
|
|---|