bitmap.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef _PERF_BITOPS_H
  2. #define _PERF_BITOPS_H
  3. #include <string.h>
  4. #include <linux/bitops.h>
  5. #include <stdlib.h>
  6. #define DECLARE_BITMAP(name,bits) \
  7. unsigned long name[BITS_TO_LONGS(bits)]
  8. int __bitmap_weight(const unsigned long *bitmap, int bits);
  9. void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
  10. const unsigned long *bitmap2, int bits);
  11. #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
  12. #define BITMAP_LAST_WORD_MASK(nbits) \
  13. ( \
  14. ((nbits) % BITS_PER_LONG) ? \
  15. (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
  16. )
  17. #define small_const_nbits(nbits) \
  18. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
  19. static inline void bitmap_zero(unsigned long *dst, int nbits)
  20. {
  21. if (small_const_nbits(nbits))
  22. *dst = 0UL;
  23. else {
  24. int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  25. memset(dst, 0, len);
  26. }
  27. }
  28. static inline int bitmap_weight(const unsigned long *src, int nbits)
  29. {
  30. if (small_const_nbits(nbits))
  31. return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
  32. return __bitmap_weight(src, nbits);
  33. }
  34. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  35. const unsigned long *src2, int nbits)
  36. {
  37. if (small_const_nbits(nbits))
  38. *dst = *src1 | *src2;
  39. else
  40. __bitmap_or(dst, src1, src2, nbits);
  41. }
  42. /**
  43. * test_and_set_bit - Set a bit and return its old value
  44. * @nr: Bit to set
  45. * @addr: Address to count from
  46. */
  47. static inline int test_and_set_bit(int nr, unsigned long *addr)
  48. {
  49. unsigned long mask = BIT_MASK(nr);
  50. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  51. unsigned long old;
  52. old = *p;
  53. *p = old | mask;
  54. return (old & mask) != 0;
  55. }
  56. /**
  57. * bitmap_alloc - Allocate bitmap
  58. * @nr: Bit to set
  59. */
  60. static inline unsigned long *bitmap_alloc(int nbits)
  61. {
  62. return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
  63. }
  64. #endif /* _PERF_BITOPS_H */