bitmap.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
  12. const unsigned long *bitmap2, unsigned int bits);
  13. #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
  14. #define BITMAP_LAST_WORD_MASK(nbits) \
  15. ( \
  16. ((nbits) % BITS_PER_LONG) ? \
  17. (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
  18. )
  19. #define small_const_nbits(nbits) \
  20. (__builtin_constant_p(nbits) && (nbits) <= BITS_PER_LONG)
  21. static inline void bitmap_zero(unsigned long *dst, int nbits)
  22. {
  23. if (small_const_nbits(nbits))
  24. *dst = 0UL;
  25. else {
  26. int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  27. memset(dst, 0, len);
  28. }
  29. }
  30. static inline int bitmap_weight(const unsigned long *src, int nbits)
  31. {
  32. if (small_const_nbits(nbits))
  33. return hweight_long(*src & BITMAP_LAST_WORD_MASK(nbits));
  34. return __bitmap_weight(src, nbits);
  35. }
  36. static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
  37. const unsigned long *src2, int nbits)
  38. {
  39. if (small_const_nbits(nbits))
  40. *dst = *src1 | *src2;
  41. else
  42. __bitmap_or(dst, src1, src2, nbits);
  43. }
  44. /**
  45. * test_and_set_bit - Set a bit and return its old value
  46. * @nr: Bit to set
  47. * @addr: Address to count from
  48. */
  49. static inline int test_and_set_bit(int nr, unsigned long *addr)
  50. {
  51. unsigned long mask = BIT_MASK(nr);
  52. unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
  53. unsigned long old;
  54. old = *p;
  55. *p = old | mask;
  56. return (old & mask) != 0;
  57. }
  58. /**
  59. * bitmap_alloc - Allocate bitmap
  60. * @nr: Bit to set
  61. */
  62. static inline unsigned long *bitmap_alloc(int nbits)
  63. {
  64. return calloc(1, BITS_TO_LONGS(nbits) * sizeof(unsigned long));
  65. }
  66. /*
  67. * bitmap_scnprintf - print bitmap list into buffer
  68. * @bitmap: bitmap
  69. * @nbits: size of bitmap
  70. * @buf: buffer to store output
  71. * @size: size of @buf
  72. */
  73. size_t bitmap_scnprintf(unsigned long *bitmap, int nbits,
  74. char *buf, size_t size);
  75. /**
  76. * bitmap_and - Do logical and on bitmaps
  77. * @dst: resulting bitmap
  78. * @src1: operand 1
  79. * @src2: operand 2
  80. * @nbits: size of bitmap
  81. */
  82. static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
  83. const unsigned long *src2, unsigned int nbits)
  84. {
  85. if (small_const_nbits(nbits))
  86. return (*dst = *src1 & *src2 & BITMAP_LAST_WORD_MASK(nbits)) != 0;
  87. return __bitmap_and(dst, src1, src2, nbits);
  88. }
  89. #endif /* _PERF_BITOPS_H */