Rev 1248 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1200 | jermar | 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 | #include <adt/bitmap.h> |
||
30 | #include <typedefs.h> |
||
31 | #include <arch/types.h> |
||
32 | #include <align.h> |
||
33 | #include <debug.h> |
||
34 | |||
35 | #define ALL_ONES 0xff |
||
36 | #define ALL_ZEROES 0x00 |
||
37 | |||
38 | /** Initialize bitmap. |
||
39 | * |
||
40 | * No portion of the bitmap is set or cleared by this function. |
||
41 | * |
||
42 | * @param bitmap Bitmap structure. |
||
43 | * @param map Address of the memory used to hold the map. |
||
44 | * @param bits Number of bits stored in bitmap. |
||
45 | */ |
||
46 | void bitmap_initialize(bitmap_t *bitmap, __u8 *map, count_t bits) |
||
47 | { |
||
48 | bitmap->map = map; |
||
49 | bitmap->bits = bits; |
||
50 | } |
||
51 | |||
52 | /** Set range of bits. |
||
53 | * |
||
54 | * @param bitmap Bitmap structure. |
||
55 | * @param start Starting bit. |
||
56 | * @param bits Number of bits to set. |
||
57 | */ |
||
58 | void bitmap_set_range(bitmap_t *bitmap, index_t start, count_t bits) |
||
59 | { |
||
60 | index_t i; |
||
61 | index_t aligned_start; |
||
62 | count_t lub; /* leading unaligned bits */ |
||
63 | count_t tub; /* trailing unaligned bits */ |
||
64 | |||
65 | ASSERT(start + bits <= bitmap->bits); |
||
66 | |||
67 | aligned_start = ALIGN_UP(start, 8); |
||
68 | lub = aligned_start - start; |
||
69 | tub = (bits - lub) % 8; |
||
70 | |||
71 | if (lub) { |
||
72 | /* |
||
73 | * Make sure to set any leading unaligned bits. |
||
74 | */ |
||
75 | bitmap->map[start / 8] |= ~((1 << (8 - lub)) - 1); |
||
76 | } |
||
77 | for (i = 0; i < (bits - lub) / 8; i++) { |
||
78 | /* |
||
79 | * The middle bits can be set byte by byte. |
||
80 | */ |
||
81 | bitmap->map[aligned_start / 8 + i] = ALL_ONES; |
||
82 | } |
||
83 | if (tub) { |
||
84 | /* |
||
85 | * Make sure to set any trailing unaligned bits. |
||
86 | */ |
||
87 | bitmap->map[aligned_start / 8 + i] |= (1 << tub) - 1; |
||
88 | } |
||
89 | |||
90 | } |
||
91 | |||
92 | /** Clear range of bits. |
||
93 | * |
||
94 | * @param bitmap Bitmap structure. |
||
95 | * @param start Starting bit. |
||
96 | * @param bits Number of bits to clear. |
||
97 | */ |
||
98 | void bitmap_clear_range(bitmap_t *bitmap, index_t start, count_t bits) |
||
99 | { |
||
100 | index_t i; |
||
101 | index_t aligned_start; |
||
102 | count_t lub; /* leading unaligned bits */ |
||
103 | count_t tub; /* trailing unaligned bits */ |
||
104 | |||
105 | ASSERT(start + bits <= bitmap->bits); |
||
106 | |||
107 | aligned_start = ALIGN_UP(start, 8); |
||
108 | lub = aligned_start - start; |
||
109 | tub = (bits - lub) % 8; |
||
110 | |||
111 | if (lub) { |
||
112 | /* |
||
113 | * Make sure to clear any leading unaligned bits. |
||
114 | */ |
||
115 | bitmap->map[start / 8] &= (1 << (8 - lub)) - 1; |
||
116 | } |
||
117 | for (i = 0; i < (bits - lub) / 8; i++) { |
||
118 | /* |
||
119 | * The middle bits can be cleared byte by byte. |
||
120 | */ |
||
121 | bitmap->map[aligned_start / 8 + i] = ALL_ZEROES; |
||
122 | } |
||
123 | if (tub) { |
||
124 | /* |
||
125 | * Make sure to clear any trailing unaligned bits. |
||
126 | */ |
||
127 | bitmap->map[aligned_start / 8 + i] &= ~((1 << tub) - 1); |
||
128 | } |
||
129 | |||
130 | } |
||
131 | |||
132 | /** Copy portion of one bitmap into another bitmap. |
||
133 | * |
||
134 | * @param dst Destination bitmap. |
||
135 | * @param src Source bitmap. |
||
136 | * @param bits Number of bits to copy. |
||
137 | */ |
||
138 | void bitmap_copy(bitmap_t *dst, bitmap_t *src, count_t bits) |
||
139 | { |
||
140 | index_t i; |
||
141 | |||
142 | ASSERT(bits <= dst->bits); |
||
143 | ASSERT(bits <= src->bits); |
||
144 | |||
145 | for (i = 0; i < bits / 8; i++) |
||
146 | dst->map[i] = src->map[i]; |
||
147 | |||
148 | if (bits % 8) { |
||
149 | bitmap_clear_range(dst, i * 8, bits % 8); |
||
150 | dst->map[i] |= src->map[i] & ((1 << (bits % 8)) - 1); |
||
151 | } |
||
152 | } |