ebitmap.h 4.1 KB

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