2
0

bitmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. qatomic_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. qatomic_or(p, mask_to_set);
  181. } else {
  182. /* If we avoided the full barrier in qatomic_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(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. bool dirty = false;
  214. assert(start >= 0 && nr >= 0);
  215. /* First word */
  216. if (nr - bits_to_clear > 0) {
  217. if ((*p) & mask_to_clear) {
  218. dirty = true;
  219. }
  220. *p &= ~mask_to_clear;
  221. nr -= bits_to_clear;
  222. bits_to_clear = BITS_PER_LONG;
  223. p++;
  224. }
  225. /* Full words */
  226. if (bits_to_clear == BITS_PER_LONG) {
  227. while (nr >= BITS_PER_LONG) {
  228. if (*p) {
  229. dirty = true;
  230. *p = 0;
  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. if ((*p) & mask_to_clear) {
  240. dirty = true;
  241. }
  242. *p &= ~mask_to_clear;
  243. }
  244. return dirty;
  245. }
  246. bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr)
  247. {
  248. unsigned long *p = map + BIT_WORD(start);
  249. const long size = start + nr;
  250. int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
  251. unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
  252. unsigned long dirty = 0;
  253. unsigned long old_bits;
  254. assert(start >= 0 && nr >= 0);
  255. /* First word */
  256. if (nr - bits_to_clear > 0) {
  257. old_bits = qatomic_fetch_and(p, ~mask_to_clear);
  258. dirty |= old_bits & mask_to_clear;
  259. nr -= bits_to_clear;
  260. bits_to_clear = BITS_PER_LONG;
  261. mask_to_clear = ~0UL;
  262. p++;
  263. }
  264. /* Full words */
  265. if (bits_to_clear == BITS_PER_LONG) {
  266. while (nr >= BITS_PER_LONG) {
  267. if (*p) {
  268. old_bits = qatomic_xchg(p, 0);
  269. dirty |= old_bits;
  270. }
  271. nr -= BITS_PER_LONG;
  272. p++;
  273. }
  274. }
  275. /* Last word */
  276. if (nr) {
  277. mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
  278. old_bits = qatomic_fetch_and(p, ~mask_to_clear);
  279. dirty |= old_bits & mask_to_clear;
  280. } else {
  281. if (!dirty) {
  282. smp_mb();
  283. }
  284. }
  285. return dirty != 0;
  286. }
  287. void bitmap_copy_and_clear_atomic(unsigned long *dst, unsigned long *src,
  288. long nr)
  289. {
  290. while (nr > 0) {
  291. *dst = qatomic_xchg(src, 0);
  292. dst++;
  293. src++;
  294. nr -= BITS_PER_LONG;
  295. }
  296. }
  297. #define ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
  298. /**
  299. * bitmap_find_next_zero_area - find a contiguous aligned zero area
  300. * @map: The address to base the search on
  301. * @size: The bitmap size in bits
  302. * @start: The bitnumber to start searching at
  303. * @nr: The number of zeroed bits we're looking for
  304. * @align_mask: Alignment mask for zero area
  305. *
  306. * The @align_mask should be one less than a power of 2; the effect is that
  307. * the bit offset of all zero areas this function finds is multiples of that
  308. * power of 2. A @align_mask of 0 means no alignment is required.
  309. */
  310. unsigned long bitmap_find_next_zero_area(unsigned long *map,
  311. unsigned long size,
  312. unsigned long start,
  313. unsigned long nr,
  314. unsigned long align_mask)
  315. {
  316. unsigned long index, end, i;
  317. again:
  318. index = find_next_zero_bit(map, size, start);
  319. /* Align allocation */
  320. index = ALIGN_MASK(index, align_mask);
  321. end = index + nr;
  322. if (end > size) {
  323. return end;
  324. }
  325. i = find_next_bit(map, end, index);
  326. if (i < end) {
  327. start = i + 1;
  328. goto again;
  329. }
  330. return index;
  331. }
  332. int slow_bitmap_intersects(const unsigned long *bitmap1,
  333. const unsigned long *bitmap2, long bits)
  334. {
  335. long k, lim = bits/BITS_PER_LONG;
  336. for (k = 0; k < lim; ++k) {
  337. if (bitmap1[k] & bitmap2[k]) {
  338. return 1;
  339. }
  340. }
  341. if (bits % BITS_PER_LONG) {
  342. if ((bitmap1[k] & bitmap2[k]) & BITMAP_LAST_WORD_MASK(bits)) {
  343. return 1;
  344. }
  345. }
  346. return 0;
  347. }
  348. long slow_bitmap_count_one(const unsigned long *bitmap, long nbits)
  349. {
  350. long k, lim = nbits / BITS_PER_LONG, result = 0;
  351. for (k = 0; k < lim; k++) {
  352. result += ctpopl(bitmap[k]);
  353. }
  354. if (nbits % BITS_PER_LONG) {
  355. result += ctpopl(bitmap[k] & BITMAP_LAST_WORD_MASK(nbits));
  356. }
  357. return result;
  358. }
  359. static void bitmap_to_from_le(unsigned long *dst,
  360. const unsigned long *src, long nbits)
  361. {
  362. long len = BITS_TO_LONGS(nbits);
  363. #if HOST_BIG_ENDIAN
  364. long index;
  365. for (index = 0; index < len; index++) {
  366. # if HOST_LONG_BITS == 64
  367. dst[index] = bswap64(src[index]);
  368. # else
  369. dst[index] = bswap32(src[index]);
  370. # endif
  371. }
  372. #else
  373. memcpy(dst, src, len * sizeof(unsigned long));
  374. #endif
  375. }
  376. void bitmap_from_le(unsigned long *dst, const unsigned long *src,
  377. long nbits)
  378. {
  379. bitmap_to_from_le(dst, src, nbits);
  380. }
  381. void bitmap_to_le(unsigned long *dst, const unsigned long *src,
  382. long nbits)
  383. {
  384. bitmap_to_from_le(dst, src, nbits);
  385. }
  386. /*
  387. * Copy "src" bitmap with a positive offset and put it into the "dst"
  388. * bitmap. The caller needs to make sure the bitmap size of "src"
  389. * is bigger than (shift + nbits).
  390. */
  391. void bitmap_copy_with_src_offset(unsigned long *dst, const unsigned long *src,
  392. unsigned long shift, unsigned long nbits)
  393. {
  394. unsigned long left_mask, right_mask, last_mask;
  395. /* Proper shift src pointer to the first word to copy from */
  396. src += BIT_WORD(shift);
  397. shift %= BITS_PER_LONG;
  398. if (!shift) {
  399. /* Fast path */
  400. bitmap_copy(dst, src, nbits);
  401. return;
  402. }
  403. right_mask = (1ul << shift) - 1;
  404. left_mask = ~right_mask;
  405. while (nbits >= BITS_PER_LONG) {
  406. *dst = (*src & left_mask) >> shift;
  407. *dst |= (src[1] & right_mask) << (BITS_PER_LONG - shift);
  408. dst++;
  409. src++;
  410. nbits -= BITS_PER_LONG;
  411. }
  412. if (nbits > BITS_PER_LONG - shift) {
  413. *dst = (*src & left_mask) >> shift;
  414. nbits -= BITS_PER_LONG - shift;
  415. last_mask = (1ul << nbits) - 1;
  416. *dst |= (src[1] & last_mask) << (BITS_PER_LONG - shift);
  417. } else if (nbits) {
  418. last_mask = (1ul << nbits) - 1;
  419. *dst = (*src >> shift) & last_mask;
  420. }
  421. }
  422. /*
  423. * Copy "src" bitmap into the "dst" bitmap with an offset in the
  424. * "dst". The caller needs to make sure the bitmap size of "dst" is
  425. * bigger than (shift + nbits).
  426. */
  427. void bitmap_copy_with_dst_offset(unsigned long *dst, const unsigned long *src,
  428. unsigned long shift, unsigned long nbits)
  429. {
  430. unsigned long left_mask, right_mask, last_mask;
  431. /* Proper shift dst pointer to the first word to copy from */
  432. dst += BIT_WORD(shift);
  433. shift %= BITS_PER_LONG;
  434. if (!shift) {
  435. /* Fast path */
  436. bitmap_copy(dst, src, nbits);
  437. return;
  438. }
  439. right_mask = (1ul << (BITS_PER_LONG - shift)) - 1;
  440. left_mask = ~right_mask;
  441. *dst &= (1ul << shift) - 1;
  442. while (nbits >= BITS_PER_LONG) {
  443. *dst |= (*src & right_mask) << shift;
  444. dst[1] = (*src & left_mask) >> (BITS_PER_LONG - shift);
  445. dst++;
  446. src++;
  447. nbits -= BITS_PER_LONG;
  448. }
  449. if (nbits > BITS_PER_LONG - shift) {
  450. *dst |= (*src & right_mask) << shift;
  451. nbits -= BITS_PER_LONG - shift;
  452. last_mask = ((1ul << nbits) - 1) << (BITS_PER_LONG - shift);
  453. dst[1] = (*src & last_mask) >> (BITS_PER_LONG - shift);
  454. } else if (nbits) {
  455. last_mask = (1ul << nbits) - 1;
  456. *dst |= (*src & last_mask) << shift;
  457. }
  458. }