Subversion Repositories HelenOS-historic

Rev

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

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