1 /* SPDX-License-Identifier: GPL-2.0 */
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.
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
13 * Author : Stephen Smalley, <stephen.smalley.work@gmail.com>
16 #ifndef _SS_EBITMAP_H_
17 #define _SS_EBITMAP_H_
19 #include <net/netlabel.h>
22 #define EBITMAP_NODE_SIZE 64
24 #define EBITMAP_NODE_SIZE 32
27 #define EBITMAP_UNIT_NUMS \
28 ((EBITMAP_NODE_SIZE - sizeof(void *) - sizeof(u32)) / \
29 sizeof(unsigned long))
30 #define EBITMAP_UNIT_SIZE BITS_PER_LONG
31 #define EBITMAP_SIZE (EBITMAP_UNIT_NUMS * EBITMAP_UNIT_SIZE)
32 #define EBITMAP_BIT 1ULL
33 #define EBITMAP_SHIFT_UNIT_SIZE(x) \
34 (((x) >> EBITMAP_UNIT_SIZE / 2) >> EBITMAP_UNIT_SIZE / 2)
37 struct ebitmap_node *next;
38 unsigned long maps[EBITMAP_UNIT_NUMS];
43 struct ebitmap_node *node; /* first node in the bitmap */
44 u32 highbit; /* highest position in the total bitmap */
47 #define ebitmap_length(e) ((e)->highbit)
49 static inline unsigned int ebitmap_start_positive(const struct ebitmap *e,
50 struct ebitmap_node **n)
54 for (*n = e->node; *n; *n = (*n)->next) {
55 ofs = find_first_bit((*n)->maps, EBITMAP_SIZE);
56 if (ofs < EBITMAP_SIZE)
57 return (*n)->startbit + ofs;
59 return ebitmap_length(e);
62 static inline void ebitmap_init(struct ebitmap *e)
64 memset(e, 0, sizeof(*e));
67 static inline unsigned int ebitmap_next_positive(const struct ebitmap *e,
68 struct ebitmap_node **n,
73 ofs = find_next_bit((*n)->maps, EBITMAP_SIZE, bit - (*n)->startbit + 1);
74 if (ofs < EBITMAP_SIZE)
75 return ofs + (*n)->startbit;
77 for (*n = (*n)->next; *n; *n = (*n)->next) {
78 ofs = find_first_bit((*n)->maps, EBITMAP_SIZE);
79 if (ofs < EBITMAP_SIZE)
80 return ofs + (*n)->startbit;
82 return ebitmap_length(e);
85 #define EBITMAP_NODE_INDEX(node, bit) \
86 (((bit) - (node)->startbit) / EBITMAP_UNIT_SIZE)
87 #define EBITMAP_NODE_OFFSET(node, bit) \
88 (((bit) - (node)->startbit) % EBITMAP_UNIT_SIZE)
90 static inline int ebitmap_node_get_bit(const struct ebitmap_node *n,
93 unsigned int index = EBITMAP_NODE_INDEX(n, bit);
94 unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
96 BUG_ON(index >= EBITMAP_UNIT_NUMS);
97 if ((n->maps[index] & (EBITMAP_BIT << ofs)))
102 static inline void ebitmap_node_set_bit(struct ebitmap_node *n,
105 unsigned int index = EBITMAP_NODE_INDEX(n, bit);
106 unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
108 BUG_ON(index >= EBITMAP_UNIT_NUMS);
109 n->maps[index] |= (EBITMAP_BIT << ofs);
112 static inline void ebitmap_node_clr_bit(struct ebitmap_node *n,
115 unsigned int index = EBITMAP_NODE_INDEX(n, bit);
116 unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
118 BUG_ON(index >= EBITMAP_UNIT_NUMS);
119 n->maps[index] &= ~(EBITMAP_BIT << ofs);
122 #define ebitmap_for_each_positive_bit(e, n, bit) \
123 for ((bit) = ebitmap_start_positive(e, &(n)); \
124 (bit) < ebitmap_length(e); \
125 (bit) = ebitmap_next_positive(e, &(n), bit))
127 int ebitmap_cmp(const struct ebitmap *e1, const struct ebitmap *e2);
128 int ebitmap_cpy(struct ebitmap *dst, const struct ebitmap *src);
129 int ebitmap_and(struct ebitmap *dst, const struct ebitmap *e1,
130 const struct ebitmap *e2);
131 int ebitmap_contains(const struct ebitmap *e1, const struct ebitmap *e2,
133 int ebitmap_get_bit(const struct ebitmap *e, unsigned long bit);
134 int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value);
135 void ebitmap_destroy(struct ebitmap *e);
136 int ebitmap_read(struct ebitmap *e, void *fp);
137 int ebitmap_write(const struct ebitmap *e, void *fp);
138 u32 ebitmap_hash(const struct ebitmap *e, u32 hash);
140 #ifdef CONFIG_NETLABEL
141 int ebitmap_netlbl_export(struct ebitmap *ebmap,
142 struct netlbl_lsm_catmap **catmap);
143 int ebitmap_netlbl_import(struct ebitmap *ebmap,
144 struct netlbl_lsm_catmap *catmap);
146 static inline int ebitmap_netlbl_export(struct ebitmap *ebmap,
147 struct netlbl_lsm_catmap **catmap)
151 static inline int ebitmap_netlbl_import(struct ebitmap *ebmap,
152 struct netlbl_lsm_catmap *catmap)
158 #endif /* _SS_EBITMAP_H_ */