ebitmap.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* Author : Stephen Smalley, <sds@epoch.ncsc.mil> */
  2. /* FLASK */
  3. /*
  4. * An extensible bitmap is a bitmap that supports an
  5. * arbitrary number of bits. Extensible bitmaps are
  6. * used to represent sets of values, such as types,
  7. * roles, categories, and classes.
  8. *
  9. * Each extensible bitmap is implemented as a linked
  10. * list of bitmap nodes, where each bitmap node has
  11. * an explicitly specified starting bit position within
  12. * the total bitmap.
  13. */
  14. #ifndef _SEPOL_POLICYDB_EBITMAP_H_
  15. #define _SEPOL_POLICYDB_EBITMAP_H_
  16. #include <stdint.h>
  17. #include <string.h>
  18. #define MAPTYPE uint64_t /* portion of bitmap in each node */
  19. #define MAPSIZE (sizeof(MAPTYPE) * 8) /* number of bits in node bitmap */
  20. #define MAPBIT 1ULL /* a bit in the node bitmap */
  21. typedef struct ebitmap_node {
  22. uint32_t startbit; /* starting position in the total bitmap */
  23. MAPTYPE map; /* this node's portion of the bitmap */
  24. struct ebitmap_node *next;
  25. } ebitmap_node_t;
  26. typedef struct ebitmap {
  27. ebitmap_node_t *node; /* first node in the bitmap */
  28. uint32_t highbit; /* highest position in the total bitmap */
  29. } ebitmap_t;
  30. #define ebitmap_length(e) ((e)->highbit)
  31. #define ebitmap_startbit(e) ((e)->node ? (e)->node->startbit : 0)
  32. #define ebitmap_startnode(e) ((e)->node)
  33. static inline unsigned int ebitmap_start(const ebitmap_t * e,
  34. ebitmap_node_t ** n)
  35. {
  36. *n = e->node;
  37. return ebitmap_startbit(e);
  38. }
  39. static inline void ebitmap_init(ebitmap_t * e)
  40. {
  41. memset(e, 0, sizeof(*e));
  42. }
  43. static inline unsigned int ebitmap_next(ebitmap_node_t ** n, unsigned int bit)
  44. {
  45. if ((bit == ((*n)->startbit + MAPSIZE - 1)) && (*n)->next) {
  46. *n = (*n)->next;
  47. return (*n)->startbit;
  48. }
  49. return (bit + 1);
  50. }
  51. static inline int ebitmap_node_get_bit(ebitmap_node_t * n, unsigned int bit)
  52. {
  53. if (n->map & (MAPBIT << (bit - n->startbit)))
  54. return 1;
  55. return 0;
  56. }
  57. #define ebitmap_for_each_bit(e, n, bit) \
  58. for (bit = ebitmap_start(e, &n); bit < ebitmap_length(e); bit = ebitmap_next(&n, bit)) \
  59. extern int ebitmap_cmp(const ebitmap_t * e1, const ebitmap_t * e2);
  60. extern int ebitmap_or(ebitmap_t * dst, const ebitmap_t * e1, const ebitmap_t * e2);
  61. extern int ebitmap_union(ebitmap_t * dst, const ebitmap_t * e1);
  62. extern int ebitmap_cpy(ebitmap_t * dst, const ebitmap_t * src);
  63. extern int ebitmap_contains(const ebitmap_t * e1, const ebitmap_t * e2);
  64. extern int ebitmap_get_bit(const ebitmap_t * e, unsigned int bit);
  65. extern int ebitmap_set_bit(ebitmap_t * e, unsigned int bit, int value);
  66. extern void ebitmap_destroy(ebitmap_t * e);
  67. extern int ebitmap_read(ebitmap_t * e, void *fp);
  68. #endif /* _EBITMAP_H_ */
  69. /* FLASK */