bitmap.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_shift_right(dst, src, n, nbits) *dst = *src >> n
  42. * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n
  43. * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src)
  44. * bitmap_bitremap(oldbit, old, new, nbits) newbit = map(old, new)(oldbit)
  45. * bitmap_scnprintf(buf, len, src, nbits) Print bitmap src to buf
  46. * bitmap_parse(buf, buflen, dst, nbits) Parse bitmap dst from kernel buf
  47. * bitmap_parse_user(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf
  48. * bitmap_scnlistprintf(buf, len, src, nbits) Print bitmap src as list to buf
  49. * bitmap_parselist(buf, dst, nbits) Parse bitmap dst from list
  50. * bitmap_find_free_region(bitmap, bits, order) Find and allocate bit region
  51. * bitmap_release_region(bitmap, pos, order) Free specified bit region
  52. * bitmap_allocate_region(bitmap, pos, order) Allocate specified bit region
  53. */
  54. /*
  55. * Also the following operations in asm/bitops.h apply to bitmaps.
  56. *
  57. * set_bit(bit, addr) *addr |= bit
  58. * clear_bit(bit, addr) *addr &= ~bit
  59. * change_bit(bit, addr) *addr ^= bit
  60. * test_bit(bit, addr) Is bit set in *addr?
  61. * test_and_set_bit(bit, addr) Set bit and return old value
  62. * test_and_clear_bit(bit, addr) Clear bit and return old value
  63. * test_and_change_bit(bit, addr) Change bit and return old value
  64. * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
  65. * find_first_bit(addr, nbits) Position first set bit in *addr
  66. * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
  67. * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
  68. */
  69. /*
  70. * The DECLARE_BITMAP(name,bits) macro, in linux/types.h, can be used
  71. * to declare an array named 'name' of just enough unsigned longs to
  72. * contain all bit positions from 0 to 'bits' - 1.
  73. */
  74. /*
  75. * lib/bitmap.c provides these functions:
  76. */
  77. extern int __bitmap_empty(const unsigned long *bitmap, int bits);
  78. extern int __bitmap_full(const unsigned long *bitmap, int bits);
  79. extern int __bitmap_equal(const unsigned long *bitmap1,
  80. const unsigned long *bitmap2, int bits);
  81. extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
  82. int bits);
  83. extern void __bitmap_shift_right(unsigned long *dst,
  84. const unsigned long *src, int shift, int bits);
  85. extern void __bitmap_shift_left(unsigned long *dst,
  86. const unsigned long *src, int shift, int bits);
  87. extern void __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  88. const unsigned long *bitmap2, int bits);
  89. extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  90. const unsigned long *bitmap2, int bits);
  91. extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  92. const unsigned long *bitmap2, int bits);
  93. extern void __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  94. const unsigned long *bitmap2, int bits);
  95. extern int __bitmap_intersects(const unsigned long *bitmap1,
  96. const unsigned long *bitmap2, int bits);
  97. extern int __bitmap_subset(const unsigned long *bitmap1,
  98. const unsigned long *bitmap2, int bits);
  99. extern int __bitmap_weight(const unsigned long *bitmap, int bits);
  100. extern int bitmap_scnprintf(char *buf, unsigned int len,
  101. const unsigned long *src, int nbits);
  102. extern int bitmap_scnprintf_len(unsigned int len);
  103. extern int __bitmap_parse(const char *buf, unsigned int buflen, int is_user,
  104. unsigned long *dst, int nbits);
  105. extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
  106. unsigned long *dst, int nbits);
  107. extern int bitmap_scnlistprintf(char *buf, unsigned int len,
  108. const unsigned long *src, int nbits);
  109. extern int bitmap_parselist(const char *buf, unsigned long *maskp,
  110. int nmaskbits);
  111. extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
  112. const unsigned long *old, const unsigned long *new, int bits);
  113. extern int bitmap_bitremap(int oldbit,
  114. const unsigned long *old, const unsigned long *new, int bits);
  115. extern int bitmap_find_free_region(unsigned long *bitmap, int bits, int order);
  116. extern void bitmap_release_region(unsigned long *bitmap, int pos, int order);
  117. extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
  118. #define BITMAP_LAST_WORD_MASK(nbits) \
  119. ( \
  120. ((nbits) % BITS_PER_LONG) ? \
  121. (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
  122. )
  123. static inline void bitmap_zero(unsigned long *dst, int nbits)
  124. {
  125. if (nbits <= BITS_PER_LONG)
  126. *dst = 0UL;
  127. else {
  128. int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  129. memset(dst, 0, len);
  130. }
  131. }
  132. static inline void bitmap_fill(unsigned long *dst, int nbits)
  133. {
  134. size_t nlongs = BITS_TO_LONGS(nbits);
  135. if (nlongs > 1) {
  136. int len = (nlongs - 1) * sizeof(unsigned long);
  137. memset(dst, 0xff, len);
  138. }
  139. dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
  140. }
  141. static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
  142. int nbits)
  143. {
  144. if (nbits <= BITS_PER_LONG)
  145. *dst = *src;
  146. else {
  147. int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  148. memcpy(dst, src, len);
  149. }
  150. }
  151. static inline void bitmap_and(unsigned long *dst, const unsigned long *src1,
  152. const unsigned long *src2, int nbits)
  153. {
  154. if (nbits <= BITS_PER_LONG)
  155. *dst = *src1 & *src2;
  156. else
  157. __bitmap_and(dst, src1, src2, nbits);
  158. }
  159. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  160. const unsigned long *src2, int nbits)
  161. {
  162. if (nbits <= BITS_PER_LONG)
  163. *dst = *src1 | *src2;
  164. else
  165. __bitmap_or(dst, src1, src2, nbits);
  166. }
  167. static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
  168. const unsigned long *src2, int nbits)
  169. {
  170. if (nbits <= BITS_PER_LONG)
  171. *dst = *src1 ^ *src2;
  172. else
  173. __bitmap_xor(dst, src1, src2, nbits);
  174. }
  175. static inline void bitmap_andnot(unsigned long *dst, const unsigned long *src1,
  176. const unsigned long *src2, int nbits)
  177. {
  178. if (nbits <= BITS_PER_LONG)
  179. *dst = *src1 & ~(*src2);
  180. else
  181. __bitmap_andnot(dst, src1, src2, nbits);
  182. }
  183. static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
  184. int nbits)
  185. {
  186. if (nbits <= BITS_PER_LONG)
  187. *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
  188. else
  189. __bitmap_complement(dst, src, nbits);
  190. }
  191. static inline int bitmap_equal(const unsigned long *src1,
  192. const unsigned long *src2, int nbits)
  193. {
  194. if (nbits <= BITS_PER_LONG)
  195. return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
  196. else
  197. return __bitmap_equal(src1, src2, nbits);
  198. }
  199. static inline int bitmap_intersects(const unsigned long *src1,
  200. const unsigned long *src2, int nbits)
  201. {
  202. if (nbits <= BITS_PER_LONG)
  203. return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  204. else
  205. return __bitmap_intersects(src1, src2, nbits);
  206. }
  207. static inline int bitmap_subset(const unsigned long *src1,
  208. const unsigned long *src2, int nbits)
  209. {
  210. if (nbits <= BITS_PER_LONG)
  211. return ! ((*src1 & ~(*src2)) & BITMAP_LAST_WORD_MASK(nbits));
  212. else
  213. return __bitmap_subset(src1, src2, nbits);
  214. }
  215. static inline int bitmap_empty(const unsigned long *src, int nbits)
  216. {
  217. if (nbits <= BITS_PER_LONG)
  218. return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
  219. else
  220. return __bitmap_empty(src, nbits);
  221. }
  222. static inline int bitmap_full(const unsigned long *src, int nbits)
  223. {
  224. if (nbits <= BITS_PER_LONG)
  225. return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
  226. else
  227. return __bitmap_full(src, nbits);
  228. }
  229. static inline int bitmap_weight(const unsigned long *src, int nbits)
  230. {
  231. if (nbits <= BITS_PER_LONG)
  232. return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
  233. return __bitmap_weight(src, nbits);
  234. }
  235. static inline void bitmap_shift_right(unsigned long *dst,
  236. const unsigned long *src, int n, int nbits)
  237. {
  238. if (nbits <= BITS_PER_LONG)
  239. *dst = *src >> n;
  240. else
  241. __bitmap_shift_right(dst, src, n, nbits);
  242. }
  243. static inline void bitmap_shift_left(unsigned long *dst,
  244. const unsigned long *src, int n, int nbits)
  245. {
  246. if (nbits <= BITS_PER_LONG)
  247. *dst = (*src << n) & BITMAP_LAST_WORD_MASK(nbits);
  248. else
  249. __bitmap_shift_left(dst, src, n, nbits);
  250. }
  251. static inline int bitmap_parse(const char *buf, unsigned int buflen,
  252. unsigned long *maskp, int nmaskbits)
  253. {
  254. return __bitmap_parse(buf, buflen, 0, maskp, nmaskbits);
  255. }
  256. #endif /* __ASSEMBLY__ */
  257. #endif /* __LINUX_BITMAP_H */