File: bitmap.cpp

package info (click to toggle)
polyml 5.6-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 31,892 kB
  • ctags: 34,453
  • sloc: cpp: 44,983; ansic: 24,520; asm: 14,850; sh: 11,730; makefile: 551; exp: 484; python: 253; awk: 91; sed: 9
file content (243 lines) | stat: -rw-r--r-- 6,482 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
    Title:  Bitmap.  Generally used by the garbage collector to indicate allocated words

    Copyright (c) 2006, 2012  David C.J. Matthews
       Based on original code in garbage_collect.c.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

/*
   Bitmaps are used particularly in the garbage collector to indicate allocated
   words.  The efficiency of this code is crucial for the speed of the garbage
   collector.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_WIN32)
#include "winconfig.h"
#else
#error "No configuration file"
#endif

#ifdef HAVE_ASSERT_H
#include <assert.h>
#define ASSERT(h) assert(h)
#else
#define ASSERT(h)
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include "bitmap.h"
#include "globals.h"

bool Bitmap::Create(POLYUNSIGNED bits)
{
    free(m_bits); // Any previous data
    size_t bytes = (bits+7) >> 3;
    m_bits = (unsigned char*)malloc(bytes);
    if (m_bits != 0) memset(m_bits, 0, bytes);
    return m_bits != 0;
}

void Bitmap::Destroy()
{
    free(m_bits);
    m_bits = 0;
}

Bitmap::~Bitmap()
{
    Destroy();
}

// Set a range of bits in a bitmap.  This checks that the bits are not already set.
void Bitmap::SetBits(POLYUNSIGNED bitno, POLYUNSIGNED length)
{
    POLYUNSIGNED byte_index = bitno >> 3;
    
    ASSERT (0 < length); // Strictly positive
    
    /* Set the first part byte */
    POLYUNSIGNED start_bit_index = bitno & 7;
    POLYUNSIGNED stop_bit_index  = start_bit_index + length;
    /* Do we need to change more than one byte? */
    if (stop_bit_index < 8)
    {
        const unsigned mask1 = 0xff << start_bit_index;
        const unsigned mask2 = 0xff << stop_bit_index;
        const unsigned mask  = mask1 & ~mask2;
        
//        ASSERT((m_bits[byte_index] & mask) == 0);
        m_bits[byte_index] |= mask;
        return;
    }
    else /* Set all the bits we can in the first byte */
    {
        const unsigned mask  = 0xff << start_bit_index;
        
//        ASSERT((m_bits[byte_index] & mask) == 0);
        m_bits[byte_index] |= mask;
        
        /* length = length - (8 - start_bit_index); */
        length = stop_bit_index - 8;
    }
    
    /* Invariant: 0 <= length */
    ASSERT(length >= 0);
    
    /* Set as many full bytes as possible */
    while (8 <= length)
    {
        /* Invariant: 8 <= length */
        byte_index ++;
//        ASSERT(m_bits[byte_index] == 0);
        m_bits[byte_index] = 0xff;
        length -= 8;
        /* Invariant: 0 <= length */
    }
    
    /* Invariant: 0 <= length < 8 */
    ASSERT(length >= 0 && length < 8);
    if (length == 0) return;
    
    /* Invariant: 0 < length < 8 */
    
    /* Set the final part byte */
    byte_index ++;
    const unsigned mask  = 0xff & ~(0xff << length);
//    ASSERT((m_bits[byte_index] & mask) == 0);
    m_bits[byte_index] |= mask;
}

// Clear a range of bits.  This is only used to clear the bitmap so
// it does not need to be exact so long as at least the range specified
// is zero.
void Bitmap::ClearBits(POLYUNSIGNED bitno, POLYUNSIGNED length)
{
    POLYUNSIGNED byte_index = bitno >> 3;
    length += bitno & 7;
    size_t bytes = length >> 3;
    if (length & 7) bytes++;
    memset(m_bits+byte_index, 0, bytes);
}

// How many zero bits (maximum n) are there in the bitmap, starting at location start? */
POLYUNSIGNED Bitmap::CountZeroBits(POLYUNSIGNED bitno, POLYUNSIGNED n) const
{
    POLYUNSIGNED byte_index = bitno >> 3;
    unsigned bit_index  = bitno & 7;
    unsigned mask  = 1 << bit_index;
    POLYUNSIGNED zero_bits  = 0;
    ASSERT (0 < n); // Strictly positive
    
    /* Check the first part byte */
    while (mask != 0)
    {
        /* zero_bits < n */
        if ((m_bits[byte_index] & mask) != 0) return zero_bits;
        zero_bits ++;
        if (zero_bits == n) return zero_bits;
        mask = (mask << 1) & 0xff;
        /* zero_bits < n */
    }
    
    /* zero_bits < n */
    
    /* Check as many bytes as possible */
    byte_index ++;
    while (zero_bits < n && m_bits[byte_index] == 0)
    {
        zero_bits += 8;
        byte_index ++;
    }
    
    /* Check the final part byte */
    mask = 1;
    while (zero_bits < n && (m_bits[byte_index] & mask) == 0)
    {
        zero_bits ++;
        mask = (mask << 1) & 0xff;
    }
    
    return zero_bits;
}


// Search the bitmap from the high end down looking for n contiguous zeros
// Returns the value of "bitno" on failure. .
POLYUNSIGNED Bitmap::FindFree
(
  POLYUNSIGNED   limit,  /* The highest numbered bit that's too small to use */
  POLYUNSIGNED   start,  /* The lowest numbered bit that's too large to use */
  POLYUNSIGNED   n       /* The number of consecutive zero bits required */
) const
{
    if (limit + n >= start)
        return start; // Failure

    POLYUNSIGNED candidate = start - n;
    ASSERT (start > limit);
    
    while (1)
    {
        POLYUNSIGNED bits_free = CountZeroBits(candidate, n);
        
        if (n <= bits_free)
            return candidate;

        if (candidate < n - bits_free + limit)
            return start; // Failure
        
        candidate -= (n - bits_free);
    }
}

// Count the number of set bits in the bitmap.
POLYUNSIGNED Bitmap::CountSetBits(POLYUNSIGNED size) const
{
    size_t bytes = (size+7) >> 3;
    POLYUNSIGNED count = 0;
    for (size_t i = 0; i < bytes; i++)
    {
        unsigned char byte = m_bits[i];
        if (byte == 0xff) // Common case
            count += 8;
        else
        {
            while (byte != 0)
            {
                unsigned char b = byte & (-byte);
                count++;
                byte -= b;
            }
        }
    }
    return count;
}