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