ebitmap.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #define EBITMAP_UNIT_NUMS ((32 - sizeof(void *) - sizeof(u32)) \
  18. / sizeof(unsigned long))
  19. #define EBITMAP_UNIT_SIZE BITS_PER_LONG
  20. #define EBITMAP_SIZE (EBITMAP_UNIT_NUMS * EBITMAP_UNIT_SIZE)
  21. #define EBITMAP_BIT 1ULL
  22. struct ebitmap_node {
  23. struct ebitmap_node *next;
  24. unsigned long maps[EBITMAP_UNIT_NUMS];
  25. u32 startbit;
  26. };
  27. struct ebitmap {
  28. struct ebitmap_node *node; /* first node in the bitmap */
  29. u32 highbit; /* highest position in the total bitmap */
  30. };
  31. #define ebitmap_length(e) ((e)->highbit)
  32. #define ebitmap_startbit(e) ((e)->node ? (e)->node->startbit : 0)
  33. static inline unsigned int ebitmap_start_positive(struct ebitmap *e,
  34. struct ebitmap_node **n)
  35. {
  36. unsigned int ofs;
  37. for (*n = e->node; *n; *n = (*n)->next) {
  38. ofs = find_first_bit((*n)->maps, EBITMAP_SIZE);
  39. if (ofs < EBITMAP_SIZE)
  40. return (*n)->startbit + ofs;
  41. }
  42. return ebitmap_length(e);
  43. }
  44. static inline void ebitmap_init(struct ebitmap *e)
  45. {
  46. memset(e, 0, sizeof(*e));
  47. }
  48. static inline unsigned int ebitmap_next_positive(struct ebitmap *e,
  49. struct ebitmap_node **n,
  50. unsigned int bit)
  51. {
  52. unsigned int ofs;
  53. ofs = find_next_bit((*n)->maps, EBITMAP_SIZE, bit - (*n)->startbit + 1);
  54. if (ofs < EBITMAP_SIZE)
  55. return ofs + (*n)->startbit;
  56. for (*n = (*n)->next; *n; *n = (*n)->next) {
  57. ofs = find_first_bit((*n)->maps, EBITMAP_SIZE);
  58. if (ofs < EBITMAP_SIZE)
  59. return ofs + (*n)->startbit;
  60. }
  61. return ebitmap_length(e);
  62. }
  63. #define EBITMAP_NODE_INDEX(node, bit) \
  64. (((bit) - (node)->startbit) / EBITMAP_UNIT_SIZE)
  65. #define EBITMAP_NODE_OFFSET(node, bit) \
  66. (((bit) - (node)->startbit) % EBITMAP_UNIT_SIZE)
  67. static inline int ebitmap_node_get_bit(struct ebitmap_node *n,
  68. unsigned int bit)
  69. {
  70. unsigned int index = EBITMAP_NODE_INDEX(n, bit);
  71. unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
  72. BUG_ON(index >= EBITMAP_UNIT_NUMS);
  73. if ((n->maps[index] & (EBITMAP_BIT << ofs)))
  74. return 1;
  75. return 0;
  76. }
  77. static inline void ebitmap_node_set_bit(struct ebitmap_node *n,
  78. unsigned int bit)
  79. {
  80. unsigned int index = EBITMAP_NODE_INDEX(n, bit);
  81. unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
  82. BUG_ON(index >= EBITMAP_UNIT_NUMS);
  83. n->maps[index] |= (EBITMAP_BIT << ofs);
  84. }
  85. static inline void ebitmap_node_clr_bit(struct ebitmap_node *n,
  86. unsigned int bit)
  87. {
  88. unsigned int index = EBITMAP_NODE_INDEX(n, bit);
  89. unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
  90. BUG_ON(index >= EBITMAP_UNIT_NUMS);
  91. n->maps[index] &= ~(EBITMAP_BIT << ofs);
  92. }
  93. #define ebitmap_for_each_positive_bit(e, n, bit) \
  94. for (bit = ebitmap_start_positive(e, &n); \
  95. bit < ebitmap_length(e); \
  96. bit = ebitmap_next_positive(e, &n, bit)) \
  97. int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2);
  98. int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src);
  99. int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2);
  100. int ebitmap_get_bit(struct ebitmap *e, unsigned long bit);
  101. int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value);
  102. void ebitmap_destroy(struct ebitmap *e);
  103. int ebitmap_read(struct ebitmap *e, void *fp);
  104. #ifdef CONFIG_NETLABEL
  105. int ebitmap_netlbl_export(struct ebitmap *ebmap,
  106. struct netlbl_lsm_secattr_catmap **catmap);
  107. int ebitmap_netlbl_import(struct ebitmap *ebmap,
  108. struct netlbl_lsm_secattr_catmap *catmap);
  109. #else
  110. static inline int ebitmap_netlbl_export(struct ebitmap *ebmap,
  111. struct netlbl_lsm_secattr_catmap **catmap)
  112. {
  113. return -ENOMEM;
  114. }
  115. static inline int ebitmap_netlbl_import(struct ebitmap *ebmap,
  116. struct netlbl_lsm_secattr_catmap *catmap)
  117. {
  118. return -ENOMEM;
  119. }
  120. #endif
  121. #endif /* _SS_EBITMAP_H_ */