File: libbitmask.c

package info (click to toggle)
libbitmask 2.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 588 kB
  • ctags: 152
  • sloc: sh: 786; ansic: 477; makefile: 58
file content (676 lines) | stat: -rw-r--r-- 16,266 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
 * bitmask user library implementation.
 *
 * Copyright (c) 2004-2006 Silicon Graphics, Inc. All rights reserved.
 *
 * Paul Jackson <pj@sgi.com>
 */

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "bitmask.h"

struct bitmask {
	unsigned int size;
	unsigned long *maskp;
};

/* How many bits in an unsigned long */
#define bitsperlong (8 * sizeof(unsigned long))

/* howmany(a,b) : how many elements of size b needed to hold all of a */
#define howmany(x,y) (((x)+((y)-1))/(y))

/* How many longs in mask of n bits */
#define longsperbits(n) howmany(n, bitsperlong)

/*
 * The routines _getbit() and _setbit() are the only
 * routines that actually understand the layout of bmp->maskp[].
 *
 * On little endian architectures, this could simply be an array of
 * bytes.  But the kernel layout of bitmasks _is_ visible to userspace
 * via the sched_(set/get)affinity calls in Linux 2.6, and on big
 * endian architectures, it is painfully obvious that this is an
 * array of unsigned longs.
 */

/* Return the value (0 or 1) of bit n in bitmask bmp */
static unsigned int _getbit(const struct bitmask *bmp, unsigned int n)
{
	if (n < bmp->size)
		return (bmp->maskp[n/bitsperlong] >> (n % bitsperlong)) & 1;
	else
		return 0;
}

/* Set bit n in bitmask bmp to value v (0 or 1) */
static void _setbit(struct bitmask *bmp, unsigned int n, unsigned int v)
{
	if (n < bmp->size) {
		if (v)
			bmp->maskp[n/bitsperlong] |= 1UL << (n % bitsperlong);
		else
			bmp->maskp[n/bitsperlong] &= ~(1UL << (n % bitsperlong));
	}
}

/*
 * Allocate and free `struct bitmask *`
 */

/* Allocate a new `struct bitmask` with a size of n bits */
struct bitmask *bitmask_alloc(unsigned int n)
{
	struct bitmask *bmp;

	bmp = malloc(sizeof(*bmp));
	if (bmp == 0)
		return 0;
	bmp->size = n;
	bmp->maskp = calloc(longsperbits(n), sizeof(unsigned long));
	if (bmp->maskp == 0) {
		free(bmp);
		return 0;
	}
	return bmp;
}

/* Free `struct bitmask` */
void bitmask_free(struct bitmask *bmp)
{
	if (bmp == 0)
		return;
	free(bmp->maskp);
	bmp->maskp = (unsigned long *)0xdeadcdef;  /* double free tripwire */
	free(bmp);
}

/*
 * Display and parse ascii string representations.
 */

#define HEXCHUNKSZ 32	/* hex binary format shows 32 bits per chunk */
#define HEXCHARSZ 8	/* hex ascii format has up to 8 chars per chunk */
#define max(a,b) ((a) > (b) ? (a) : (b))

/*
 * Write hex word representation of bmp to buf, 32 bits per
 * comma-separated, zero-filled hex word.  Do not write more
 * than buflen chars to buf.
 *
 * Return number of chars that would have been written
 * if buf were large enough.
 */

int bitmask_displayhex(char *buf, int buflen, const struct bitmask *bmp)
{
	int chunk;
	int cnt = 0;
	const char *sep = "";

	if (buflen < 1)
		return 0;
	buf[0] = 0;

	for (chunk = howmany(bmp->size, HEXCHUNKSZ) - 1; chunk >= 0; chunk--) {
		uint32_t val = 0;
		int bit;

		for (bit = HEXCHUNKSZ - 1; bit >= 0; bit--)
			val = val << 1 | _getbit(bmp, chunk * HEXCHUNKSZ + bit);
		cnt += snprintf(buf + cnt, max(buflen - cnt, 0), "%s%0*x",
			sep, HEXCHARSZ, val);
		sep = ",";
	}
	return cnt;
}

