Rev 1248 | Rev 1549 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1248 | Rev 1262 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Jakub Jermar |
2 | * Copyright (C) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** |
29 | /** |
30 | * @file bitmap.c |
30 | * @file bitmap.c |
31 | * @brief Implementation of bitmap ADT. |
31 | * @brief Implementation of bitmap ADT. |
32 | * |
32 | * |
33 | * This file implements bitmap ADT and provides functions for |
33 | * This file implements bitmap ADT and provides functions for |
34 | * setting and clearing ranges of bits. |
34 | * setting and clearing ranges of bits. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <adt/bitmap.h> |
37 | #include <adt/bitmap.h> |
38 | #include <typedefs.h> |
38 | #include <typedefs.h> |
39 | #include <arch/types.h> |
39 | #include <arch/types.h> |
40 | #include <align.h> |
40 | #include <align.h> |
41 | #include <debug.h> |
41 | #include <debug.h> |
- | 42 | #include <macros.h> |
|
42 | 43 | ||
43 | #define ALL_ONES 0xff |
44 | #define ALL_ONES 0xff |
44 | #define ALL_ZEROES 0x00 |
45 | #define ALL_ZEROES 0x00 |
45 | 46 | ||
46 | /** Initialize bitmap. |
47 | /** Initialize bitmap. |
47 | * |
48 | * |
48 | * No portion of the bitmap is set or cleared by this function. |
49 | * No portion of the bitmap is set or cleared by this function. |
49 | * |
50 | * |
50 | * @param bitmap Bitmap structure. |
51 | * @param bitmap Bitmap structure. |
51 | * @param map Address of the memory used to hold the map. |
52 | * @param map Address of the memory used to hold the map. |
52 | * @param bits Number of bits stored in bitmap. |
53 | * @param bits Number of bits stored in bitmap. |
53 | */ |
54 | */ |
54 | void bitmap_initialize(bitmap_t *bitmap, __u8 *map, count_t bits) |
55 | void bitmap_initialize(bitmap_t *bitmap, __u8 *map, count_t bits) |
55 | { |
56 | { |
56 | bitmap->map = map; |
57 | bitmap->map = map; |
57 | bitmap->bits = bits; |
58 | bitmap->bits = bits; |
58 | } |
59 | } |
59 | 60 | ||
60 | /** Set range of bits. |
61 | /** Set range of bits. |
61 | * |
62 | * |
62 | * @param bitmap Bitmap structure. |
63 | * @param bitmap Bitmap structure. |
63 | * @param start Starting bit. |
64 | * @param start Starting bit. |
64 | * @param bits Number of bits to set. |
65 | * @param bits Number of bits to set. |
65 | */ |
66 | */ |
66 | void bitmap_set_range(bitmap_t *bitmap, index_t start, count_t bits) |
67 | void bitmap_set_range(bitmap_t *bitmap, index_t start, count_t bits) |
67 | { |
68 | { |
68 | index_t i; |
69 | index_t i; |
69 | index_t aligned_start; |
70 | index_t aligned_start; |
70 | count_t lub; /* leading unaligned bits */ |
71 | count_t lub; /* leading unaligned bits */ |
- | 72 | count_t amb; /* aligned middle bits */ |
|
71 | count_t tub; /* trailing unaligned bits */ |
73 | count_t tab; /* trailing aligned bits */ |
72 | 74 | ||
73 | ASSERT(start + bits <= bitmap->bits); |
75 | ASSERT(start + bits <= bitmap->bits); |
74 | 76 | ||
75 | aligned_start = ALIGN_UP(start, 8); |
77 | aligned_start = ALIGN_UP(start, 8); |
76 | lub = aligned_start - start; |
78 | lub = min(aligned_start - start, bits); |
- | 79 | amb = bits > lub ? bits - lub : 0; |
|
77 | tub = (bits - lub) % 8; |
80 | tab = amb % 8; |
78 | 81 | ||
79 | if (lub) { |
82 | if (lub) { |
80 | /* |
83 | /* |
81 | * Make sure to set any leading unaligned bits. |
84 | * Make sure to set any leading unaligned bits. |
82 | */ |
85 | */ |
83 | bitmap->map[start / 8] |= ~((1 << (8 - lub)) - 1); |
86 | bitmap->map[start / 8] |= ~((1 << (8 - lub)) - 1); |
84 | } |
87 | } |
85 | for (i = 0; i < (bits - lub) / 8; i++) { |
88 | for (i = 0; i < amb / 8; i++) { |
86 | /* |
89 | /* |
87 | * The middle bits can be set byte by byte. |
90 | * The middle bits can be set byte by byte. |
88 | */ |
91 | */ |
89 | bitmap->map[aligned_start / 8 + i] = ALL_ONES; |
92 | bitmap->map[aligned_start / 8 + i] = ALL_ONES; |
90 | } |
93 | } |
91 | if (tub) { |
94 | if (tab) { |
92 | /* |
95 | /* |
93 | * Make sure to set any trailing unaligned bits. |
96 | * Make sure to set any trailing aligned bits. |
94 | */ |
97 | */ |
95 | bitmap->map[aligned_start / 8 + i] |= (1 << tub) - 1; |
98 | bitmap->map[aligned_start / 8 + i] |= (1 << tab) - 1; |
96 | } |
99 | } |
97 | 100 | ||
98 | } |
101 | } |
99 | 102 | ||
100 | /** Clear range of bits. |
103 | /** Clear range of bits. |
101 | * |
104 | * |
102 | * @param bitmap Bitmap structure. |
105 | * @param bitmap Bitmap structure. |
103 | * @param start Starting bit. |
106 | * @param start Starting bit. |
104 | * @param bits Number of bits to clear. |
107 | * @param bits Number of bits to clear. |
105 | */ |
108 | */ |
106 | void bitmap_clear_range(bitmap_t *bitmap, index_t start, count_t bits) |
109 | void bitmap_clear_range(bitmap_t *bitmap, index_t start, count_t bits) |
107 | { |
110 | { |
108 | index_t i; |
111 | index_t i; |
109 | index_t aligned_start; |
112 | index_t aligned_start; |
110 | count_t lub; /* leading unaligned bits */ |
113 | count_t lub; /* leading unaligned bits */ |
- | 114 | count_t amb; /* aligned middle bits */ |
|
111 | count_t tub; /* trailing unaligned bits */ |
115 | count_t tab; /* trailing aligned bits */ |
112 | 116 | ||
113 | ASSERT(start + bits <= bitmap->bits); |
117 | ASSERT(start + bits <= bitmap->bits); |
114 | 118 | ||
115 | aligned_start = ALIGN_UP(start, 8); |
119 | aligned_start = ALIGN_UP(start, 8); |
116 | lub = aligned_start - start; |
120 | lub = min(aligned_start - start, bits); |
- | 121 | amb = bits > lub ? bits - lub : 0; |
|
117 | tub = (bits - lub) % 8; |
122 | tab = amb % 8; |
118 | 123 | ||
119 | if (lub) { |
124 | if (lub) { |
120 | /* |
125 | /* |
121 | * Make sure to clear any leading unaligned bits. |
126 | * Make sure to clear any leading unaligned bits. |
122 | */ |
127 | */ |
123 | bitmap->map[start / 8] &= (1 << (8 - lub)) - 1; |
128 | bitmap->map[start / 8] &= (1 << (8 - lub)) - 1; |
124 | } |
129 | } |
125 | for (i = 0; i < (bits - lub) / 8; i++) { |
130 | for (i = 0; i < amb / 8; i++) { |
126 | /* |
131 | /* |
127 | * The middle bits can be cleared byte by byte. |
132 | * The middle bits can be cleared byte by byte. |
128 | */ |
133 | */ |
129 | bitmap->map[aligned_start / 8 + i] = ALL_ZEROES; |
134 | bitmap->map[aligned_start / 8 + i] = ALL_ZEROES; |
130 | } |
135 | } |
131 | if (tub) { |
136 | if (tab) { |
132 | /* |
137 | /* |
133 | * Make sure to clear any trailing unaligned bits. |
138 | * Make sure to clear any trailing aligned bits. |
134 | */ |
139 | */ |
135 | bitmap->map[aligned_start / 8 + i] &= ~((1 << tub) - 1); |
140 | bitmap->map[aligned_start / 8 + i] &= ~((1 << tab) - 1); |
136 | } |
141 | } |
137 | 142 | ||
138 | } |
143 | } |
139 | 144 | ||
140 | /** Copy portion of one bitmap into another bitmap. |
145 | /** Copy portion of one bitmap into another bitmap. |
141 | * |
146 | * |
142 | * @param dst Destination bitmap. |
147 | * @param dst Destination bitmap. |
143 | * @param src Source bitmap. |
148 | * @param src Source bitmap. |
144 | * @param bits Number of bits to copy. |
149 | * @param bits Number of bits to copy. |
145 | */ |
150 | */ |
146 | void bitmap_copy(bitmap_t *dst, bitmap_t *src, count_t bits) |
151 | void bitmap_copy(bitmap_t *dst, bitmap_t *src, count_t bits) |
147 | { |
152 | { |
148 | index_t i; |
153 | index_t i; |
149 | 154 | ||
150 | ASSERT(bits <= dst->bits); |
155 | ASSERT(bits <= dst->bits); |
151 | ASSERT(bits <= src->bits); |
156 | ASSERT(bits <= src->bits); |
152 | 157 | ||
153 | for (i = 0; i < bits / 8; i++) |
158 | for (i = 0; i < bits / 8; i++) |
154 | dst->map[i] = src->map[i]; |
159 | dst->map[i] = src->map[i]; |
155 | 160 | ||
156 | if (bits % 8) { |
161 | if (bits % 8) { |
157 | bitmap_clear_range(dst, i * 8, bits % 8); |
162 | bitmap_clear_range(dst, i * 8, bits % 8); |
158 | dst->map[i] |= src->map[i] & ((1 << (bits % 8)) - 1); |
163 | dst->map[i] |= src->map[i] & ((1 << (bits % 8)) - 1); |
159 | } |
164 | } |
160 | } |
165 | } |
161 | 166 |