[97a7eff] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Jakub Jermar
|
---|
[97a7eff] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[cc73a8a1] | 29 | /** @addtogroup genericadt
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
[9179d0a] | 32 | /**
|
---|
[7257021e] | 33 | * @file
|
---|
[da88a079] | 34 | * @brief Implementation of bitmap ADT.
|
---|
[9179d0a] | 35 | *
|
---|
| 36 | * This file implements bitmap ADT and provides functions for
|
---|
[7de18418] | 37 | * setting and clearing ranges of bits and for finding ranges
|
---|
| 38 | * of unset bits.
|
---|
| 39 | *
|
---|
| 40 | * The bitmap ADT can optionally implement a two-level hierarchy
|
---|
| 41 | * for faster range searches. The second level bitmap (of blocks)
|
---|
| 42 | * is not precise, but conservative. This means that if the block
|
---|
| 43 | * bit is set, it guarantees that all bits in the block are set.
|
---|
| 44 | * But if the block bit is unset, nothing can be said about the
|
---|
| 45 | * bits in the block.
|
---|
| 46 | *
|
---|
[9179d0a] | 47 | */
|
---|
| 48 |
|
---|
[97a7eff] | 49 | #include <adt/bitmap.h>
|
---|
[d99c1d2] | 50 | #include <typedefs.h>
|
---|
[97a7eff] | 51 | #include <align.h>
|
---|
| 52 | #include <debug.h>
|
---|
[4fd61ba] | 53 | #include <macros.h>
|
---|
[97a7eff] | 54 |
|
---|
[7de18418] | 55 | #define ALL_ONES 0xff
|
---|
| 56 | #define ALL_ZEROES 0x00
|
---|
[97a7eff] | 57 |
|
---|
[7de18418] | 58 | /** Get bitmap size
|
---|
[97a7eff] | 59 | *
|
---|
[7de18418] | 60 | * Return the size (in bytes) required for the bitmap.
|
---|
| 61 | *
|
---|
| 62 | * @param elements Number bits stored in bitmap.
|
---|
| 63 | * @param block_size Block size of the 2nd level bitmap.
|
---|
| 64 | * If set to zero, no 2nd level is used.
|
---|
| 65 | *
|
---|
| 66 | * @return Size (in bytes) required for the bitmap.
|
---|
[97a7eff] | 67 | *
|
---|
| 68 | */
|
---|
[7de18418] | 69 | size_t bitmap_size(size_t elements, size_t block_size)
|
---|
[97a7eff] | 70 | {
|
---|
[7de18418] | 71 | size_t size = elements / BITMAP_ELEMENT;
|
---|
| 72 |
|
---|
| 73 | if ((elements % BITMAP_ELEMENT) != 0)
|
---|
| 74 | size++;
|
---|
| 75 |
|
---|
| 76 | if (block_size > 0) {
|
---|
| 77 | size += elements / block_size;
|
---|
| 78 |
|
---|
| 79 | if ((elements % block_size) != 0)
|
---|
| 80 | size++;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | return size;
|
---|
[97a7eff] | 84 | }
|
---|
| 85 |
|
---|
[7de18418] | 86 | /** Initialize bitmap.
|
---|
| 87 | *
|
---|
| 88 | * No portion of the bitmap is set or cleared by this function.
|
---|
| 89 | *
|
---|
| 90 | * @param bitmap Bitmap structure.
|
---|
| 91 | * @param elements Number of bits stored in bitmap.
|
---|
| 92 | * @param block_size Block size of the 2nd level bitmap.
|
---|
| 93 | * If set to zero, no 2nd level is used.
|
---|
| 94 | * @param data Address of the memory used to hold the map.
|
---|
| 95 | * The optional 2nd level bitmap follows the 1st
|
---|
| 96 | * level bitmap.
|
---|
[97a7eff] | 97 | *
|
---|
| 98 | */
|
---|
[7de18418] | 99 | void bitmap_initialize(bitmap_t *bitmap, size_t elements, size_t block_size,
|
---|
| 100 | void *data)
|
---|
[97a7eff] | 101 | {
|
---|
[7de18418] | 102 | bitmap->elements = elements;
|
---|
| 103 | bitmap->bits = (uint8_t *) data;
|
---|
[97a7eff] | 104 |
|
---|
[7de18418] | 105 | if (block_size > 0) {
|
---|
| 106 | bitmap->block_size = block_size;
|
---|
| 107 | bitmap->blocks = bitmap->bits +
|
---|
| 108 | bitmap_size(elements, 0);
|
---|
| 109 | } else {
|
---|
| 110 | bitmap->block_size = 0;
|
---|
| 111 | bitmap->blocks = NULL;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | static void bitmap_set_range_internal(uint8_t *bits, size_t start, size_t count)
|
---|
| 116 | {
|
---|
| 117 | if (count == 0)
|
---|
| 118 | return;
|
---|
[97a7eff] | 119 |
|
---|
[7de18418] | 120 | size_t aligned_start = ALIGN_UP(start, BITMAP_ELEMENT);
|
---|
[97a7eff] | 121 |
|
---|
[7de18418] | 122 | /* Leading unaligned bits */
|
---|
| 123 | size_t lub = min(aligned_start - start, count);
|
---|
| 124 |
|
---|
| 125 | /* Aligned middle bits */
|
---|
| 126 | size_t amb = (count > lub) ? (count - lub) : 0;
|
---|
| 127 |
|
---|
| 128 | /* Trailing aligned bits */
|
---|
| 129 | size_t tab = amb % BITMAP_ELEMENT;
|
---|
| 130 |
|
---|
| 131 | if (start + count < aligned_start) {
|
---|
[10285ad] | 132 | /* Set bits in the middle of byte. */
|
---|
[7de18418] | 133 | bits[start / BITMAP_ELEMENT] |=
|
---|
| 134 | ((1 << lub) - 1) << (start & BITMAP_REMAINER);
|
---|
[10285ad] | 135 | return;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[97a7eff] | 138 | if (lub) {
|
---|
[da88a079] | 139 | /* Make sure to set any leading unaligned bits. */
|
---|
[7de18418] | 140 | bits[start / BITMAP_ELEMENT] |=
|
---|
| 141 | ~((1 << (BITMAP_ELEMENT - lub)) - 1);
|
---|
[97a7eff] | 142 | }
|
---|
[7de18418] | 143 |
|
---|
| 144 | size_t i;
|
---|
| 145 |
|
---|
| 146 | for (i = 0; i < amb / BITMAP_ELEMENT; i++) {
|
---|
[da88a079] | 147 | /* The middle bits can be set byte by byte. */
|
---|
[7de18418] | 148 | bits[aligned_start / BITMAP_ELEMENT + i] = ALL_ONES;
|
---|
[97a7eff] | 149 | }
|
---|
[7de18418] | 150 |
|
---|
[4fd61ba] | 151 | if (tab) {
|
---|
[da88a079] | 152 | /* Make sure to set any trailing aligned bits. */
|
---|
[7de18418] | 153 | bits[aligned_start / BITMAP_ELEMENT + i] |= (1 << tab) - 1;
|
---|
[97a7eff] | 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[7de18418] | 157 | /** Set range of bits.
|
---|
| 158 | *
|
---|
| 159 | * @param bitmap Bitmap structure.
|
---|
| 160 | * @param start Starting bit.
|
---|
| 161 | * @param count Number of bits to set.
|
---|
[97a7eff] | 162 | *
|
---|
| 163 | */
|
---|
[7de18418] | 164 | void bitmap_set_range(bitmap_t *bitmap, size_t start, size_t count)
|
---|
[97a7eff] | 165 | {
|
---|
[7de18418] | 166 | ASSERT(start + count <= bitmap->elements);
|
---|
| 167 |
|
---|
| 168 | bitmap_set_range_internal(bitmap->bits, start, count);
|
---|
| 169 |
|
---|
| 170 | if (bitmap->block_size > 0) {
|
---|
| 171 | size_t aligned_start = ALIGN_UP(start, bitmap->block_size);
|
---|
| 172 |
|
---|
| 173 | /* Leading unaligned bits */
|
---|
| 174 | size_t lub = min(aligned_start - start, count);
|
---|
| 175 |
|
---|
| 176 | /* Aligned middle bits */
|
---|
| 177 | size_t amb = (count > lub) ? (count - lub) : 0;
|
---|
| 178 |
|
---|
| 179 | size_t aligned_size = amb / bitmap->block_size;
|
---|
| 180 |
|
---|
| 181 | bitmap_set_range_internal(bitmap->blocks, aligned_start,
|
---|
| 182 | aligned_size);
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
[4fd61ba] | 185 |
|
---|
[7de18418] | 186 | static void bitmap_clear_range_internal(uint8_t *bits, size_t start,
|
---|
| 187 | size_t count)
|
---|
| 188 | {
|
---|
| 189 | if (count == 0)
|
---|
[4fe18151] | 190 | return;
|
---|
[7de18418] | 191 |
|
---|
| 192 | size_t aligned_start = ALIGN_UP(start, BITMAP_ELEMENT);
|
---|
| 193 |
|
---|
| 194 | /* Leading unaligned bits */
|
---|
| 195 | size_t lub = min(aligned_start - start, count);
|
---|
| 196 |
|
---|
| 197 | /* Aligned middle bits */
|
---|
| 198 | size_t amb = (count > lub) ? (count - lub) : 0;
|
---|
| 199 |
|
---|
| 200 | /* Trailing aligned bits */
|
---|
| 201 | size_t tab = amb % BITMAP_ELEMENT;
|
---|
| 202 |
|
---|
| 203 | if (start + count < aligned_start) {
|
---|
[10285ad] | 204 | /* Set bits in the middle of byte */
|
---|
[7de18418] | 205 | bits[start / BITMAP_ELEMENT] &=
|
---|
| 206 | ~(((1 << lub) - 1) << (start & BITMAP_REMAINER));
|
---|
[10285ad] | 207 | return;
|
---|
| 208 | }
|
---|
[7de18418] | 209 |
|
---|
[97a7eff] | 210 | if (lub) {
|
---|
[da88a079] | 211 | /* Make sure to clear any leading unaligned bits. */
|
---|
[7de18418] | 212 | bits[start / BITMAP_ELEMENT] &=
|
---|
| 213 | (1 << (BITMAP_ELEMENT - lub)) - 1;
|
---|
[97a7eff] | 214 | }
|
---|
[7de18418] | 215 |
|
---|
| 216 | size_t i;
|
---|
| 217 |
|
---|
| 218 | for (i = 0; i < amb / BITMAP_ELEMENT; i++) {
|
---|
[da88a079] | 219 | /* The middle bits can be cleared byte by byte. */
|
---|
[7de18418] | 220 | bits[aligned_start / BITMAP_ELEMENT + i] = ALL_ZEROES;
|
---|
[97a7eff] | 221 | }
|
---|
[7de18418] | 222 |
|
---|
[4fd61ba] | 223 | if (tab) {
|
---|
[da88a079] | 224 | /* Make sure to clear any trailing aligned bits. */
|
---|
[7de18418] | 225 | bits[aligned_start / BITMAP_ELEMENT + i] &= ~((1 << tab) - 1);
|
---|
[97a7eff] | 226 | }
|
---|
[7de18418] | 227 | }
|
---|
[4fd61ba] | 228 |
|
---|
[7de18418] | 229 | /** Clear range of bits.
|
---|
| 230 | *
|
---|
| 231 | * @param bitmap Bitmap structure.
|
---|
| 232 | * @param start Starting bit.
|
---|
| 233 | * @param count Number of bits to clear.
|
---|
| 234 | *
|
---|
| 235 | */
|
---|
| 236 | void bitmap_clear_range(bitmap_t *bitmap, size_t start, size_t count)
|
---|
| 237 | {
|
---|
| 238 | ASSERT(start + count <= bitmap->elements);
|
---|
| 239 |
|
---|
| 240 | bitmap_clear_range_internal(bitmap->bits, start, count);
|
---|
| 241 |
|
---|
| 242 | if (bitmap->block_size > 0) {
|
---|
| 243 | size_t aligned_start = start / bitmap->block_size;
|
---|
| 244 |
|
---|
| 245 | size_t aligned_end = (start + count) / bitmap->block_size;
|
---|
| 246 |
|
---|
| 247 | if (((start + count) % bitmap->block_size) != 0)
|
---|
| 248 | aligned_end++;
|
---|
| 249 |
|
---|
| 250 | size_t aligned_size = aligned_end - aligned_start;
|
---|
| 251 |
|
---|
| 252 | bitmap_clear_range_internal(bitmap->blocks, aligned_start,
|
---|
| 253 | aligned_size);
|
---|
| 254 | }
|
---|
[97a7eff] | 255 | }
|
---|
| 256 |
|
---|
| 257 | /** Copy portion of one bitmap into another bitmap.
|
---|
| 258 | *
|
---|
[7de18418] | 259 | * @param dst Destination bitmap.
|
---|
| 260 | * @param src Source bitmap.
|
---|
| 261 | * @param count Number of bits to copy.
|
---|
| 262 | *
|
---|
[97a7eff] | 263 | */
|
---|
[7de18418] | 264 | void bitmap_copy(bitmap_t *dst, bitmap_t *src, size_t count)
|
---|
[97a7eff] | 265 | {
|
---|
[7de18418] | 266 | ASSERT(count <= dst->elements);
|
---|
| 267 | ASSERT(count <= src->elements);
|
---|
[97a7eff] | 268 |
|
---|
[7de18418] | 269 | size_t i;
|
---|
[97a7eff] | 270 |
|
---|
[7de18418] | 271 | for (i = 0; i < count / BITMAP_ELEMENT; i++)
|
---|
| 272 | dst->bits[i] = src->bits[i];
|
---|
[97a7eff] | 273 |
|
---|
[7de18418] | 274 | if (count % BITMAP_ELEMENT) {
|
---|
| 275 | bitmap_clear_range(dst, i * BITMAP_ELEMENT,
|
---|
| 276 | count % BITMAP_ELEMENT);
|
---|
| 277 | dst->bits[i] |= src->bits[i] &
|
---|
| 278 | ((1 << (count % BITMAP_ELEMENT)) - 1);
|
---|
[97a7eff] | 279 | }
|
---|
| 280 | }
|
---|
[b45c443] | 281 |
|
---|
[cc73a8a1] | 282 | /** @}
|
---|
[b45c443] | 283 | */
|
---|