/*
 * emit(buf, buflen, rbot, rtop, len)
 *
 * Helper routine for bitmask_displaylist().  Write decimal number
 * or range to buf+len, suppressing output past buf+buflen, with optional
 * comma-prefix.  Return len of what would be written to buf, if it
 * all fit.
 */

static inline int emit(char *buf, int buflen, int rbot, int rtop, int len)
{
	if (len > 0)
		len += snprintf(buf + len, max(buflen - len, 0), ",");
	if (rbot == rtop)
		len += snprintf(buf + len, max(buflen - len, 0), "%d", rbot);
	else
		len += snprintf(buf + len, max(buflen - len, 0), "%d-%d", rbot, rtop);
	return len;
}

/*
 * Write decimal list representation of bmp to buf.
 *
 * Output format is a comma-separated list of decimal numbers and
 * ranges.  Consecutively set bits are shown as two hyphen-separated
 * decimal numbers, the smallest and largest bit numbers set in
 * the range.  Output format is compatible with the format
 * accepted as input by bitmap_parselist().
 *
 * The return value is the number of characters which would be
 * generated for the given input, excluding the trailing '\0', as
 * per ISO C99.
 */

int bitmask_displaylist(char *buf, int buflen, const struct bitmask *bmp)
{
	int len = 0;
	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
	int cur, rbot, rtop;

	if (buflen > 0)
		*buf = 0;
	rbot = cur = bitmask_first(bmp);
	while (cur < bmp->size) {
		rtop = cur;
		cur = bitmask_next(bmp, cur+1);
		if (cur >= bmp->size || cur > rtop + 1) {
			len = emit(buf, buflen, rbot, rtop, len);
			rbot = cur;
		}
	}
	return len;
}

static const char *nexttoken(const char *q,  int sep)
{
	if (q)
		q = strchr(q, sep);
	if (q)
		q++;
	return q;
}

/*
 * Parse hex word representation in buf to bmp.
 *
 * Returns -1 on error, leaving unspecified results in bmp.
 */

int bitmask_parsehex(const char *buf, struct bitmask *bmp)
{
	const char *p, *q;
	int nchunks = 0, chunk;

	bitmask_clearall(bmp);

	q = buf;
	while (p = q, q = nexttoken(q, ','), p)
		nchunks++;

	chunk = nchunks - 1;
	q = buf;
	while (p = q, q = nexttoken(q, ','), p) {
		uint32_t val;
		int bit;
		char *endptr;
		int nchars_read, nchars_unread;

		val = strtoul(p, &endptr, 16);

		/* We should have consumed 1 to 8 (HEXCHARSZ) chars */
		nchars_read = endptr - p;
		if (nchars_read < 1 || nchars_read > HEXCHARSZ)
			goto err;

		/* We should have consumed up to next comma */
		nchars_unread = q - endptr;
		if (q != NULL && nchars_unread != 1)
			goto err;

		for (bit = HEXCHUNKSZ - 1; bit >= 0; bit--) {
			int n = chunk * HEXCHUNKSZ + bit;
			if (n >= bmp->size)
				goto err;
			_setbit(bmp, n, (val >> bit) & 1);
		}
		chunk--;
	}
	return 0;
err:
	bitmask_clearall(bmp);
	return -1;
}

/*
 * When parsing bitmask lists, only allow numbers, separated by one
 * of the allowed next characters.
 *
 * The parameter 'sret' is the return from a sscanf "%u%c".  It is
 * -1 if the sscanf input string was empty.  It is 0 if the first
 * character in the sscanf input string was not a decimal number.
 * It is 1 if the unsigned number matching the "%u" was the end of the
 * input string.  It is 2 if one or more additional characters followed
 * the matched unsigned number.  If it is 2, then 'nextc' is the first
 * character following the number.  The parameter 'ok_next_chars'
 * is the nul-terminated list of allowed next characters.
 *
 * The mask term just scanned was ok if and only if either the numbers
 * matching the %u were all of the input or if the next character in
 * the input past the numbers was one of the allowed next characters.
 */
