bitmap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Bitmap Module
  3. *
  4. * Stolen from linux/src/lib/bitmap.c
  5. *
  6. * Copyright (C) 2010 Corentin Chary
  7. *
  8. * This source code is licensed under the GNU General Public License,
  9. * Version 2.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "qemu/bitops.h"
  13. #include "qemu/bitmap.h"
  14. #include "qemu/atomic.h"
  15. /*
  16. * bitmaps provide an array of bits, implemented using an
  17. * array of unsigned longs. The number of valid bits in a
  18. * given bitmap does _not_ need to be an exact multiple of
  19. * BITS_PER_LONG.
  20. *
  21. * The possible unused bits in the last, partially used word
  22. * of a bitmap are 'don't care'. The implementation makes
  23. * no particular effort to keep them zero. It ensures that
  24. * their value will not affect the results of any operation.
  25. * The bitmap operations that return Boolean (bitmap_empty,
  26. * for example) or scalar (bitmap_weight, for example) results
  27. * carefully filter out these unused bits from impacting their
  28. * results.
  29. *
  30. * These operations actually hold to a slightly stronger rule:
  31. * if you don't input any bitmaps to these ops that have some
  32. * unused bits set, then they won't output any set unused bits
  33. * in output bitmaps.
  34. *
  35. * The byte ordering of bitmaps is more natural on little
  36. * endian architectures.
  37. */
  38. int slow_bitmap_empty(const unsigned long *bitmap, long bits)
  39. {
  40. long k, lim = bits/BITS_PER_LONG;
  41. for (k = 0; k < lim; ++k) {
  42. if (bitmap[k]) {
  43. return 0;
  44. }
  45. }
  46. if (bits % BITS_PER_LONG) {
  47. if (bitmap[k] & BITMAP_LAST_WORD_MASK(bits)) {
  48. return 0;
  49. }
  50. }
  51. return 1;
  52. }
  53. int slow_bitmap_full(const unsigned long *bitmap, long bits)
  54. {
  55. long k, lim = bits/BITS_PER_LONG;
  56. for (k = 0; k < lim; ++k) {
  57. if (~bitmap[k]) {
  58. return 0;
  59. }
  60. }
  61. if (bits % BITS_PER_LONG) {
  62. if (~bitmap[k] & BITMAP_LAST_WORD_MASK(bits)) {
  63. return 0;
  64. }
  65. }
  66. return 1;
  67. }
  68. int slow_bitmap_equal(const unsigned long *bitmap1,
  69. const unsigned long *bitmap2, long bits)
  70. {
  71. long k, lim = bits/BITS_PER_LONG;
  72. for (k = 0; k < lim; ++k) {
  73. if (bitmap1[k] != bitmap2[k]) {
  74. return 0;
  75. }
  76. }
  77. if (bits % BITS_PER_LONG) {
  78. if ((bitmap1[k] ^ bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) {
  79. return 0;
  80. }
  81. }
  82. return 1;
  83. }
  84. void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
  85. long bits)
  86. {
  87. long k, lim = bits/BITS_PER_LONG;
  88. for (k = 0; k < lim; ++k) {
  89. dst[k] = ~src[k];
  90. }
  91. if (bits % BITS_PER_LONG) {
  92. dst[k] = ~src[k] & BITMAP_LAST_WORD_MASK(bits);
  93. }
  94. }
  95. int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  96. const unsigned long *bitmap2, long bits)
  97. {
  98. long k;
  99. long nr = BITS_TO_LONGS(bits);
  100. unsigned long result = 0;
  101. for (k = 0; k < nr; k++) {
  102. result |= (dst[k] = bitmap1[k] & bitmap2[k]);
  103. }
  104. return result != 0;
  105. }
  106. void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  107. const unsigned long *bitmap2, long bits)
  108. {
  109. long k;
  110. long nr = BITS_TO_LONGS(bits);
  111. for (k = 0; k < nr; k++) {
  112. dst[k] = bitmap1[k] | bitmap2[k];
  113. }
  114. }
  115. void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
  116. const unsigned long *bitmap2, long bits)
  117. {
  118. long k;
  119. long nr = BITS_TO_LONGS(bits);
  120. for (k = 0; k < nr; k++) {
  121. dst[k] = bitmap1[k] ^ bitmap2[k];
  122. }
  123. }
  124. int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
  125. const unsigned long *bitmap2, long bits)
  126. {
  127. long k;
  128. long nr = BITS_TO_LONGS(bits);
  129. unsigned long result = 0;
  130. for (k = 0; k < nr; k++) {
  131. result |= (dst[k] = bitmap1[k] & ~bitmap2[k]);
  132. }
  133. return result != 0;
  134. }
  135. void bitmap_set(unsigned long *map, long start, long nr)
  136. {
  137. unsigned long *p = map + BIT_WORD(start);
  138. const long size = start + nr;
  139. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  140. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  141. assert(start >= 0 && nr >= 0);
  142. while (nr - bits_to_set >= 0) {
  143. *p |= mask_to_set;
  144. nr -= bits_to_set;
  145. bits_to_set = BITS_PER_LONG;
  146. mask_to_set = ~0UL;
  147. p++;
  148. }
  149. if (nr) {
  150. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  151. *p |= mask_to_set;
  152. }
  153. }
  154. void bitmap_set_atomic(unsigned long *map, long start, long nr)
  155. {
  156. unsigned long *p = map + BIT_WORD(start);
  157. const long size = start + nr;
  158. int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
  159. unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
  160. assert(start >= 0 && nr >= 0);
  161. /* First word */
  162. if (nr - bits_to_set > 0) {
  163. atomic_or(p, mask_to_set);
  164. nr -= bits_to_set;
  165. bits_to_set = BITS_PER_LONG;
  166. mask_to_set = ~0UL;
  167. p++;
  168. }
  169. /* Full words */
  170. if (bits_to_set == BITS_PER_LONG) {
  171. while (nr >= BITS_PER_LONG) {
  172. *p = ~0UL;
  173. nr -= BITS_PER_LONG;
  174. p++;
  175. }
  176. }
  177. /* Last word */
  178. if (nr) {
  179. mask_to_set &= BITMAP_LAST_WORD_MASK(size);
  180. atomic_or(p, mask_to_set);
  181. } else {
  182. /* If we avoided the full barrier in atomic_or(), issue a
  183. * barrier to account for the assignments in the while loop.
  184. */
  185. smp_mb();
  186. }
  187. }
  188. void bitmap_clear(unsigned long *map, long start, long nr)
  189. {
  190. unsigned long *p = map + BIT_WORD(start);
  191. const long size = start + nr;
  192. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  193. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  194. assert(start >= 0 && nr >= 0);
  195. while (nr - bits_to_clear >= 0) {
  196. *p &= ~mask_to_clear;
  197. nr -= bits_to_clear;
  198. bits_to_clear = BITS_PER_LONG;
  199. mask_to_clear = ~0UL;
  200. p++;
  201. }
  202. if (nr) {
  203. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  204. *p &= ~mask_to_clear;
  205. }
  206. }
  207. bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)
  208. {
  209. unsigned long *p = map + BIT_WORD(start);
  210. const long size = start + nr;
  211. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  212. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  213. unsigned long dirty = 0;
  214. unsigned long old_bits;
  215. assert(start >= 0 && nr >= 0);
  216. /* First word */
  217. if (nr - bits_to_clear > 0) {
  218. old_bits = atomic_fetch_and(p, ~mask_to_clear);
  219. dirty |= old_bits & mask_to_clear;
  220. nr -= bits_to_clear;
  221. bits_to_clear = BITS_PER_LONG;
  222. mask_to_clear = ~0UL;
  223. p++;
  224. }
  225. /* Full words */
  226. if (bits_to_clear == BITS_PER_LONG) {
  227. while (nr >= BITS_PER_LONG) {
  228. if (*p) {
  229. old_bits = atomic_xchg(p, 0);
  230. dirty |= old_bits;
  231. }
  232. nr -= BITS_PER_LONG;
  233. p++;
  234. }
  235. }
  236. /* Last word */
  237. if (nr) {
  238. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  239. old_bits = atomic_fetch_and(p, ~mask_to_clear);
  240. dirty |= old_bits & mask_to_clear;
  241. } else {
  242. if (!dirty) {
  243. smp_mb();
  244. }
  245. }
  246. return dirty != 0;
  247. }
  248. void bitmap_copy_and_clear_atomic(unsigned long *dst, unsigned long *src,
  249. long nr)
  250. {
  251. while (nr > 0) {
  252. *dst = atomic_xchg(src, 0);
  253. dst++;
  254. src++;
  255. nr -= BITS_PER_LONG;
  256. }
  257. }
  258. #define ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
  259. /**
  260. * bitmap_find_next_zero_area - find a contiguous aligned zero area
  261. * @map: The address to base the search on
  262. * @size: The bitmap size in bits
  263. * @start: The bitnumber to start searching at
  264. * @nr: The number of zeroed bits we're looking for
  265. * @align_mask: Alignment mask for zero area
  266. *
  267. * The @align_mask should be one less than a power of 2; the effect is that
  268. * the bit offset of all zero areas this function finds is multiples of that
  269. * power of 2. A @align_mask of 0 means no alignment is required.
  270. */
  271. unsigned long bitmap_find_next_zero_area(unsigned long *map,
  272. unsigned long size,
  273. unsigned long start,
  274. unsigned long nr,
  275. unsigned long align_mask)
  276. {
  277. unsigned long index, end, i;
  278. again:
  279. index = find_next_zero_bit(map, size, start);
  280. /* Align allocation */
  281. index = ALIGN_MASK(index, align_mask);
  282. end = index + nr;
  283. if (end > size) {
  284. return end;
  285. }
  286. i = find_next_bit(map, end, index);
  287. if (i < end) {
  288. start = i + 1;
  289. goto again;
  290. }
  291. return index;
  292. }
  293. int slow_bitmap_intersects(const unsigned long *bitmap1,
  294. const unsigned long *bitmap2, long bits)
  295. {
  296. long k, lim = bits/BITS_PER_LONG;
  297. for (k = 0; k < lim; ++k) {
  298. if (bitmap1[k] & bitmap2[k]) {
  299. return 1;
  300. }
  301. }
  302. if (bits % BITS_PER_LONG) {
  303. if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) {
  304. return 1;
  305. }
  306. }
  307. return 0;
  308. }
  309. long slow_bitmap_count_one(const unsigned long *bitmap, long nbits)
  310. {
  311. long k, lim = nbits / BITS_PER_LONG, result = 0;
  312. for (k = 0; k < lim; k++) {
  313. result += ctpopl(bitmap[k]);
  314. }
  315. if (nbits % BITS_PER_LONG) {
  316. result += ctpopl(bitmap[k] & BITMAP_LAST_WORD_MASK(nbits));
  317. }
  318. return result;
  319. }
  320. static void bitmap_to_from_le(unsigned long *dst,
  321. const unsigned long *src, long nbits)
  322. {
  323. long len = BITS_TO_LONGS(nbits);
  324. #ifdef HOST_WORDS_BIGENDIAN
  325. long index;
  326. for (index = 0; index < len; index++) {
  327. # if HOST_LONG_BITS == 64
  328. dst[index] = bswap64(src[index]);
  329. # else
  330. dst[index] = bswap32(src[index]);
  331. # endif
  332. }
  333. #else
  334. memcpy(dst, src, len * sizeof(unsigned long));
  335. #endif
  336. }
  337. void bitmap_from_le(unsigned long *dst, const unsigned long *src,
  338. long nbits)
  339. {
  340. bitmap_to_from_le(dst, src, nbits);
  341. }
  342. void bitmap_to_le(unsigned long *dst, const unsigned long *src,
  343. long nbits)
  344. {
  345. bitmap_to_from_le(dst, src, nbits);
  346. }
  347. /*
  348. * Copy "src" bitmap with a positive offset and put it into the "dst"
  349. * bitmap. The caller needs to make sure the bitmap size of "src"
  350. * is bigger than (shift + nbits).
  351. */
  352. void bitmap_copy_with_src_offset(unsigned long *dst, const unsigned long *src,
  353. unsigned long shift, unsigned long nbits)
  354. {
  355. unsigned long left_mask, right_mask, last_mask;
  356. /* Proper shift src pointer to the first word to copy from */
  357. src += BIT_WORD(shift);
  358. shift %= BITS_PER_LONG;
  359. if (!shift) {
  360. /* Fast path */
  361. bitmap_copy(dst, src, nbits);
  362. return;
  363. }
  364. right_mask = (1ul << shift) - 1;
  365. left_mask = ~right_mask;
  366. while (nbits >= BITS_PER_LONG) {
  367. *dst = (*src & left_mask) >> shift;
  368. *dst |= (src[1] & right_mask) << (BITS_PER_LONG - shift);
  369. dst++;
  370. src++;
  371. nbits -= BITS_PER_LONG;
  372. }
  373. if (nbits > BITS_PER_LONG - shift) {
  374. *dst = (*src & left_mask) >> shift;
  375. nbits -= BITS_PER_LONG - shift;
  376. last_mask = (1ul << nbits) - 1;
  377. *dst |= (src[1] & last_mask) << (BITS_PER_LONG - shift);
  378. } else if (nbits) {
  379. last_mask = (1ul << nbits) - 1;
  380. *dst = (*src >> shift) & last_mask;
  381. }
  382. }
  383. /*
  384. * Copy "src" bitmap into the "dst" bitmap with an offset in the
  385. * "dst". The caller needs to make sure the bitmap size of "dst" is
  386. * bigger than (shift + nbits).
  387. */
  388. void bitmap_copy_with_dst_offset(unsigned long *dst, const unsigned long *src,
  389. unsigned long shift, unsigned long nbits)
  390. {
  391. unsigned long left_mask, right_mask, last_mask;
  392. /* Proper shift dst pointer to the first word to copy from */
  393. dst += BIT_WORD(shift);
  394. shift %= BITS_PER_LONG;
  395. if (!shift) {
  396. /* Fast path */
  397. bitmap_copy(dst, src, nbits);
  398. return;
  399. }
  400. right_mask = (1ul << (BITS_PER_LONG - shift)) - 1;
  401. left_mask = ~right_mask;
  402. *dst &= (1ul << shift) - 1;
  403. while (nbits >= BITS_PER_LONG) {
  404. *dst |= (*src & right_mask) << shift;
  405. dst[1] = (*src & left_mask) >> (BITS_PER_LONG - shift);
  406. dst++;
  407. src++;
  408. nbits -= BITS_PER_LONG;
  409. }
  410. if (nbits > BITS_PER_LONG - shift) {
  411. *dst |= (*src & right_mask) << shift;
  412. nbits -= BITS_PER_LONG - shift;
  413. last_mask = ((1ul << nbits) - 1) << (BITS_PER_LONG - shift);
  414. dst[1] = (*src & last_mask) >> (BITS_PER_LONG - shift);
  415. } else if (nbits) {
  416. last_mask = (1ul << nbits) - 1;
  417. *dst |= (*src & last_mask) << shift;
  418. }
  419. }