File: bitmap.h

package info (click to toggle)
vzctl 3.0.30.2-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,056 kB
  • sloc: ansic: 17,282; sh: 14,549; makefile: 528
file content (78 lines) | stat: -rw-r--r-- 2,329 bytes parent folder | download | duplicates (5)
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
/*
 *  Copyright (C) 2010-2011, Parallels, Inc. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU 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
 */

#ifndef _BITMAP_H_
#define _BITMAP_H_

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

#ifndef DIV_ROUND_UP
#define DIV_ROUND_UP(n,d)	(((n) + (d) - 1) / (d))
#endif
#ifndef MIN
#define MIN(a, b)		((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b)		((a) > (b) ? (a) : (b))
#endif

#define BITS_PER_BYTE		8
#define BITS_PER_LONG		(BITS_PER_BYTE * sizeof(long))

#define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_LONG)

#define BIT_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
#define BIT_WORD(nr)		((nr) / BITS_PER_LONG)

static inline unsigned long *alloc_bitmap(int nmaskbits)
{
	return malloc(BITS_TO_LONGS(nmaskbits) * sizeof(long));
}

static inline void bitmap_zero(unsigned long *maskp, int nmaskbits)
{
	memset(maskp, 0, BITS_TO_LONGS(nmaskbits) * sizeof(long));
}

static inline void bitmap_set_all(unsigned long *maskp, int nmaskbits)
{
	memset(maskp, -1, BITS_TO_LONGS(nmaskbits) * sizeof(long));
}

static inline int bitmap_test_bit(int nr, const unsigned long *maskp)
{
	return (maskp[BIT_WORD(nr)] & BIT_MASK(nr)) != 0;
}

static inline void bitmap_set_bit(int nr, unsigned long *maskp)
{
	maskp[BIT_WORD(nr)] |= BIT_MASK(nr);
}

static inline void bitmap_clear_bit(int nr, unsigned long *maskp)
{
	maskp[BIT_WORD(nr)] &= ~BIT_MASK(nr);
}

int bitmap_find_first_zero_bit(const unsigned long *maskp, int nmaskbits);
int bitmap_parse(const char *str, unsigned long *maskp, int nmaskbits);
int bitmap_snprintf(char *buf, unsigned int buflen,
		    const unsigned long *maskp, int nmaskbits);

#endif /* _BITMAP_H_ */