bitmap_i.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifndef KERN_BITMAP_I_H
  21. #define KERN_BITMAP_I_H
  22. #include <limits.h>
  23. #include <kern/macros.h>
  24. #define BITMAP_LONGS(nr_bits) DIV_CEIL(nr_bits, LONG_BIT)
  25. /*
  26. * Adjust the bitmap pointer and the bit index so that the latter refers
  27. * to a bit inside the word pointed by the former.
  28. *
  29. * Implemented as a macro for const-correctness.
  30. */
  31. #define bitmap_lookup(bmp, bitp) \
  32. MACRO_BEGIN \
  33. int i; \
  34. \
  35. i = BITMAP_LONGS(*(bitp) + 1) - 1; \
  36. *(bmp) += i; \
  37. *(bitp) -= i * LONG_BIT; \
  38. MACRO_END
  39. static inline unsigned long
  40. bitmap_mask(int bit)
  41. {
  42. return (1UL << bit);
  43. }
  44. /*
  45. * Return the index of the next set bit in the bitmap, starting (and
  46. * including) the given bit index, or -1 if the bitmap is empty. If
  47. * complement is true, bits are toggled before searching so that the
  48. * result is the index of the next zero bit.
  49. */
  50. int bitmap_find_next_bit(const unsigned long *bm, int nr_bits, int bit,
  51. int complement);
  52. #endif /* KERN_BITMAP_I_H */