annotate src/share/vm/utilities/bitMap.cpp @ 342:37f87013dfd8

6711316: Open source the Garbage-First garbage collector Summary: First mercurial integration of the code for the Garbage-First garbage collector. Reviewed-by: apetrusenko, iveresov, jmasa, sgoldman, tonyp, ysr
author ysr
date Thu, 05 Jun 2008 15:57:56 -0700
parents a61af66fc99e
children 6e2afda171db
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
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
29 BitMap::BitMap(bm_word_t* map, idx_t size_in_bits) :
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
30 _map(map), _size(size_in_bits)
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
31 {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
32 assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
33 assert(size_in_bits >= 0, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
34 }
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
37 BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) :
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
38 _map(NULL), _size(0)
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
39 {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
40 assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
41 resize(size_in_bits, in_resource_area);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
42 }
a61af66fc99e Initial load
duke
parents:
diff changeset
43
a61af66fc99e Initial load
duke
parents:
diff changeset
44
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
45 void BitMap::verify_index(idx_t index) const {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
46 assert(index < _size, "BitMap index out of bounds");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
47 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
48
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
49 void BitMap::verify_range(idx_t beg_index, idx_t end_index) const {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
50 #ifdef ASSERT
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
51 assert(beg_index <= end_index, "BitMap range error");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
52 // Note that [0,0) and [size,size) are both valid ranges.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
53 if (end_index != _size) verify_index(end_index);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
54 #endif
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
55 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
56
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
57 void BitMap::resize(idx_t size_in_bits, bool in_resource_area) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
58 assert(size_in_bits >= 0, "just checking");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
59 idx_t old_size_in_words = size_in_words();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
60 bm_word_t* old_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
61
0
a61af66fc99e Initial load
duke
parents:
diff changeset
62 _size = size_in_bits;
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
63 idx_t new_size_in_words = size_in_words();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
64 if (in_resource_area) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
65 _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
66 } else {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
67 if (old_map != NULL) FREE_C_HEAP_ARRAY(bm_word_t, _map);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
68 _map = NEW_C_HEAP_ARRAY(bm_word_t, new_size_in_words);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
69 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
70 Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
71 MIN2(old_size_in_words, new_size_in_words));
0
a61af66fc99e Initial load
duke
parents:
diff changeset
72 if (new_size_in_words > old_size_in_words) {
a61af66fc99e Initial load
duke
parents:
diff changeset
73 clear_range_of_words(old_size_in_words, size_in_words());
a61af66fc99e Initial load
duke
parents:
diff changeset
74 }
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 void BitMap::set_range_within_word(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
78 // With a valid range (beg <= end), this test ensures that end != 0, as
a61af66fc99e Initial load
duke
parents:
diff changeset
79 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
a61af66fc99e Initial load
duke
parents:
diff changeset
80 if (beg != end) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
81 bm_word_t mask = inverted_bit_mask_for_range(beg, end);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
82 *word_addr(beg) |= ~mask;
a61af66fc99e Initial load
duke
parents:
diff changeset
83 }
a61af66fc99e Initial load
duke
parents:
diff changeset
84 }
a61af66fc99e Initial load
duke
parents:
diff changeset
85
a61af66fc99e Initial load
duke
parents:
diff changeset
86 void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
87 // With a valid range (beg <= end), this test ensures that end != 0, as
a61af66fc99e Initial load
duke
parents:
diff changeset
88 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
a61af66fc99e Initial load
duke
parents:
diff changeset
89 if (beg != end) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
90 bm_word_t mask = inverted_bit_mask_for_range(beg, end);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
91 *word_addr(beg) &= mask;
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93 }
a61af66fc99e Initial load
duke
parents:
diff changeset
94
a61af66fc99e Initial load
duke
parents:
diff changeset
95 void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
96 assert(value == 0 || value == 1, "0 for clear, 1 for set");
a61af66fc99e Initial load
duke
parents:
diff changeset
97 // With a valid range (beg <= end), this test ensures that end != 0, as
a61af66fc99e Initial load
duke
parents:
diff changeset
98 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
a61af66fc99e Initial load
duke
parents:
diff changeset
99 if (beg != end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 intptr_t* pw = (intptr_t*)word_addr(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
101 intptr_t w = *pw;
a61af66fc99e Initial load
duke
parents:
diff changeset
102 intptr_t mr = (intptr_t)inverted_bit_mask_for_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
103 intptr_t nw = value ? (w | ~mr) : (w & mr);
a61af66fc99e Initial load
duke
parents:
diff changeset
104 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
a61af66fc99e Initial load
duke
parents:
diff changeset
106 if (res == w) break;
a61af66fc99e Initial load
duke
parents:
diff changeset
107 w = *pw;
a61af66fc99e Initial load
duke
parents:
diff changeset
108 nw = value ? (w | ~mr) : (w & mr);
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110 }
a61af66fc99e Initial load
duke
parents:
diff changeset
111 }
a61af66fc99e Initial load
duke
parents:
diff changeset
112
a61af66fc99e Initial load
duke
parents:
diff changeset
113 void BitMap::set_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
117 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
118
a61af66fc99e Initial load
duke
parents:
diff changeset
119 if (beg_full_word < end_full_word) {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
121 set_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
122 set_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
123 set_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
124 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
125 // The range spans at most 2 partial words.
a61af66fc99e Initial load
duke
parents:
diff changeset
126 idx_t boundary = MIN2(bit_index(beg_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
127 set_range_within_word(beg, boundary);
a61af66fc99e Initial load
duke
parents:
diff changeset
128 set_range_within_word(boundary, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
129 }
a61af66fc99e Initial load
duke
parents:
diff changeset
130 }
a61af66fc99e Initial load
duke
parents:
diff changeset
131
a61af66fc99e Initial load
duke
parents:
diff changeset
132 void BitMap::clear_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
133 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
136 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
137
a61af66fc99e Initial load
duke
parents:
diff changeset
138 if (beg_full_word < end_full_word) {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
140 clear_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
141 clear_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
142 clear_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
143 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 // The range spans at most 2 partial words.
a61af66fc99e Initial load
duke
parents:
diff changeset
145 idx_t boundary = MIN2(bit_index(beg_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 clear_range_within_word(beg, boundary);
a61af66fc99e Initial load
duke
parents:
diff changeset
147 clear_range_within_word(boundary, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149 }
a61af66fc99e Initial load
duke
parents:
diff changeset
150
a61af66fc99e Initial load
duke
parents:
diff changeset
151 void BitMap::set_large_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
152 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
153
a61af66fc99e Initial load
duke
parents:
diff changeset
154 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
155 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
156
a61af66fc99e Initial load
duke
parents:
diff changeset
157 assert(end_full_word - beg_full_word >= 32,
a61af66fc99e Initial load
duke
parents:
diff changeset
158 "the range must include at least 32 bytes");
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
161 set_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
162 set_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
163 set_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
164 }
a61af66fc99e Initial load
duke
parents:
diff changeset
165
a61af66fc99e Initial load
duke
parents:
diff changeset
166 void BitMap::clear_large_range(idx_t beg, idx_t end) {
a61af66fc99e Initial load
duke
parents:
diff changeset
167 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
168
a61af66fc99e Initial load
duke
parents:
diff changeset
169 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
170 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
171
a61af66fc99e Initial load
duke
parents:
diff changeset
172 assert(end_full_word - beg_full_word >= 32,
a61af66fc99e Initial load
duke
parents:
diff changeset
173 "the range must include at least 32 bytes");
a61af66fc99e Initial load
duke
parents:
diff changeset
174
a61af66fc99e Initial load
duke
parents:
diff changeset
175 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
176 clear_range_within_word(beg, bit_index(beg_full_word));
a61af66fc99e Initial load
duke
parents:
diff changeset
177 clear_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
178 clear_range_within_word(bit_index(end_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
179 }
a61af66fc99e Initial load
duke
parents:
diff changeset
180
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
181 void BitMap::mostly_disjoint_range_union(BitMap* from_bitmap,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
182 idx_t from_start_index,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
183 idx_t to_start_index,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
184 size_t word_num) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
185 // Ensure that the parameters are correct.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
186 // These shouldn't be that expensive to check, hence I left them as
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
187 // guarantees.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
188 guarantee(from_bitmap->bit_in_word(from_start_index) == 0,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
189 "it should be aligned on a word boundary");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
190 guarantee(bit_in_word(to_start_index) == 0,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
191 "it should be aligned on a word boundary");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
192 guarantee(word_num >= 2, "word_num should be at least 2");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
193
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
194 intptr_t* from = (intptr_t*) from_bitmap->word_addr(from_start_index);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
195 intptr_t* to = (intptr_t*) word_addr(to_start_index);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
196
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
197 if (*from != 0) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
198 // if it's 0, then there's no point in doing the CAS
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
199 while (true) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
200 intptr_t old_value = *to;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
201 intptr_t new_value = old_value | *from;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
202 intptr_t res = Atomic::cmpxchg_ptr(new_value, to, old_value);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
203 if (res == old_value) break;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
204 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
205 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
206 ++from;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
207 ++to;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
208
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
209 for (size_t i = 0; i < word_num - 2; ++i) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
210 if (*from != 0) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
211 // if it's 0, then there's no point in doing the CAS
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
212 assert(*to == 0, "nobody else should be writing here");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
213 intptr_t new_value = *from;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
214 *to = new_value;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
215 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
216
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
217 ++from;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
218 ++to;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
219 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
220
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
221 if (*from != 0) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
222 // if it's 0, then there's no point in doing the CAS
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
223 while (true) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
224 intptr_t old_value = *to;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
225 intptr_t new_value = old_value | *from;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
226 intptr_t res = Atomic::cmpxchg_ptr(new_value, to, old_value);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
227 if (res == old_value) break;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
228 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
229 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
230
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
231 // the -1 is because we didn't advance them after the final CAS
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
232 assert(from ==
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
233 (intptr_t*) from_bitmap->word_addr(from_start_index) + word_num - 1,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
234 "invariant");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
235 assert(to == (intptr_t*) word_addr(to_start_index) + word_num - 1,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
236 "invariant");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
237 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
238
0
a61af66fc99e Initial load
duke
parents:
diff changeset
239 void BitMap::at_put(idx_t offset, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
240 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
241 set_bit(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
242 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
243 clear_bit(offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
244 }
a61af66fc99e Initial load
duke
parents:
diff changeset
245 }
a61af66fc99e Initial load
duke
parents:
diff changeset
246
a61af66fc99e Initial load
duke
parents:
diff changeset
247 // Return true to indicate that this thread changed
a61af66fc99e Initial load
duke
parents:
diff changeset
248 // the bit, false to indicate that someone else did.
a61af66fc99e Initial load
duke
parents:
diff changeset
249 // In either case, the requested bit is in the
a61af66fc99e Initial load
duke
parents:
diff changeset
250 // requested state some time during the period that
a61af66fc99e Initial load
duke
parents:
diff changeset
251 // this thread is executing this call. More importantly,
a61af66fc99e Initial load
duke
parents:
diff changeset
252 // if no other thread is executing an action to
a61af66fc99e Initial load
duke
parents:
diff changeset
253 // change the requested bit to a state other than
a61af66fc99e Initial load
duke
parents:
diff changeset
254 // the one that this thread is trying to set it to,
a61af66fc99e Initial load
duke
parents:
diff changeset
255 // then the the bit is in the expected state
a61af66fc99e Initial load
duke
parents:
diff changeset
256 // at exit from this method. However, rather than
a61af66fc99e Initial load
duke
parents:
diff changeset
257 // make such a strong assertion here, based on
a61af66fc99e Initial load
duke
parents:
diff changeset
258 // assuming such constrained use (which though true
a61af66fc99e Initial load
duke
parents:
diff changeset
259 // today, could change in the future to service some
a61af66fc99e Initial load
duke
parents:
diff changeset
260 // funky parallel algorithm), we encourage callers
a61af66fc99e Initial load
duke
parents:
diff changeset
261 // to do such verification, as and when appropriate.
a61af66fc99e Initial load
duke
parents:
diff changeset
262 bool BitMap::par_at_put(idx_t bit, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
263 return value ? par_set_bit(bit) : par_clear_bit(bit);
a61af66fc99e Initial load
duke
parents:
diff changeset
264 }
a61af66fc99e Initial load
duke
parents:
diff changeset
265
a61af66fc99e Initial load
duke
parents:
diff changeset
266 void BitMap::at_put_grow(idx_t offset, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
267 if (offset >= size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
268 resize(2 * MAX2(size(), offset));
a61af66fc99e Initial load
duke
parents:
diff changeset
269 }
a61af66fc99e Initial load
duke
parents:
diff changeset
270 at_put(offset, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
271 }
a61af66fc99e Initial load
duke
parents:
diff changeset
272
a61af66fc99e Initial load
duke
parents:
diff changeset
273 void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
274 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
275 set_range(start_offset, end_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
276 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
277 clear_range(start_offset, end_offset);
a61af66fc99e Initial load
duke
parents:
diff changeset
278 }
a61af66fc99e Initial load
duke
parents:
diff changeset
279 }
a61af66fc99e Initial load
duke
parents:
diff changeset
280
a61af66fc99e Initial load
duke
parents:
diff changeset
281 void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
282 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
283
a61af66fc99e Initial load
duke
parents:
diff changeset
284 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
285 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
286
a61af66fc99e Initial load
duke
parents:
diff changeset
287 if (beg_full_word < end_full_word) {
a61af66fc99e Initial load
duke
parents:
diff changeset
288 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
289 par_put_range_within_word(beg, bit_index(beg_full_word), value);
a61af66fc99e Initial load
duke
parents:
diff changeset
290 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
291 set_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
292 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
293 clear_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
294 }
a61af66fc99e Initial load
duke
parents:
diff changeset
295 par_put_range_within_word(bit_index(end_full_word), end, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
296 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
297 // The range spans at most 2 partial words.
a61af66fc99e Initial load
duke
parents:
diff changeset
298 idx_t boundary = MIN2(bit_index(beg_full_word), end);
a61af66fc99e Initial load
duke
parents:
diff changeset
299 par_put_range_within_word(beg, boundary, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
300 par_put_range_within_word(boundary, end, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
301 }
a61af66fc99e Initial load
duke
parents:
diff changeset
302
a61af66fc99e Initial load
duke
parents:
diff changeset
303 }
a61af66fc99e Initial load
duke
parents:
diff changeset
304
a61af66fc99e Initial load
duke
parents:
diff changeset
305 void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
306 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
307 set_large_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
308 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
309 clear_large_range(beg, end);
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 void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 verify_range(beg, end);
a61af66fc99e Initial load
duke
parents:
diff changeset
315
a61af66fc99e Initial load
duke
parents:
diff changeset
316 idx_t beg_full_word = word_index_round_up(beg);
a61af66fc99e Initial load
duke
parents:
diff changeset
317 idx_t end_full_word = word_index(end);
a61af66fc99e Initial load
duke
parents:
diff changeset
318
a61af66fc99e Initial load
duke
parents:
diff changeset
319 assert(end_full_word - beg_full_word >= 32,
a61af66fc99e Initial load
duke
parents:
diff changeset
320 "the range must include at least 32 bytes");
a61af66fc99e Initial load
duke
parents:
diff changeset
321
a61af66fc99e Initial load
duke
parents:
diff changeset
322 // The range includes at least one full word.
a61af66fc99e Initial load
duke
parents:
diff changeset
323 par_put_range_within_word(beg, bit_index(beg_full_word), value);
a61af66fc99e Initial load
duke
parents:
diff changeset
324 if (value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
325 set_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
326 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
327 clear_large_range_of_words(beg_full_word, end_full_word);
a61af66fc99e Initial load
duke
parents:
diff changeset
328 }
a61af66fc99e Initial load
duke
parents:
diff changeset
329 par_put_range_within_word(bit_index(end_full_word), end, value);
a61af66fc99e Initial load
duke
parents:
diff changeset
330 }
a61af66fc99e Initial load
duke
parents:
diff changeset
331
a61af66fc99e Initial load
duke
parents:
diff changeset
332 bool BitMap::contains(const BitMap other) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
333 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
334 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
335 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
336 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
337 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
338 bm_word_t word_union = dest_map[index] | other_map[index];
0
a61af66fc99e Initial load
duke
parents:
diff changeset
339 // If this has more bits set than dest_map[index], then other is not a
a61af66fc99e Initial load
duke
parents:
diff changeset
340 // subset.
a61af66fc99e Initial load
duke
parents:
diff changeset
341 if (word_union != dest_map[index]) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
342 }
a61af66fc99e Initial load
duke
parents:
diff changeset
343 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
344 }
a61af66fc99e Initial load
duke
parents:
diff changeset
345
a61af66fc99e Initial load
duke
parents:
diff changeset
346 bool BitMap::intersects(const BitMap other) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
347 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
348 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
349 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
350 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
351 for (idx_t index = 0; index < size_in_words(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
352 if ((dest_map[index] & other_map[index]) != 0) return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
353 }
a61af66fc99e Initial load
duke
parents:
diff changeset
354 // Otherwise, no intersection.
a61af66fc99e Initial load
duke
parents:
diff changeset
355 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
356 }
a61af66fc99e Initial load
duke
parents:
diff changeset
357
a61af66fc99e Initial load
duke
parents:
diff changeset
358 void BitMap::set_union(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
359 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
360 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
361 bm_word_t* other_map = other.map();
0
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_in_words(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
364 dest_map[index] = dest_map[index] | other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
365 }
a61af66fc99e Initial load
duke
parents:
diff changeset
366 }
a61af66fc99e Initial load
duke
parents:
diff changeset
367
a61af66fc99e Initial load
duke
parents:
diff changeset
368
a61af66fc99e Initial load
duke
parents:
diff changeset
369 void BitMap::set_difference(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
370 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
371 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
372 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
373 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
374 for (idx_t index = 0; index < size_in_words(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
375 dest_map[index] = dest_map[index] & ~(other_map[index]);
a61af66fc99e Initial load
duke
parents:
diff changeset
376 }
a61af66fc99e Initial load
duke
parents:
diff changeset
377 }
a61af66fc99e Initial load
duke
parents:
diff changeset
378
a61af66fc99e Initial load
duke
parents:
diff changeset
379
a61af66fc99e Initial load
duke
parents:
diff changeset
380 void BitMap::set_intersection(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
381 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
382 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
383 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
384 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
385 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
386 dest_map[index] = dest_map[index] & other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
387 }
a61af66fc99e Initial load
duke
parents:
diff changeset
388 }
a61af66fc99e Initial load
duke
parents:
diff changeset
389
a61af66fc99e Initial load
duke
parents:
diff changeset
390
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
391 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
392 assert(other.size() >= offset, "offset not in range");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
393 assert(other.size() - offset >= size(), "other not large enough");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
394 // XXX Ideally, we would remove this restriction.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
395 guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
396 "Only handle aligned cases so far.");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
397 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
398 bm_word_t* other_map = other.map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
399 idx_t offset_word_ind = word_index(offset);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
400 idx_t size = size_in_words();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
401 for (idx_t index = 0; index < size; index++) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
402 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
403 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
404 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
405
0
a61af66fc99e Initial load
duke
parents:
diff changeset
406 bool BitMap::set_union_with_result(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
407 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
408 bool changed = false;
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
409 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
410 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
411 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
412 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
413 idx_t temp = map(index) | other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
414 changed = changed || (temp != map(index));
a61af66fc99e Initial load
duke
parents:
diff changeset
415 map()[index] = temp;
a61af66fc99e Initial load
duke
parents:
diff changeset
416 }
a61af66fc99e Initial load
duke
parents:
diff changeset
417 return changed;
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::set_difference_with_result(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
422 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
423 bool changed = false;
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
424 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
425 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
426 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
427 for (idx_t index = 0; index < size; index++) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
428 bm_word_t temp = dest_map[index] & ~(other_map[index]);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
429 changed = changed || (temp != dest_map[index]);
a61af66fc99e Initial load
duke
parents:
diff changeset
430 dest_map[index] = temp;
a61af66fc99e Initial load
duke
parents:
diff changeset
431 }
a61af66fc99e Initial load
duke
parents:
diff changeset
432 return changed;
a61af66fc99e Initial load
duke
parents:
diff changeset
433 }
a61af66fc99e Initial load
duke
parents:
diff changeset
434
a61af66fc99e Initial load
duke
parents:
diff changeset
435
a61af66fc99e Initial load
duke
parents:
diff changeset
436 bool BitMap::set_intersection_with_result(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
437 assert(size() == other.size(), "must have same size");
a61af66fc99e Initial load
duke
parents:
diff changeset
438 bool changed = false;
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
439 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
440 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
441 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
442 for (idx_t index = 0; index < size; index++) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
443 bm_word_t orig = dest_map[index];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
444 bm_word_t temp = orig & other_map[index];
0
a61af66fc99e Initial load
duke
parents:
diff changeset
445 changed = changed || (temp != orig);
a61af66fc99e Initial load
duke
parents:
diff changeset
446 dest_map[index] = temp;
a61af66fc99e Initial load
duke
parents:
diff changeset
447 }
a61af66fc99e Initial load
duke
parents:
diff changeset
448 return changed;
a61af66fc99e Initial load
duke
parents:
diff changeset
449 }
a61af66fc99e Initial load
duke
parents:
diff changeset
450
a61af66fc99e Initial load
duke
parents:
diff changeset
451
a61af66fc99e Initial load
duke
parents:
diff changeset
452 void BitMap::set_from(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
453 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
454 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
455 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
456 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
457 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
458 dest_map[index] = other_map[index];
a61af66fc99e Initial load
duke
parents:
diff changeset
459 }
a61af66fc99e Initial load
duke
parents:
diff changeset
460 }
a61af66fc99e Initial load
duke
parents:
diff changeset
461
a61af66fc99e Initial load
duke
parents:
diff changeset
462
a61af66fc99e Initial load
duke
parents:
diff changeset
463 bool BitMap::is_same(BitMap other) {
a61af66fc99e Initial load
duke
parents:
diff changeset
464 assert(size() == other.size(), "must have same size");
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
465 bm_word_t* dest_map = map();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
466 bm_word_t* other_map = other.map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
467 idx_t size = size_in_words();
a61af66fc99e Initial load
duke
parents:
diff changeset
468 for (idx_t index = 0; index < size; index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
469 if (dest_map[index] != other_map[index]) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
470 }
a61af66fc99e Initial load
duke
parents:
diff changeset
471 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
472 }
a61af66fc99e Initial load
duke
parents:
diff changeset
473
a61af66fc99e Initial load
duke
parents:
diff changeset
474 bool BitMap::is_full() const {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
475 bm_word_t* word = map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
476 idx_t rest = size();
a61af66fc99e Initial load
duke
parents:
diff changeset
477 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
478 if (*word != (bm_word_t) AllBits) return false;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
479 word++;
a61af66fc99e Initial load
duke
parents:
diff changeset
480 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
481 return rest == 0 || (*word | ~right_n_bits((int)rest)) == (bm_word_t) AllBits;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
482 }
a61af66fc99e Initial load
duke
parents:
diff changeset
483
a61af66fc99e Initial load
duke
parents:
diff changeset
484
a61af66fc99e Initial load
duke
parents:
diff changeset
485 bool BitMap::is_empty() const {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
486 bm_word_t* word = map();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
487 idx_t rest = size();
a61af66fc99e Initial load
duke
parents:
diff changeset
488 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
489 if (*word != (bm_word_t) NoBits) return false;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
490 word++;
a61af66fc99e Initial load
duke
parents:
diff changeset
491 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
492 return rest == 0 || (*word & right_n_bits((int)rest)) == (bm_word_t) NoBits;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
493 }
a61af66fc99e Initial load
duke
parents:
diff changeset
494
a61af66fc99e Initial load
duke
parents:
diff changeset
495 void BitMap::clear_large() {
a61af66fc99e Initial load
duke
parents:
diff changeset
496 clear_large_range_of_words(0, size_in_words());
a61af66fc99e Initial load
duke
parents:
diff changeset
497 }
a61af66fc99e Initial load
duke
parents:
diff changeset
498
a61af66fc99e Initial load
duke
parents:
diff changeset
499 // Note that if the closure itself modifies the bitmap
a61af66fc99e Initial load
duke
parents:
diff changeset
500 // then modifications in and to the left of the _bit_ being
a61af66fc99e Initial load
duke
parents:
diff changeset
501 // currently sampled will not be seen. Note also that the
a61af66fc99e Initial load
duke
parents:
diff changeset
502 // interval [leftOffset, rightOffset) is right open.
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
503 bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
504 verify_range(leftOffset, rightOffset);
a61af66fc99e Initial load
duke
parents:
diff changeset
505
a61af66fc99e Initial load
duke
parents:
diff changeset
506 idx_t startIndex = word_index(leftOffset);
a61af66fc99e Initial load
duke
parents:
diff changeset
507 idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words());
a61af66fc99e Initial load
duke
parents:
diff changeset
508 for (idx_t index = startIndex, offset = leftOffset;
a61af66fc99e Initial load
duke
parents:
diff changeset
509 offset < rightOffset && index < endIndex;
a61af66fc99e Initial load
duke
parents:
diff changeset
510 offset = (++index) << LogBitsPerWord) {
a61af66fc99e Initial load
duke
parents:
diff changeset
511 idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
512 for (; offset < rightOffset && rest != (bm_word_t)NoBits; offset++) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
513 if (rest & 1) {
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
514 if (!blk->do_bit(offset)) return false;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
515 // resample at each closure application
a61af66fc99e Initial load
duke
parents:
diff changeset
516 // (see, for instance, CMS bug 4525989)
a61af66fc99e Initial load
duke
parents:
diff changeset
517 rest = map(index) >> (offset & (BitsPerWord -1));
a61af66fc99e Initial load
duke
parents:
diff changeset
518 }
a61af66fc99e Initial load
duke
parents:
diff changeset
519 rest = rest >> 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
520 }
a61af66fc99e Initial load
duke
parents:
diff changeset
521 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
522 return true;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
523 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
524
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
525 BitMap::idx_t* BitMap::_pop_count_table = NULL;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
526
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
527 void BitMap::init_pop_count_table() {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
528 if (_pop_count_table == NULL) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
529 BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
530 for (uint i = 0; i < 256; i++) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
531 table[i] = num_set_bits(i);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
532 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
533
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
534 intptr_t res = Atomic::cmpxchg_ptr((intptr_t) table,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
535 (intptr_t*) &_pop_count_table,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
536 (intptr_t) NULL_WORD);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
537 if (res != NULL_WORD) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
538 guarantee( _pop_count_table == (void*) res, "invariant" );
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
539 FREE_C_HEAP_ARRAY(bm_word_t, table);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
540 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
541 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
542 }
a61af66fc99e Initial load
duke
parents:
diff changeset
543
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
544 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
545 idx_t bits = 0;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
546
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
547 while (w != 0) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
548 while ((w & 1) == 0) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
549 w >>= 1;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
550 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
551 bits++;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
552 w >>= 1;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
553 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
554 return bits;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
555 }
a61af66fc99e Initial load
duke
parents:
diff changeset
556
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
557 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
558 assert(_pop_count_table != NULL, "precondition");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
559 return _pop_count_table[c];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
560 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
561
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
562 BitMap::idx_t BitMap::count_one_bits() const {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
563 init_pop_count_table(); // If necessary.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
564 idx_t sum = 0;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
565 typedef unsigned char uchar;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
566 for (idx_t i = 0; i < size_in_words(); i++) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
567 bm_word_t w = map()[i];
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
568 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
569 sum += num_set_bits_from_table(uchar(w & 255));
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
570 w >>= 8;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
571 }
a61af66fc99e Initial load
duke
parents:
diff changeset
572 }
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
573 return sum;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
574 }
a61af66fc99e Initial load
duke
parents:
diff changeset
575
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
576
0
a61af66fc99e Initial load
duke
parents:
diff changeset
577 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
578
a61af66fc99e Initial load
duke
parents:
diff changeset
579 void BitMap::print_on(outputStream* st) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
580 tty->print("Bitmap(%d):", size());
a61af66fc99e Initial load
duke
parents:
diff changeset
581 for (idx_t index = 0; index < size(); index++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
582 tty->print("%c", at(index) ? '1' : '0');
a61af66fc99e Initial load
duke
parents:
diff changeset
583 }
a61af66fc99e Initial load
duke
parents:
diff changeset
584 tty->cr();
a61af66fc99e Initial load
duke
parents:
diff changeset
585 }
a61af66fc99e Initial load
duke
parents:
diff changeset
586
a61af66fc99e Initial load
duke
parents:
diff changeset
587 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
588
a61af66fc99e Initial load
duke
parents:
diff changeset
589
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 0
diff changeset
590 BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot)
0
a61af66fc99e Initial load
duke
parents:
diff changeset
591 : _bits_per_slot(bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
592 , _map(map, size_in_slots * bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
593 {
a61af66fc99e Initial load
duke
parents:
diff changeset
594 }
a61af66fc99e Initial load
duke
parents:
diff changeset
595
a61af66fc99e Initial load
duke
parents:
diff changeset
596
a61af66fc99e Initial load
duke
parents:
diff changeset
597 BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
598 : _bits_per_slot(bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
599 , _map(size_in_slots * bits_per_slot)
a61af66fc99e Initial load
duke
parents:
diff changeset
600 {
a61af66fc99e Initial load
duke
parents:
diff changeset
601 }