5 #include <helpers/bitmask.h>
7 /* How many bits in an unsigned long */
8 #define bitsperlong (8 * sizeof(unsigned long))
10 /* howmany(a,b) : how many elements of size b needed to hold all of a */
11 #define howmany(x, y) (((x)+((y)-1))/(y))
13 /* How many longs in mask of n bits */
14 #define longsperbits(n) howmany(n, bitsperlong)
16 #define max(a, b) ((a) > (b) ? (a) : (b))
19 * Allocate and free `struct bitmask *`
22 /* Allocate a new `struct bitmask` with a size of n bits */
23 struct bitmask
*bitmask_alloc(unsigned int n
)
27 bmp
= malloc(sizeof(*bmp
));
31 bmp
->maskp
= calloc(longsperbits(n
), sizeof(unsigned long));
32 if (bmp
->maskp
== 0) {
39 /* Free `struct bitmask` */
40 void bitmask_free(struct bitmask
*bmp
)
45 bmp
->maskp
= (unsigned long *)0xdeadcdef; /* double free tripwire */
50 * The routines _getbit() and _setbit() are the only
51 * routines that actually understand the layout of bmp->maskp[].
53 * On little endian architectures, this could simply be an array of
54 * bytes. But the kernel layout of bitmasks _is_ visible to userspace
55 * via the sched_(set/get)affinity calls in Linux 2.6, and on big
56 * endian architectures, it is painfully obvious that this is an
57 * array of unsigned longs.
60 /* Return the value (0 or 1) of bit n in bitmask bmp */
61 static unsigned int _getbit(const struct bitmask
*bmp
, unsigned int n
)
64 return (bmp
->maskp
[n
/bitsperlong
] >> (n
% bitsperlong
)) & 1;
69 /* Set bit n in bitmask bmp to value v (0 or 1) */
70 static void _setbit(struct bitmask
*bmp
, unsigned int n
, unsigned int v
)
74 bmp
->maskp
[n
/bitsperlong
] |= 1UL << (n
% bitsperlong
);
76 bmp
->maskp
[n
/bitsperlong
] &=
77 ~(1UL << (n
% bitsperlong
));
82 * When parsing bitmask lists, only allow numbers, separated by one
83 * of the allowed next characters.
85 * The parameter 'sret' is the return from a sscanf "%u%c". It is
86 * -1 if the sscanf input string was empty. It is 0 if the first
87 * character in the sscanf input string was not a decimal number.
88 * It is 1 if the unsigned number matching the "%u" was the end of the
89 * input string. It is 2 if one or more additional characters followed
90 * the matched unsigned number. If it is 2, then 'nextc' is the first
91 * character following the number. The parameter 'ok_next_chars'
92 * is the nul-terminated list of allowed next characters.
94 * The mask term just scanned was ok if and only if either the numbers
95 * matching the %u were all of the input or if the next character in
96 * the input past the numbers was one of the allowed next characters.
98 static int scan_was_ok(int sret
, char nextc
, const char *ok_next_chars
)
101 (sret
== 2 && strchr(ok_next_chars
, nextc
) != NULL
);
104 static const char *nexttoken(const char *q
, int sep
)
113 /* Set a single bit i in bitmask */
114 struct bitmask
*bitmask_setbit(struct bitmask
*bmp
, unsigned int i
)
120 /* Set all bits in bitmask: bmp = ~0 */
121 struct bitmask
*bitmask_setall(struct bitmask
*bmp
)
124 for (i
= 0; i
< bmp
->size
; i
++)
129 /* Clear all bits in bitmask: bmp = 0 */
130 struct bitmask
*bitmask_clearall(struct bitmask
*bmp
)
133 for (i
= 0; i
< bmp
->size
; i
++)
138 /* True if all bits are clear */
139 int bitmask_isallclear(const struct bitmask
*bmp
)
142 for (i
= 0; i
< bmp
->size
; i
++)
148 /* True if specified bit i is set */
149 int bitmask_isbitset(const struct bitmask
*bmp
, unsigned int i
)
151 return _getbit(bmp
, i
);
154 /* Number of lowest set bit (min) */
155 unsigned int bitmask_first(const struct bitmask
*bmp
)
157 return bitmask_next(bmp
, 0);
160 /* Number of highest set bit (max) */
161 unsigned int bitmask_last(const struct bitmask
*bmp
)
164 unsigned int m
= bmp
->size
;
165 for (i
= 0; i
< bmp
->size
; i
++)
171 /* Number of next set bit at or above given bit i */
172 unsigned int bitmask_next(const struct bitmask
*bmp
, unsigned int i
)
175 for (n
= i
; n
< bmp
->size
; n
++)
182 * Parses a comma-separated list of numbers and ranges of numbers,
183 * with optional ':%u' strides modifying ranges, into provided bitmask.
184 * Some examples of input lists and their equivalent simple list:
185 * Input Equivalent to
189 * 0-3:2,8-15:4 0,2,8,12
191 int bitmask_parselist(const char *buf
, struct bitmask
*bmp
)
195 bitmask_clearall(bmp
);
198 while (p
= q
, q
= nexttoken(q
, ','), p
) {
199 unsigned int a
; /* begin of range */
200 unsigned int b
; /* end of range */
201 unsigned int s
; /* stride */
202 const char *c1
, *c2
; /* next tokens after '-' or ',' */
203 char nextc
; /* char after sscanf %u match */
204 int sret
; /* sscanf return (number of matches) */
206 sret
= sscanf(p
, "%u%c", &a
, &nextc
);
207 if (!scan_was_ok(sret
, nextc
, ",-"))
211 c1
= nexttoken(p
, '-');
212 c2
= nexttoken(p
, ',');
213 if (c1
!= NULL
&& (c2
== NULL
|| c1
< c2
)) {
214 sret
= sscanf(c1
, "%u%c", &b
, &nextc
);
215 if (!scan_was_ok(sret
, nextc
, ",:"))
217 c1
= nexttoken(c1
, ':');
218 if (c1
!= NULL
&& (c2
== NULL
|| c1
< c2
)) {
219 sret
= sscanf(c1
, "%u%c", &s
, &nextc
);
220 if (!scan_was_ok(sret
, nextc
, ","))
235 bitmask_clearall(bmp
);
240 * emit(buf, buflen, rbot, rtop, len)
242 * Helper routine for bitmask_displaylist(). Write decimal number
243 * or range to buf+len, suppressing output past buf+buflen, with optional
244 * comma-prefix. Return len of what would be written to buf, if it
248 static inline int emit(char *buf
, int buflen
, int rbot
, int rtop
, int len
)
251 len
+= snprintf(buf
+ len
, max(buflen
- len
, 0), ",");
253 len
+= snprintf(buf
+ len
, max(buflen
- len
, 0), "%d", rbot
);
255 len
+= snprintf(buf
+ len
, max(buflen
- len
, 0), "%d-%d",
261 * Write decimal list representation of bmp to buf.
263 * Output format is a comma-separated list of decimal numbers and
264 * ranges. Consecutively set bits are shown as two hyphen-separated
265 * decimal numbers, the smallest and largest bit numbers set in
266 * the range. Output format is compatible with the format
267 * accepted as input by bitmap_parselist().
269 * The return value is the number of characters which would be
270 * generated for the given input, excluding the trailing '\0', as
274 int bitmask_displaylist(char *buf
, int buflen
, const struct bitmask
*bmp
)
277 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
278 unsigned int cur
, rbot
, rtop
;
282 rbot
= cur
= bitmask_first(bmp
);
283 while (cur
< bmp
->size
) {
285 cur
= bitmask_next(bmp
, cur
+1);
286 if (cur
>= bmp
->size
|| cur
> rtop
+ 1) {
287 len
= emit(buf
, buflen
, rbot
, rtop
, len
);