Rev 1967 | Rev 4065 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1967 | Rev 3391 | ||
---|---|---|---|
Line 10... | Line 10... | ||
10 | */ |
10 | */ |
Line 11... | Line 11... | ||
11 | 11 | ||
12 | #ifndef __IDR_H__ |
12 | #ifndef __IDR_H__ |
Line -... | Line 13... | ||
- | 13 | #define __IDR_H__ |
|
13 | #define __IDR_H__ |
14 | |
14 | 15 | #include |
|
15 | #include |
16 | #include |
16 | #include |
17 | #include |
17 | #include |
18 | #include |
- | 19 | //#include |
|
- | 20 | //#include |
|
- | 21 | #include |
|
Line 18... | Line -... | ||
18 | //#include |
- | |
19 | //#include |
- | |
20 | - | ||
21 | struct rcu_head { |
- | |
22 | struct rcu_head *next; |
- | |
23 | void (*func)(struct rcu_head *head); |
- | |
24 | }; |
- | |
25 | - | ||
26 | - | ||
27 | #if BITS_PER_LONG == 32 |
- | |
28 | # define IDR_BITS 5 |
- | |
29 | # define IDR_FULL 0xfffffffful |
- | |
30 | /* We can only use two of the bits in the top level because there is |
- | |
31 | only one possible bit in the top level (5 bits * 7 levels = 35 |
- | |
32 | bits, but you only use 31 bits in the id). */ |
- | |
33 | # define TOP_LEVEL_FULL (IDR_FULL >> 30) |
- | |
34 | #elif BITS_PER_LONG == 64 |
- | |
35 | # define IDR_BITS 6 |
- | |
36 | # define IDR_FULL 0xfffffffffffffffful |
- | |
37 | /* We can only use two of the bits in the top level because there is |
- | |
38 | only one possible bit in the top level (6 bits * 6 levels = 36 |
- | |
39 | bits, but you only use 31 bits in the id). */ |
- | |
40 | # define TOP_LEVEL_FULL (IDR_FULL >> 62) |
- | |
Line -... | Line 22... | ||
- | 22 | #include |
|
- | 23 | #include |
|
- | 24 | ||
- | 25 | ||
- | 26 | /* |
|
- | 27 | * We want shallower trees and thus more bits covered at each layer. 8 |
|
- | 28 | * bits gives us large enough first layer for most use cases and maximum |
|
41 | #else |
29 | * tree depth of 4. Each idr_layer is slightly larger than 2k on 64bit and |
42 | # error "BITS_PER_LONG is not 32 or 64" |
30 | * 1k on 32bit. |
Line 43... | Line -... | ||
43 | #endif |
- | |
44 | - | ||
45 | #define IDR_SIZE (1 << IDR_BITS) |
- | |
46 | #define IDR_MASK ((1 << IDR_BITS)-1) |
- | |
47 | - | ||
48 | #define MAX_ID_SHIFT (sizeof(int)*8 - 1) |
- | |
49 | #define MAX_ID_BIT (1U << MAX_ID_SHIFT) |
- | |
50 | #define MAX_ID_MASK (MAX_ID_BIT - 1) |
- | |
51 | - | ||
52 | /* Leave the possibility of an incomplete final layer */ |
- | |
53 | #define MAX_LEVEL (MAX_ID_SHIFT + IDR_BITS - 1) / IDR_BITS |
31 | */ |
- | 32 | #define IDR_BITS 8 |
|
54 | 33 | #define IDR_SIZE (1 << IDR_BITS) |
|
55 | /* Number of id_layer structs to leave in free list */ |
34 | #define IDR_MASK ((1 << IDR_BITS)-1) |
56 | #define IDR_FREE_MAX MAX_LEVEL + MAX_LEVEL |
35 | |
57 | 36 | struct idr_layer { |
|
58 | struct idr_layer { |
37 | int prefix; /* the ID prefix of this idr_layer */ |
59 | unsigned long bitmap; /* A zero bit means "space here" */ |
38 | DECLARE_BITMAP(bitmap, IDR_SIZE); /* A zero bit means "space here" */ |
Line 60... | Line 39... | ||
60 | struct idr_layer __rcu *ary[1< |
39 | struct idr_layer __rcu *ary[1< |
- | 40 | int count; /* When zero, we can release it */ |
|
61 | int count; /* When zero, we can release it */ |
41 | int layer; /* distance from leaf */ |
62 | int layer; /* distance from leaf */ |
42 | struct rcu_head rcu_head; |
63 | struct rcu_head rcu_head; |
43 | }; |
64 | }; |
44 | |
65 | 45 | struct idr { |
|
66 | struct idr { |
46 | struct idr_layer __rcu *hint; /* the last layer allocated from */ |
Line 67... | Line 47... | ||
67 | struct idr_layer __rcu *top; |
47 | struct idr_layer __rcu *top; |
68 | struct idr_layer *id_free; |
48 | struct idr_layer *id_free; |
69 | int layers; /* only valid without concurrent changes */ |
- | |
70 | int id_free_cnt; |
- | |
71 | // spinlock_t lock; |
- | |
72 | }; |
- | |
73 | 49 | int layers; /* only valid w/o concurrent changes */ |
|
74 | #define IDR_INIT(name) \ |
50 | int id_free_cnt; |
75 | { \ |
51 | spinlock_t lock; |
Line 76... | Line -... | ||
76 | .top = NULL, \ |
- | |
77 | .id_free = NULL, \ |
- | |
78 | .layers = 0, \ |
- | |
79 | .id_free_cnt = 0, \ |
- | |
80 | // .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ |
- | |
81 | } |
- | |
82 | #define DEFINE_IDR(name) struct idr name = IDR_INIT(name) |
52 | }; |
83 | 53 | ||
84 | /* Actions to be taken after a call to _idr_sub_alloc */ |
54 | #define IDR_INIT(name) \ |
85 | #define IDR_NEED_TO_GROW -2 |
55 | { \ |
86 | #define IDR_NOMORE_SPACE -3 |
56 | .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ |
Line 106... | Line 76... | ||
106 | 76 | ||
107 | /* |
77 | /* |
108 | * This is what we export. |
78 | * This is what we export. |
Line 109... | Line 79... | ||
109 | */ |
79 | */ |
110 | 80 | ||
111 | void *idr_find(struct idr *idp, int id); |
- | |
112 | int idr_pre_get(struct idr *idp, gfp_t gfp_mask); |
81 | void *idr_find_slowpath(struct idr *idp, int id); |
- | 82 | int idr_pre_get(struct idr *idp, gfp_t gfp_mask); |
|
- | 83 | int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id); |
|
113 | int idr_get_new(struct idr *idp, void *ptr, int *id); |
84 | void idr_preload(gfp_t gfp_mask); |
114 | int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id); |
85 | int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask); |
115 | int idr_for_each(struct idr *idp, |
86 | int idr_for_each(struct idr *idp, |
116 | int (*fn)(int id, void *p, void *data), void *data); |
87 | int (*fn)(int id, void *p, void *data), void *data); |
117 | void *idr_get_next(struct idr *idp, int *nextid); |
88 | void *idr_get_next(struct idr *idp, int *nextid); |
118 | void *idr_replace(struct idr *idp, void *ptr, int id); |
89 | void *idr_replace(struct idr *idp, void *ptr, int id); |
119 | void idr_remove(struct idr *idp, int id); |
90 | void idr_remove(struct idr *idp, int id); |
120 | void idr_remove_all(struct idr *idp); |
91 | void idr_free(struct idr *idp, int id); |
Line -... | Line 92... | ||
- | 92 | void idr_destroy(struct idr *idp); |
|
- | 93 | void idr_init(struct idr *idp); |
|
- | 94 | ||
- | 95 | /** |
|
- | 96 | * idr_preload_end - end preload section started with idr_preload() |
|
- | 97 | * |
|
- | 98 | * Each idr_preload() should be matched with an invocation of this |
|
- | 99 | * function. See idr_preload() for details. |
|
- | 100 | */ |
|
- | 101 | static inline void idr_preload_end(void) |
|
- | 102 | { |
|
- | 103 | // preempt_enable(); |
|
- | 104 | } |
|
- | 105 | ||
- | 106 | /** |
|
- | 107 | * idr_find - return pointer for given id |
|
- | 108 | * @idp: idr handle |
|
- | 109 | * @id: lookup key |
|
- | 110 | * |
|
- | 111 | * Return the pointer given the id it has been registered with. A %NULL |
|
- | 112 | * return indicates that @id is not valid or you passed %NULL in |
|
- | 113 | * idr_get_new(). |
|
- | 114 | * |
|
- | 115 | * This function can be called under rcu_read_lock(), given that the leaf |
|
- | 116 | * pointers lifetimes are correctly managed. |
|
- | 117 | */ |
|
- | 118 | static inline void *idr_find(struct idr *idr, int id) |
|
- | 119 | { |
|
- | 120 | struct idr_layer *hint = rcu_dereference_raw(idr->hint); |
|
- | 121 | ||
- | 122 | if (hint && (id & ~IDR_MASK) == hint->prefix) |
|
- | 123 | return rcu_dereference_raw(hint->ary[id & IDR_MASK]); |
|
- | 124 | ||
- | 125 | return idr_find_slowpath(idr, id); |
|
- | 126 | } |
|
- | 127 | ||
- | 128 | /** |
|
- | 129 | * idr_get_new - allocate new idr entry |
|
- | 130 | * @idp: idr handle |
|
- | 131 | * @ptr: pointer you want associated with the id |
|
- | 132 | * @id: pointer to the allocated handle |
|
- | 133 | * |
|
- | 134 | * Simple wrapper around idr_get_new_above() w/ @starting_id of zero. |
|
- | 135 | */ |
|
- | 136 | static inline int idr_get_new(struct idr *idp, void *ptr, int *id) |
|
- | 137 | { |
|
- | 138 | return idr_get_new_above(idp, ptr, 0, id); |
|
- | 139 | } |
|
- | 140 | ||
- | 141 | /** |
|
- | 142 | * idr_for_each_entry - iterate over an idr's elements of a given type |
|
- | 143 | * @idp: idr handle |
|
- | 144 | * @entry: the type * to use as cursor |
|
- | 145 | * @id: id entry's key |
|
- | 146 | */ |
|
- | 147 | #define idr_for_each_entry(idp, entry, id) \ |
|
- | 148 | for (id = 0, entry = (typeof(entry))idr_get_next((idp), &(id)); \ |
|
- | 149 | entry != NULL; \ |
|
- | 150 | ++id, entry = (typeof(entry))idr_get_next((idp), &(id))) |
|
- | 151 | ||
- | 152 | void __idr_remove_all(struct idr *idp); /* don't use */ |
|
- | 153 | ||
- | 154 | /** |
|
- | 155 | * idr_remove_all - remove all ids from the given idr tree |
|
- | 156 | * @idp: idr handle |
|
- | 157 | * |
|
- | 158 | * If you're trying to destroy @idp, calling idr_destroy() is enough. |
|
- | 159 | * This is going away. Don't use. |
|
- | 160 | */ |
|
- | 161 | static inline void __deprecated idr_remove_all(struct idr *idp) |
|
Line 121... | Line 162... | ||
121 | void idr_destroy(struct idr *idp); |
162 | { |
122 | void idr_init(struct idr *idp); |
163 | __idr_remove_all(idp); |
123 | 164 | } |
|
124 | 165 | ||
Line 141... | Line 182... | ||
141 | struct ida { |
182 | struct ida { |
142 | struct idr idr; |
183 | struct idr idr; |
143 | struct ida_bitmap *free_bitmap; |
184 | struct ida_bitmap *free_bitmap; |
144 | }; |
185 | }; |
Line 145... | Line 186... | ||
145 | 186 | ||
146 | #define IDA_INIT(name) { .idr = IDR_INIT(name), .free_bitmap = NULL, } |
187 | #define IDA_INIT(name) { .idr = IDR_INIT((name).idr), .free_bitmap = NULL, } |
Line 147... | Line 188... | ||
147 | #define DEFINE_IDA(name) struct ida name = IDA_INIT(name) |
188 | #define DEFINE_IDA(name) struct ida name = IDA_INIT(name) |
148 | 189 | ||
149 | int ida_pre_get(struct ida *ida, gfp_t gfp_mask); |
- | |
150 | int ida_get_new_above(struct ida *ida, int starting_id, int *p_id); |
190 | int ida_pre_get(struct ida *ida, gfp_t gfp_mask); |
151 | int ida_get_new(struct ida *ida, int *p_id); |
191 | int ida_get_new_above(struct ida *ida, int starting_id, int *p_id); |
152 | void ida_remove(struct ida *ida, int id); |
192 | void ida_remove(struct ida *ida, int id); |
Line 153... | Line 193... | ||
153 | void ida_destroy(struct ida *ida); |
193 | void ida_destroy(struct ida *ida); |
- | 194 | void ida_init(struct ida *ida); |
|
- | 195 | ||
Line 154... | Line 196... | ||
154 | void ida_init(struct ida *ida); |
196 | void __init idr_init_cache(void); |