bitmap.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2013-2015 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will 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, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Upstream site with license notes :
  18. * http://git.sceen.net/rbraun/librbraun.git/
  19. *
  20. *
  21. * Arbitrary-length bit arrays.
  22. *
  23. * Most functions do not check whether the given parameters are valid. This
  24. * is the responsibility of the caller.
  25. */
  26. #ifndef KERN_BITMAP_H
  27. #define KERN_BITMAP_H
  28. #include <limits.h>
  29. #include <string.h>
  30. #include <kern/atomic.h>
  31. #include <kern/bitmap_i.h>
  32. #define BITMAP_DECLARE(name, nr_bits) unsigned long name[BITMAP_LONGS(nr_bits)]
  33. int bitmap_cmp(const unsigned long *a, const unsigned long *b, int nr_bits);
  34. static inline void
  35. bitmap_zero(unsigned long *bm, int nr_bits)
  36. {
  37. int n;
  38. n = BITMAP_LONGS(nr_bits);
  39. memset(bm, 0, n * sizeof(unsigned long));
  40. }
  41. static inline void
  42. bitmap_fill(unsigned long *bm, int nr_bits)
  43. {
  44. int n;
  45. n = BITMAP_LONGS(nr_bits);
  46. memset(bm, 0xff, n * sizeof(unsigned long));
  47. }
  48. static inline void
  49. bitmap_copy(unsigned long *dest, const unsigned long *src, int nr_bits)
  50. {
  51. int n;
  52. n = BITMAP_LONGS(nr_bits);
  53. memcpy(dest, src, n * sizeof(unsigned long));
  54. }
  55. static inline void
  56. bitmap_set(unsigned long *bm, int bit)
  57. {
  58. if (bit >= LONG_BIT) {
  59. bitmap_lookup(&bm, &bit);
  60. }
  61. *bm |= bitmap_mask(bit);
  62. }
  63. static inline void
  64. bitmap_set_atomic(unsigned long *bm, int bit)
  65. {
  66. if (bit >= LONG_BIT) {
  67. bitmap_lookup(&bm, &bit);
  68. }
  69. atomic_or(bm, bitmap_mask(bit), ATOMIC_RELEASE);
  70. }
  71. static inline void
  72. bitmap_clear(unsigned long *bm, int bit)
  73. {
  74. if (bit >= LONG_BIT) {
  75. bitmap_lookup(&bm, &bit);
  76. }
  77. *bm &= ~bitmap_mask(bit);
  78. }
  79. static inline void
  80. bitmap_clear_atomic(unsigned long *bm, int bit)
  81. {
  82. if (bit >= LONG_BIT) {
  83. bitmap_lookup(&bm, &bit);
  84. }
  85. atomic_and(bm, ~bitmap_mask(bit), ATOMIC_RELEASE);
  86. }
  87. static inline int
  88. bitmap_test(const unsigned long *bm, int bit)
  89. {
  90. if (bit >= LONG_BIT) {
  91. bitmap_lookup(&bm, &bit);
  92. }
  93. return ((*bm & bitmap_mask(bit)) != 0);
  94. }
  95. static inline int
  96. bitmap_test_atomic(const unsigned long *bm, int bit)
  97. {
  98. if (bit >= LONG_BIT) {
  99. bitmap_lookup(&bm, &bit);
  100. }
  101. return ((atomic_load(bm, ATOMIC_ACQUIRE) & bitmap_mask(bit)) != 0);
  102. }
  103. static inline void
  104. bitmap_and(unsigned long *a, const unsigned long *b, int nr_bits)
  105. {
  106. int i, n;
  107. n = BITMAP_LONGS(nr_bits);
  108. for (i = 0; i < n; i++) {
  109. a[i] &= b[i];
  110. }
  111. }
  112. static inline void
  113. bitmap_or(unsigned long *a, const unsigned long *b, int nr_bits)
  114. {
  115. int i, n;
  116. n = BITMAP_LONGS(nr_bits);
  117. for (i = 0; i < n; i++) {
  118. a[i] |= b[i];
  119. }
  120. }
  121. static inline void
  122. bitmap_xor(unsigned long *a, const unsigned long *b, int nr_bits)
  123. {
  124. int i, n;
  125. n = BITMAP_LONGS(nr_bits);
  126. for (i = 0; i < n; i++) {
  127. a[i] ^= b[i];
  128. }
  129. }
  130. static inline int
  131. bitmap_find_next(const unsigned long *bm, int nr_bits, int bit)
  132. {
  133. return bitmap_find_next_bit(bm, nr_bits, bit, 0);
  134. }
  135. static inline int
  136. bitmap_find_first(const unsigned long *bm, int nr_bits)
  137. {
  138. return bitmap_find_next(bm, nr_bits, 0);
  139. }
  140. static inline int
  141. bitmap_find_next_zero(const unsigned long *bm, int nr_bits, int bit)
  142. {
  143. return bitmap_find_next_bit(bm, nr_bits, bit, 1);
  144. }
  145. static inline int
  146. bitmap_find_first_zero(const unsigned long *bm, int nr_bits)
  147. {
  148. return bitmap_find_next_zero(bm, nr_bits, 0);
  149. }
  150. static inline bool
  151. bitmap_intersects(const unsigned long *a, const unsigned long *b, int nr_bits)
  152. {
  153. int i, n;
  154. n = BITMAP_LONGS(nr_bits);
  155. for (i = 0; i < n; i++) {
  156. if (a[i] & b[i]) {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. #define bitmap_for_each(bm, nr_bits, bit) \
  163. for ((bit) = 0; \
  164. ((bit) < nr_bits) \
  165. && (((bit) = bitmap_find_next(bm, nr_bits, bit)) != -1); \
  166. (bit)++)
  167. #define bitmap_for_each_zero(bm, nr_bits, bit) \
  168. for ((bit) = 0; \
  169. ((bit) < nr_bits) \
  170. && (((bit) = bitmap_find_next_zero(bm, nr_bits, bit)) != -1); \
  171. (bit)++)
  172. #endif /* KERN_BITMAP_H */