xfs_bit.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_trans.h"
  22. #include "xfs_buf_item.h"
  23. /*
  24. * XFS bit manipulation routines, used in non-realtime code.
  25. */
  26. /*
  27. * Return whether bitmap is empty.
  28. * Size is number of words in the bitmap, which is padded to word boundary
  29. * Returns 1 for empty, 0 for non-empty.
  30. */
  31. int
  32. xfs_bitmap_empty(uint *map, uint size)
  33. {
  34. uint i;
  35. uint ret = 0;
  36. for (i = 0; i < size; i++) {
  37. ret |= map[i];
  38. }
  39. return (ret == 0);
  40. }
  41. /*
  42. * Count the number of contiguous bits set in the bitmap starting with bit
  43. * start_bit. Size is the size of the bitmap in words.
  44. */
  45. int
  46. xfs_contig_bits(uint *map, uint size, uint start_bit)
  47. {
  48. uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
  49. uint result = 0;
  50. uint tmp;
  51. size <<= BIT_TO_WORD_SHIFT;
  52. ASSERT(start_bit < size);
  53. size -= start_bit & ~(NBWORD - 1);
  54. start_bit &= (NBWORD - 1);
  55. if (start_bit) {
  56. tmp = *p++;
  57. /* set to one first offset bits prior to start */
  58. tmp |= (~0U >> (NBWORD-start_bit));
  59. if (tmp != ~0U)
  60. goto found;
  61. result += NBWORD;
  62. size -= NBWORD;
  63. }
  64. while (size) {
  65. if ((tmp = *p++) != ~0U)
  66. goto found;
  67. result += NBWORD;
  68. size -= NBWORD;
  69. }
  70. return result - start_bit;
  71. found:
  72. return result + ffz(tmp) - start_bit;
  73. }
  74. /*
  75. * This takes the bit number to start looking from and
  76. * returns the next set bit from there. It returns -1
  77. * if there are no more bits set or the start bit is
  78. * beyond the end of the bitmap.
  79. *
  80. * Size is the number of words, not bytes, in the bitmap.
  81. */
  82. int xfs_next_bit(uint *map, uint size, uint start_bit)
  83. {
  84. uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT);
  85. uint result = start_bit & ~(NBWORD - 1);
  86. uint tmp;
  87. size <<= BIT_TO_WORD_SHIFT;
  88. if (start_bit >= size)
  89. return -1;
  90. size -= result;
  91. start_bit &= (NBWORD - 1);
  92. if (start_bit) {
  93. tmp = *p++;
  94. /* set to zero first offset bits prior to start */
  95. tmp &= (~0U << start_bit);
  96. if (tmp != 0U)
  97. goto found;
  98. result += NBWORD;
  99. size -= NBWORD;
  100. }
  101. while (size) {
  102. if ((tmp = *p++) != 0U)
  103. goto found;
  104. result += NBWORD;
  105. size -= NBWORD;
  106. }
  107. return -1;
  108. found:
  109. return result + ffs(tmp) - 1;
  110. }