Clear range of bits. * * @param bitmap Bitmap structure. * @param start Starting bit. * @param count Number of bits to clear. * */
| 164 | * |
| 165 | */ |
| 166 | void bitmap_clear_range(bitmap_t *bitmap, size_t start, size_t count) |
| 167 | { |
| 168 | assert(start + count <= bitmap->elements); |
| 169 | |
| 170 | if (count == 0) |
| 171 | return; |
| 172 | |
| 173 | size_t start_byte = start / BITMAP_ELEMENT; |
| 174 | size_t aligned_start = ALIGN_UP(start, BITMAP_ELEMENT); |
| 175 | |
| 176 | /* Leading unaligned bits */ |
| 177 | size_t lub = min(aligned_start - start, count); |
| 178 | |
| 179 | /* Aligned middle bits */ |
| 180 | size_t amb = (count > lub) ? (count - lub) : 0; |
| 181 | |
| 182 | /* Trailing aligned bits */ |
| 183 | size_t tab = amb % BITMAP_ELEMENT; |
| 184 | |
| 185 | if (start + count < aligned_start) { |
| 186 | /* Set bits in the middle of byte */ |
| 187 | bitmap->bits[start_byte] &= |
| 188 | ~(((1 << lub) - 1) << (start & BITMAP_REMAINER)); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | if (lub) { |
| 193 | /* Make sure to clear any leading unaligned bits. */ |
| 194 | bitmap->bits[start_byte] &= |
| 195 | (1 << (BITMAP_ELEMENT - lub)) - 1; |
| 196 | } |
| 197 | |
| 198 | size_t i; |
| 199 | |
| 200 | for (i = 0; i < amb / BITMAP_ELEMENT; i++) { |
| 201 | /* The middle bits can be cleared byte by byte. */ |
| 202 | bitmap->bits[aligned_start / BITMAP_ELEMENT + i] = |
| 203 | ALL_ZEROES; |
| 204 | } |
| 205 | |
| 206 | if (tab) { |
| 207 | /* Make sure to clear any trailing aligned bits. */ |
| 208 | bitmap->bits[aligned_start / BITMAP_ELEMENT + i] &= |
| 209 | ~((1 << tab) - 1); |
| 210 | } |
| 211 | |
| 212 | bitmap->next_fit = start_byte; |
| 213 | } |
| 214 | |
| 215 | /** Copy portion of one bitmap into another bitmap. |
| 216 | * |
no outgoing calls
no test coverage detected