ebitmap.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * An extensible bitmap is a bitmap that supports an
  4. * arbitrary number of bits. Extensible bitmaps are
  5. * used to represent sets of values, such as types,
  6. * roles, categories, and classes.
  7. *
  8. * Each extensible bitmap is implemented as a linked
  9. * list of bitmap nodes, where each bitmap node has
  10. * an explicitly specified starting bit position within
  11. * the total bitmap.
  12. *
  13. * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
  14. */
  15. #ifndef _SS_EBITMAP_H_
  16. #define _SS_EBITMAP_H_
  17. #include <net/netlabel.h>
  18. #ifdef CONFIG_64BIT
  19. #define EBITMAP_NODE_SIZE 64
  20. #else
  21. #define EBITMAP_NODE_SIZE 32
  22. #endif
  23. #define EBITMAP_UNIT_NUMS \
  24. ((EBITMAP_NODE_SIZE - sizeof(void *) - sizeof(u32)) / \
  25. sizeof(unsigned long))
  26. #define EBITMAP_UNIT_SIZE BITS_PER_LONG
  27. #define EBITMAP_SIZE (EBITMAP_UNIT_NUMS * EBITMAP_UNIT_SIZE)
  28. #define EBITMAP_BIT 1UL
  29. #define EBITMAP_SHIFT_UNIT_SIZE(x) \
  30. (((x) >> EBITMAP_UNIT_SIZE / 2) >> EBITMAP_UNIT_SIZE / 2)
  31. struct ebitmap_node {
  32. struct ebitmap_node *next;
  33. unsigned long maps[EBITMAP_UNIT_NUMS];
  34. u32 startbit;
  35. };
  36. struct ebitmap {
  37. struct ebitmap_node *node; /* first node in the bitmap */
  38. u32 highbit; /* highest position in the total bitmap */
  39. };
  40. #define ebitmap_length(e) ((e)->highbit)
  41. static inline u32 ebitmap_start_positive(const struct ebitmap *e,
  42. struct ebitmap_node **n)
  43. {
  44. u32 ofs;
  45. for (*n = e->node; *n; *n = (*n)->next) {
  46. ofs = find_first_bit((*n)->maps, EBITMAP_SIZE);
  47. if (ofs < EBITMAP_SIZE)
  48. return (*n)->startbit + ofs;
  49. }
  50. return ebitmap_length(e);
  51. }
  52. static inline void ebitmap_init(struct ebitmap *e)
  53. {
  54. memset(e, 0, sizeof(*e));
  55. }
  56. static inline u32 ebitmap_next_positive(const struct ebitmap *e,
  57. struct ebitmap_node **n, u32 bit)
  58. {
  59. u32 ofs;
  60. ofs = find_next_bit((*n)->maps, EBITMAP_SIZE, bit - (*n)->startbit + 1);
  61. if (ofs < EBITMAP_SIZE)
  62. return ofs + (*n)->startbit;
  63. for (*n = (*n)->next; *n; *n = (*n)->next) {
  64. ofs = find_first_bit((*n)->maps, EBITMAP_SIZE);
  65. if (ofs < EBITMAP_SIZE)
  66. return ofs + (*n)->startbit;
  67. }
  68. return ebitmap_length(e);
  69. }
  70. #define EBITMAP_NODE_INDEX(node, bit) \
  71. (((bit) - (node)->startbit) / EBITMAP_UNIT_SIZE)
  72. #define EBITMAP_NODE_OFFSET(node, bit) \
  73. (((bit) - (node)->startbit) % EBITMAP_UNIT_SIZE)
  74. static inline int ebitmap_node_get_bit(const struct ebitmap_node *n, u32 bit)
  75. {
  76. u32 index = EBITMAP_NODE_INDEX(n, bit);
  77. u32 ofs = EBITMAP_NODE_OFFSET(n, bit);
  78. BUG_ON(index >= EBITMAP_UNIT_NUMS);
  79. if ((n->maps[index] & (EBITMAP_BIT << ofs)))
  80. return 1;
  81. return 0;
  82. }
  83. static inline void ebitmap_node_set_bit(struct ebitmap_node *n, u32 bit)
  84. {
  85. u32 index = EBITMAP_NODE_INDEX(n, bit);
  86. u32 ofs = EBITMAP_NODE_OFFSET(n, bit);
  87. BUG_ON(index >= EBITMAP_UNIT_NUMS);
  88. n->maps[index] |= (EBITMAP_BIT << ofs);
  89. }
  90. static inline void ebitmap_node_clr_bit(struct ebitmap_node *n, u32 bit)
  91. {
  92. u32 index = EBITMAP_NODE_INDEX(n, bit);
  93. u32 ofs = EBITMAP_NODE_OFFSET(n, bit);
  94. BUG_ON(index >= EBITMAP_UNIT_NUMS);
  95. n->maps[index] &= ~(EBITMAP_BIT << ofs);
  96. }
  97. #define ebitmap_for_each_positive_bit(e, n, bit) \
  98. for ((bit) = ebitmap_start_positive(e, &(n)); \
  99. (bit) < ebitmap_length(e); \
  100. (bit) = ebitmap_next_positive(e, &(n), bit))
  101. int ebitmap_cmp(const struct ebitmap *e1, const struct ebitmap *e2);
  102. int ebitmap_cpy(struct ebitmap *dst, const struct ebitmap *src);
  103. int ebitmap_and(struct ebitmap *dst, const struct ebitmap *e1,
  104. const struct ebitmap *e2);
  105. int ebitmap_contains(const struct ebitmap *e1, const struct ebitmap *e2,
  106. u32 last_e2bit);
  107. int ebitmap_get_bit(const struct ebitmap *e, u32 bit);
  108. int ebitmap_set_bit(struct ebitmap *e, u32 bit, int value);
  109. void ebitmap_destroy(struct ebitmap *e);
  110. int ebitmap_read(struct ebitmap *e, void *fp);
  111. int ebitmap_write(const struct ebitmap *e, void *fp);
  112. u32 ebitmap_hash(const struct ebitmap *e, u32 hash);
  113. #ifdef CONFIG_NETLABEL
  114. int ebitmap_netlbl_export(struct ebitmap *ebmap,
  115. struct netlbl_lsm_catmap **catmap);
  116. int ebitmap_netlbl_import(struct ebitmap *ebmap,
  117. struct netlbl_lsm_catmap *catmap);
  118. #else
  119. static inline int ebitmap_netlbl_export(struct ebitmap *ebmap,
  120. struct netlbl_lsm_catmap **catmap)
  121. {
  122. return -ENOMEM;
  123. }
  124. static inline int ebitmap_netlbl_import(struct ebitmap *ebmap,
  125. struct netlbl_lsm_catmap *catmap)
  126. {
  127. return -ENOMEM;
  128. }
  129. #endif
  130. #endif /* _SS_EBITMAP_H_ */