static int scan_was_ok(int sret, char nextc, const char *ok_next_chars)
{
	return sret == 1 ||
		(sret == 2 && strchr(ok_next_chars, nextc) != NULL);
}

/*
 * Parses a comma-separated list of numbers and ranges of numbers,
 * with optional ':%u' strides modifying ranges, into provided bitmask.
 * Some examples of input lists and their equivalent simple list:
 *	Input		Equivalent to
 *	0-3		0,1,2,3
 *	0-7:2		0,2,4,6
 *	1,3,5-7		1,3,5,6,7
 *	0-3:2,8-15:4	0,2,8,12	
 */
int bitmask_parselist(const char *buf, struct bitmask *bmp)
{
	const char *p, *q;

	bitmask_clearall(bmp);

	q = buf;
	while (p = q, q = nexttoken(q, ','), p) {
		unsigned int a;		/* begin of range */
		unsigned int b;		/* end of range */
		unsigned int s;		/* stride */
		const char *c1, *c2;	/* next tokens after '-' or ',' */
		char nextc;		/* char after sscanf %u match */
		int sret;		/* sscanf return (number of matches) */

		sret = sscanf(p, "%u%c", &a, &nextc);
		if (!scan_was_ok(sret, nextc, ",-"))
			goto err;
		b = a;
		s = 1;
		c1 = nexttoken(p, '-');
		c2 = nexttoken(p, ',');
		if (c1 != NULL && (c2 == NULL || c1 < c2)) {
			sret = sscanf(c1, "%u%c", &b, &nextc);
			if (!scan_was_ok(sret, nextc, ",:"))
				goto err;
			c1 = nexttoken(c1, ':');
			if (c1 != NULL && (c2 == NULL || c1 < c2)) {
				sret = sscanf(c1, "%u%c", &s, &nextc);
				if (!scan_was_ok(sret, nextc, ","))
					goto err;
			}
		}
		if (!(a <= b))
			goto err;
		if (b >= bmp->size)
			goto err;
		while (a <= b) {
			_setbit(bmp, a, 1);
			a += s;
		}
	}
	return 0;
err:
	bitmask_clearall(bmp);
	return -1;
}

/*
 * Basic assignment operations
 */

/* Copy bmp2 to bmp1 */
struct bitmask *bitmask_copy(struct bitmask *bmp1, const struct bitmask *bmp2)
{
	unsigned int i;
	for (i = 0; i < bmp1->size; i++)
		_setbit(bmp1, i, _getbit(bmp2, i));
	return bmp1;
}

/* Set all bits in bitmask: bmp = ~0 */
struct bitmask *bitmask_setall(struct bitmask *bmp)
{
	unsigned int i;
	for (i = 0; i < bmp->size; i++)
		_setbit(bmp, i, 1);
	return bmp;
}

/* Clear all bits in bitmask: bmp = 0 */
struct bitmask *bitmask_clearall(struct bitmask *bmp)
{
	unsigned int i;
	for (i = 0; i < bmp->size; i++)
		_setbit(bmp, i, 0);
	return bmp;
}

/*
 * Interface to kernel sched_setaffinity system call
 */

/* Length in bytes of mask - use as second argument to sched_setaffinity */
unsigned int bitmask_nbytes(struct bitmask *bmp)
{
	return longsperbits(bmp->size) * sizeof(unsigned long);
}

/* Direct pointer to bit mask - use as third argument to sched_setaffinty */
unsigned long *bitmask_mask(struct bitmask *bmp)
{
	return bmp->maskp;
}

/*
 * Unary numeric queries
 */

/* Size in bits of entire bitmask */
unsigned int bitmask_nbits(const struct bitmask *bmp)
{
	return bmp->size;
}

/* Hamming Weight: number of set bits */
unsigned int bitmask_weight(const struct bitmask *bmp)
{
	unsigned int i;
	unsigned int w = 0;
	for (i = 0; i < bmp->size; i++)
		if (_getbit(bmp, i))
			w++;
	return w;
}

