Path: blob/master/tools/power/cpupower/utils/helpers/bitmask.c
26299 views
// SPDX-License-Identifier: GPL-2.01#include <stdio.h>2#include <stdlib.h>3#include <string.h>45#include <helpers/bitmask.h>67/* How many bits in an unsigned long */8#define bitsperlong (8 * sizeof(unsigned long))910/* howmany(a,b) : how many elements of size b needed to hold all of a */11#define howmany(x, y) (((x)+((y)-1))/(y))1213/* How many longs in mask of n bits */14#define longsperbits(n) howmany(n, bitsperlong)1516#define max(a, b) ((a) > (b) ? (a) : (b))1718/*19* Allocate and free `struct bitmask *`20*/2122/* Allocate a new `struct bitmask` with a size of n bits */23struct bitmask *bitmask_alloc(unsigned int n)24{25struct bitmask *bmp;2627bmp = malloc(sizeof(*bmp));28if (!bmp)29return 0;30bmp->size = n;31bmp->maskp = calloc(longsperbits(n), sizeof(unsigned long));32if (!bmp->maskp) {33free(bmp);34return 0;35}36return bmp;37}3839/* Free `struct bitmask` */40void bitmask_free(struct bitmask *bmp)41{42if (!bmp)43return;44free(bmp->maskp);45bmp->maskp = (unsigned long *)0xdeadcdef; /* double free tripwire */46free(bmp);47}4849/*50* The routines _getbit() and _setbit() are the only51* routines that actually understand the layout of bmp->maskp[].52*53* On little endian architectures, this could simply be an array of54* bytes. But the kernel layout of bitmasks _is_ visible to userspace55* via the sched_(set/get)affinity calls in Linux 2.6, and on big56* endian architectures, it is painfully obvious that this is an57* array of unsigned longs.58*/5960/* Return the value (0 or 1) of bit n in bitmask bmp */61static unsigned int _getbit(const struct bitmask *bmp, unsigned int n)62{63if (n < bmp->size)64return (bmp->maskp[n/bitsperlong] >> (n % bitsperlong)) & 1;65else66return 0;67}6869/* Set bit n in bitmask bmp to value v (0 or 1) */70static void _setbit(struct bitmask *bmp, unsigned int n, unsigned int v)71{72if (n < bmp->size) {73if (v)74bmp->maskp[n/bitsperlong] |= 1UL << (n % bitsperlong);75else76bmp->maskp[n/bitsperlong] &=77~(1UL << (n % bitsperlong));78}79}8081/*82* When parsing bitmask lists, only allow numbers, separated by one83* of the allowed next characters.84*85* The parameter 'sret' is the return from a sscanf "%u%c". It is86* -1 if the sscanf input string was empty. It is 0 if the first87* 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 the89* input string. It is 2 if one or more additional characters followed90* the matched unsigned number. If it is 2, then 'nextc' is the first91* character following the number. The parameter 'ok_next_chars'92* is the nul-terminated list of allowed next characters.93*94* The mask term just scanned was ok if and only if either the numbers95* matching the %u were all of the input or if the next character in96* the input past the numbers was one of the allowed next characters.97*/98static int scan_was_ok(int sret, char nextc, const char *ok_next_chars)99{100return sret == 1 ||101(sret == 2 && strchr(ok_next_chars, nextc) != NULL);102}103104static const char *nexttoken(const char *q, int sep)105{106if (q)107q = strchr(q, sep);108if (q)109q++;110return q;111}112113/* Set a single bit i in bitmask */114struct bitmask *bitmask_setbit(struct bitmask *bmp, unsigned int i)115{116_setbit(bmp, i, 1);117return bmp;118}119120/* Set all bits in bitmask: bmp = ~0 */121struct bitmask *bitmask_setall(struct bitmask *bmp)122{123unsigned int i;124for (i = 0; i < bmp->size; i++)125_setbit(bmp, i, 1);126return bmp;127}128129/* Clear all bits in bitmask: bmp = 0 */130struct bitmask *bitmask_clearall(struct bitmask *bmp)131{132unsigned int i;133for (i = 0; i < bmp->size; i++)134_setbit(bmp, i, 0);135return bmp;136}137138/* True if all bits are clear */139int bitmask_isallclear(const struct bitmask *bmp)140{141unsigned int i;142for (i = 0; i < bmp->size; i++)143if (_getbit(bmp, i))144return 0;145return 1;146}147148/* True if specified bit i is set */149int bitmask_isbitset(const struct bitmask *bmp, unsigned int i)150{151return _getbit(bmp, i);152}153154/* Number of lowest set bit (min) */155unsigned int bitmask_first(const struct bitmask *bmp)156{157return bitmask_next(bmp, 0);158}159160/* Number of highest set bit (max) */161unsigned int bitmask_last(const struct bitmask *bmp)162{163unsigned int i;164unsigned int m = bmp->size;165for (i = 0; i < bmp->size; i++)166if (_getbit(bmp, i))167m = i;168return m;169}170171/* Number of next set bit at or above given bit i */172unsigned int bitmask_next(const struct bitmask *bmp, unsigned int i)173{174unsigned int n;175for (n = i; n < bmp->size; n++)176if (_getbit(bmp, n))177break;178return n;179}180181/*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 to186* 0-3 0,1,2,3187* 0-7:2 0,2,4,6188* 1,3,5-7 1,3,5,6,7189* 0-3:2,8-15:4 0,2,8,12190*/191int bitmask_parselist(const char *buf, struct bitmask *bmp)192{193const char *p, *q;194195bitmask_clearall(bmp);196197q = buf;198while (p = q, q = nexttoken(q, ','), p) {199unsigned int a; /* begin of range */200unsigned int b; /* end of range */201unsigned int s; /* stride */202const char *c1, *c2; /* next tokens after '-' or ',' */203char nextc; /* char after sscanf %u match */204int sret; /* sscanf return (number of matches) */205206sret = sscanf(p, "%u%c", &a, &nextc);207if (!scan_was_ok(sret, nextc, ",-"))208goto err;209b = a;210s = 1;211c1 = nexttoken(p, '-');212c2 = nexttoken(p, ',');213if (c1 != NULL && (c2 == NULL || c1 < c2)) {214sret = sscanf(c1, "%u%c", &b, &nextc);215if (!scan_was_ok(sret, nextc, ",:"))216goto err;217c1 = nexttoken(c1, ':');218if (c1 != NULL && (c2 == NULL || c1 < c2)) {219sret = sscanf(c1, "%u%c", &s, &nextc);220if (!scan_was_ok(sret, nextc, ","))221goto err;222}223}224if (!(a <= b))225goto err;226if (b >= bmp->size)227goto err;228while (a <= b) {229_setbit(bmp, a, 1);230a += s;231}232}233return 0;234err:235bitmask_clearall(bmp);236return -1;237}238239/*240* emit(buf, buflen, rbot, rtop, len)241*242* Helper routine for bitmask_displaylist(). Write decimal number243* or range to buf+len, suppressing output past buf+buflen, with optional244* comma-prefix. Return len of what would be written to buf, if it245* all fit.246*/247248static inline int emit(char *buf, int buflen, int rbot, int rtop, int len)249{250if (len > 0)251len += snprintf(buf + len, max(buflen - len, 0), ",");252if (rbot == rtop)253len += snprintf(buf + len, max(buflen - len, 0), "%d", rbot);254else255len += snprintf(buf + len, max(buflen - len, 0), "%d-%d",256rbot, rtop);257return len;258}259260/*261* Write decimal list representation of bmp to buf.262*263* Output format is a comma-separated list of decimal numbers and264* ranges. Consecutively set bits are shown as two hyphen-separated265* decimal numbers, the smallest and largest bit numbers set in266* the range. Output format is compatible with the format267* accepted as input by bitmap_parselist().268*269* The return value is the number of characters which would be270* generated for the given input, excluding the trailing '\0', as271* per ISO C99.272*/273274int bitmask_displaylist(char *buf, int buflen, const struct bitmask *bmp)275{276int len = 0;277/* current bit is 'cur', most recently seen range is [rbot, rtop] */278unsigned int cur, rbot, rtop;279280if (buflen > 0)281*buf = 0;282rbot = cur = bitmask_first(bmp);283while (cur < bmp->size) {284rtop = cur;285cur = bitmask_next(bmp, cur+1);286if (cur >= bmp->size || cur > rtop + 1) {287len = emit(buf, buflen, rbot, rtop, len);288rbot = cur;289}290}291return len;292}293294295