11 /* Number of bits in an element. */
12 #define ELEM_BITS (sizeof (elem_type) * CHAR_BIT)
14 /* Returns the index of the element that contains the bit
17 elem_idx (size_t bit_idx)
19 return bit_idx / ELEM_BITS;
22 /* Returns an elem_type where only the bit corresponding to
23 BIT_IDX is turned on. */
24 static inline elem_type
25 bit_mask (size_t bit_idx)
27 return (elem_type) 1 << (bit_idx % ELEM_BITS);
30 /* Returns the number of elements required for B's bits. */
32 elem_cnt (const struct bitmap *b)
34 return DIV_ROUND_UP (b->bit_cnt, ELEM_BITS);
37 /* Returns the number of bytes required by B's elements. */
39 byte_cnt (const struct bitmap *b)
41 return sizeof (elem_type) * elem_cnt (b);
44 /* Returns a bit mask in which the bits actually used in the last
45 element of B's bits are set to 1 and the rest are set to 0. */
46 static inline elem_type
47 last_mask (const struct bitmap *b)
49 int last_bits = b->bit_cnt % ELEM_BITS;
50 return last_bits ? ((elem_type) 1 << last_bits) - 1 : (elem_type) -1;
53 /* Initializes B to be a bitmap of BIT_CNT bits.
54 Returns true if successfalse, false if memory allocation
57 bitmap_init (struct bitmap *b, size_t bit_cnt)
60 b->bits = malloc (byte_cnt (b));
61 if (b->bits == NULL && bit_cnt > 0)
64 bitmap_set_all (b, false);
68 /* Destroys bitmap B, freeing its storage. */
70 bitmap_destroy (struct bitmap *b)
77 /* Returns the number of bits in B. */
79 bitmap_size (const struct bitmap *b)
84 /* Sets the bit numbered IDX in B to VALUE. */
86 bitmap_set (struct bitmap *b, size_t idx, bool value)
89 ASSERT (idx < b->bit_cnt);
91 b->bits[elem_idx (idx)] |= bit_mask (idx);
93 b->bits[elem_idx (idx)] &= ~bit_mask (idx);
96 /* Sets all bits in B to VALUE. */
98 bitmap_set_all (struct bitmap *b, bool value)
104 for (i = 0; i < elem_cnt (b); i++)
105 b->bits[i] = value ? (elem_type) -1 : 0;
106 b->bits[elem_cnt (b) - 1] &= last_mask (b);
109 /* Sets the bit numbered IDX in B to true. */
111 bitmap_mark (struct bitmap *b, size_t idx)
113 bitmap_set (b, idx, true);
116 /* Sets the bit numbered IDX in B to false. */
118 bitmap_reset (struct bitmap *b, size_t idx)
120 bitmap_set (b, idx, false);
123 /* Toggles the bit numbered IDX in B;
124 that is, if it is true, makes it false,
125 and if it is false, makes it true. */
127 bitmap_flip (struct bitmap *b, size_t idx)
130 ASSERT (idx < b->bit_cnt);
131 b->bits[elem_idx (idx)] ^= bit_mask (idx);
134 /* Returns the value of the bit numbered IDX in B. */
136 bitmap_test (const struct bitmap *b, size_t idx)
139 ASSERT (idx < b->bit_cnt);
140 return (b->bits[elem_idx (idx)] & bit_mask (idx)) != 0;
143 /* Returns the smallest index of a bit set to VALUE in B.
144 If no bits in B are set to VALUE, returns BITMAP_ERROR. */
146 bitmap_scan (const struct bitmap *b, bool value)
148 elem_type ignore = value ? 0 : (elem_type) -1;
152 for (idx = 0; idx < elem_cnt (b); idx++)
154 elem_type e = b->bits[idx];
159 while ((e & 1) != value)
165 return idx < b->bit_cnt ? idx : BITMAP_ERROR;
171 /* Finds the smallest index of a bit set to false in B,
172 sets it to true, and returns the index.
173 If no bits in B are set to false, changes no bits and
174 returns BITMAP_ERROR. */
176 bitmap_find_and_set (struct bitmap *b)
178 size_t idx = bitmap_scan (b, false);
179 if (idx != BITMAP_ERROR)
180 bitmap_mark (b, idx);
184 /* Finds the smallest index of a bit set to true in B,
185 sets it to false, and returns the index.
186 If no bits in B are set to true, changes no bits and
187 returns BITMAP_ERROR. */
189 bitmap_find_and_clear (struct bitmap *b)
191 size_t idx = bitmap_scan (b, true);
192 if (idx != BITMAP_ERROR)
193 bitmap_reset (b, idx);
197 /* Returns the number of bits in B set to true. */
199 bitmap_set_cnt (const struct bitmap *b)
206 for (i = 0; i < elem_cnt (b); i++)
208 elem_type e = b->bits[i];
218 /* Returns true if any bits in B are set to true,
219 and false otherwise.*/
221 bitmap_any (const struct bitmap *b)
226 for (i = 0; i < elem_cnt (b); i++)
232 /* Returns the number of bits in B set to false. */
234 bitmap_clear_cnt (const struct bitmap *b)
236 return b->bit_cnt - bitmap_set_cnt (b);
239 /* Returns true if no bits in B are set to true,
240 and false otherwise.*/
242 bitmap_none (const struct bitmap *b)
244 return !bitmap_any (b);
247 /* Returns true if every bit in B is set to true,
248 and false otherwise. */
250 bitmap_all (const struct bitmap *b)
259 for (i = 0; i < elem_cnt (b) - 1; i++)
260 if (b->bits[i] != (elem_type) -1)
262 return b->bits[i] == last_mask (b);
266 /* Returns the number of bytes needed to store B in a file. */
268 bitmap_file_size (const struct bitmap *b)
273 /* Reads FILE into B, ignoring errors. */
275 bitmap_read (struct bitmap *b, struct file *file)
277 file_read_at (file, b->bits, byte_cnt (b), 0);
278 b->bits[elem_cnt (b) - 1] &= last_mask (b);
281 /* Writes FILE to B, ignoring errors. */
283 bitmap_write (const struct bitmap *b, struct file *file)
285 file_write_at (file, b->bits, byte_cnt (b), 0);
289 /* Dumps the contents of B to the console as hexadecimal. */
291 bitmap_dump (const struct bitmap *b)
293 hex_dump (b->bits, byte_cnt (b), false);