1 | /*
|
---|
2 | * Copyright (c) 2006 Jakub 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 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief Implementation of bitmap ADT.
|
---|
35 | *
|
---|
36 | * This file implements bitmap ADT and provides functions for
|
---|
37 | * setting and clearing ranges of bits and for finding ranges
|
---|
38 | * of unset bits.
|
---|
39 | */
|
---|
40 |
|
---|
41 | #include <adt/bitmap.h>
|
---|
42 | #include <align.h>
|
---|
43 | #include <assert.h>
|
---|
44 | #include <macros.h>
|
---|
45 |
|
---|
46 | #define ALL_ONES 0xff
|
---|
47 | #define ALL_ZEROES 0x00
|
---|
48 |
|
---|
49 | /** Unchecked version of bitmap_get()
|
---|
50 | *
|
---|
51 | * This version of bitmap_get() does not do any boundary checks.
|
---|
52 | *
|
---|
53 | * @param bitmap Bitmap to access.
|
---|
54 | * @param element Element to access.
|
---|
55 | *
|
---|
56 | * @return Bit value of the element in the bitmap.
|
---|
57 | *
|
---|
58 | */
|
---|
59 | static unsigned int bitmap_get_fast(bitmap_t *bitmap, size_t element)
|
---|
60 | {
|
---|
61 | size_t byte = element / BITMAP_ELEMENT;
|
---|
62 | uint8_t mask = 1 << (element & BITMAP_REMAINER);
|
---|
63 |
|
---|
64 | return !!((bitmap->bits)[byte] & mask);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** Get bitmap size
|
---|
68 | *
|
---|
69 | * Return the size (in bytes) required for the bitmap.
|
---|
70 | *
|
---|
71 | * @param elements Number bits stored in bitmap.
|
---|
72 | *
|
---|
73 | * @return Size (in bytes) required for the bitmap.
|
---|
74 | *
|
---|
75 | */
|
---|
76 | size_t bitmap_size(size_t elements)
|
---|
77 | {
|
---|
78 | size_t size = elements / BITMAP_ELEMENT;
|
---|
79 |
|
---|
80 | if ((elements % BITMAP_ELEMENT) != 0)
|
---|
81 | size++;
|
---|
82 |
|
---|
83 | return size;
|
---|
84 | }
|
---|
85 |
|
---|
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 data Address of the memory used to hold the map.
|
---|
93 | * The optional 2nd level bitmap follows the 1st
|
---|
94 | * level bitmap.
|
---|
95 | *
|
---|
96 | */
|
---|
97 | void bitmap_initialize(bitmap_t *bitmap, size_t elements, void *data)
|
---|
98 | {
|
---|
99 | bitmap->elements = elements;
|
---|
100 | bitmap->bits = (uint8_t *) data;
|
---|
101 | bitmap->next_fit = 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** Set range of bits.
|
---|
105 | *
|
---|
106 | * @param bitmap Bitmap structure.
|
---|
107 | * @param start Starting bit.
|
---|
108 | * @param count Number of bits to set.
|
---|
109 | *
|
---|
110 | */
|
---|
111 | void bitmap_set_range(bitmap_t *bitmap, size_t start, size_t count)
|
---|
112 | {
|
---|
113 | assert(start + count <= bitmap->elements);
|
---|
114 |
|
---|
115 | if (count == 0)
|
---|
116 | return;
|
---|
117 |
|
---|
118 | size_t start_byte = start / BITMAP_ELEMENT;
|
---|
119 | size_t aligned_start = ALIGN_UP(start, BITMAP_ELEMENT);
|
---|
120 |
|
---|
121 | /* Leading unaligned bits */
|
---|
122 | size_t lub = min(aligned_start - start, count);
|
---|
123 |
|
---|
124 | /* Aligned middle bits */
|
---|
125 | size_t amb = (count > lub) ? (count - lub) : 0;
|
---|
126 |
|
---|
127 | /* Trailing aligned bits */
|
---|
128 | size_t tab = amb % BITMAP_ELEMENT;
|
---|
129 |
|
---|
130 | if (start + count < aligned_start) {
|
---|
131 | /* Set bits in the middle of byte. */
|
---|
132 | bitmap->bits[start_byte] |=
|
---|
133 | ((1 << lub) - 1) << (start & BITMAP_REMAINER);
|
---|
134 | return;
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (lub) {
|
---|
138 | /* Make sure to set any leading unaligned bits. */
|
---|
139 | bitmap->bits[start_byte] |=
|
---|
140 | ~((1 << (BITMAP_ELEMENT - lub)) - 1);
|
---|
141 | }
|
---|
142 |
|
---|
143 | size_t i;
|
---|
144 |
|
---|
145 | for (i = 0; i < amb / BITMAP_ELEMENT; i++) {
|
---|
146 | /* The middle bits can be set byte by byte. */
|
---|
147 | bitmap->bits[aligned_start / BITMAP_ELEMENT + i] =
|
---|
148 | ALL_ONES;
|
---|
149 | }
|
---|
150 |
|
---|
151 | if (tab) {
|
---|
152 | /* Make sure to set any trailing aligned bits. */
|
---|
153 | bitmap->bits[aligned_start / BITMAP_ELEMENT + i] |=
|
---|
154 | (1 << tab) - 1;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Clear range of bits.
|
---|
159 | *
|
---|
160 | * @param bitmap Bitmap structure.
|
---|
161 | * @param start Starting bit.
|
---|
162 | * @param count Number of bits to clear.
|
---|
163 | *
|
---|
164 | */
|
---|
165 | void bitmap_clear_range(bitmap_t *bitmap, size_t start, size_t count)
|
---|
166 | {
|
---|
167 | assert(start + count <= bitmap->elements);
|
---|
168 |
|
---|
169 | if (count == 0)
|
---|
170 | return;
|
---|
171 |
|
---|
172 | size_t start_byte = start / BITMAP_ELEMENT;
|
---|
173 | size_t aligned_start = ALIGN_UP(start, BITMAP_ELEMENT);
|
---|
174 |
|
---|
175 | /* Leading unaligned bits */
|
---|
176 | size_t lub = min(aligned_start - start, count);
|
---|
177 |
|
---|
178 | /* Aligned middle bits */
|
---|
179 | size_t amb = (count > lub) ? (count - lub) : 0;
|
---|
180 |
|
---|
181 | /* Trailing aligned bits */
|
---|
182 | size_t tab = amb % BITMAP_ELEMENT;
|
---|
183 |
|
---|
184 | if (start + count < aligned_start) {
|
---|
185 | /* Set bits in the middle of byte */
|
---|
186 | bitmap->bits[start_byte] &=
|
---|
187 | ~(((1 << lub) - 1) << (start & BITMAP_REMAINER));
|
---|
188 | return;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (lub) {
|
---|
192 | /* Make sure to clear any leading unaligned bits. */
|
---|
193 | bitmap->bits[start_byte] &=
|
---|
194 | (1 << (BITMAP_ELEMENT - lub)) - 1;
|
---|
195 | }
|
---|
196 |
|
---|
197 | size_t i;
|
---|
198 |
|
---|
199 | for (i = 0; i < amb / BITMAP_ELEMENT; i++) {
|
---|
200 | /* The middle bits can be cleared byte by byte. */
|
---|
201 | bitmap->bits[aligned_start / BITMAP_ELEMENT + i] =
|
---|
202 | ALL_ZEROES;
|
---|
203 | }
|
---|
204 |
|
---|
205 | if (tab) {
|
---|
206 | /* Make sure to clear any trailing aligned bits. */
|
---|
207 | bitmap->bits[aligned_start / BITMAP_ELEMENT + i] &=
|
---|
208 | ~((1 << tab) - 1);
|
---|
209 | }
|
---|
210 |
|
---|
211 | bitmap->next_fit = start_byte;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** Copy portion of one bitmap into another bitmap.
|
---|
215 | *
|
---|
216 | * @param dst Destination bitmap.
|
---|
217 | * @param src Source bitmap.
|
---|
218 | * @param count Number of bits to copy.
|
---|
219 | *
|
---|
220 | */
|
---|
221 | void bitmap_copy(bitmap_t *dst, bitmap_t *src, size_t count)
|
---|
222 | {
|
---|
223 | assert(count <= dst->elements);
|
---|
224 | assert(count <= src->elements);
|
---|
225 |
|
---|
226 | size_t i;
|
---|
227 |
|
---|
228 | for (i = 0; i < count / BITMAP_ELEMENT; i++)
|
---|
229 | dst->bits[i] = src->bits[i];
|
---|
230 |
|
---|
231 | if (count % BITMAP_ELEMENT) {
|
---|
232 | bitmap_clear_range(dst, i * BITMAP_ELEMENT,
|
---|
233 | count % BITMAP_ELEMENT);
|
---|
234 | dst->bits[i] |= src->bits[i] &
|
---|
235 | ((1 << (count % BITMAP_ELEMENT)) - 1);
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | static int constraint_satisfy(size_t index, size_t base, size_t constraint)
|
---|
240 | {
|
---|
241 | return (((base + index) & constraint) == 0);
|
---|
242 | }
|
---|
243 |
|
---|
244 | /** Find a continuous zero bit range
|
---|
245 | *
|
---|
246 | * Find a continuous zero bit range in the bitmap. The address
|
---|
247 | * computed as the sum of the index of the first zero bit and
|
---|
248 | * the base argument needs to be compliant with the constraint
|
---|
249 | * (those bits that are set in the constraint cannot be set in
|
---|
250 | * the address).
|
---|
251 | *
|
---|
252 | * If the index argument is non-NULL, the continuous zero range
|
---|
253 | * is set and the index of the first bit is stored to index.
|
---|
254 | * Otherwise the bitmap stays untouched.
|
---|
255 | *
|
---|
256 | * @param bitmap Bitmap structure.
|
---|
257 | * @param count Number of continuous zero bits to find.
|
---|
258 | * @param base Address of the first bit in the bitmap.
|
---|
259 | * @param prefered Prefered address to start searching from.
|
---|
260 | * @param constraint Constraint for the address of the first zero bit.
|
---|
261 | * @param index Place to store the index of the first zero
|
---|
262 | * bit. Can be NULL (in which case the bitmap
|
---|
263 | * is not modified).
|
---|
264 | *
|
---|
265 | * @return Non-zero if a continuous range of zero bits satisfying
|
---|
266 | * the constraint has been found.
|
---|
267 | * @return Zero otherwise.
|
---|
268 | *
|
---|
269 | */
|
---|
270 | bool bitmap_allocate_range(bitmap_t *bitmap, size_t count, size_t base,
|
---|
271 | size_t prefered, size_t constraint, size_t *index)
|
---|
272 | {
|
---|
273 | if (count == 0)
|
---|
274 | return false;
|
---|
275 |
|
---|
276 | size_t size = bitmap_size(bitmap->elements);
|
---|
277 | size_t next_fit = bitmap->next_fit;
|
---|
278 |
|
---|
279 | /*
|
---|
280 | * Adjust the next-fit value according to the address
|
---|
281 | * the caller prefers to start the search at.
|
---|
282 | */
|
---|
283 | if ((prefered > base) && (prefered < base + bitmap->elements)) {
|
---|
284 | size_t prefered_fit = (prefered - base) / BITMAP_ELEMENT;
|
---|
285 |
|
---|
286 | if (prefered_fit > next_fit)
|
---|
287 | next_fit = prefered_fit;
|
---|
288 | }
|
---|
289 |
|
---|
290 | for (size_t pos = 0; pos < size; pos++) {
|
---|
291 | size_t byte = (next_fit + pos) % size;
|
---|
292 |
|
---|
293 | /* Skip if the current byte has all bits set */
|
---|
294 | if (bitmap->bits[byte] == ALL_ONES)
|
---|
295 | continue;
|
---|
296 |
|
---|
297 | size_t byte_bit = byte * BITMAP_ELEMENT;
|
---|
298 |
|
---|
299 | for (size_t bit = 0; bit < BITMAP_ELEMENT; bit++) {
|
---|
300 | size_t i = byte_bit + bit;
|
---|
301 |
|
---|
302 | if (i >= bitmap->elements)
|
---|
303 | break;
|
---|
304 |
|
---|
305 | if (!constraint_satisfy(i, base, constraint))
|
---|
306 | continue;
|
---|
307 |
|
---|
308 | if (!bitmap_get_fast(bitmap, i)) {
|
---|
309 | size_t continuous = 1;
|
---|
310 |
|
---|
311 | for (size_t j = 1; j < count; j++) {
|
---|
312 | if ((i + j < bitmap->elements) &&
|
---|
313 | (!bitmap_get_fast(bitmap, i + j)))
|
---|
314 | continuous++;
|
---|
315 | else
|
---|
316 | break;
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (continuous == count) {
|
---|
320 | if (index != NULL) {
|
---|
321 | bitmap_set_range(bitmap, i, count);
|
---|
322 | bitmap->next_fit = i / BITMAP_ELEMENT;
|
---|
323 | *index = i;
|
---|
324 | }
|
---|
325 |
|
---|
326 | return true;
|
---|
327 | } else
|
---|
328 | i += continuous;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | return false;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /** @}
|
---|
337 | */
|
---|