forked from agroce/testfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap.c
executable file
·175 lines (147 loc) · 3.31 KB
/
bitmap.c
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
/*
* Manager for arrays of bits.
* See bitmap.h for more information.
*/
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "bitmap.h"
#include "testfs.h"
/*
* It would be a lot more efficient on most platforms to use u_int32_t
* or unsigned long as the base type for holding bits. But we don't,
* because if one uses any data type more than a single byte wide,
* bitmap data saved on disk becomes endian-dependent, which is a
* severe nuisance.
*/
struct bitmap {
u_int32_t nbits;
WORD_TYPE *v;
};
/* return negative value on error */
int
bitmap_create(u_int32_t nbits, struct bitmap **bp)
{
struct bitmap *b;
u_int32_t words;
words = DIVROUNDUP(nbits, BITS_PER_WORD);
b = malloc(sizeof(struct bitmap));
if (b == NULL) {
return -ENOMEM;
}
b->v = calloc(words, sizeof(WORD_TYPE));
if (b->v == NULL) {
free(b);
return -ENOMEM;
}
b->nbits = nbits;
/* Mark any leftover bits at the end in use */
if (nbits / BITS_PER_WORD < words) {
u_int32_t j, ix = words-1;
u_int32_t overbits = nbits - ix*BITS_PER_WORD;
assert(nbits / BITS_PER_WORD == words-1);
assert(overbits > 0 && overbits < BITS_PER_WORD);
for (j=overbits; j<BITS_PER_WORD; j++) {
b->v[ix] |= ((WORD_TYPE)1 << j);
}
}
*bp = b;
return 0;
}
void *
bitmap_getdata(struct bitmap *b)
{
return b->v;
}
/* return negative value on error */
int
bitmap_alloc(struct bitmap *b, u_int32_t *index)
{
u_int32_t ix;
u_int32_t maxix = DIVROUNDUP(b->nbits, BITS_PER_WORD);
u_int32_t offset;
for (ix=0; ix<maxix; ix++) {
if (b->v[ix]!=WORD_ALLBITS) {
for (offset = 0; offset < BITS_PER_WORD; offset++) {
WORD_TYPE mask = ((WORD_TYPE)1)<<offset;
if ((b->v[ix] & mask)==0) {
b->v[ix] |= mask;
*index = (ix*BITS_PER_WORD)+offset;
assert(*index < b->nbits);
return 0;
}
}
assert(0);
}
}
return -ENOSPC;
}
static inline void
bitmap_translate(u_int32_t bitno, u_int32_t *ix, WORD_TYPE *mask)
{
u_int32_t offset;
*ix = bitno / BITS_PER_WORD;
offset = bitno % BITS_PER_WORD;
*mask = ((WORD_TYPE)1) << offset;
}
void
bitmap_mark(struct bitmap *b, u_int32_t index)
{
u_int32_t ix;
WORD_TYPE mask;
assert(index < b->nbits);
bitmap_translate(index, &ix, &mask);
assert((b->v[ix] & mask)==0);
b->v[ix] |= mask;
}
void
bitmap_unmark(struct bitmap *b, u_int32_t index)
{
u_int32_t ix;
WORD_TYPE mask;
assert(index < b->nbits);
bitmap_translate(index, &ix, &mask);
assert((b->v[ix] & mask)!=0);
b->v[ix] &= ~mask;
}
int
bitmap_isset(struct bitmap *b, u_int32_t index)
{
u_int32_t ix;
WORD_TYPE mask;
bitmap_translate(index, &ix, &mask);
return (b->v[ix] & mask);
}
void
bitmap_destroy(struct bitmap *b)
{
free(b->v);
free(b);
}
/* return TRUE when equal, FALSE when not equal */
int
bitmap_equal(struct bitmap *a, struct bitmap *b)
{
u_int32_t ix;
u_int32_t maxix;
if (a->nbits != b->nbits)
return 0;
maxix = DIVROUNDUP(b->nbits, BITS_PER_WORD);
for (ix=0; ix<maxix; ix++) {
if (a->v[ix] != b->v[ix])
return 0;
}
return 1;
}
int
bitmap_nr_allocated(struct bitmap *b)
{
int i;
int nr = 0;
for (i = 0; i < b->nbits; i++) {
if (bitmap_isset(b, i))
nr++;
}
return nr;
}