Subversion Repositories HelenOS-historic

Rev

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

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