sparsebit.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * tools/testing/selftests/kvm/include/sparsebit.h
  4. *
  5. * Copyright (C) 2018, Google LLC.
  6. *
  7. * Header file that describes API to the sparsebit library.
  8. * This library provides a memory efficient means of storing
  9. * the settings of bits indexed via a uint64_t. Memory usage
  10. * is reasonable, significantly less than (2^64 / 8) bytes, as
  11. * long as bits that are mostly set or mostly cleared are close
  12. * to each other. This library is efficient in memory usage
  13. * even in the case where most bits are set.
  14. */
  15. #ifndef SELFTEST_KVM_SPARSEBIT_H
  16. #define SELFTEST_KVM_SPARSEBIT_H
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. struct sparsebit;
  24. typedef uint64_t sparsebit_idx_t;
  25. typedef uint64_t sparsebit_num_t;
  26. struct sparsebit *sparsebit_alloc(void);
  27. void sparsebit_free(struct sparsebit **sbitp);
  28. void sparsebit_copy(struct sparsebit *dstp, const struct sparsebit *src);
  29. bool sparsebit_is_set(const struct sparsebit *sbit, sparsebit_idx_t idx);
  30. bool sparsebit_is_set_num(const struct sparsebit *sbit,
  31. sparsebit_idx_t idx, sparsebit_num_t num);
  32. bool sparsebit_is_clear(const struct sparsebit *sbit, sparsebit_idx_t idx);
  33. bool sparsebit_is_clear_num(const struct sparsebit *sbit,
  34. sparsebit_idx_t idx, sparsebit_num_t num);
  35. sparsebit_num_t sparsebit_num_set(const struct sparsebit *sbit);
  36. bool sparsebit_any_set(const struct sparsebit *sbit);
  37. bool sparsebit_any_clear(const struct sparsebit *sbit);
  38. bool sparsebit_all_set(const struct sparsebit *sbit);
  39. bool sparsebit_all_clear(const struct sparsebit *sbit);
  40. sparsebit_idx_t sparsebit_first_set(const struct sparsebit *sbit);
  41. sparsebit_idx_t sparsebit_first_clear(const struct sparsebit *sbit);
  42. sparsebit_idx_t sparsebit_next_set(const struct sparsebit *sbit, sparsebit_idx_t prev);
  43. sparsebit_idx_t sparsebit_next_clear(const struct sparsebit *sbit, sparsebit_idx_t prev);
  44. sparsebit_idx_t sparsebit_next_set_num(const struct sparsebit *sbit,
  45. sparsebit_idx_t start, sparsebit_num_t num);
  46. sparsebit_idx_t sparsebit_next_clear_num(const struct sparsebit *sbit,
  47. sparsebit_idx_t start, sparsebit_num_t num);
  48. void sparsebit_set(struct sparsebit *sbitp, sparsebit_idx_t idx);
  49. void sparsebit_set_num(struct sparsebit *sbitp, sparsebit_idx_t start,
  50. sparsebit_num_t num);
  51. void sparsebit_set_all(struct sparsebit *sbitp);
  52. void sparsebit_clear(struct sparsebit *sbitp, sparsebit_idx_t idx);
  53. void sparsebit_clear_num(struct sparsebit *sbitp,
  54. sparsebit_idx_t start, sparsebit_num_t num);
  55. void sparsebit_clear_all(struct sparsebit *sbitp);
  56. void sparsebit_dump(FILE *stream, const struct sparsebit *sbit,
  57. unsigned int indent);
  58. void sparsebit_validate_internal(const struct sparsebit *sbit);
  59. /*
  60. * Iterate over an inclusive ranges within sparsebit @s. In each iteration,
  61. * @range_begin and @range_end will take the beginning and end of the set
  62. * range, which are of type sparsebit_idx_t.
  63. *
  64. * For example, if the range [3, 7] (inclusive) is set, within the
  65. * iteration,@range_begin will take the value 3 and @range_end will take
  66. * the value 7.
  67. *
  68. * Ensure that there is at least one bit set before using this macro with
  69. * sparsebit_any_set(), because sparsebit_first_set() will abort if none
  70. * are set.
  71. */
  72. #define sparsebit_for_each_set_range(s, range_begin, range_end) \
  73. for (range_begin = sparsebit_first_set(s), \
  74. range_end = sparsebit_next_clear(s, range_begin) - 1; \
  75. range_begin && range_end; \
  76. range_begin = sparsebit_next_set(s, range_end), \
  77. range_end = sparsebit_next_clear(s, range_begin) - 1)
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif /* SELFTEST_KVM_SPARSEBIT_H */