/*
 * Unary Boolean queries
 */

/* True if specified bit i is set */
int bitmask_isbitset(const struct bitmask *bmp, unsigned int i)
{
	return _getbit(bmp, i);
}

/* True if specified bit i is clear */
int bitmask_isbitclear(const struct bitmask *bmp, unsigned int i)
{
	return ! _getbit(bmp, i);
}

/* True if all bits are set */
int bitmask_isallset(const struct bitmask *bmp)
{
	unsigned int i;
	for (i = 0; i < bmp->size; i++)
		if (! _getbit(bmp, i))
			return 0;
	return 1;
}

/* True if all bits are clear */
int bitmask_isallclear(const struct bitmask *bmp)
{
	unsigned int i;
	for (i = 0; i < bmp->size; i++)
		if (_getbit(bmp, i))
			return 0;
	return 1;
}

/*
 * Single bit operations
 */

/* Set a single bit i in bitmask */
struct bitmask *bitmask_setbit(struct bitmask *bmp, unsigned int i)
{
	_setbit(bmp, i, 1);
	return bmp;
}

/* Clear a single bit i in bitmask */
struct bitmask *bitmask_clearbit(struct bitmask *bmp, unsigned int i)
{
	_setbit(bmp, i, 0);
	return bmp;
}

/*
 * Binary Boolean operations: bmp1 op? bmp2
 */

/* True if two bitmasks are equal */
int bitmask_equal(const struct bitmask *bmp1, const struct bitmask *bmp2)
{
	unsigned int i;
	for (i = 0; i < bmp1->size || i < bmp2->size; i++)
		if (_getbit(bmp1, i) != _getbit(bmp2, i))
			return 0;
	return 1;
}

/* True if first bitmask is subset of second */
int bitmask_subset(const struct bitmask *bmp1, const struct bitmask *bmp2)
{
	unsigned int i;
	for (i = 0; i < bmp1->size; i++)
		if (_getbit(bmp1, i) > _getbit(bmp2, i))
			return 0;
	return 1;
}

/* True if two bitmasks don't overlap */
int bitmask_disjoint(const struct bitmask *bmp1, const struct bitmask *bmp2)
{
	unsigned int i;
	for (i = 0; i < bmp1->size; i++)
		if (_getbit(bmp1, i) & _getbit(bmp2, i))
			return 0;
	return 1;
}

/* True if two bitmasks do overlap */
int bitmask_intersects(const struct bitmask *bmp1, const struct bitmask *bmp2)
{
	unsigned int i;
	for (i = 0; i < bmp1->size; i++)
		if (_getbit(bmp1, i) & _getbit(bmp2, i))
			return 1;
	return 0;
}

/*
 * Range operations
 */

/* Set bits of bitmask in specified range [i, j) */
struct bitmask *bitmask_setrange(struct bitmask *bmp,
				unsigned int i, unsigned int j)
{
	unsigned int n;
	for (n = i; n < j; n++)
		_setbit(bmp, n, 1);
	return bmp;
}

/* Clear bits of bitmask in specified range */
struct bitmask *bitmask_clearrange(struct bitmask *bmp,
				unsigned int i, unsigned int j)
{
	unsigned int n;
	for (n = i; n < j; n++)
		_setbit(bmp, n, 0);
	return bmp;
}

/* Clear all but specified range */
struct bitmask *bitmask_keeprange(struct bitmask *bmp,
				unsigned int i, unsigned int j)
{
	bitmask_clearrange(bmp, 0, i);
	bitmask_clearrange(bmp, j, bmp->size);
	return bmp;
}

/*
 * Unary operations: bmp1 = op(struct bitmask *bmp2)
 */

/* Complement: bmp1 = ~bmp2 */
struct bitmask *bitmask_complement(struct bitmask *bmp1,
				const struct bitmask *bmp2)
{
	unsigned int i;
	for (i = 0; i < bmp1->size; i++)
		_setbit(bmp1, i, ! _getbit(bmp2, i));
	return bmp1;
}

