patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / linux / idr.h
1 /*
2  * include/linux/id.h
3  * 
4  * 2002-10-18  written by Jim Houston jim.houston@ccur.com
5  *      Copyright (C) 2002 by Concurrent Computer Corporation
6  *      Distributed under the GNU GPL license version 2.
7  *
8  * Small id to pointer translation service avoiding fixed sized
9  * tables.
10  */
11 #include <linux/types.h>
12 #include <asm/bitops.h>
13
14 #define RESERVED_ID_BITS 8
15
16 #if BITS_PER_LONG == 32
17 # define IDR_BITS 5
18 # define IDR_FULL 0xfffffffful
19 #elif BITS_PER_LONG == 64
20 # define IDR_BITS 6
21 # define IDR_FULL 0xfffffffffffffffful
22 #else
23 # error "BITS_PER_LONG is not 32 or 64"
24 #endif
25
26 #define IDR_SIZE (1 << IDR_BITS)
27 #define IDR_MASK ((1 << IDR_BITS)-1)
28
29 /* Define the size of the id's */
30 #define BITS_PER_INT (sizeof(int)*8)
31
32 #define MAX_ID_SHIFT (BITS_PER_INT - RESERVED_ID_BITS)
33 #define MAX_ID_BIT (1 << MAX_ID_SHIFT)
34 #define MAX_ID_MASK (MAX_ID_BIT - 1)
35
36 /* Leave the possibility of an incomplete final layer */
37 #define MAX_LEVEL (MAX_ID_SHIFT + IDR_BITS - 1) / IDR_BITS
38
39 /* Number of id_layer structs to leave in free list */
40 #define IDR_FREE_MAX MAX_LEVEL + MAX_LEVEL
41
42 struct idr_layer {
43         unsigned long            bitmap;        /* A zero bit means "space here" */
44         struct idr_layer        *ary[1<<IDR_BITS];
45         int                      count;         /* When zero, we can release it */
46 };
47
48 struct idr {
49         struct idr_layer *top;
50         struct idr_layer *id_free;
51         long              count;
52         int               layers;
53         int               id_free_cnt;
54         spinlock_t        lock;
55 };
56
57 #define IDR_INIT(name)  \
58 {                                                               \
59         .top            = NULL,                                 \
60         .id_free        = NULL,                                 \
61         .count          = 0,                                    \
62         .layers         = 0,                                    \
63         .id_free_cnt    = 0,                                    \
64         .lock           = SPIN_LOCK_UNLOCKED,                   \
65 }
66 #define DEFINE_IDR(name)        struct idr name = IDR_INIT(name)
67
68 /*
69  * This is what we export.
70  */
71
72 void *idr_find(struct idr *idp, int id);
73 int idr_pre_get(struct idr *idp, unsigned gfp_mask);
74 int idr_get_new(struct idr *idp, void *ptr);
75 void idr_remove(struct idr *idp, int id);
76 void idr_init(struct idr *idp);
77
78 extern kmem_cache_t *idr_layer_cache;
79