[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
|
---|
| 37 | * setting and clearing ranges of bits.
|
---|
| 38 | */
|
---|
| 39 |
|
---|
[97a7eff] | 40 | #include <adt/bitmap.h>
|
---|
[d99c1d2] | 41 | #include <typedefs.h>
|
---|
[97a7eff] | 42 | #include <align.h>
|
---|
| 43 | #include <debug.h>
|
---|
[4fd61ba] | 44 | #include <macros.h>
|
---|
[97a7eff] | 45 |
|
---|
| 46 | #define ALL_ONES 0xff
|
---|
| 47 | #define ALL_ZEROES 0x00
|
---|
| 48 |
|
---|
| 49 | /** Initialize bitmap.
|
---|
| 50 | *
|
---|
| 51 | * No portion of the bitmap is set or cleared by this function.
|
---|
| 52 | *
|
---|
[da88a079] | 53 | * @param bitmap Bitmap structure.
|
---|
| 54 | * @param map Address of the memory used to hold the map.
|
---|
| 55 | * @param bits Number of bits stored in bitmap.
|
---|
[97a7eff] | 56 | */
|
---|
[98000fb] | 57 | void bitmap_initialize(bitmap_t *bitmap, uint8_t *map, size_t bits)
|
---|
[97a7eff] | 58 | {
|
---|
| 59 | bitmap->map = map;
|
---|
| 60 | bitmap->bits = bits;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /** Set range of bits.
|
---|
| 64 | *
|
---|
[da88a079] | 65 | * @param bitmap Bitmap structure.
|
---|
| 66 | * @param start Starting bit.
|
---|
| 67 | * @param bits Number of bits to set.
|
---|
[97a7eff] | 68 | */
|
---|
[98000fb] | 69 | void bitmap_set_range(bitmap_t *bitmap, size_t start, size_t bits)
|
---|
[97a7eff] | 70 | {
|
---|
[10285ad] | 71 | size_t i = 0;
|
---|
[98000fb] | 72 | size_t aligned_start;
|
---|
[da88a079] | 73 | size_t lub; /* leading unaligned bits */
|
---|
| 74 | size_t amb; /* aligned middle bits */
|
---|
| 75 | size_t tab; /* trailing aligned bits */
|
---|
[97a7eff] | 76 |
|
---|
| 77 | ASSERT(start + bits <= bitmap->bits);
|
---|
| 78 |
|
---|
| 79 | aligned_start = ALIGN_UP(start, 8);
|
---|
[4fd61ba] | 80 | lub = min(aligned_start - start, bits);
|
---|
| 81 | amb = bits > lub ? bits - lub : 0;
|
---|
| 82 | tab = amb % 8;
|
---|
[97a7eff] | 83 |
|
---|
[4fe18151] | 84 | if (!bits)
|
---|
| 85 | return;
|
---|
| 86 |
|
---|
[10285ad] | 87 | if (start + bits < aligned_start) {
|
---|
| 88 | /* Set bits in the middle of byte. */
|
---|
| 89 | bitmap->map[start / 8] |= ((1 << lub) - 1) << (start & 7);
|
---|
| 90 | return;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[97a7eff] | 93 | if (lub) {
|
---|
[da88a079] | 94 | /* Make sure to set any leading unaligned bits. */
|
---|
[97a7eff] | 95 | bitmap->map[start / 8] |= ~((1 << (8 - lub)) - 1);
|
---|
| 96 | }
|
---|
[4fd61ba] | 97 | for (i = 0; i < amb / 8; i++) {
|
---|
[da88a079] | 98 | /* The middle bits can be set byte by byte. */
|
---|
[97a7eff] | 99 | bitmap->map[aligned_start / 8 + i] = ALL_ONES;
|
---|
| 100 | }
|
---|
[4fd61ba] | 101 | if (tab) {
|
---|
[da88a079] | 102 | /* Make sure to set any trailing aligned bits. */
|
---|
[4fd61ba] | 103 | bitmap->map[aligned_start / 8 + i] |= (1 << tab) - 1;
|
---|
[97a7eff] | 104 | }
|
---|
| 105 |
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /** Clear range of bits.
|
---|
| 109 | *
|
---|
[da88a079] | 110 | * @param bitmap Bitmap structure.
|
---|
| 111 | * @param start Starting bit.
|
---|
| 112 | * @param bits Number of bits to clear.
|
---|
[97a7eff] | 113 | */
|
---|
[98000fb] | 114 | void bitmap_clear_range(bitmap_t *bitmap, size_t start, size_t bits)
|
---|
[97a7eff] | 115 | {
|
---|
[10285ad] | 116 | size_t i = 0;
|
---|
[98000fb] | 117 | size_t aligned_start;
|
---|
[da88a079] | 118 | size_t lub; /* leading unaligned bits */
|
---|
| 119 | size_t amb; /* aligned middle bits */
|
---|
| 120 | size_t tab; /* trailing aligned bits */
|
---|
[97a7eff] | 121 |
|
---|
| 122 | ASSERT(start + bits <= bitmap->bits);
|
---|
| 123 |
|
---|
| 124 | aligned_start = ALIGN_UP(start, 8);
|
---|
[4fd61ba] | 125 | lub = min(aligned_start - start, bits);
|
---|
| 126 | amb = bits > lub ? bits - lub : 0;
|
---|
| 127 | tab = amb % 8;
|
---|
| 128 |
|
---|
[4fe18151] | 129 | if (!bits)
|
---|
| 130 | return;
|
---|
| 131 |
|
---|
[10285ad] | 132 | if (start + bits < aligned_start) {
|
---|
| 133 | /* Set bits in the middle of byte */
|
---|
| 134 | bitmap->map[start / 8] &= ~(((1 << lub) - 1) << (start & 7));
|
---|
| 135 | return;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[97a7eff] | 138 | if (lub) {
|
---|
[da88a079] | 139 | /* Make sure to clear any leading unaligned bits. */
|
---|
[97a7eff] | 140 | bitmap->map[start / 8] &= (1 << (8 - lub)) - 1;
|
---|
| 141 | }
|
---|
[4fd61ba] | 142 | for (i = 0; i < amb / 8; i++) {
|
---|
[da88a079] | 143 | /* The middle bits can be cleared byte by byte. */
|
---|
[97a7eff] | 144 | bitmap->map[aligned_start / 8 + i] = ALL_ZEROES;
|
---|
| 145 | }
|
---|
[4fd61ba] | 146 | if (tab) {
|
---|
[da88a079] | 147 | /* Make sure to clear any trailing aligned bits. */
|
---|
[4fd61ba] | 148 | bitmap->map[aligned_start / 8 + i] &= ~((1 << tab) - 1);
|
---|
[97a7eff] | 149 | }
|
---|
[4fd61ba] | 150 |
|
---|
[97a7eff] | 151 | }
|
---|
| 152 |
|
---|
| 153 | /** Copy portion of one bitmap into another bitmap.
|
---|
| 154 | *
|
---|
[da88a079] | 155 | * @param dst Destination bitmap.
|
---|
| 156 | * @param src Source bitmap.
|
---|
| 157 | * @param bits Number of bits to copy.
|
---|
[97a7eff] | 158 | */
|
---|
[98000fb] | 159 | void bitmap_copy(bitmap_t *dst, bitmap_t *src, size_t bits)
|
---|
[97a7eff] | 160 | {
|
---|
[98000fb] | 161 | size_t i;
|
---|
[97a7eff] | 162 |
|
---|
| 163 | ASSERT(bits <= dst->bits);
|
---|
| 164 | ASSERT(bits <= src->bits);
|
---|
| 165 |
|
---|
| 166 | for (i = 0; i < bits / 8; i++)
|
---|
| 167 | dst->map[i] = src->map[i];
|
---|
| 168 |
|
---|
| 169 | if (bits % 8) {
|
---|
| 170 | bitmap_clear_range(dst, i * 8, bits % 8);
|
---|
| 171 | dst->map[i] |= src->map[i] & ((1 << (bits % 8)) - 1);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
[b45c443] | 174 |
|
---|
[cc73a8a1] | 175 | /** @}
|
---|
[b45c443] | 176 | */
|
---|