bitmap.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #ifndef __LINUX_BITMAP_H
  2. #define __LINUX_BITMAP_H
  3. #ifndef __ASSEMBLY__
  4. #include <linux/types.h>
  5. #include <linux/bitops.h>
  6. #include <linux/string.h>
  7. #include <linux/kernel.h>
  8. /*
  9. * bitmaps provide bit arrays that consume one or more unsigned
  10. * longs. The bitmap interface and available operations are listed
  11. * here, in bitmap.h
  12. *
  13. * Function implementations generic to all architectures are in
  14. * lib/bitmap.c. Functions implementations that are architecture
  15. * specific are in various include/asm-<arch>/bitops.h headers
  16. * and other arch/<arch> specific files.
  17. *
  18. * See lib/bitmap.c for more details.
  19. */
  20. /**
  21. * DOC: bitmap overview
  22. *
  23. * The available bitmap operations and their rough meaning in the
  24. * case that the bitmap is a single unsigned long are thus:
  25. *
  26. * Note that nbits should be always a compile time evaluable constant.
  27. * Otherwise many inlines will generate horrible code.
  28. *
  29. * ::
  30. *
  31. * bitmap_zero(dst, nbits) *dst = 0UL
  32. * bitmap_fill(dst, nbits) *dst = ~0UL
  33. * bitmap_copy(dst, src, nbits) *dst = *src
  34. * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
  35. * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
  36. * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
  37. * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
  38. * bitmap_complement(dst, src, nbits) *dst = ~(*src)
  39. * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
  40. * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
  41. * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2?
  42. * bitmap_empty(src, nbits) Are all bits zero in *src?
  43. * bitmap_full(src, nbits) Are all bits set in *src?
  44. * bitmap_weight(src, nbits) Hamming Weight: number set bits
  45. * bitmap_set(dst, pos, nbits) Set specified bit area
  46. * bitmap_clear(dst, pos, nbits) Clear specified bit area
  47. * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
  48. * bitmap_find_next_zero_area_off(buf, len, pos, n, mask) as above
  49. * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
  50. * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
  51. * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src)
  52. * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit)
  53. * bitmap_onto(dst, orig, relmap, nbits) *dst = orig relative to relmap
  54. * bitmap_fold(dst, orig, sz, nbits) dst bits = orig bits mod sz
  55. * bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf
  56. * bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
  57. * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from kernel buf
  58. * bitmap_parselist_user(buf, dst, nbits) Parse bitmap dst from user buf
  59. * bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region
  60. * bitmap_release_region(bitmap, pos, order) Free specified bit region
  61. * bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region
  62. * bitmap_from_u32array(dst, nbits, buf, nwords) *dst = *buf (nwords 32b words)
  63. * bitmap_to_u32array(buf, nwords, src, nbits) *buf = *dst (nwords 32b words)
  64. *
  65. */
  66. /**
  67. * DOC: bitmap bitops
  68. *
  69. * Also the following operations in asm/bitops.h apply to bitmaps.::
  70. *
  71. * set_bit(bit, addr) *addr |= bit
  72. * clear_bit(bit, addr) *addr &= ~bit
  73. * change_bit(bit, addr) *addr ^= bit
  74. * test_bit(bit, addr) Is bit set in *addr?
  75. * test_and_set_bit(bit, addr) Set bit and return old value
  76. * test_and_clear_bit(bit, addr) Clear bit and return old value
  77. * test_and_change_bit(bit, addr) Change bit and return old value
  78. * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
  79. * find_first_bit(addr, nbits) Position first set bit in *addr
  80. * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
  81. * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
  82. *
  83. */
  84. /**
  85. * DOC: declare bitmap
  86. * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
  87. * to declare an array named 'name' of just enough unsigned longs to
  88. * contain all bit positions from 0 to 'bits' - 1.
  89. */
  90. /*
  91. * lib/bitmap.c provides these functions:
  92. */
  93. extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
  94. extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
  95. extern int __bitmap_equal(const unsigned long *bitmap1,
  96. const unsigned long *bitmap2, unsigned int nbits);
  97. extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
  98. unsigned int nbits);
  99. extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  100. unsigned int shift, unsigned int nbits);
  101. extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  102. unsigned int shift, unsigned int nbits);
  103. extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  104. const unsigned long *bitmap2, unsigned int nbits);
  105. extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  106. const unsigned long *bitmap2, unsigned int nbits);
  107. extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  108. const unsigned long *bitmap2, unsigned int nbits);
  109. extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  110. const unsigned long *bitmap2, unsigned int nbits);
  111. extern int __bitmap_intersects(const unsigned long *bitmap1,
  112. const unsigned long *bitmap2, unsigned int nbits);
  113. extern int __bitmap_subset(const unsigned long *bitmap1,
  114. const unsigned long *bitmap2, unsigned int nbits);
  115. extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
  116. extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
  117. extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
  118. extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
  119. unsigned long size,
  120. unsigned long start,
  121. unsigned int nr,
  122. unsigned long align_mask,
  123. unsigned long align_offset);
  124. /**
  125. * bitmap_find_next_zero_area - find a contiguous aligned zero area
  126. * @map: The address to base the search on
  127. * @size: The bitmap size in bits
  128. * @start: The bitnumber to start searching at
  129. * @nr: The number of zeroed bits we're looking for
  130. * @align_mask: Alignment mask for zero area
  131. *
  132. * The @align_mask should be one less than a power of 2; the effect is that
  133. * the bit offset of all zero areas this function finds is multiples of that
  134. * power of 2. A @align_mask of 0 means no alignment is required.
  135. */
  136. static inline unsigned long
  137. bitmap_find_next_zero_area(unsigned long *map,
  138. unsigned long size,
  139. unsigned long start,
  140. unsigned int nr,
  141. unsigned long align_mask)
  142. {
  143. return bitmap_find_next_zero_area_off(map, size, start, nr,
  144. align_mask, 0);
  145. }
  146. extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
  147. unsigned long *dst, int nbits);
  148. extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
  149. unsigned long *dst, int nbits);
  150. extern int bitmap_parselist(const char *buf, unsigned long *maskp,
  151. int nmaskbits);
  152. extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
  153. unsigned long *dst, int nbits);
  154. extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
  155. const unsigned long *old, const unsigned long *new, unsigned int nbits);
  156. extern int bitmap_bitremap(int oldbit,
  157. const unsigned long *old, const unsigned long *new, int bits);
  158. extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
  159. const unsigned long *relmap, unsigned int bits);
  160. extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
  161. unsigned int sz, unsigned int nbits);
  162. extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
  163. extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
  164. extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
  165. extern unsigned int bitmap_from_u32array(unsigned long *bitmap,
  166. unsigned int nbits,
  167. const u32 *buf,
  168. unsigned int nwords);
  169. extern unsigned int bitmap_to_u32array(u32 *buf,
  170. unsigned int nwords,
  171. const unsigned long *bitmap,
  172. unsigned int nbits);
  173. #ifdef __BIG_ENDIAN
  174. extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
  175. #else
  176. #define bitmap_copy_le bitmap_copy
  177. #endif
  178. extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
  179. extern int bitmap_print_to_pagebuf(bool list, char *buf,
  180. const unsigned long *maskp, int nmaskbits);
  181. #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
  182. #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
  183. #define small_const_nbits(nbits) \
  184. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
  185. static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
  186. {
  187. if (small_const_nbits(nbits))
  188. *dst = 0UL;
  189. else {
  190. unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  191. memset(dst, 0, len);
  192. }
  193. }
  194. static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
  195. {
  196. unsigned int nlongs = BITS_TO_LONGS(nbits);
  197. if (!small_const_nbits(nbits)) {
  198. unsigned int len = (nlongs - 1) * sizeof(unsigned long);
  199. memset(dst, 0xff, len);
  200. }
  201. dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
  202. }
  203. static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
  204. unsigned int nbits)
  205. {
  206. if (small_const_nbits(nbits))
  207. *dst = *src;
  208. else {
  209. unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  210. memcpy(dst, src, len);
  211. }
  212. }
  213. static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
  214. const unsigned long *src2, unsigned int nbits)
  215. {
  216. if (small_const_nbits(nbits))
  217. return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  218. return __bitmap_and(dst, src1, src2, nbits);
  219. }
  220. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  221. const unsigned long *src2, unsigned int nbits)
  222. {
  223. if (small_const_nbits(nbits))
  224. *dst = *src1 | *src2;
  225. else
  226. __bitmap_or(dst, src1, src2, nbits);
  227. }
  228. static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
  229. const unsigned long *src2, unsigned int nbits)
  230. {
  231. if (small_const_nbits(nbits))
  232. *dst = *src1 ^ *src2;
  233. else
  234. __bitmap_xor(dst, src1, src2, nbits);
  235. }
  236. static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
  237. const unsigned long *src2, unsigned int nbits)
  238. {
  239. if (small_const_nbits(nbits))
  240. return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  241. return __bitmap_andnot(dst, src1, src2, nbits);
  242. }
  243. static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
  244. unsigned int nbits)
  245. {
  246. if (small_const_nbits(nbits))
  247. *dst = ~(*src);
  248. else
  249. __bitmap_complement(dst, src, nbits);
  250. }
  251. static inline int bitmap_equal(const unsigned long *src1,
  252. const unsigned long *src2, unsigned int nbits)
  253. {
  254. if (small_const_nbits(nbits))
  255. return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
  256. if (__builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
  257. return !memcmp(src1, src2, nbits / 8);
  258. return __bitmap_equal(src1, src2, nbits);
  259. }
  260. static inline int bitmap_intersects(const unsigned long *src1,
  261. const unsigned long *src2, unsigned int nbits)
  262. {
  263. if (small_const_nbits(nbits))
  264. return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  265. else
  266. return __bitmap_intersects(src1, src2, nbits);
  267. }
  268. static inline int bitmap_subset(const unsigned long *src1,
  269. const unsigned long *src2, unsigned int nbits)
  270. {
  271. if (small_const_nbits(nbits))
  272. return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
  273. else
  274. return __bitmap_subset(src1, src2, nbits);
  275. }
  276. static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
  277. {
  278. if (small_const_nbits(nbits))
  279. return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
  280. return find_first_bit(src, nbits) == nbits;
  281. }
  282. static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
  283. {
  284. if (small_const_nbits(nbits))
  285. return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
  286. return find_first_zero_bit(src, nbits) == nbits;
  287. }
  288. static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
  289. {
  290. if (small_const_nbits(nbits))
  291. return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
  292. return __bitmap_weight(src, nbits);
  293. }
  294. static __always_inline void bitmap_set(unsigned long *map, unsigned int start,
  295. unsigned int nbits)
  296. {
  297. if (__builtin_constant_p(nbits) && nbits == 1)
  298. __set_bit(start, map);
  299. else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) &&
  300. __builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
  301. memset((char *)map + start / 8, 0xff, nbits / 8);
  302. else
  303. __bitmap_set(map, start, nbits);
  304. }
  305. static __always_inline void bitmap_clear(unsigned long *map, unsigned int start,
  306. unsigned int nbits)
  307. {
  308. if (__builtin_constant_p(nbits) && nbits == 1)
  309. __clear_bit(start, map);
  310. else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) &&
  311. __builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8))
  312. memset((char *)map + start / 8, 0, nbits / 8);
  313. else
  314. __bitmap_clear(map, start, nbits);
  315. }
  316. static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  317. unsigned int shift, int nbits)
  318. {
  319. if (small_const_nbits(nbits))
  320. *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
  321. else
  322. __bitmap_shift_right(dst, src, shift, nbits);
  323. }
  324. static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  325. unsigned int shift, unsigned int nbits)
  326. {
  327. if (small_const_nbits(nbits))
  328. *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
  329. else
  330. __bitmap_shift_left(dst, src, shift, nbits);
  331. }
  332. static inline int bitmap_parse(const char *buf, unsigned int buflen,
  333. unsigned long *maskp, int nmaskbits)
  334. {
  335. return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
  336. }
  337. /**
  338. * BITMAP_FROM_U64() - Represent u64 value in the format suitable for bitmap.
  339. * @n: u64 value
  340. *
  341. * Linux bitmaps are internally arrays of unsigned longs, i.e. 32-bit
  342. * integers in 32-bit environment, and 64-bit integers in 64-bit one.
  343. *
  344. * There are four combinations of endianness and length of the word in linux
  345. * ABIs: LE64, BE64, LE32 and BE32.
  346. *
  347. * On 64-bit kernels 64-bit LE and BE numbers are naturally ordered in
  348. * bitmaps and therefore don't require any special handling.
  349. *
  350. * On 32-bit kernels 32-bit LE ABI orders lo word of 64-bit number in memory
  351. * prior to hi, and 32-bit BE orders hi word prior to lo. The bitmap on the
  352. * other hand is represented as an array of 32-bit words and the position of
  353. * bit N may therefore be calculated as: word #(N/32) and bit #(N%32) in that
  354. * word. For example, bit #42 is located at 10th position of 2nd word.
  355. * It matches 32-bit LE ABI, and we can simply let the compiler store 64-bit
  356. * values in memory as it usually does. But for BE we need to swap hi and lo
  357. * words manually.
  358. *
  359. * With all that, the macro BITMAP_FROM_U64() does explicit reordering of hi and
  360. * lo parts of u64. For LE32 it does nothing, and for BE environment it swaps
  361. * hi and lo words, as is expected by bitmap.
  362. */
  363. #if __BITS_PER_LONG == 64
  364. #define BITMAP_FROM_U64(n) (n)
  365. #else
  366. #define BITMAP_FROM_U64(n) ((unsigned long) ((u64)(n) & ULONG_MAX)), \
  367. ((unsigned long) ((u64)(n) >> 32))
  368. #endif
  369. /**
  370. * bitmap_from_u64 - Check and swap words within u64.
  371. * @mask: source bitmap
  372. * @dst: destination bitmap
  373. *
  374. * In 32-bit Big Endian kernel, when using ``(u32 *)(&val)[*]``
  375. * to read u64 mask, we will get the wrong word.
  376. * That is ``(u32 *)(&val)[0]`` gets the upper 32 bits,
  377. * but we expect the lower 32-bits of u64.
  378. */
  379. static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
  380. {
  381. dst[0] = mask & ULONG_MAX;
  382. if (sizeof(mask) > sizeof(unsigned long))
  383. dst[1] = mask >> 32;
  384. }
  385. #endif /* __ASSEMBLY__ */
  386. #endif /* __LINUX_BITMAP_H */