bitmap.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. * The available bitmap operations and their rough meaning in the
  22. * case that the bitmap is a single unsigned long are thus:
  23. *
  24. * Note that nbits should be always a compile time evaluable constant.
  25. * Otherwise many inlines will generate horrible code.
  26. *
  27. * bitmap_zero(dst, nbits) *dst = 0UL
  28. * bitmap_fill(dst, nbits) *dst = ~0UL
  29. * bitmap_copy(dst, src, nbits) *dst = *src
  30. * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
  31. * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
  32. * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
  33. * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
  34. * bitmap_complement(dst, src, nbits) *dst = ~(*src)
  35. * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
  36. * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
  37. * bitmap_subset(src1, src2, nbits) Is *src1 a subset of *src2?
  38. * bitmap_empty(src, nbits) Are all bits zero in *src?
  39. * bitmap_full(src, nbits) Are all bits set in *src?
  40. * bitmap_weight(src, nbits) Hamming Weight: number set bits
  41. * bitmap_set(dst, pos, nbits) Set specified bit area
  42. * bitmap_clear(dst, pos, nbits) Clear specified bit area
  43. * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
  44. * bitmap_find_next_zero_area_off(buf, len, pos, n, mask) as above
  45. * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n
  46. * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
  47. * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src)
  48. * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit)
  49. * bitmap_onto(dst, orig, relmap, nbits) *dst = orig relative to relmap
  50. * bitmap_fold(dst, orig, sz, nbits) dst bits = orig bits mod sz
  51. * bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf
  52. * bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
  53. * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from kernel buf
  54. * bitmap_parselist_user(buf, dst, nbits) Parse bitmap dst from user buf
  55. * bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region
  56. * bitmap_release_region(bitmap, pos, order) Free specified bit region
  57. * bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region
  58. * bitmap_from_u32array(dst, nbits, buf, nwords) *dst = *buf (nwords 32b words)
  59. * bitmap_to_u32array(buf, nwords, src, nbits) *buf = *dst (nwords 32b words)
  60. */
  61. /*
  62. * Also the following operations in asm/bitops.h apply to bitmaps.
  63. *
  64. * set_bit(bit, addr) *addr |= bit
  65. * clear_bit(bit, addr) *addr &= ~bit
  66. * change_bit(bit, addr) *addr ^= bit
  67. * test_bit(bit, addr) Is bit set in *addr?
  68. * test_and_set_bit(bit, addr) Set bit and return old value
  69. * test_and_clear_bit(bit, addr) Clear bit and return old value
  70. * test_and_change_bit(bit, addr) Change bit and return old value
  71. * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
  72. * find_first_bit(addr, nbits) Position first set bit in *addr
  73. * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
  74. * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
  75. */
  76. /*
  77. * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
  78. * to declare an array named 'name' of just enough unsigned longs to
  79. * contain all bit positions from 0 to 'bits' - 1.
  80. */
  81. /*
  82. * lib/bitmap.c provides these functions:
  83. */
  84. extern int __bitmap_empty(const unsigned long *bitmap, unsigned int nbits);
  85. extern int __bitmap_full(const unsigned long *bitmap, unsigned int nbits);
  86. extern int __bitmap_equal(const unsigned long *bitmap1,
  87. const unsigned long *bitmap2, unsigned int nbits);
  88. extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
  89. unsigned int nbits);
  90. extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  91. unsigned int shift, unsigned int nbits);
  92. extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  93. unsigned int shift, unsigned int nbits);
  94. extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  95. const unsigned long *bitmap2, unsigned int nbits);
  96. extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  97. const unsigned long *bitmap2, unsigned int nbits);
  98. extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  99. const unsigned long *bitmap2, unsigned int nbits);
  100. extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  101. const unsigned long *bitmap2, unsigned int nbits);
  102. extern int __bitmap_intersects(const unsigned long *bitmap1,
  103. const unsigned long *bitmap2, unsigned int nbits);
  104. extern int __bitmap_subset(const unsigned long *bitmap1,
  105. const unsigned long *bitmap2, unsigned int nbits);
  106. extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
  107. extern void bitmap_set(unsigned long *map, unsigned int start, int len);
  108. extern void bitmap_clear(unsigned long *map, unsigned int start, int len);
  109. extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
  110. unsigned long size,
  111. unsigned long start,
  112. unsigned int nr,
  113. unsigned long align_mask,
  114. unsigned long align_offset);
  115. /**
  116. * bitmap_find_next_zero_area - find a contiguous aligned zero area
  117. * @map: The address to base the search on
  118. * @size: The bitmap size in bits
  119. * @start: The bitnumber to start searching at
  120. * @nr: The number of zeroed bits we're looking for
  121. * @align_mask: Alignment mask for zero area
  122. *
  123. * The @align_mask should be one less than a power of 2; the effect is that
  124. * the bit offset of all zero areas this function finds is multiples of that
  125. * power of 2. A @align_mask of 0 means no alignment is required.
  126. */
  127. static inline unsigned long
  128. bitmap_find_next_zero_area(unsigned long *map,
  129. unsigned long size,
  130. unsigned long start,
  131. unsigned int nr,
  132. unsigned long align_mask)
  133. {
  134. return bitmap_find_next_zero_area_off(map, size, start, nr,
  135. align_mask, 0);
  136. }
  137. extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
  138. unsigned long *dst, int nbits);
  139. extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
  140. unsigned long *dst, int nbits);
  141. extern int bitmap_parselist(const char *buf, unsigned long *maskp,
  142. int nmaskbits);
  143. extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
  144. unsigned long *dst, int nbits);
  145. extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
  146. const unsigned long *old, const unsigned long *new, unsigned int nbits);
  147. extern int bitmap_bitremap(int oldbit,
  148. const unsigned long *old, const unsigned long *new, int bits);
  149. extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
  150. const unsigned long *relmap, unsigned int bits);
  151. extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
  152. unsigned int sz, unsigned int nbits);
  153. extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
  154. extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
  155. extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
  156. extern unsigned int bitmap_from_u32array(unsigned long *bitmap,
  157. unsigned int nbits,
  158. const u32 *buf,
  159. unsigned int nwords);
  160. extern unsigned int bitmap_to_u32array(u32 *buf,
  161. unsigned int nwords,
  162. const unsigned long *bitmap,
  163. unsigned int nbits);
  164. #ifdef __BIG_ENDIAN
  165. extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
  166. #else
  167. #define bitmap_copy_le bitmap_copy
  168. #endif
  169. extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
  170. extern int bitmap_print_to_pagebuf(bool list, char *buf,
  171. const unsigned long *maskp, int nmaskbits);
  172. #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
  173. #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1)))
  174. #define small_const_nbits(nbits) \
  175. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
  176. static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
  177. {
  178. if (small_const_nbits(nbits))
  179. *dst = 0UL;
  180. else {
  181. unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  182. memset(dst, 0, len);
  183. }
  184. }
  185. static inline void bitmap_fill(unsigned long *dst, unsigned int nbits)
  186. {
  187. unsigned int nlongs = BITS_TO_LONGS(nbits);
  188. if (!small_const_nbits(nbits)) {
  189. unsigned int len = (nlongs - 1) * sizeof(unsigned long);
  190. memset(dst, 0xff, len);
  191. }
  192. dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
  193. }
  194. static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
  195. unsigned int nbits)
  196. {
  197. if (small_const_nbits(nbits))
  198. *dst = *src;
  199. else {
  200. unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  201. memcpy(dst, src, len);
  202. }
  203. }
  204. static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
  205. const unsigned long *src2, unsigned int nbits)
  206. {
  207. if (small_const_nbits(nbits))
  208. return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  209. return __bitmap_and(dst, src1, src2, nbits);
  210. }
  211. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  212. const unsigned long *src2, unsigned int nbits)
  213. {
  214. if (small_const_nbits(nbits))
  215. *dst = *src1 | *src2;
  216. else
  217. __bitmap_or(dst, src1, src2, nbits);
  218. }
  219. static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
  220. const unsigned long *src2, unsigned int nbits)
  221. {
  222. if (small_const_nbits(nbits))
  223. *dst = *src1 ^ *src2;
  224. else
  225. __bitmap_xor(dst, src1, src2, nbits);
  226. }
  227. static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
  228. const unsigned long *src2, unsigned int nbits)
  229. {
  230. if (small_const_nbits(nbits))
  231. return (*dst = *src1 & ~(*src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  232. return __bitmap_andnot(dst, src1, src2, nbits);
  233. }
  234. static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
  235. unsigned int nbits)
  236. {
  237. if (small_const_nbits(nbits))
  238. *dst = ~(*src);
  239. else
  240. __bitmap_complement(dst, src, nbits);
  241. }
  242. static inline int bitmap_equal(const unsigned long *src1,
  243. const unsigned long *src2, unsigned int nbits)
  244. {
  245. if (small_const_nbits(nbits))
  246. return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
  247. #ifdef CONFIG_S390
  248. if (__builtin_constant_p(nbits) && (nbits % BITS_PER_LONG) == 0)
  249. return !memcmp(src1, src2, nbits / 8);
  250. #endif
  251. return __bitmap_equal(src1, src2, nbits);
  252. }
  253. static inline int bitmap_intersects(const unsigned long *src1,
  254. const unsigned long *src2, unsigned int nbits)
  255. {
  256. if (small_const_nbits(nbits))
  257. return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  258. else
  259. return __bitmap_intersects(src1, src2, nbits);
  260. }
  261. static inline int bitmap_subset(const unsigned long *src1,
  262. const unsigned long *src2, unsigned int nbits)
  263. {
  264. if (small_const_nbits(nbits))
  265. return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
  266. else
  267. return __bitmap_subset(src1, src2, nbits);
  268. }
  269. static inline int bitmap_empty(const unsigned long *src, unsigned nbits)
  270. {
  271. if (small_const_nbits(nbits))
  272. return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
  273. return find_first_bit(src, nbits) == nbits;
  274. }
  275. static inline int bitmap_full(const unsigned long *src, unsigned int nbits)
  276. {
  277. if (small_const_nbits(nbits))
  278. return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
  279. return find_first_zero_bit(src, nbits) == nbits;
  280. }
  281. static __always_inline int bitmap_weight(const unsigned long *src, unsigned int nbits)
  282. {
  283. if (small_const_nbits(nbits))
  284. return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
  285. return __bitmap_weight(src, nbits);
  286. }
  287. static inline void bitmap_shift_right(unsigned long *dst, const unsigned long *src,
  288. unsigned int shift, int nbits)
  289. {
  290. if (small_const_nbits(nbits))
  291. *dst = (*src & BITMAP_LAST_WORD_MASK(nbits)) >> shift;
  292. else
  293. __bitmap_shift_right(dst, src, shift, nbits);
  294. }
  295. static inline void bitmap_shift_left(unsigned long *dst, const unsigned long *src,
  296. unsigned int shift, unsigned int nbits)
  297. {
  298. if (small_const_nbits(nbits))
  299. *dst = (*src << shift) & BITMAP_LAST_WORD_MASK(nbits);
  300. else
  301. __bitmap_shift_left(dst, src, shift, nbits);
  302. }
  303. static inline int bitmap_parse(const char *buf, unsigned int buflen,
  304. unsigned long *maskp, int nmaskbits)
  305. {
  306. return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
  307. }
  308. /*
  309. * bitmap_from_u64 - Check and swap words within u64.
  310. * @mask: source bitmap
  311. * @dst: destination bitmap
  312. *
  313. * In 32-bit Big Endian kernel, when using (u32 *)(&val)[*]
  314. * to read u64 mask, we will get the wrong word.
  315. * That is "(u32 *)(&val)[0]" gets the upper 32 bits,
  316. * but we expect the lower 32-bits of u64.
  317. */
  318. static inline void bitmap_from_u64(unsigned long *dst, u64 mask)
  319. {
  320. dst[0] = mask & ULONG_MAX;
  321. if (sizeof(mask) > sizeof(unsigned long))
  322. dst[1] = mask >> 32;
  323. }
  324. #endif /* __ASSEMBLY__ */
  325. #endif /* __LINUX_BITMAP_H */