/* Right shift: bmp1 = bmp2 >> n */
struct bitmask *bitmask_shiftright(struct bitmask *bmp1,
				const struct bitmask *bmp2, unsigned int n)
{
	unsigned int i;
	for (i = 0; i < bmp1->size; i++)
		_setbit(bmp1, i, _getbit(bmp2, i+n));
	return bmp1;
}

/* Left shift: bmp1 = bmp2 << n */
struct bitmask *bitmask_shiftleft(struct bitmask *bmp1,
				const struct bitmask *bmp2, unsigned int n)
{
	int i;
	for (i = bmp1->size - 1; i >= 0; i--)
		_setbit(bmp1, i, _getbit(bmp2, i-n));
	return bmp1;
}

/*
 * Binary operations: bmp1 = bmp2 op bmp3
 */

/* Logical `and` of two bitmasks: bmp1 = bmp2 & bmp3 */
struct bitmask *bitmask_and(struct bitmask *bmp1, const struct bitmask *bmp2,
				const struct bitmask *bmp3)
{
	unsigned int i;
	for (i = 0; i < bmp1->size; i++)
		_setbit(bmp1, i, _getbit(bmp2, i) & _getbit(bmp3, i));
	return bmp1;
}

/* Logical `andnot` of two bitmasks: bmp1 = bmp2 & ~bmp3 */
struct bitmask *bitmask_andnot(struct bitmask *bmp1, const struct bitmask *bmp2,
				const struct bitmask *bmp3)
{
	unsigned int i;
	for (i = 0; i < bmp1->size; i++)
		_setbit(bmp1, i, _getbit(bmp2, i) & ~_getbit(bmp3, i));
	return bmp1;
}

/* Logical `or` of two bitmasks: bmp1 = bmp2 | bmp3 */
struct bitmask *bitmask_or(struct bitmask *bmp1, const struct bitmask *bmp2,
				const struct bitmask *bmp3)
{
	unsigned int i;
	for (i = 0; i < bmp1->size; i++)
		_setbit(bmp1, i, _getbit(bmp2, i) | _getbit(bmp3, i));
	return bmp1;
}

/* Logical `eor` of two bitmasks: bmp1 = bmp2 ^ bmp3 */
struct bitmask *bitmask_eor(struct bitmask *bmp1, const struct bitmask *bmp2,
				const struct bitmask *bmp3)
{
	unsigned int i;
	for (i = 0; i < bmp1->size; i++)
		_setbit(bmp1, i, _getbit(bmp2, i) ^ _getbit(bmp3, i));
	return bmp1;
}


/*
 * Iteration operators
 */

/* Number of lowest set bit (min) */
unsigned int bitmask_first(const struct bitmask *bmp)
{
	return bitmask_next(bmp, 0);
}

/* Number of next set bit at or above given bit i */
unsigned int bitmask_next(const struct bitmask *bmp, unsigned int i)
{
	unsigned int n;
	for (n = i; n < bmp->size; n++)
		if (_getbit(bmp, n))
			break;
	return n;
}

/* Absolute position of nth set bit */
unsigned int bitmask_rel_to_abs_pos(const struct bitmask *bmp, unsigned int n)
{
	unsigned int i;
	for (i = 0; i < bmp->size; i++)
		if (_getbit(bmp, i))
			if (n-- == 0)
				break;
	return i;
}

/* Relative position amongst set bits of bit n */
unsigned int bitmask_abs_to_rel_pos(const struct bitmask *bmp, unsigned int n)
{
	unsigned int i;
	unsigned int w = 0;

	if (!_getbit(bmp, n))
		return bmp->size;
	/* Add in number bits set before bit n */
	for (i = 0; i < n; i++)
		if (_getbit(bmp, i))
			w++;
	return w;
}

/* Number of highest set bit (max) */
unsigned int bitmask_last(const struct bitmask *bmp)
{
	unsigned int i;
	unsigned int m = bmp->size;
	for (i = 0; i < bmp->size; i++)
		if (_getbit(bmp, i))
			m = i;
	return m;
}