~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/security/selinux/ss/ebitmap.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 16 #ifndef _SS_EBITMAP_H_
 17 #define _SS_EBITMAP_H_
 18 
 19 #include <net/netlabel.h>
 20 
 21 #ifdef CONFIG_64BIT
 22 #define EBITMAP_NODE_SIZE 64
 23 #else
 24 #define EBITMAP_NODE_SIZE 32
 25 #endif
 26 
 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       1UL
 33 #define EBITMAP_SHIFT_UNIT_SIZE(x) \
 34         (((x) >> EBITMAP_UNIT_SIZE / 2) >> EBITMAP_UNIT_SIZE / 2)
 35 
 36 struct ebitmap_node {
 37         struct ebitmap_node *next;
 38         unsigned long maps[EBITMAP_UNIT_NUMS];
 39         u32 startbit;
 40 };
 41 
 42 struct ebitmap {
 43         struct ebitmap_node *node; /* first node in the bitmap */
 44         u32 highbit; /* highest position in the total bitmap */
 45 };
 46 
 47 #define ebitmap_length(e) ((e)->highbit)
 48 
 49 static inline u32 ebitmap_start_positive(const struct ebitmap *e,
 50                                          struct ebitmap_node **n)
 51 {
 52         u32 ofs;
 53 
 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;
 58         }
 59         return ebitmap_length(e);
 60 }
 61 
 62 static inline void ebitmap_init(struct ebitmap *e)
 63 {
 64         memset(e, 0, sizeof(*e));
 65 }
 66 
 67 static inline u32 ebitmap_next_positive(const struct ebitmap *e,
 68                                         struct ebitmap_node **n, u32 bit)
 69 {
 70         u32 ofs;
 71 
 72         ofs = find_next_bit((*n)->maps, EBITMAP_SIZE, bit - (*n)->startbit + 1);
 73         if (ofs < EBITMAP_SIZE)
 74                 return ofs + (*n)->startbit;
 75 
 76         for (*n = (*n)->next; *n; *n = (*n)->next) {
 77                 ofs = find_first_bit((*n)->maps, EBITMAP_SIZE);
 78                 if (ofs < EBITMAP_SIZE)
 79                         return ofs + (*n)->startbit;
 80         }
 81         return ebitmap_length(e);
 82 }
 83 
 84 #define EBITMAP_NODE_INDEX(node, bit) \
 85         (((bit) - (node)->startbit) / EBITMAP_UNIT_SIZE)
 86 #define EBITMAP_NODE_OFFSET(node, bit) \
 87         (((bit) - (node)->startbit) % EBITMAP_UNIT_SIZE)
 88 
 89 static inline int ebitmap_node_get_bit(const struct ebitmap_node *n, u32 bit)
 90 {
 91         u32 index = EBITMAP_NODE_INDEX(n, bit);
 92         u32 ofs = EBITMAP_NODE_OFFSET(n, bit);
 93 
 94         BUG_ON(index >= EBITMAP_UNIT_NUMS);
 95         if ((n->maps[index] & (EBITMAP_BIT << ofs)))
 96                 return 1;
 97         return 0;
 98 }
 99 
100 static inline void ebitmap_node_set_bit(struct ebitmap_node *n, u32 bit)
101 {
102         u32 index = EBITMAP_NODE_INDEX(n, bit);
103         u32 ofs = EBITMAP_NODE_OFFSET(n, bit);
104 
105         BUG_ON(index >= EBITMAP_UNIT_NUMS);
106         n->maps[index] |= (EBITMAP_BIT << ofs);
107 }
108 
109 static inline void ebitmap_node_clr_bit(struct ebitmap_node *n, u32 bit)
110 {
111         u32 index = EBITMAP_NODE_INDEX(n, bit);
112         u32 ofs = EBITMAP_NODE_OFFSET(n, bit);
113 
114         BUG_ON(index >= EBITMAP_UNIT_NUMS);
115         n->maps[index] &= ~(EBITMAP_BIT << ofs);
116 }
117 
118 #define ebitmap_for_each_positive_bit(e, n, bit)      \
119         for ((bit) = ebitmap_start_positive(e, &(n)); \
120              (bit) < ebitmap_length(e);               \
121              (bit) = ebitmap_next_positive(e, &(n), bit))
122 
123 int ebitmap_cmp(const struct ebitmap *e1, const struct ebitmap *e2);
124 int ebitmap_cpy(struct ebitmap *dst, const struct ebitmap *src);
125 int ebitmap_and(struct ebitmap *dst, const struct ebitmap *e1,
126                 const struct ebitmap *e2);
127 int ebitmap_contains(const struct ebitmap *e1, const struct ebitmap *e2,
128                      u32 last_e2bit);
129 int ebitmap_get_bit(const struct ebitmap *e, u32 bit);
130 int ebitmap_set_bit(struct ebitmap *e, u32 bit, int value);
131 void ebitmap_destroy(struct ebitmap *e);
132 int ebitmap_read(struct ebitmap *e, void *fp);
133 int ebitmap_write(const struct ebitmap *e, void *fp);
134 u32 ebitmap_hash(const struct ebitmap *e, u32 hash);
135 
136 #ifdef CONFIG_NETLABEL
137 int ebitmap_netlbl_export(struct ebitmap *ebmap,
138                           struct netlbl_lsm_catmap **catmap);
139 int ebitmap_netlbl_import(struct ebitmap *ebmap,
140                           struct netlbl_lsm_catmap *catmap);
141 #else
142 static inline int ebitmap_netlbl_export(struct ebitmap *ebmap,
143                                         struct netlbl_lsm_catmap **catmap)
144 {
145         return -ENOMEM;
146 }
147 static inline int ebitmap_netlbl_import(struct ebitmap *ebmap,
148                                         struct netlbl_lsm_catmap *catmap)
149 {
150         return -ENOMEM;
151 }
152 #endif
153 
154 #endif /* _SS_EBITMAP_H_ */
155 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php