Rev 2089 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1200 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2006 Jakub Jermar |
1200 | jermar | 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 | |||
1757 | jermar | 29 | /** @addtogroup genericadt |
1702 | cejka | 30 | * @{ |
31 | */ |
||
1248 | jermar | 32 | /** |
1704 | cejka | 33 | * @file |
1248 | jermar | 34 | * @brief Implementation of bitmap ADT. |
35 | * |
||
36 | * This file implements bitmap ADT and provides functions for |
||
37 | * setting and clearing ranges of bits. |
||
38 | */ |
||
39 | |||
1200 | jermar | 40 | #include <adt/bitmap.h> |
41 | #include <arch/types.h> |
||
42 | #include <align.h> |
||
43 | #include <debug.h> |
||
1262 | jermar | 44 | #include <macros.h> |
1200 | jermar | 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 | * |
||
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. |
||
56 | */ |
||
4490 | decky | 57 | void bitmap_initialize(bitmap_t *bitmap, uint8_t *map, size_t bits) |
1200 | jermar | 58 | { |
59 | bitmap->map = map; |
||
60 | bitmap->bits = bits; |
||
61 | } |
||
62 | |||
63 | /** Set range of bits. |
||
64 | * |
||
65 | * @param bitmap Bitmap structure. |
||
66 | * @param start Starting bit. |
||
67 | * @param bits Number of bits to set. |
||
68 | */ |
||
4490 | decky | 69 | void bitmap_set_range(bitmap_t *bitmap, size_t start, size_t bits) |
1200 | jermar | 70 | { |
4490 | decky | 71 | size_t i = 0; |
72 | size_t aligned_start; |
||
73 | size_t lub; /* leading unaligned bits */ |
||
74 | size_t amb; /* aligned middle bits */ |
||
75 | size_t tab; /* trailing aligned bits */ |
||
1200 | jermar | 76 | |
77 | ASSERT(start + bits <= bitmap->bits); |
||
78 | |||
79 | aligned_start = ALIGN_UP(start, 8); |
||
1262 | jermar | 80 | lub = min(aligned_start - start, bits); |
81 | amb = bits > lub ? bits - lub : 0; |
||
82 | tab = amb % 8; |
||
1200 | jermar | 83 | |
1549 | vana | 84 | if ( start + bits < aligned_start ) { |
85 | /* |
||
86 | * Set bits in the middle of byte |
||
87 | */ |
||
88 | bitmap->map[start / 8] |= ((1 << lub)-1) << (start&7); |
||
89 | return; |
||
90 | } |
||
91 | |||
1200 | jermar | 92 | if (lub) { |
93 | /* |
||
94 | * Make sure to set any leading unaligned bits. |
||
95 | */ |
||
96 | bitmap->map[start / 8] |= ~((1 << (8 - lub)) - 1); |
||
97 | } |
||
1262 | jermar | 98 | for (i = 0; i < amb / 8; i++) { |
1200 | jermar | 99 | /* |
100 | * The middle bits can be set byte by byte. |
||
101 | */ |
||
102 | bitmap->map[aligned_start / 8 + i] = ALL_ONES; |
||
103 | } |
||
1262 | jermar | 104 | if (tab) { |
1200 | jermar | 105 | /* |
1262 | jermar | 106 | * Make sure to set any trailing aligned bits. |
1200 | jermar | 107 | */ |
1262 | jermar | 108 | bitmap->map[aligned_start / 8 + i] |= (1 << tab) - 1; |
1200 | jermar | 109 | } |
110 | |||
111 | } |
||
112 | |||
113 | /** Clear range of bits. |
||
114 | * |
||
115 | * @param bitmap Bitmap structure. |
||
116 | * @param start Starting bit. |
||
117 | * @param bits Number of bits to clear. |
||
118 | */ |
||
4490 | decky | 119 | void bitmap_clear_range(bitmap_t *bitmap, size_t start, size_t bits) |
1200 | jermar | 120 | { |
4490 | decky | 121 | size_t i = 0; |
122 | size_t aligned_start; |
||
123 | size_t lub; /* leading unaligned bits */ |
||
124 | size_t amb; /* aligned middle bits */ |
||
125 | size_t tab; /* trailing aligned bits */ |
||
1200 | jermar | 126 | |
127 | ASSERT(start + bits <= bitmap->bits); |
||
128 | |||
129 | aligned_start = ALIGN_UP(start, 8); |
||
1262 | jermar | 130 | lub = min(aligned_start - start, bits); |
131 | amb = bits > lub ? bits - lub : 0; |
||
132 | tab = amb % 8; |
||
133 | |||
1549 | vana | 134 | if ( start + bits < aligned_start ) |
135 | { |
||
136 | /* |
||
137 | * Set bits in the middle of byte |
||
138 | */ |
||
139 | bitmap->map[start / 8] &= ~(((1 << lub)-1) << (start&7)); |
||
140 | return; |
||
141 | } |
||
142 | |||
143 | |||
1200 | jermar | 144 | if (lub) { |
145 | /* |
||
146 | * Make sure to clear any leading unaligned bits. |
||
147 | */ |
||
148 | bitmap->map[start / 8] &= (1 << (8 - lub)) - 1; |
||
149 | } |
||
1262 | jermar | 150 | for (i = 0; i < amb / 8; i++) { |
1200 | jermar | 151 | /* |
152 | * The middle bits can be cleared byte by byte. |
||
153 | */ |
||
154 | bitmap->map[aligned_start / 8 + i] = ALL_ZEROES; |
||
155 | } |
||
1262 | jermar | 156 | if (tab) { |
1200 | jermar | 157 | /* |
1262 | jermar | 158 | * Make sure to clear any trailing aligned bits. |
1200 | jermar | 159 | */ |
1262 | jermar | 160 | bitmap->map[aligned_start / 8 + i] &= ~((1 << tab) - 1); |
1200 | jermar | 161 | } |
1262 | jermar | 162 | |
1200 | jermar | 163 | } |
164 | |||
165 | /** Copy portion of one bitmap into another bitmap. |
||
166 | * |
||
167 | * @param dst Destination bitmap. |
||
168 | * @param src Source bitmap. |
||
169 | * @param bits Number of bits to copy. |
||
170 | */ |
||
4490 | decky | 171 | void bitmap_copy(bitmap_t *dst, bitmap_t *src, size_t bits) |
1200 | jermar | 172 | { |
4490 | decky | 173 | size_t i; |
1200 | jermar | 174 | |
175 | ASSERT(bits <= dst->bits); |
||
176 | ASSERT(bits <= src->bits); |
||
177 | |||
178 | for (i = 0; i < bits / 8; i++) |
||
179 | dst->map[i] = src->map[i]; |
||
180 | |||
181 | if (bits % 8) { |
||
182 | bitmap_clear_range(dst, i * 8, bits % 8); |
||
183 | dst->map[i] |= src->map[i] & ((1 << (bits % 8)) - 1); |
||
184 | } |
||
185 | } |
||
1702 | cejka | 186 | |
1757 | jermar | 187 | /** @} |
1702 | cejka | 188 | */ |