Subversion Repositories HelenOS

Rev

Rev 2071 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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