-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap.h
125 lines (107 loc) · 3.08 KB
/
bitmap.h
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
/*
* ksm - a really simple and fast x64 hypervisor
* Copyright (C) 2016, 2017 Ahmed Samy <[email protected]>
*
* A simple bitmap to easily manage large bitmaps. Bitmaps can be
* 32-bit or 64-bit, 64-bit if on GCC or so, because unsigned long is
* 64-bit there, however, MSVC treats unsigned long as 32-bit, so each
* entry can handle up to that and is determined via BITMAP_NBITS.
*
* For usage examples, see ksm.c: init_msr_bitmaps() / init_io_bitmaps().
* Those initialize the MSR/IO bitmaps required for the VMM to run e.g.
* nested VMMs, etc.
*
* Some functions from the Linux kernel bitmap implementation:
* lib/find_bit.c
*
* Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
* Written by David Howells ([email protected])
*
* Rewritten by Yury Norov <[email protected]> to decrease
* size and improve performance, 2015.
*
* 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.
*/
#ifndef __BITMAP_H
#define __BITMAP_H
#ifndef __linux__
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
#define BITMAP_BITS (sizeof(unsigned long) * CHAR_BIT)
#define BIT_MASK(nr) (1UL << ((nr) % BITMAP_BITS))
#define BIT_WORD(nr) ((nr) / BITMAP_BITS)
#define DECLARE_BITMAP(name, bits) \
unsigned long name[DIV_ROUND_UP(bits, BITMAP_BITS)]
static inline void set_bit(unsigned long nr, unsigned long *bmp)
{
bmp[BIT_WORD(nr)] |= BIT_MASK(nr);
}
static inline void clear_bit(unsigned long nr, unsigned long *bmp)
{
bmp[BIT_WORD(nr)] &= ~BIT_MASK(nr);
}
static inline bool test_bit(unsigned long nr, volatile const unsigned long *bmp)
{
return !!(bmp[BIT_WORD(nr)] & BIT_MASK(nr));
}
static inline unsigned long count_bits(unsigned long count)
{
return DIV_ROUND_UP(count, BITMAP_BITS) * sizeof(unsigned long);
}
static inline void fill_bits(unsigned long *bmp, unsigned char bits, unsigned long count)
{
memset(bmp, bits, count_bits(count));
}
static inline void clear_bits(unsigned long *bmp, unsigned long count)
{
return fill_bits(bmp, 0, count);
}
static inline unsigned long __ffs(unsigned long x)
{
#ifdef _MSC_VER
unsigned long i;
_BitScanForward(&i, x);
return i;
#else
__asm __volatile("rep; bsf %1, %0"
: "=r" (x)
: "rm" (x));
return x;
#endif
}
static inline unsigned long __ffz(unsigned long x)
{
return __ffs(~x);
}
static inline unsigned long long __ffs64(unsigned long long x)
{
#ifdef _MSC_VER
unsigned long long i;
_BitScanForward64(&i, x);
return i;
#else
return __ffs(x);
#endif
}
static inline unsigned long find_first_bit(unsigned long *bmp, unsigned long size)
{
unsigned long i;
for (i = 0; i * BITMAP_BITS < size; ++i)
if (bmp[i])
return min(i * BITMAP_BITS + __ffs(bmp[i]), size);
return size;
}
static inline unsigned long find_first_zero_bit(unsigned long *bmp, unsigned long size)
{
unsigned long i;
for (i = 0; i * BITMAP_BITS < size; ++i)
if (bmp[i] != ~0UL)
return min(i * BITMAP_BITS + __ffz(bmp[i]), size);
return size;
}
#endif
#endif