bitmap.h 11 KB

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