Rev 5056 | Rev 6934 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5056 | Rev 5270 | ||
---|---|---|---|
Line 18... | Line 18... | ||
18 | * pointer or what ever, we treat it as a (void *). You can pass this |
18 | * pointer or what ever, we treat it as a (void *). You can pass this |
19 | * id to a user for him to pass back at a later time. You then pass |
19 | * id to a user for him to pass back at a later time. You then pass |
20 | * that id to this code and it returns your pointer. |
20 | * that id to this code and it returns your pointer. |
21 | */ |
21 | */ |
Line -... | Line 22... | ||
- | 22 | ||
22 | 23 | #ifndef TEST // to test in user space... |
|
23 | #include |
24 | #include |
- | 25 | #include |
|
- | 26 | #endif |
|
24 | #include |
27 | #include |
25 | #include |
- | |
26 | #include |
28 | #include |
27 | #include |
29 | #include |
Line 28... | Line -... | ||
28 | //#include |
- | |
29 | - | ||
30 | static inline void * __must_check ERR_PTR(long error) |
- | |
31 | { |
- | |
Line 32... | Line -... | ||
32 | return (void *) error; |
- | |
33 | } |
- | |
Line 34... | Line 30... | ||
34 | 30 | #include |
|
35 | unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, |
31 | |
Line 130... | Line 126... | ||
130 | 126 | ||
131 | static inline void free_layer(struct idr *idr, struct idr_layer *p) |
127 | static inline void free_layer(struct idr *idr, struct idr_layer *p) |
132 | { |
128 | { |
133 | if (idr->hint == p) |
129 | if (idr->hint == p) |
134 | RCU_INIT_POINTER(idr->hint, NULL); |
130 | RCU_INIT_POINTER(idr->hint, NULL); |
135 | idr_layer_rcu_free(&p->rcu_head); |
131 | call_rcu(&p->rcu_head, idr_layer_rcu_free); |
Line 136... | Line 132... | ||
136 | } |
132 | } |
137 | 133 | ||
138 | /* only called when idp->lock is held */ |
134 | /* only called when idp->lock is held */ |
Line 498... | Line 494... | ||
498 | shift -= IDR_BITS; |
494 | shift -= IDR_BITS; |
499 | } |
495 | } |
500 | n = id & IDR_MASK; |
496 | n = id & IDR_MASK; |
501 | if (likely(p != NULL && test_bit(n, p->bitmap))) { |
497 | if (likely(p != NULL && test_bit(n, p->bitmap))) { |
502 | __clear_bit(n, p->bitmap); |
498 | __clear_bit(n, p->bitmap); |
503 | rcu_assign_pointer(p->ary[n], NULL); |
499 | RCU_INIT_POINTER(p->ary[n], NULL); |
504 | to_free = NULL; |
500 | to_free = NULL; |
505 | while(*paa && ! --((**paa)->count)){ |
501 | while(*paa && ! --((**paa)->count)){ |
506 | if (to_free) |
502 | if (to_free) |
507 | free_layer(idp, to_free); |
503 | free_layer(idp, to_free); |
508 | to_free = **paa; |
504 | to_free = **paa; |
Line 562... | Line 558... | ||
562 | struct idr_layer *pa[MAX_IDR_LEVEL + 1]; |
558 | struct idr_layer *pa[MAX_IDR_LEVEL + 1]; |
563 | struct idr_layer **paa = &pa[0]; |
559 | struct idr_layer **paa = &pa[0]; |
Line 564... | Line 560... | ||
564 | 560 | ||
565 | n = idp->layers * IDR_BITS; |
561 | n = idp->layers * IDR_BITS; |
566 | *paa = idp->top; |
562 | *paa = idp->top; |
567 | rcu_assign_pointer(idp->top, NULL); |
563 | RCU_INIT_POINTER(idp->top, NULL); |
Line 568... | Line 564... | ||
568 | max = idr_max(idp->layers); |
564 | max = idr_max(idp->layers); |
569 | 565 | ||
570 | id = 0; |
566 | id = 0; |
Line 597... | Line 593... | ||
597 | * completely unused and can be freed / recycled. The caller is |
593 | * completely unused and can be freed / recycled. The caller is |
598 | * responsible for ensuring that no one else accesses @idp during or after |
594 | * responsible for ensuring that no one else accesses @idp during or after |
599 | * idr_destroy(). |
595 | * idr_destroy(). |
600 | * |
596 | * |
601 | * A typical clean-up sequence for objects stored in an idr tree will use |
597 | * A typical clean-up sequence for objects stored in an idr tree will use |
602 | * idr_for_each() to free all objects, if necessay, then idr_destroy() to |
598 | * idr_for_each() to free all objects, if necessary, then idr_destroy() to |
603 | * free up the id mappings and cached idr_layers. |
599 | * free up the id mappings and cached idr_layers. |
604 | */ |
600 | */ |
605 | void idr_destroy(struct idr *idp) |
601 | void idr_destroy(struct idr *idp) |
606 | { |
602 | { |
607 | __idr_remove_all(idp); |
603 | __idr_remove_all(idp); |
Line 1117... | Line 1113... | ||
1117 | memset(ida, 0, sizeof(struct ida)); |
1113 | memset(ida, 0, sizeof(struct ida)); |
1118 | idr_init(&ida->idr); |
1114 | idr_init(&ida->idr); |
Line 1119... | Line 1115... | ||
1119 | 1115 | ||
1120 | } |
1116 | } |
1121 | EXPORT_SYMBOL(ida_init); |
- | |
1122 | - | ||
1123 | - | ||
1124 | - | ||
1125 | unsigned long find_first_bit(const unsigned long *addr, unsigned long size) |
- | |
1126 | { |
- | |
1127 | const unsigned long *p = addr; |
- | |
1128 | unsigned long result = 0; |
- | |
1129 | unsigned long tmp; |
- | |
1130 | - | ||
1131 | while (size & ~(BITS_PER_LONG-1)) { |
- | |
1132 | if ((tmp = *(p++))) |
- | |
1133 | goto found; |
- | |
1134 | result += BITS_PER_LONG; |
- | |
1135 | size -= BITS_PER_LONG; |
- | |
1136 | } |
- | |
1137 | if (!size) |
- | |
1138 | return result; |
- | |
1139 | - | ||
1140 | tmp = (*p) & (~0UL >> (BITS_PER_LONG - size)); |
- | |
1141 | if (tmp == 0UL) /* Are any bits set? */ |
- | |
1142 | return result + size; /* Nope. */ |
- | |
1143 | found: |
- | |
1144 | return result + __ffs(tmp); |
- | |
1145 | } |
- | |
1146 | - | ||
1147 | unsigned long find_next_bit(const unsigned long *addr, unsigned long size, |
- | |
1148 | unsigned long offset) |
- | |
1149 | { |
- | |
1150 | const unsigned long *p = addr + BITOP_WORD(offset); |
- | |
1151 | unsigned long result = offset & ~(BITS_PER_LONG-1); |
- | |
1152 | unsigned long tmp; |
- | |
1153 | - | ||
1154 | if (offset >= size) |
- | |
1155 | return size; |
- | |
1156 | size -= result; |
- | |
1157 | offset %= BITS_PER_LONG; |
- | |
1158 | if (offset) { |
- | |
1159 | tmp = *(p++); |
- | |
1160 | tmp &= (~0UL << offset); |
- | |
1161 | if (size < BITS_PER_LONG) |
- | |
1162 | goto found_first; |
- | |
1163 | if (tmp) |
- | |
1164 | goto found_middle; |
- | |
1165 | size -= BITS_PER_LONG; |
- | |
1166 | result += BITS_PER_LONG; |
- | |
1167 | } |
- | |
1168 | while (size & ~(BITS_PER_LONG-1)) { |
- | |
1169 | if ((tmp = *(p++))) |
- | |
1170 | goto found_middle; |
- | |
1171 | result += BITS_PER_LONG; |
- | |
1172 | size -= BITS_PER_LONG; |
- | |
1173 | } |
- | |
1174 | if (!size) |
- | |
1175 | return result; |
- | |
1176 | tmp = *p; |
- | |
1177 | - | ||
1178 | found_first: |
- | |
1179 | tmp &= (~0UL >> (BITS_PER_LONG - size)); |
- | |
1180 | if (tmp == 0UL) /* Are any bits set? */ |
- | |
1181 | return result + size; /* Nope. */ |
- | |
1182 | found_middle: |
- | |
1183 | return result + __ffs(tmp); |
- | |
1184 | } |
- | |
1185 | - | ||
1186 | unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, |
- | |
1187 | unsigned long offset) |
- | |
1188 | { |
- | |
1189 | const unsigned long *p = addr + BITOP_WORD(offset); |
- | |
1190 | unsigned long result = offset & ~(BITS_PER_LONG-1); |
- | |
1191 | unsigned long tmp; |
- | |
1192 | - | ||
1193 | if (offset >= size) |
- | |
1194 | return size; |
- | |
1195 | size -= result; |
- | |
1196 | offset %= BITS_PER_LONG; |
- | |
1197 | if (offset) { |
- | |
1198 | tmp = *(p++); |
- | |
1199 | tmp |= ~0UL >> (BITS_PER_LONG - offset); |
- | |
1200 | if (size < BITS_PER_LONG) |
- | |
1201 | goto found_first; |
- | |
1202 | if (~tmp) |
- | |
1203 | goto found_middle; |
- | |
1204 | size -= BITS_PER_LONG; |
- | |
1205 | result += BITS_PER_LONG; |
- | |
1206 | } |
- | |
1207 | while (size & ~(BITS_PER_LONG-1)) { |
- | |
1208 | if (~(tmp = *(p++))) |
- | |
1209 | goto found_middle; |
- | |
1210 | result += BITS_PER_LONG; |
- | |
1211 | size -= BITS_PER_LONG; |
- | |
1212 | } |
- | |
1213 | if (!size) |
- | |
1214 | return result; |
- | |
1215 | tmp = *p; |
- | |
1216 | - | ||
1217 | found_first: |
- | |
1218 | tmp |= ~0UL << size; |
- | |
1219 | if (tmp == ~0UL) /* Are any bits zero? */ |
- | |
1220 | return result + size; /* Nope. */ |
- | |
1221 | found_middle: |
- | |
1222 | return result + ffz(tmp); |
- | |
1223 | } |
- | |
1224 | - | ||
1225 | unsigned int hweight32(unsigned int w) |
- | |
1226 | { |
- | |
1227 | unsigned int res = w - ((w >> 1) & 0x55555555); |
- | |
1228 | res = (res & 0x33333333) + ((res >> 2) & 0x33333333); |
- | |
1229 | res = (res + (res >> 4)) & 0x0F0F0F0F; |
- | |
1230 | res = res + (res >> 8); |
- | |
1231 | return (res + (res >> 16)) & 0x000000FF; |
- | |
1232 | } |
- | |
1233 | - | ||
1234 | unsigned long hweight64(__u64 w) |
- | |
1235 | { |
- | |
1236 | #if BITS_PER_LONG == 32 |
- | |
1237 | return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w); |
- | |
1238 | #elif BITS_PER_LONG == 64 |
- | |
1239 | __u64 res = w - ((w >> 1) & 0x5555555555555555ul); |
- | |
1240 | res = (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul); |
- | |
1241 | res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful; |
- | |
1242 | res = res + (res >> 8); |
- | |
1243 | res = res + (res >> 16); |
- | |
1244 | return (res + (res >> 32)) & 0x00000000000000FFul; |
- | |
1245 | #endif |
- | |
1246 | }><>>>><>>>>>>=>>>>>>><>><>=>>><>=>>>><>=>>=>>>>=>=>>>>><>><>>><>><> |
- |