annotate src/share/vm/utilities/bitMap.cpp @ 196:d1605aabd0a1 jdk7-b30

6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
author xdono
date Wed, 02 Jul 2008 12:55:16 -0700
parents a61af66fc99e
children 37f87013dfd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 # include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
26 # include "incls/_bitMap.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29 BitMap::BitMap(idx_t* map, idx_t size_in_bits) {
a61af66fc99e Initial load
duke
parents:
diff changeset
30 assert(size_in_bits >= 0, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
31 _map = map;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 _size = size_in_bits;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 }
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 BitMap::BitMap(idx_t size_in_bits) {
a61af66fc99e Initial load
duke
parents:
diff changeset
37 assert(size_in_bits >= 0, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
38 _size = size_in_bits;
a61af66fc99e Initial load
duke
parents:
diff changeset
39 _map = NEW_RESOURCE_ARRAY(idx_t, size_in_words());
a61af66fc99e Initial load
duke
parents:
diff changeset
40 }
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42
a61af66fc99e Initial load
duke
parents:
diff changeset
43 void BitMap::resize(idx_t size_in_bits) {
a61af66fc99e Initial load
duke
parents:
diff changeset
44 assert(size_in_bits >= 0, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
45 size_t old_size_in_words = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
46 uintptr_t* old_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
47 _size = size_in_bits;
a61af66fc99e Initial load
duke
parents:
diff changeset
48 size_t new_size_in_words = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
49 _map = NEW_RESOURCE_ARRAY(idx_t, new_size_in_words);
a61af66fc99e Initial load
duke
parents:
diff changeset
50 Copy::disjoint_words((HeapWord*) old_map, (HeapWord*) _map, MIN2(old_size_in_words, new_size_in_words));
a61af66fc99e Initial load
duke
parents:
diff changeset
51 if (new_size_in_words > old_size_in_words) {
a61af66fc99e Initial load
duke
parents:
diff changeset
52 clear_range_of_words(old_size_in_words, size_in_words());
a61af66fc99e Initial load
duke
parents:
diff changeset
53 }
a61af66fc99e Initial load
duke
parents:
diff changeset
54 }
a61af66fc99e Initial load
duke
parents:
diff changeset
55
a61af66fc99e Initial load
duke
parents:
diff changeset
56 // Returns a bit mask for a range of bits [beg, end) within a single word. Each
a61af66fc99e Initial load
duke
parents:
diff changeset
57 // bit in the mask is 0 if the bit is in the range, 1 if not in the range. The
a61af66fc99e Initial load
duke
parents:
diff changeset
58 // returned mask can be used directly to clear the range, or inverted to set the
a61af66fc99e Initial load
duke
parents:
diff changeset
59 // range. Note: end must not be 0.
a61af66fc99e Initial load
duke
parents:
diff changeset
60 inline BitMap::idx_t
a61af66fc99e Initial load
duke
parents:
diff changeset
61 BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
62 assert(end != 0, "does not work when end == 0");
a61af66fc99e Initial load
duke
parents:
diff changeset
63 assert(beg == end || word_index(beg) == word_index(end - 1),
a61af66fc99e Initial load
duke
parents:
diff changeset
64 "must be a single-word range");
a61af66fc99e Initial load
duke
parents:
diff changeset
65 idx_t mask = bit_mask(beg) - 1; // low (right) bits
a61af66fc99e Initial load
duke
parents:
diff changeset
66 if (bit_in_word(end) != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
67 mask |= ~(bit_mask(end) - 1); // high (left) bits
a61af66fc99e Initial load
duke
parents:
diff changeset
68 }
a61af66fc99e Initial load
duke
parents:
diff changeset
69 return mask;
a61af66fc99e Initial load
duke
parents:
diff changeset
70 }
a61af66fc99e Initial load
duke
parents:
diff changeset
71
a61af66fc99e Initial load
duke
parents:
diff changeset
72 void BitMap::set_range_within_word(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
73 // With a valid range (beg <= end), this test ensures that end != 0, as
a61af66fc99e Initial load
duke
parents:
diff changeset
74 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
a61af66fc99e Initial load
duke
parents:
diff changeset
75 if (beg != end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
76 idx_t mask = inverted_bit_mask_for_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
77 *word_addr(beg) |= ~mask;
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79 }
a61af66fc99e Initial load
duke
parents:
diff changeset
80
a61af66fc99e Initial load
duke
parents:
diff changeset
81 void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 // With a valid range (beg <= end), this test ensures that end != 0, as
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
a61af66fc99e Initial load
duke
parents:
diff changeset
84 if (beg != end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 idx_t mask = inverted_bit_mask_for_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
86 *word_addr(beg) &= mask;
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
91 assert(value == 0 || value == 1, "0 for clear, 1 for set");
a61af66fc99e Initial load
duke
parents:
diff changeset
92 // With a valid range (beg <= end), this test ensures that end != 0, as
a61af66fc99e Initial load
duke
parents:
diff changeset
93 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
a61af66fc99e Initial load
duke
parents:
diff changeset
94 if (beg != end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 intptr_t* pw = (intptr_t*)word_addr(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
96 intptr_t w = *pw;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 intptr_t mr = (intptr_t)inverted_bit_mask_for_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
98 intptr_t nw = value ? (w | ~mr) : (w & mr);
a61af66fc99e Initial load
duke
parents:
diff changeset
99 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
a61af66fc99e Initial load
duke
parents:
diff changeset
101 if (res == w) break;
a61af66fc99e Initial load
duke
parents:
diff changeset
102 w = *pw;
a61af66fc99e Initial load
duke
parents:
diff changeset
103 nw = value ? (w | ~mr) : (w & mr);
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106 }
a61af66fc99e Initial load
duke
parents:
diff changeset
107
a61af66fc99e Initial load
duke
parents:
diff changeset
108 inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
109 memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t));
a61af66fc99e Initial load
duke
parents:
diff changeset
110 }
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
113 memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t));
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 idx_t bit_rounded_up = bit + (BitsPerWord - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
118 // Check for integer arithmetic overflow.
a61af66fc99e Initial load
duke
parents:
diff changeset
119 return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
120 }
a61af66fc99e Initial load
duke
parents:
diff changeset
121
a61af66fc99e Initial load
duke
parents:
diff changeset
122 void BitMap::set_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
123 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
124
a61af66fc99e Initial load
duke
parents:
diff changeset
125 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
126 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
127
a61af66fc99e Initial load
duke
parents:
diff changeset
128 if (beg_full_word < end_full_word) {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
130 set_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
131 set_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
132 set_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
133 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
134 // The range spans at most 2 partial words.
a61af66fc99e Initial load
duke
parents:
diff changeset
135 idx_t boundary = MIN2(bit_index(beg_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
136 set_range_within_word(beg, boundary);
a61af66fc99e Initial load
duke
parents:
diff changeset
137 set_range_within_word(boundary, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 void BitMap::clear_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
142 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
143
a61af66fc99e Initial load
duke
parents:
diff changeset
144 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
145 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
146
a61af66fc99e Initial load
duke
parents:
diff changeset
147 if (beg_full_word < end_full_word) {
a61af66fc99e Initial load
duke
parents:
diff changeset
148 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
149 clear_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
150 clear_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
151 clear_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
152 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 // The range spans at most 2 partial words.
a61af66fc99e Initial load
duke
parents:
diff changeset
154 idx_t boundary = MIN2(bit_index(beg_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
155 clear_range_within_word(beg, boundary);
a61af66fc99e Initial load
duke
parents:
diff changeset
156 clear_range_within_word(boundary, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
157 }
a61af66fc99e Initial load
duke
parents:
diff changeset
158 }
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 void BitMap::set_large_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
161 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
162
a61af66fc99e Initial load
duke
parents:
diff changeset
163 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
164 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
165
a61af66fc99e Initial load
duke
parents:
diff changeset
166 assert(end_full_word - beg_full_word >= 32,
a61af66fc99e Initial load
duke
parents:
diff changeset
167 "the range must include at least 32 bytes");
a61af66fc99e Initial load
duke
parents:
diff changeset
168
a61af66fc99e Initial load
duke
parents:
diff changeset
169 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
170 set_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
171 set_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
172 set_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
173 }
a61af66fc99e Initial load
duke
parents:
diff changeset
174
a61af66fc99e Initial load
duke
parents:
diff changeset
175 void BitMap::clear_large_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
176 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
177
a61af66fc99e Initial load
duke
parents:
diff changeset
178 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
179 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
180
a61af66fc99e Initial load
duke
parents:
diff changeset
181 assert(end_full_word - beg_full_word >= 32,
a61af66fc99e Initial load
duke
parents:
diff changeset
182 "the range must include at least 32 bytes");
a61af66fc99e Initial load
duke
parents:
diff changeset
183
a61af66fc99e Initial load
duke
parents:
diff changeset
184 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
185 clear_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
186 clear_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
187 clear_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
188 }
a61af66fc99e Initial load
duke
parents:
diff changeset
189
a61af66fc99e Initial load
duke
parents:
diff changeset
190 void BitMap::at_put(idx_t offset, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
191 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
192 set_bit(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
193 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 clear_bit(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }
a61af66fc99e Initial load
duke
parents:
diff changeset
197
a61af66fc99e Initial load
duke
parents:
diff changeset
198 // Return true to indicate that this thread changed
a61af66fc99e Initial load
duke
parents:
diff changeset
199 // the bit, false to indicate that someone else did.
a61af66fc99e Initial load
duke
parents:
diff changeset
200 // In either case, the requested bit is in the
a61af66fc99e Initial load
duke
parents:
diff changeset
201 // requested state some time during the period that
a61af66fc99e Initial load
duke
parents:
diff changeset
202 // this thread is executing this call. More importantly,
a61af66fc99e Initial load
duke
parents:
diff changeset
203 // if no other thread is executing an action to
a61af66fc99e Initial load
duke
parents:
diff changeset
204 // change the requested bit to a state other than
a61af66fc99e Initial load
duke
parents:
diff changeset
205 // the one that this thread is trying to set it to,
a61af66fc99e Initial load
duke
parents:
diff changeset
206 // then the the bit is in the expected state
a61af66fc99e Initial load
duke
parents:
diff changeset
207 // at exit from this method. However, rather than
a61af66fc99e Initial load
duke
parents:
diff changeset
208 // make such a strong assertion here, based on
a61af66fc99e Initial load
duke
parents:
diff changeset
209 // assuming such constrained use (which though true
a61af66fc99e Initial load
duke
parents:
diff changeset
210 // today, could change in the future to service some
a61af66fc99e Initial load
duke
parents:
diff changeset
211 // funky parallel algorithm), we encourage callers
a61af66fc99e Initial load
duke
parents:
diff changeset
212 // to do such verification, as and when appropriate.
a61af66fc99e Initial load
duke
parents:
diff changeset
213 bool BitMap::par_at_put(idx_t bit, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
214 return value ? par_set_bit(bit) : par_clear_bit(bit);
a61af66fc99e Initial load
duke
parents:
diff changeset
215 }
a61af66fc99e Initial load
duke
parents:
diff changeset
216
a61af66fc99e Initial load
duke
parents:
diff changeset
217 void BitMap::at_put_grow(idx_t offset, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
218 if (offset >= size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
219 resize(2 * MAX2(size(), offset));
a61af66fc99e Initial load
duke
parents:
diff changeset
220 }
a61af66fc99e Initial load
duke
parents:
diff changeset
221 at_put(offset, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
222 }
a61af66fc99e Initial load
duke
parents:
diff changeset
223
a61af66fc99e Initial load
duke
parents:
diff changeset
224 void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
225 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
226 set_range(start_offset, end_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
227 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
228 clear_range(start_offset, end_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
229 }
a61af66fc99e Initial load
duke
parents:
diff changeset
230 }
a61af66fc99e Initial load
duke
parents:
diff changeset
231
a61af66fc99e Initial load
duke
parents:
diff changeset
232 void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
233 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
234
a61af66fc99e Initial load
duke
parents:
diff changeset
235 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
236 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
237
a61af66fc99e Initial load
duke
parents:
diff changeset
238 if (beg_full_word < end_full_word) {
a61af66fc99e Initial load
duke
parents:
diff changeset
239 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
240 par_put_range_within_word(beg, bit_index(beg_full_word), value);
a61af66fc99e Initial load
duke
parents:
diff changeset
241 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
242 set_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
243 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
244 clear_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
245 }
a61af66fc99e Initial load
duke
parents:
diff changeset
246 par_put_range_within_word(bit_index(end_full_word), end, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
247 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
248 // The range spans at most 2 partial words.
a61af66fc99e Initial load
duke
parents:
diff changeset
249 idx_t boundary = MIN2(bit_index(beg_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
250 par_put_range_within_word(beg, boundary, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
251 par_put_range_within_word(boundary, end, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
252 }
a61af66fc99e Initial load
duke
parents:
diff changeset
253
a61af66fc99e Initial load
duke
parents:
diff changeset
254 }
a61af66fc99e Initial load
duke
parents:
diff changeset
255
a61af66fc99e Initial load
duke
parents:
diff changeset
256 void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
257 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
258 set_large_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
259 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
260 clear_large_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
261 }
a61af66fc99e Initial load
duke
parents:
diff changeset
262 }
a61af66fc99e Initial load
duke
parents:
diff changeset
263
a61af66fc99e Initial load
duke
parents:
diff changeset
264 void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
265 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
266
a61af66fc99e Initial load
duke
parents:
diff changeset
267 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
268 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
269
a61af66fc99e Initial load
duke
parents:
diff changeset
270 assert(end_full_word - beg_full_word >= 32,
a61af66fc99e Initial load
duke
parents:
diff changeset
271 "the range must include at least 32 bytes");
a61af66fc99e Initial load
duke
parents:
diff changeset
272
a61af66fc99e Initial load
duke
parents:
diff changeset
273 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
274 par_put_range_within_word(beg, bit_index(beg_full_word), value);
a61af66fc99e Initial load
duke
parents:
diff changeset
275 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
276 set_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
277 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 clear_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
279 }
a61af66fc99e Initial load
duke
parents:
diff changeset
280 par_put_range_within_word(bit_index(end_full_word), end, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
281 }
a61af66fc99e Initial load
duke
parents:
diff changeset
282
a61af66fc99e Initial load
duke
parents:
diff changeset
283 bool BitMap::contains(const BitMap other) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
284 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
285 uintptr_t* dest_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
286 uintptr_t* other_map = other.map();
a61af66fc99e Initial load
duke
parents:
diff changeset
287 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
288 for (idx_t index = 0; index < size_in_words(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
289 uintptr_t word_union = dest_map[index] | other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
290 // If this has more bits set than dest_map[index], then other is not a
a61af66fc99e Initial load
duke
parents:
diff changeset
291 // subset.
a61af66fc99e Initial load
duke
parents:
diff changeset
292 if (word_union != dest_map[index]) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
293 }
a61af66fc99e Initial load
duke
parents:
diff changeset
294 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
295 }
a61af66fc99e Initial load
duke
parents:
diff changeset
296
a61af66fc99e Initial load
duke
parents:
diff changeset
297 bool BitMap::intersects(const BitMap other) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
298 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
299 uintptr_t* dest_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
300 uintptr_t* other_map = other.map();
a61af66fc99e Initial load
duke
parents:
diff changeset
301 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
302 for (idx_t index = 0; index < size_in_words(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 if ((dest_map[index] & other_map[index]) != 0) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
304 }
a61af66fc99e Initial load
duke
parents:
diff changeset
305 // Otherwise, no intersection.
a61af66fc99e Initial load
duke
parents:
diff changeset
306 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
307 }
a61af66fc99e Initial load
duke
parents:
diff changeset
308
a61af66fc99e Initial load
duke
parents:
diff changeset
309 void BitMap::set_union(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
310 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
311 idx_t* dest_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
312 idx_t* other_map = other.map();
a61af66fc99e Initial load
duke
parents:
diff changeset
313 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
314 for (idx_t index = 0; index < size_in_words(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
315 dest_map[index] = dest_map[index] | other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
316 }
a61af66fc99e Initial load
duke
parents:
diff changeset
317 }
a61af66fc99e Initial load
duke
parents:
diff changeset
318
a61af66fc99e Initial load
duke
parents:
diff changeset
319
a61af66fc99e Initial load
duke
parents:
diff changeset
320 void BitMap::set_difference(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
321 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
322 idx_t* dest_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
323 idx_t* other_map = other.map();
a61af66fc99e Initial load
duke
parents:
diff changeset
324 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
325 for (idx_t index = 0; index < size_in_words(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
326 dest_map[index] = dest_map[index] & ~(other_map[index]);
a61af66fc99e Initial load
duke
parents:
diff changeset
327 }
a61af66fc99e Initial load
duke
parents:
diff changeset
328 }
a61af66fc99e Initial load
duke
parents:
diff changeset
329
a61af66fc99e Initial load
duke
parents:
diff changeset
330
a61af66fc99e Initial load
duke
parents:
diff changeset
331 void BitMap::set_intersection(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
332 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
333 idx_t* dest_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
334 idx_t* other_map = other.map();
a61af66fc99e Initial load
duke
parents:
diff changeset
335 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
336 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
337 dest_map[index] = dest_map[index] & other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
338 }
a61af66fc99e Initial load
duke
parents:
diff changeset
339 }
a61af66fc99e Initial load
duke
parents:
diff changeset
340
a61af66fc99e Initial load
duke
parents:
diff changeset
341
a61af66fc99e Initial load
duke
parents:
diff changeset
342 bool BitMap::set_union_with_result(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
343 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
344 bool changed = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
345 idx_t* dest_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
346 idx_t* other_map = other.map();
a61af66fc99e Initial load
duke
parents:
diff changeset
347 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
348 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
349 idx_t temp = map(index) | other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
350 changed = changed || (temp != map(index));
a61af66fc99e Initial load
duke
parents:
diff changeset
351 map()[index] = temp;
a61af66fc99e Initial load
duke
parents:
diff changeset
352 }
a61af66fc99e Initial load
duke
parents:
diff changeset
353 return changed;
a61af66fc99e Initial load
duke
parents:
diff changeset
354 }
a61af66fc99e Initial load
duke
parents:
diff changeset
355
a61af66fc99e Initial load
duke
parents:
diff changeset
356
a61af66fc99e Initial load
duke
parents:
diff changeset
357 bool BitMap::set_difference_with_result(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
358 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
359 bool changed = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
360 idx_t* dest_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
361 idx_t* other_map = other.map();
a61af66fc99e Initial load
duke
parents:
diff changeset
362 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
363 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
364 idx_t temp = dest_map[index] & ~(other_map[index]);
a61af66fc99e Initial load
duke
parents:
diff changeset
365 changed = changed || (temp != dest_map[index]);
a61af66fc99e Initial load
duke
parents:
diff changeset
366 dest_map[index] = temp;
a61af66fc99e Initial load
duke
parents:
diff changeset
367 }
a61af66fc99e Initial load
duke
parents:
diff changeset
368 return changed;
a61af66fc99e Initial load
duke
parents:
diff changeset
369 }
a61af66fc99e Initial load
duke
parents:
diff changeset
370
a61af66fc99e Initial load
duke
parents:
diff changeset
371
a61af66fc99e Initial load
duke
parents:
diff changeset
372 bool BitMap::set_intersection_with_result(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
373 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
374 bool changed = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
375 idx_t* dest_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
376 idx_t* other_map = other.map();
a61af66fc99e Initial load
duke
parents:
diff changeset
377 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
378 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
379 idx_t orig = dest_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
380 idx_t temp = orig & other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
381 changed = changed || (temp != orig);
a61af66fc99e Initial load
duke
parents:
diff changeset
382 dest_map[index] = temp;
a61af66fc99e Initial load
duke
parents:
diff changeset
383 }
a61af66fc99e Initial load
duke
parents:
diff changeset
384 return changed;
a61af66fc99e Initial load
duke
parents:
diff changeset
385 }
a61af66fc99e Initial load
duke
parents:
diff changeset
386
a61af66fc99e Initial load
duke
parents:
diff changeset
387
a61af66fc99e Initial load
duke
parents:
diff changeset
388 void BitMap::set_from(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
389 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
390 idx_t* dest_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
391 idx_t* other_map = other.map();
a61af66fc99e Initial load
duke
parents:
diff changeset
392 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
393 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
394 dest_map[index] = other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
395 }
a61af66fc99e Initial load
duke
parents:
diff changeset
396 }
a61af66fc99e Initial load
duke
parents:
diff changeset
397
a61af66fc99e Initial load
duke
parents:
diff changeset
398
a61af66fc99e Initial load
duke
parents:
diff changeset
399 bool BitMap::is_same(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
400 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
401 idx_t* dest_map = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
402 idx_t* other_map = other.map();
a61af66fc99e Initial load
duke
parents:
diff changeset
403 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
404 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
405 if (dest_map[index] != other_map[index]) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
406 }
a61af66fc99e Initial load
duke
parents:
diff changeset
407 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
408 }
a61af66fc99e Initial load
duke
parents:
diff changeset
409
a61af66fc99e Initial load
duke
parents:
diff changeset
410 bool BitMap::is_full() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
411 uintptr_t* word = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
412 idx_t rest = size();
a61af66fc99e Initial load
duke
parents:
diff changeset
413 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
a61af66fc99e Initial load
duke
parents:
diff changeset
414 if (*word != (uintptr_t) AllBits) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
415 word++;
a61af66fc99e Initial load
duke
parents:
diff changeset
416 }
a61af66fc99e Initial load
duke
parents:
diff changeset
417 return rest == 0 || (*word | ~right_n_bits((int)rest)) == (uintptr_t) AllBits;
a61af66fc99e Initial load
duke
parents:
diff changeset
418 }
a61af66fc99e Initial load
duke
parents:
diff changeset
419
a61af66fc99e Initial load
duke
parents:
diff changeset
420
a61af66fc99e Initial load
duke
parents:
diff changeset
421 bool BitMap::is_empty() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
422 uintptr_t* word = map();
a61af66fc99e Initial load
duke
parents:
diff changeset
423 idx_t rest = size();
a61af66fc99e Initial load
duke
parents:
diff changeset
424 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
a61af66fc99e Initial load
duke
parents:
diff changeset
425 if (*word != (uintptr_t) NoBits) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
426 word++;
a61af66fc99e Initial load
duke
parents:
diff changeset
427 }
a61af66fc99e Initial load
duke
parents:
diff changeset
428 return rest == 0 || (*word & right_n_bits((int)rest)) == (uintptr_t) NoBits;
a61af66fc99e Initial load
duke
parents:
diff changeset
429 }
a61af66fc99e Initial load
duke
parents:
diff changeset
430
a61af66fc99e Initial load
duke
parents:
diff changeset
431 void BitMap::clear_large() {
a61af66fc99e Initial load
duke
parents:
diff changeset
432 clear_large_range_of_words(0, size_in_words());
a61af66fc99e Initial load
duke
parents:
diff changeset
433 }
a61af66fc99e Initial load
duke
parents:
diff changeset
434
a61af66fc99e Initial load
duke
parents:
diff changeset
435 // Note that if the closure itself modifies the bitmap
a61af66fc99e Initial load
duke
parents:
diff changeset
436 // then modifications in and to the left of the _bit_ being
a61af66fc99e Initial load
duke
parents:
diff changeset
437 // currently sampled will not be seen. Note also that the
a61af66fc99e Initial load
duke
parents:
diff changeset
438 // interval [leftOffset, rightOffset) is right open.
a61af66fc99e Initial load
duke
parents:
diff changeset
439 void BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
a61af66fc99e Initial load
duke
parents:
diff changeset
440 verify_range(leftOffset, rightOffset);
a61af66fc99e Initial load
duke
parents:
diff changeset
441
a61af66fc99e Initial load
duke
parents:
diff changeset
442 idx_t startIndex = word_index(leftOffset);
a61af66fc99e Initial load
duke
parents:
diff changeset
443 idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words());
a61af66fc99e Initial load
duke
parents:
diff changeset
444 for (idx_t index = startIndex, offset = leftOffset;
a61af66fc99e Initial load
duke
parents:
diff changeset
445 offset < rightOffset && index < endIndex;
a61af66fc99e Initial load
duke
parents:
diff changeset
446 offset = (++index) << LogBitsPerWord) {
a61af66fc99e Initial load
duke
parents:
diff changeset
447 idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
a61af66fc99e Initial load
duke
parents:
diff changeset
448 for (; offset < rightOffset && rest != (uintptr_t)NoBits; offset++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
449 if (rest & 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
450 blk->do_bit(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
451 // resample at each closure application
a61af66fc99e Initial load
duke
parents:
diff changeset
452 // (see, for instance, CMS bug 4525989)
a61af66fc99e Initial load
duke
parents:
diff changeset
453 rest = map(index) >> (offset & (BitsPerWord -1));
a61af66fc99e Initial load
duke
parents:
diff changeset
454 // XXX debugging: remove
a61af66fc99e Initial load
duke
parents:
diff changeset
455 // The following assertion assumes that closure application
a61af66fc99e Initial load
duke
parents:
diff changeset
456 // doesn't clear bits (may not be true in general, e.g. G1).
a61af66fc99e Initial load
duke
parents:
diff changeset
457 assert(rest & 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
458 "incorrect shift or closure application can clear bits?");
a61af66fc99e Initial load
duke
parents:
diff changeset
459 }
a61af66fc99e Initial load
duke
parents:
diff changeset
460 rest = rest >> 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
461 }
a61af66fc99e Initial load
duke
parents:
diff changeset
462 }
a61af66fc99e Initial load
duke
parents:
diff changeset
463 }
a61af66fc99e Initial load
duke
parents:
diff changeset
464
a61af66fc99e Initial load
duke
parents:
diff changeset
465 BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset,
a61af66fc99e Initial load
duke
parents:
diff changeset
466 idx_t r_offset) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
467 assert(l_offset <= size(), "BitMap index out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
468 assert(r_offset <= size(), "BitMap index out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
469 assert(l_offset <= r_offset, "l_offset > r_offset ?");
a61af66fc99e Initial load
duke
parents:
diff changeset
470
a61af66fc99e Initial load
duke
parents:
diff changeset
471 if (l_offset == r_offset) {
a61af66fc99e Initial load
duke
parents:
diff changeset
472 return l_offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
473 }
a61af66fc99e Initial load
duke
parents:
diff changeset
474 idx_t index = word_index(l_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
475 idx_t r_index = word_index(r_offset-1) + 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
476 idx_t res_offset = l_offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
477
a61af66fc99e Initial load
duke
parents:
diff changeset
478 // check bits including and to the _left_ of offset's position
a61af66fc99e Initial load
duke
parents:
diff changeset
479 idx_t pos = bit_in_word(res_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
480 idx_t res = map(index) >> pos;
a61af66fc99e Initial load
duke
parents:
diff changeset
481 if (res != (uintptr_t)NoBits) {
a61af66fc99e Initial load
duke
parents:
diff changeset
482 // find the position of the 1-bit
a61af66fc99e Initial load
duke
parents:
diff changeset
483 for (; !(res & 1); res_offset++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
484 res = res >> 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
485 }
a61af66fc99e Initial load
duke
parents:
diff changeset
486 assert(res_offset >= l_offset, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
487 return MIN2(res_offset, r_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
488 }
a61af66fc99e Initial load
duke
parents:
diff changeset
489 // skip over all word length 0-bit runs
a61af66fc99e Initial load
duke
parents:
diff changeset
490 for (index++; index < r_index; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
491 res = map(index);
a61af66fc99e Initial load
duke
parents:
diff changeset
492 if (res != (uintptr_t)NoBits) {
a61af66fc99e Initial load
duke
parents:
diff changeset
493 // found a 1, return the offset
a61af66fc99e Initial load
duke
parents:
diff changeset
494 for (res_offset = index << LogBitsPerWord; !(res & 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
495 res_offset++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
496 res = res >> 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
497 }
a61af66fc99e Initial load
duke
parents:
diff changeset
498 assert(res & 1, "tautology; see loop condition");
a61af66fc99e Initial load
duke
parents:
diff changeset
499 assert(res_offset >= l_offset, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
500 return MIN2(res_offset, r_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
501 }
a61af66fc99e Initial load
duke
parents:
diff changeset
502 }
a61af66fc99e Initial load
duke
parents:
diff changeset
503 return r_offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
504 }
a61af66fc99e Initial load
duke
parents:
diff changeset
505
a61af66fc99e Initial load
duke
parents:
diff changeset
506 BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset,
a61af66fc99e Initial load
duke
parents:
diff changeset
507 idx_t r_offset) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
508 assert(l_offset <= size(), "BitMap index out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
509 assert(r_offset <= size(), "BitMap index out of bounds");
a61af66fc99e Initial load
duke
parents:
diff changeset
510 assert(l_offset <= r_offset, "l_offset > r_offset ?");
a61af66fc99e Initial load
duke
parents:
diff changeset
511
a61af66fc99e Initial load
duke
parents:
diff changeset
512 if (l_offset == r_offset) {
a61af66fc99e Initial load
duke
parents:
diff changeset
513 return l_offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
514 }
a61af66fc99e Initial load
duke
parents:
diff changeset
515 idx_t index = word_index(l_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
516 idx_t r_index = word_index(r_offset-1) + 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
517 idx_t res_offset = l_offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
518
a61af66fc99e Initial load
duke
parents:
diff changeset
519 // check bits including and to the _left_ of offset's position
a61af66fc99e Initial load
duke
parents:
diff changeset
520 idx_t pos = res_offset & (BitsPerWord - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
521 idx_t res = (map(index) >> pos) | left_n_bits((int)pos);
a61af66fc99e Initial load
duke
parents:
diff changeset
522
a61af66fc99e Initial load
duke
parents:
diff changeset
523 if (res != (uintptr_t)AllBits) {
a61af66fc99e Initial load
duke
parents:
diff changeset
524 // find the position of the 0-bit
a61af66fc99e Initial load
duke
parents:
diff changeset
525 for (; res & 1; res_offset++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
526 res = res >> 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
527 }
a61af66fc99e Initial load
duke
parents:
diff changeset
528 assert(res_offset >= l_offset, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
529 return MIN2(res_offset, r_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
530 }
a61af66fc99e Initial load
duke
parents:
diff changeset
531 // skip over all word length 1-bit runs
a61af66fc99e Initial load
duke
parents:
diff changeset
532 for (index++; index < r_index; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
533 res = map(index);
a61af66fc99e Initial load
duke
parents:
diff changeset
534 if (res != (uintptr_t)AllBits) {
a61af66fc99e Initial load
duke
parents:
diff changeset
535 // found a 0, return the offset
a61af66fc99e Initial load
duke
parents:
diff changeset
536 for (res_offset = index << LogBitsPerWord; res & 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
537 res_offset++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
538 res = res >> 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
539 }
a61af66fc99e Initial load
duke
parents:
diff changeset
540 assert(!(res & 1), "tautology; see loop condition");
a61af66fc99e Initial load
duke
parents:
diff changeset
541 assert(res_offset >= l_offset, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
542 return MIN2(res_offset, r_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
543 }
a61af66fc99e Initial load
duke
parents:
diff changeset
544 }
a61af66fc99e Initial load
duke
parents:
diff changeset
545 return r_offset;
a61af66fc99e Initial load
duke
parents:
diff changeset
546 }
a61af66fc99e Initial load
duke
parents:
diff changeset
547
a61af66fc99e Initial load
duke
parents:
diff changeset
548 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
549
a61af66fc99e Initial load
duke
parents:
diff changeset
550 void BitMap::print_on(outputStream* st) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
551 tty->print("Bitmap(%d):", size());
a61af66fc99e Initial load
duke
parents:
diff changeset
552 for (idx_t index = 0; index < size(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
553 tty->print("%c", at(index) ? '1' : '0');
a61af66fc99e Initial load
duke
parents:
diff changeset
554 }
a61af66fc99e Initial load
duke
parents:
diff changeset
555 tty->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
556 }
a61af66fc99e Initial load
duke
parents:
diff changeset
557
a61af66fc99e Initial load
duke
parents:
diff changeset
558 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
559
a61af66fc99e Initial load
duke
parents:
diff changeset
560
a61af66fc99e Initial load
duke
parents:
diff changeset
561 BitMap2D::BitMap2D(uintptr_t* map, idx_t size_in_slots, idx_t bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
562 : _bits_per_slot(bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
563 , _map(map, size_in_slots * bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
564 {
a61af66fc99e Initial load
duke
parents:
diff changeset
565 }
a61af66fc99e Initial load
duke
parents:
diff changeset
566
a61af66fc99e Initial load
duke
parents:
diff changeset
567
a61af66fc99e Initial load
duke
parents:
diff changeset
568 BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
569 : _bits_per_slot(bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
570 , _map(size_in_slots * bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
571 {
a61af66fc99e Initial load
duke
parents:
diff changeset
572 }