xref: /OK3568_Linux_fs/kernel/tools/power/cpupower/utils/helpers/bitmask.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <stdio.h>
3*4882a593Smuzhiyun #include <stdlib.h>
4*4882a593Smuzhiyun #include <string.h>
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <helpers/bitmask.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /* How many bits in an unsigned long */
9*4882a593Smuzhiyun #define bitsperlong (8 * sizeof(unsigned long))
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /* howmany(a,b) : how many elements of size b needed to hold all of a */
12*4882a593Smuzhiyun #define howmany(x, y) (((x)+((y)-1))/(y))
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /* How many longs in mask of n bits */
15*4882a593Smuzhiyun #define longsperbits(n) howmany(n, bitsperlong)
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #define max(a, b) ((a) > (b) ? (a) : (b))
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * Allocate and free `struct bitmask *`
21*4882a593Smuzhiyun  */
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* Allocate a new `struct bitmask` with a size of n bits */
bitmask_alloc(unsigned int n)24*4882a593Smuzhiyun struct bitmask *bitmask_alloc(unsigned int n)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun 	struct bitmask *bmp;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	bmp = malloc(sizeof(*bmp));
29*4882a593Smuzhiyun 	if (!bmp)
30*4882a593Smuzhiyun 		return 0;
31*4882a593Smuzhiyun 	bmp->size = n;
32*4882a593Smuzhiyun 	bmp->maskp = calloc(longsperbits(n), sizeof(unsigned long));
33*4882a593Smuzhiyun 	if (!bmp->maskp) {
34*4882a593Smuzhiyun 		free(bmp);
35*4882a593Smuzhiyun 		return 0;
36*4882a593Smuzhiyun 	}
37*4882a593Smuzhiyun 	return bmp;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /* Free `struct bitmask` */
bitmask_free(struct bitmask * bmp)41*4882a593Smuzhiyun void bitmask_free(struct bitmask *bmp)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	if (!bmp)
44*4882a593Smuzhiyun 		return;
45*4882a593Smuzhiyun 	free(bmp->maskp);
46*4882a593Smuzhiyun 	bmp->maskp = (unsigned long *)0xdeadcdef;  /* double free tripwire */
47*4882a593Smuzhiyun 	free(bmp);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun  * The routines _getbit() and _setbit() are the only
52*4882a593Smuzhiyun  * routines that actually understand the layout of bmp->maskp[].
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * On little endian architectures, this could simply be an array of
55*4882a593Smuzhiyun  * bytes.  But the kernel layout of bitmasks _is_ visible to userspace
56*4882a593Smuzhiyun  * via the sched_(set/get)affinity calls in Linux 2.6, and on big
57*4882a593Smuzhiyun  * endian architectures, it is painfully obvious that this is an
58*4882a593Smuzhiyun  * array of unsigned longs.
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* Return the value (0 or 1) of bit n in bitmask bmp */
_getbit(const struct bitmask * bmp,unsigned int n)62*4882a593Smuzhiyun static unsigned int _getbit(const struct bitmask *bmp, unsigned int n)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	if (n < bmp->size)
65*4882a593Smuzhiyun 		return (bmp->maskp[n/bitsperlong] >> (n % bitsperlong)) & 1;
66*4882a593Smuzhiyun 	else
67*4882a593Smuzhiyun 		return 0;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /* Set bit n in bitmask bmp to value v (0 or 1) */
_setbit(struct bitmask * bmp,unsigned int n,unsigned int v)71*4882a593Smuzhiyun static void _setbit(struct bitmask *bmp, unsigned int n, unsigned int v)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	if (n < bmp->size) {
74*4882a593Smuzhiyun 		if (v)
75*4882a593Smuzhiyun 			bmp->maskp[n/bitsperlong] |= 1UL << (n % bitsperlong);
76*4882a593Smuzhiyun 		else
77*4882a593Smuzhiyun 			bmp->maskp[n/bitsperlong] &=
78*4882a593Smuzhiyun 				~(1UL << (n % bitsperlong));
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /*
83*4882a593Smuzhiyun  * When parsing bitmask lists, only allow numbers, separated by one
84*4882a593Smuzhiyun  * of the allowed next characters.
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  * The parameter 'sret' is the return from a sscanf "%u%c".  It is
87*4882a593Smuzhiyun  * -1 if the sscanf input string was empty.  It is 0 if the first
88*4882a593Smuzhiyun  * character in the sscanf input string was not a decimal number.
89*4882a593Smuzhiyun  * It is 1 if the unsigned number matching the "%u" was the end of the
90*4882a593Smuzhiyun  * input string.  It is 2 if one or more additional characters followed
91*4882a593Smuzhiyun  * the matched unsigned number.  If it is 2, then 'nextc' is the first
92*4882a593Smuzhiyun  * character following the number.  The parameter 'ok_next_chars'
93*4882a593Smuzhiyun  * is the nul-terminated list of allowed next characters.
94*4882a593Smuzhiyun  *
95*4882a593Smuzhiyun  * The mask term just scanned was ok if and only if either the numbers
96*4882a593Smuzhiyun  * matching the %u were all of the input or if the next character in
97*4882a593Smuzhiyun  * the input past the numbers was one of the allowed next characters.
98*4882a593Smuzhiyun  */
scan_was_ok(int sret,char nextc,const char * ok_next_chars)99*4882a593Smuzhiyun static int scan_was_ok(int sret, char nextc, const char *ok_next_chars)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	return sret == 1 ||
102*4882a593Smuzhiyun 		(sret == 2 && strchr(ok_next_chars, nextc) != NULL);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
nexttoken(const char * q,int sep)105*4882a593Smuzhiyun static const char *nexttoken(const char *q,  int sep)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	if (q)
108*4882a593Smuzhiyun 		q = strchr(q, sep);
109*4882a593Smuzhiyun 	if (q)
110*4882a593Smuzhiyun 		q++;
111*4882a593Smuzhiyun 	return q;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /* Set a single bit i in bitmask */
bitmask_setbit(struct bitmask * bmp,unsigned int i)115*4882a593Smuzhiyun struct bitmask *bitmask_setbit(struct bitmask *bmp, unsigned int i)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	_setbit(bmp, i, 1);
118*4882a593Smuzhiyun 	return bmp;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun /* Set all bits in bitmask: bmp = ~0 */
bitmask_setall(struct bitmask * bmp)122*4882a593Smuzhiyun struct bitmask *bitmask_setall(struct bitmask *bmp)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun 	unsigned int i;
125*4882a593Smuzhiyun 	for (i = 0; i < bmp->size; i++)
126*4882a593Smuzhiyun 		_setbit(bmp, i, 1);
127*4882a593Smuzhiyun 	return bmp;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /* Clear all bits in bitmask: bmp = 0 */
bitmask_clearall(struct bitmask * bmp)131*4882a593Smuzhiyun struct bitmask *bitmask_clearall(struct bitmask *bmp)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	unsigned int i;
134*4882a593Smuzhiyun 	for (i = 0; i < bmp->size; i++)
135*4882a593Smuzhiyun 		_setbit(bmp, i, 0);
136*4882a593Smuzhiyun 	return bmp;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /* True if all bits are clear */
bitmask_isallclear(const struct bitmask * bmp)140*4882a593Smuzhiyun int bitmask_isallclear(const struct bitmask *bmp)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	unsigned int i;
143*4882a593Smuzhiyun 	for (i = 0; i < bmp->size; i++)
144*4882a593Smuzhiyun 		if (_getbit(bmp, i))
145*4882a593Smuzhiyun 			return 0;
146*4882a593Smuzhiyun 	return 1;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun /* True if specified bit i is set */
bitmask_isbitset(const struct bitmask * bmp,unsigned int i)150*4882a593Smuzhiyun int bitmask_isbitset(const struct bitmask *bmp, unsigned int i)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	return _getbit(bmp, i);
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /* Number of lowest set bit (min) */
bitmask_first(const struct bitmask * bmp)156*4882a593Smuzhiyun unsigned int bitmask_first(const struct bitmask *bmp)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	return bitmask_next(bmp, 0);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun /* Number of highest set bit (max) */
bitmask_last(const struct bitmask * bmp)162*4882a593Smuzhiyun unsigned int bitmask_last(const struct bitmask *bmp)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	unsigned int i;
165*4882a593Smuzhiyun 	unsigned int m = bmp->size;
166*4882a593Smuzhiyun 	for (i = 0; i < bmp->size; i++)
167*4882a593Smuzhiyun 		if (_getbit(bmp, i))
168*4882a593Smuzhiyun 			m = i;
169*4882a593Smuzhiyun 	return m;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun /* Number of next set bit at or above given bit i */
bitmask_next(const struct bitmask * bmp,unsigned int i)173*4882a593Smuzhiyun unsigned int bitmask_next(const struct bitmask *bmp, unsigned int i)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	unsigned int n;
176*4882a593Smuzhiyun 	for (n = i; n < bmp->size; n++)
177*4882a593Smuzhiyun 		if (_getbit(bmp, n))
178*4882a593Smuzhiyun 			break;
179*4882a593Smuzhiyun 	return n;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun  * Parses a comma-separated list of numbers and ranges of numbers,
184*4882a593Smuzhiyun  * with optional ':%u' strides modifying ranges, into provided bitmask.
185*4882a593Smuzhiyun  * Some examples of input lists and their equivalent simple list:
186*4882a593Smuzhiyun  *	Input		Equivalent to
187*4882a593Smuzhiyun  *	0-3		0,1,2,3
188*4882a593Smuzhiyun  *	0-7:2		0,2,4,6
189*4882a593Smuzhiyun  *	1,3,5-7		1,3,5,6,7
190*4882a593Smuzhiyun  *	0-3:2,8-15:4	0,2,8,12
191*4882a593Smuzhiyun  */
bitmask_parselist(const char * buf,struct bitmask * bmp)192*4882a593Smuzhiyun int bitmask_parselist(const char *buf, struct bitmask *bmp)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	const char *p, *q;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	bitmask_clearall(bmp);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	q = buf;
199*4882a593Smuzhiyun 	while (p = q, q = nexttoken(q, ','), p) {
200*4882a593Smuzhiyun 		unsigned int a;		/* begin of range */
201*4882a593Smuzhiyun 		unsigned int b;		/* end of range */
202*4882a593Smuzhiyun 		unsigned int s;		/* stride */
203*4882a593Smuzhiyun 		const char *c1, *c2;	/* next tokens after '-' or ',' */
204*4882a593Smuzhiyun 		char nextc;		/* char after sscanf %u match */
205*4882a593Smuzhiyun 		int sret;		/* sscanf return (number of matches) */
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 		sret = sscanf(p, "%u%c", &a, &nextc);
208*4882a593Smuzhiyun 		if (!scan_was_ok(sret, nextc, ",-"))
209*4882a593Smuzhiyun 			goto err;
210*4882a593Smuzhiyun 		b = a;
211*4882a593Smuzhiyun 		s = 1;
212*4882a593Smuzhiyun 		c1 = nexttoken(p, '-');
213*4882a593Smuzhiyun 		c2 = nexttoken(p, ',');
214*4882a593Smuzhiyun 		if (c1 != NULL && (c2 == NULL || c1 < c2)) {
215*4882a593Smuzhiyun 			sret = sscanf(c1, "%u%c", &b, &nextc);
216*4882a593Smuzhiyun 			if (!scan_was_ok(sret, nextc, ",:"))
217*4882a593Smuzhiyun 				goto err;
218*4882a593Smuzhiyun 			c1 = nexttoken(c1, ':');
219*4882a593Smuzhiyun 			if (c1 != NULL && (c2 == NULL || c1 < c2)) {
220*4882a593Smuzhiyun 				sret = sscanf(c1, "%u%c", &s, &nextc);
221*4882a593Smuzhiyun 				if (!scan_was_ok(sret, nextc, ","))
222*4882a593Smuzhiyun 					goto err;
223*4882a593Smuzhiyun 			}
224*4882a593Smuzhiyun 		}
225*4882a593Smuzhiyun 		if (!(a <= b))
226*4882a593Smuzhiyun 			goto err;
227*4882a593Smuzhiyun 		if (b >= bmp->size)
228*4882a593Smuzhiyun 			goto err;
229*4882a593Smuzhiyun 		while (a <= b) {
230*4882a593Smuzhiyun 			_setbit(bmp, a, 1);
231*4882a593Smuzhiyun 			a += s;
232*4882a593Smuzhiyun 		}
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 	return 0;
235*4882a593Smuzhiyun err:
236*4882a593Smuzhiyun 	bitmask_clearall(bmp);
237*4882a593Smuzhiyun 	return -1;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun /*
241*4882a593Smuzhiyun  * emit(buf, buflen, rbot, rtop, len)
242*4882a593Smuzhiyun  *
243*4882a593Smuzhiyun  * Helper routine for bitmask_displaylist().  Write decimal number
244*4882a593Smuzhiyun  * or range to buf+len, suppressing output past buf+buflen, with optional
245*4882a593Smuzhiyun  * comma-prefix.  Return len of what would be written to buf, if it
246*4882a593Smuzhiyun  * all fit.
247*4882a593Smuzhiyun  */
248*4882a593Smuzhiyun 
emit(char * buf,int buflen,int rbot,int rtop,int len)249*4882a593Smuzhiyun static inline int emit(char *buf, int buflen, int rbot, int rtop, int len)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	if (len > 0)
252*4882a593Smuzhiyun 		len += snprintf(buf + len, max(buflen - len, 0), ",");
253*4882a593Smuzhiyun 	if (rbot == rtop)
254*4882a593Smuzhiyun 		len += snprintf(buf + len, max(buflen - len, 0), "%d", rbot);
255*4882a593Smuzhiyun 	else
256*4882a593Smuzhiyun 		len += snprintf(buf + len, max(buflen - len, 0), "%d-%d",
257*4882a593Smuzhiyun 				rbot, rtop);
258*4882a593Smuzhiyun 	return len;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun /*
262*4882a593Smuzhiyun  * Write decimal list representation of bmp to buf.
263*4882a593Smuzhiyun  *
264*4882a593Smuzhiyun  * Output format is a comma-separated list of decimal numbers and
265*4882a593Smuzhiyun  * ranges.  Consecutively set bits are shown as two hyphen-separated
266*4882a593Smuzhiyun  * decimal numbers, the smallest and largest bit numbers set in
267*4882a593Smuzhiyun  * the range.  Output format is compatible with the format
268*4882a593Smuzhiyun  * accepted as input by bitmap_parselist().
269*4882a593Smuzhiyun  *
270*4882a593Smuzhiyun  * The return value is the number of characters which would be
271*4882a593Smuzhiyun  * generated for the given input, excluding the trailing '\0', as
272*4882a593Smuzhiyun  * per ISO C99.
273*4882a593Smuzhiyun  */
274*4882a593Smuzhiyun 
bitmask_displaylist(char * buf,int buflen,const struct bitmask * bmp)275*4882a593Smuzhiyun int bitmask_displaylist(char *buf, int buflen, const struct bitmask *bmp)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	int len = 0;
278*4882a593Smuzhiyun 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
279*4882a593Smuzhiyun 	unsigned int cur, rbot, rtop;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (buflen > 0)
282*4882a593Smuzhiyun 		*buf = 0;
283*4882a593Smuzhiyun 	rbot = cur = bitmask_first(bmp);
284*4882a593Smuzhiyun 	while (cur < bmp->size) {
285*4882a593Smuzhiyun 		rtop = cur;
286*4882a593Smuzhiyun 		cur = bitmask_next(bmp, cur+1);
287*4882a593Smuzhiyun 		if (cur >= bmp->size || cur > rtop + 1) {
288*4882a593Smuzhiyun 			len = emit(buf, buflen, rbot, rtop, len);
289*4882a593Smuzhiyun 			rbot = cur;
290*4882a593Smuzhiyun 		}
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 	return len;
293*4882a593Smuzhiyun }
294