annotate src/share/vm/utilities/bitMap.cpp @ 17524:89152779163c

Merge with jdk8-b132
author Gilles Duboscq <duboscq@ssw.jku.at>
date Wed, 15 Oct 2014 11:59:32 +0200
parents 4ca6dc0799b6
children 52b4284cb496
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
17524
89152779163c Merge with jdk8-b132
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 14909
diff changeset
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
0
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 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 844
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
25 #include "precompiled.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
26 #include "memory/allocation.inline.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
27 #include "utilities/bitMap.inline.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
28 #include "utilities/copy.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
29 #ifdef TARGET_OS_FAMILY_linux
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
30 # include "os_linux.inline.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
31 #endif
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
32 #ifdef TARGET_OS_FAMILY_solaris
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
33 # include "os_solaris.inline.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
34 #endif
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
35 #ifdef TARGET_OS_FAMILY_windows
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
36 # include "os_windows.inline.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
37 #endif
3960
f08d439fab8c 7089790: integrate bsd-port changes
never
parents: 1972
diff changeset
38 #ifdef TARGET_OS_FAMILY_bsd
f08d439fab8c 7089790: integrate bsd-port changes
never
parents: 1972
diff changeset
39 # include "os_bsd.inline.hpp"
f08d439fab8c 7089790: integrate bsd-port changes
never
parents: 1972
diff changeset
40 #endif
0
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
43 BitMap::BitMap(bm_word_t* map, idx_t size_in_bits) :
10992
b9d151496930 8016556: G1: Use ArrayAllocator for BitMaps
brutisso
parents: 9076
diff changeset
44 _map(map), _size(size_in_bits), _map_allocator(false)
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
45 {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
46 assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
47 assert(size_in_bits >= 0, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
48 }
a61af66fc99e Initial load
duke
parents:
diff changeset
49
a61af66fc99e Initial load
duke
parents:
diff changeset
50
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
51 BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) :
10992
b9d151496930 8016556: G1: Use ArrayAllocator for BitMaps
brutisso
parents: 9076
diff changeset
52 _map(NULL), _size(0), _map_allocator(false)
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
53 {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
54 assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
55 resize(size_in_bits, in_resource_area);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
56 }
a61af66fc99e Initial load
duke
parents:
diff changeset
57
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
58 void BitMap::resize(idx_t size_in_bits, bool in_resource_area) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
59 assert(size_in_bits >= 0, "just checking");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
60 idx_t old_size_in_words = size_in_words();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
61 bm_word_t* old_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
62
0
a61af66fc99e Initial load
duke
parents:
diff changeset
63 _size = size_in_bits;
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
64 idx_t new_size_in_words = size_in_words();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
65 if (in_resource_area) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
66 _map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
67 } else {
10992
b9d151496930 8016556: G1: Use ArrayAllocator for BitMaps
brutisso
parents: 9076
diff changeset
68 if (old_map != NULL) {
b9d151496930 8016556: G1: Use ArrayAllocator for BitMaps
brutisso
parents: 9076
diff changeset
69 _map_allocator.free();
b9d151496930 8016556: G1: Use ArrayAllocator for BitMaps
brutisso
parents: 9076
diff changeset
70 }
b9d151496930 8016556: G1: Use ArrayAllocator for BitMaps
brutisso
parents: 9076
diff changeset
71 _map = _map_allocator.allocate(new_size_in_words);
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
72 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
73 Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
74 MIN2(old_size_in_words, new_size_in_words));
0
a61af66fc99e Initial load
duke
parents:
diff changeset
75 if (new_size_in_words > old_size_in_words) {
a61af66fc99e Initial load
duke
parents:
diff changeset
76 clear_range_of_words(old_size_in_words, size_in_words());
a61af66fc99e Initial load
duke
parents:
diff changeset
77 }
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 void BitMap::set_range_within_word(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
81 // With a valid range (beg <= end), this test ensures that end != 0, as
a61af66fc99e Initial load
duke
parents:
diff changeset
82 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
a61af66fc99e Initial load
duke
parents:
diff changeset
83 if (beg != end) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
84 bm_word_t mask = inverted_bit_mask_for_range(beg, end);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
85 *word_addr(beg) |= ~mask;
a61af66fc99e Initial load
duke
parents:
diff changeset
86 }
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
90 // With a valid range (beg <= end), this test ensures that end != 0, as
a61af66fc99e Initial load
duke
parents:
diff changeset
91 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
a61af66fc99e Initial load
duke
parents:
diff changeset
92 if (beg != end) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
93 bm_word_t mask = inverted_bit_mask_for_range(beg, end);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
94 *word_addr(beg) &= mask;
a61af66fc99e Initial load
duke
parents:
diff changeset
95 }
a61af66fc99e Initial load
duke
parents:
diff changeset
96 }
a61af66fc99e Initial load
duke
parents:
diff changeset
97
a61af66fc99e Initial load
duke
parents:
diff changeset
98 void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
99 assert(value == 0 || value == 1, "0 for clear, 1 for set");
a61af66fc99e Initial load
duke
parents:
diff changeset
100 // With a valid range (beg <= end), this test ensures that end != 0, as
a61af66fc99e Initial load
duke
parents:
diff changeset
101 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
a61af66fc99e Initial load
duke
parents:
diff changeset
102 if (beg != end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 intptr_t* pw = (intptr_t*)word_addr(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
104 intptr_t w = *pw;
a61af66fc99e Initial load
duke
parents:
diff changeset
105 intptr_t mr = (intptr_t)inverted_bit_mask_for_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
106 intptr_t nw = value ? (w | ~mr) : (w & mr);
a61af66fc99e Initial load
duke
parents:
diff changeset
107 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
a61af66fc99e Initial load
duke
parents:
diff changeset
109 if (res == w) break;
14909
4ca6dc0799b6 Backout jdk9 merge
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 14460
diff changeset
110 w = *pw;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
111 nw = value ? (w | ~mr) : (w & mr);
a61af66fc99e Initial load
duke
parents:
diff changeset
112 }
a61af66fc99e Initial load
duke
parents:
diff changeset
113 }
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 void BitMap::set_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
118
a61af66fc99e Initial load
duke
parents:
diff changeset
119 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
120 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
121
a61af66fc99e Initial load
duke
parents:
diff changeset
122 if (beg_full_word < end_full_word) {
a61af66fc99e Initial load
duke
parents:
diff changeset
123 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
124 set_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
125 set_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
126 set_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
127 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
128 // The range spans at most 2 partial words.
a61af66fc99e Initial load
duke
parents:
diff changeset
129 idx_t boundary = MIN2(bit_index(beg_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
130 set_range_within_word(beg, boundary);
a61af66fc99e Initial load
duke
parents:
diff changeset
131 set_range_within_word(boundary, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
132 }
a61af66fc99e Initial load
duke
parents:
diff changeset
133 }
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135 void BitMap::clear_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
136 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
137
a61af66fc99e Initial load
duke
parents:
diff changeset
138 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
139 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 if (beg_full_word < end_full_word) {
a61af66fc99e Initial load
duke
parents:
diff changeset
142 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
143 clear_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
144 clear_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
145 clear_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 // The range spans at most 2 partial words.
a61af66fc99e Initial load
duke
parents:
diff changeset
148 idx_t boundary = MIN2(bit_index(beg_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
149 clear_range_within_word(beg, boundary);
a61af66fc99e Initial load
duke
parents:
diff changeset
150 clear_range_within_word(boundary, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
152 }
a61af66fc99e Initial load
duke
parents:
diff changeset
153
a61af66fc99e Initial load
duke
parents:
diff changeset
154 void BitMap::set_large_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
155 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
156
a61af66fc99e Initial load
duke
parents:
diff changeset
157 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
158 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 assert(end_full_word - beg_full_word >= 32,
a61af66fc99e Initial load
duke
parents:
diff changeset
161 "the range must include at least 32 bytes");
a61af66fc99e Initial load
duke
parents:
diff changeset
162
a61af66fc99e Initial load
duke
parents:
diff changeset
163 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
164 set_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
165 set_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
166 set_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
167 }
a61af66fc99e Initial load
duke
parents:
diff changeset
168
a61af66fc99e Initial load
duke
parents:
diff changeset
169 void BitMap::clear_large_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
170 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
171
a61af66fc99e Initial load
duke
parents:
diff changeset
172 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
173 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
174
a61af66fc99e Initial load
duke
parents:
diff changeset
175 assert(end_full_word - beg_full_word >= 32,
a61af66fc99e Initial load
duke
parents:
diff changeset
176 "the range must include at least 32 bytes");
a61af66fc99e Initial load
duke
parents:
diff changeset
177
a61af66fc99e Initial load
duke
parents:
diff changeset
178 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
179 clear_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
180 clear_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
181 clear_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
182 }
a61af66fc99e Initial load
duke
parents:
diff changeset
183
a61af66fc99e Initial load
duke
parents:
diff changeset
184 void BitMap::at_put(idx_t offset, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
185 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
186 set_bit(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
187 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
188 clear_bit(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
189 }
a61af66fc99e Initial load
duke
parents:
diff changeset
190 }
a61af66fc99e Initial load
duke
parents:
diff changeset
191
a61af66fc99e Initial load
duke
parents:
diff changeset
192 // Return true to indicate that this thread changed
a61af66fc99e Initial load
duke
parents:
diff changeset
193 // the bit, false to indicate that someone else did.
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // In either case, the requested bit is in the
a61af66fc99e Initial load
duke
parents:
diff changeset
195 // requested state some time during the period that
a61af66fc99e Initial load
duke
parents:
diff changeset
196 // this thread is executing this call. More importantly,
a61af66fc99e Initial load
duke
parents:
diff changeset
197 // if no other thread is executing an action to
a61af66fc99e Initial load
duke
parents:
diff changeset
198 // change the requested bit to a state other than
a61af66fc99e Initial load
duke
parents:
diff changeset
199 // the one that this thread is trying to set it to,
a61af66fc99e Initial load
duke
parents:
diff changeset
200 // then the the bit is in the expected state
a61af66fc99e Initial load
duke
parents:
diff changeset
201 // at exit from this method. However, rather than
a61af66fc99e Initial load
duke
parents:
diff changeset
202 // make such a strong assertion here, based on
a61af66fc99e Initial load
duke
parents:
diff changeset
203 // assuming such constrained use (which though true
a61af66fc99e Initial load
duke
parents:
diff changeset
204 // today, could change in the future to service some
a61af66fc99e Initial load
duke
parents:
diff changeset
205 // funky parallel algorithm), we encourage callers
a61af66fc99e Initial load
duke
parents:
diff changeset
206 // to do such verification, as and when appropriate.
a61af66fc99e Initial load
duke
parents:
diff changeset
207 bool BitMap::par_at_put(idx_t bit, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
208 return value ? par_set_bit(bit) : par_clear_bit(bit);
a61af66fc99e Initial load
duke
parents:
diff changeset
209 }
a61af66fc99e Initial load
duke
parents:
diff changeset
210
a61af66fc99e Initial load
duke
parents:
diff changeset
211 void BitMap::at_put_grow(idx_t offset, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
212 if (offset >= size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
213 resize(2 * MAX2(size(), offset));
a61af66fc99e Initial load
duke
parents:
diff changeset
214 }
a61af66fc99e Initial load
duke
parents:
diff changeset
215 at_put(offset, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
216 }
a61af66fc99e Initial load
duke
parents:
diff changeset
217
a61af66fc99e Initial load
duke
parents:
diff changeset
218 void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
219 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
220 set_range(start_offset, end_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
221 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
222 clear_range(start_offset, end_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
223 }
a61af66fc99e Initial load
duke
parents:
diff changeset
224 }
a61af66fc99e Initial load
duke
parents:
diff changeset
225
a61af66fc99e Initial load
duke
parents:
diff changeset
226 void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
227 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
228
a61af66fc99e Initial load
duke
parents:
diff changeset
229 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
230 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
231
a61af66fc99e Initial load
duke
parents:
diff changeset
232 if (beg_full_word < end_full_word) {
a61af66fc99e Initial load
duke
parents:
diff changeset
233 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
234 par_put_range_within_word(beg, bit_index(beg_full_word), value);
a61af66fc99e Initial load
duke
parents:
diff changeset
235 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
236 set_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
237 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
238 clear_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
239 }
a61af66fc99e Initial load
duke
parents:
diff changeset
240 par_put_range_within_word(bit_index(end_full_word), end, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
241 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
242 // The range spans at most 2 partial words.
a61af66fc99e Initial load
duke
parents:
diff changeset
243 idx_t boundary = MIN2(bit_index(beg_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
244 par_put_range_within_word(beg, boundary, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
245 par_put_range_within_word(boundary, end, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
246 }
a61af66fc99e Initial load
duke
parents:
diff changeset
247
a61af66fc99e Initial load
duke
parents:
diff changeset
248 }
a61af66fc99e Initial load
duke
parents:
diff changeset
249
a61af66fc99e Initial load
duke
parents:
diff changeset
250 void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
251 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
252 set_large_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
253 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
254 clear_large_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
255 }
a61af66fc99e Initial load
duke
parents:
diff changeset
256 }
a61af66fc99e Initial load
duke
parents:
diff changeset
257
a61af66fc99e Initial load
duke
parents:
diff changeset
258 void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
259 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
260
a61af66fc99e Initial load
duke
parents:
diff changeset
261 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
262 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
263
a61af66fc99e Initial load
duke
parents:
diff changeset
264 assert(end_full_word - beg_full_word >= 32,
a61af66fc99e Initial load
duke
parents:
diff changeset
265 "the range must include at least 32 bytes");
a61af66fc99e Initial load
duke
parents:
diff changeset
266
a61af66fc99e Initial load
duke
parents:
diff changeset
267 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
268 par_put_range_within_word(beg, bit_index(beg_full_word), value);
a61af66fc99e Initial load
duke
parents:
diff changeset
269 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
270 set_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
271 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
272 clear_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
273 }
a61af66fc99e Initial load
duke
parents:
diff changeset
274 par_put_range_within_word(bit_index(end_full_word), end, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
275 }
a61af66fc99e Initial load
duke
parents:
diff changeset
276
a61af66fc99e Initial load
duke
parents:
diff changeset
277 bool BitMap::contains(const BitMap other) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
279 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
280 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
281 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
282 for (idx_t index = 0; index < size_in_words(); index++) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
283 bm_word_t word_union = dest_map[index] | other_map[index];
0
a61af66fc99e Initial load
duke
parents:
diff changeset
284 // If this has more bits set than dest_map[index], then other is not a
a61af66fc99e Initial load
duke
parents:
diff changeset
285 // subset.
a61af66fc99e Initial load
duke
parents:
diff changeset
286 if (word_union != dest_map[index]) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
287 }
a61af66fc99e Initial load
duke
parents:
diff changeset
288 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
289 }
a61af66fc99e Initial load
duke
parents:
diff changeset
290
a61af66fc99e Initial load
duke
parents:
diff changeset
291 bool BitMap::intersects(const BitMap other) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
292 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
293 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
294 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
295 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
296 for (idx_t index = 0; index < size_in_words(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
297 if ((dest_map[index] & other_map[index]) != 0) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
298 }
a61af66fc99e Initial load
duke
parents:
diff changeset
299 // Otherwise, no intersection.
a61af66fc99e Initial load
duke
parents:
diff changeset
300 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
301 }
a61af66fc99e Initial load
duke
parents:
diff changeset
302
a61af66fc99e Initial load
duke
parents:
diff changeset
303 void BitMap::set_union(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
304 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
305 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
306 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
307 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
308 for (idx_t index = 0; index < size_in_words(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
309 dest_map[index] = dest_map[index] | other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
310 }
a61af66fc99e Initial load
duke
parents:
diff changeset
311 }
a61af66fc99e Initial load
duke
parents:
diff changeset
312
a61af66fc99e Initial load
duke
parents:
diff changeset
313
a61af66fc99e Initial load
duke
parents:
diff changeset
314 void BitMap::set_difference(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
315 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
316 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
317 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
318 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
319 for (idx_t index = 0; index < size_in_words(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
320 dest_map[index] = dest_map[index] & ~(other_map[index]);
a61af66fc99e Initial load
duke
parents:
diff changeset
321 }
a61af66fc99e Initial load
duke
parents:
diff changeset
322 }
a61af66fc99e Initial load
duke
parents:
diff changeset
323
a61af66fc99e Initial load
duke
parents:
diff changeset
324
a61af66fc99e Initial load
duke
parents:
diff changeset
325 void BitMap::set_intersection(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
326 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
327 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
328 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
329 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
330 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
331 dest_map[index] = dest_map[index] & other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
332 }
a61af66fc99e Initial load
duke
parents:
diff changeset
333 }
a61af66fc99e Initial load
duke
parents:
diff changeset
334
a61af66fc99e Initial load
duke
parents:
diff changeset
335
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
336 void BitMap::set_intersection_at_offset(BitMap other, idx_t offset) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
337 assert(other.size() >= offset, "offset not in range");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
338 assert(other.size() - offset >= size(), "other not large enough");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
339 // XXX Ideally, we would remove this restriction.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
340 guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
341 "Only handle aligned cases so far.");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
342 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
343 bm_word_t* other_map = other.map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
344 idx_t offset_word_ind = word_index(offset);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
345 idx_t size = size_in_words();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
346 for (idx_t index = 0; index < size; index++) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
347 dest_map[index] = dest_map[index] & other_map[offset_word_ind + index];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
348 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
349 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
350
0
a61af66fc99e Initial load
duke
parents:
diff changeset
351 bool BitMap::set_union_with_result(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
352 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
353 bool changed = false;
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
354 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
355 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
356 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
357 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
358 idx_t temp = map(index) | other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
359 changed = changed || (temp != map(index));
a61af66fc99e Initial load
duke
parents:
diff changeset
360 map()[index] = temp;
a61af66fc99e Initial load
duke
parents:
diff changeset
361 }
a61af66fc99e Initial load
duke
parents:
diff changeset
362 return changed;
a61af66fc99e Initial load
duke
parents:
diff changeset
363 }
a61af66fc99e Initial load
duke
parents:
diff changeset
364
a61af66fc99e Initial load
duke
parents:
diff changeset
365
a61af66fc99e Initial load
duke
parents:
diff changeset
366 bool BitMap::set_difference_with_result(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
367 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
368 bool changed = false;
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
369 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
370 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
371 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
372 for (idx_t index = 0; index < size; index++) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
373 bm_word_t temp = dest_map[index] & ~(other_map[index]);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
374 changed = changed || (temp != dest_map[index]);
a61af66fc99e Initial load
duke
parents:
diff changeset
375 dest_map[index] = temp;
a61af66fc99e Initial load
duke
parents:
diff changeset
376 }
a61af66fc99e Initial load
duke
parents:
diff changeset
377 return changed;
a61af66fc99e Initial load
duke
parents:
diff changeset
378 }
a61af66fc99e Initial load
duke
parents:
diff changeset
379
a61af66fc99e Initial load
duke
parents:
diff changeset
380
a61af66fc99e Initial load
duke
parents:
diff changeset
381 bool BitMap::set_intersection_with_result(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
382 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
383 bool changed = false;
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
384 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
385 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
386 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
387 for (idx_t index = 0; index < size; index++) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
388 bm_word_t orig = dest_map[index];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
389 bm_word_t temp = orig & other_map[index];
0
a61af66fc99e Initial load
duke
parents:
diff changeset
390 changed = changed || (temp != orig);
a61af66fc99e Initial load
duke
parents:
diff changeset
391 dest_map[index] = temp;
a61af66fc99e Initial load
duke
parents:
diff changeset
392 }
a61af66fc99e Initial load
duke
parents:
diff changeset
393 return changed;
a61af66fc99e Initial load
duke
parents:
diff changeset
394 }
a61af66fc99e Initial load
duke
parents:
diff changeset
395
a61af66fc99e Initial load
duke
parents:
diff changeset
396
a61af66fc99e Initial load
duke
parents:
diff changeset
397 void BitMap::set_from(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
398 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
399 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
400 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
401 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
402 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
403 dest_map[index] = other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
404 }
a61af66fc99e Initial load
duke
parents:
diff changeset
405 }
a61af66fc99e Initial load
duke
parents:
diff changeset
406
a61af66fc99e Initial load
duke
parents:
diff changeset
407
a61af66fc99e Initial load
duke
parents:
diff changeset
408 bool BitMap::is_same(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
409 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
410 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
411 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
412 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
413 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
414 if (dest_map[index] != other_map[index]) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
415 }
a61af66fc99e Initial load
duke
parents:
diff changeset
416 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
417 }
a61af66fc99e Initial load
duke
parents:
diff changeset
418
a61af66fc99e Initial load
duke
parents:
diff changeset
419 bool BitMap::is_full() const {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
420 bm_word_t* word = map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
421 idx_t rest = size();
a61af66fc99e Initial load
duke
parents:
diff changeset
422 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
423 if (*word != (bm_word_t) AllBits) return false;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
424 word++;
a61af66fc99e Initial load
duke
parents:
diff changeset
425 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
426 return rest == 0 || (*word | ~right_n_bits((int)rest)) == (bm_word_t) AllBits;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
427 }
a61af66fc99e Initial load
duke
parents:
diff changeset
428
a61af66fc99e Initial load
duke
parents:
diff changeset
429
a61af66fc99e Initial load
duke
parents:
diff changeset
430 bool BitMap::is_empty() const {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
431 bm_word_t* word = map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
432 idx_t rest = size();
a61af66fc99e Initial load
duke
parents:
diff changeset
433 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
434 if (*word != (bm_word_t) NoBits) return false;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
435 word++;
a61af66fc99e Initial load
duke
parents:
diff changeset
436 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
437 return rest == 0 || (*word & right_n_bits((int)rest)) == (bm_word_t) NoBits;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
438 }
a61af66fc99e Initial load
duke
parents:
diff changeset
439
a61af66fc99e Initial load
duke
parents:
diff changeset
440 void BitMap::clear_large() {
a61af66fc99e Initial load
duke
parents:
diff changeset
441 clear_large_range_of_words(0, size_in_words());
a61af66fc99e Initial load
duke
parents:
diff changeset
442 }
a61af66fc99e Initial load
duke
parents:
diff changeset
443
a61af66fc99e Initial load
duke
parents:
diff changeset
444 // Note that if the closure itself modifies the bitmap
a61af66fc99e Initial load
duke
parents:
diff changeset
445 // then modifications in and to the left of the _bit_ being
a61af66fc99e Initial load
duke
parents:
diff changeset
446 // currently sampled will not be seen. Note also that the
a61af66fc99e Initial load
duke
parents:
diff changeset
447 // interval [leftOffset, rightOffset) is right open.
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
448 bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
449 verify_range(leftOffset, rightOffset);
a61af66fc99e Initial load
duke
parents:
diff changeset
450
a61af66fc99e Initial load
duke
parents:
diff changeset
451 idx_t startIndex = word_index(leftOffset);
a61af66fc99e Initial load
duke
parents:
diff changeset
452 idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words());
a61af66fc99e Initial load
duke
parents:
diff changeset
453 for (idx_t index = startIndex, offset = leftOffset;
a61af66fc99e Initial load
duke
parents:
diff changeset
454 offset < rightOffset && index < endIndex;
a61af66fc99e Initial load
duke
parents:
diff changeset
455 offset = (++index) << LogBitsPerWord) {
a61af66fc99e Initial load
duke
parents:
diff changeset
456 idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
457 for (; offset < rightOffset && rest != (bm_word_t)NoBits; offset++) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
458 if (rest & 1) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
459 if (!blk->do_bit(offset)) return false;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
460 // resample at each closure application
a61af66fc99e Initial load
duke
parents:
diff changeset
461 // (see, for instance, CMS bug 4525989)
a61af66fc99e Initial load
duke
parents:
diff changeset
462 rest = map(index) >> (offset & (BitsPerWord -1));
a61af66fc99e Initial load
duke
parents:
diff changeset
463 }
a61af66fc99e Initial load
duke
parents:
diff changeset
464 rest = rest >> 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
465 }
a61af66fc99e Initial load
duke
parents:
diff changeset
466 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
467 return true;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
468 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
469
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
470 BitMap::idx_t* BitMap::_pop_count_table = NULL;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
471
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
472 void BitMap::init_pop_count_table() {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
473 if (_pop_count_table == NULL) {
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 5988
diff changeset
474 BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256, mtInternal);
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
475 for (uint i = 0; i < 256; i++) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
476 table[i] = num_set_bits(i);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
477 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
478
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
479 intptr_t res = Atomic::cmpxchg_ptr((intptr_t) table,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
480 (intptr_t*) &_pop_count_table,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
481 (intptr_t) NULL_WORD);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
482 if (res != NULL_WORD) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
483 guarantee( _pop_count_table == (void*) res, "invariant" );
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 5988
diff changeset
484 FREE_C_HEAP_ARRAY(bm_word_t, table, mtInternal);
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
485 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
486 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
487 }
a61af66fc99e Initial load
duke
parents:
diff changeset
488
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
489 BitMap::idx_t BitMap::num_set_bits(bm_word_t w) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
490 idx_t bits = 0;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
491
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
492 while (w != 0) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
493 while ((w & 1) == 0) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
494 w >>= 1;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
495 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
496 bits++;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
497 w >>= 1;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
498 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
499 return bits;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
500 }
a61af66fc99e Initial load
duke
parents:
diff changeset
501
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
502 BitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
503 assert(_pop_count_table != NULL, "precondition");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
504 return _pop_count_table[c];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
505 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
506
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
507 BitMap::idx_t BitMap::count_one_bits() const {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
508 init_pop_count_table(); // If necessary.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
509 idx_t sum = 0;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
510 typedef unsigned char uchar;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
511 for (idx_t i = 0; i < size_in_words(); i++) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
512 bm_word_t w = map()[i];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
513 for (size_t j = 0; j < sizeof(bm_word_t); j++) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
514 sum += num_set_bits_from_table(uchar(w & 255));
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
515 w >>= 8;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
516 }
a61af66fc99e Initial load
duke
parents:
diff changeset
517 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
518 return sum;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
519 }
a61af66fc99e Initial load
duke
parents:
diff changeset
520
9076
7b835924c31c 8011872: Include Bit Map addresses in the hs_err files
stefank
parents: 6197
diff changeset
521 void BitMap::print_on_error(outputStream* st, const char* prefix) const {
7b835924c31c 8011872: Include Bit Map addresses in the hs_err files
stefank
parents: 6197
diff changeset
522 st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")",
7b835924c31c 8011872: Include Bit Map addresses in the hs_err files
stefank
parents: 6197
diff changeset
523 prefix, map(), (char*)map() + (size() >> LogBitsPerByte));
7b835924c31c 8011872: Include Bit Map addresses in the hs_err files
stefank
parents: 6197
diff changeset
524 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
525
0
a61af66fc99e Initial load
duke
parents:
diff changeset
526 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
527
a61af66fc99e Initial load
duke
parents:
diff changeset
528 void BitMap::print_on(outputStream* st) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
529 tty->print("Bitmap(%d):", size());
a61af66fc99e Initial load
duke
parents:
diff changeset
530 for (idx_t index = 0; index < size(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
531 tty->print("%c", at(index) ? '1' : '0');
a61af66fc99e Initial load
duke
parents:
diff changeset
532 }
a61af66fc99e Initial load
duke
parents:
diff changeset
533 tty->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
534 }
a61af66fc99e Initial load
duke
parents:
diff changeset
535
a61af66fc99e Initial load
duke
parents:
diff changeset
536 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
537
a61af66fc99e Initial load
duke
parents:
diff changeset
538
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
539 BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot)
0
a61af66fc99e Initial load
duke
parents:
diff changeset
540 : _bits_per_slot(bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
541 , _map(map, size_in_slots * bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
542 {
a61af66fc99e Initial load
duke
parents:
diff changeset
543 }
a61af66fc99e Initial load
duke
parents:
diff changeset
544
a61af66fc99e Initial load
duke
parents:
diff changeset
545
a61af66fc99e Initial load
duke
parents:
diff changeset
546 BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
547 : _bits_per_slot(bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
548 , _map(size_in_slots * bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
549 {
a61af66fc99e Initial load
duke
parents:
diff changeset
550 }