Wed, 03 Jul 2019 20:42:37 +0800
Merge
1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "memory/allocation.inline.hpp"
27 #include "utilities/bitMap.inline.hpp"
28 #include "utilities/copy.hpp"
29 #ifdef TARGET_OS_FAMILY_linux
30 # include "os_linux.inline.hpp"
31 #endif
32 #ifdef TARGET_OS_FAMILY_solaris
33 # include "os_solaris.inline.hpp"
34 #endif
35 #ifdef TARGET_OS_FAMILY_windows
36 # include "os_windows.inline.hpp"
37 #endif
38 #ifdef TARGET_OS_FAMILY_aix
39 # include "os_aix.inline.hpp"
40 #endif
41 #ifdef TARGET_OS_FAMILY_bsd
42 # include "os_bsd.inline.hpp"
43 #endif
46 BitMap::BitMap(bm_word_t* map, idx_t size_in_bits) :
47 _map(map), _size(size_in_bits), _map_allocator(false)
48 {
49 assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
50 assert(size_in_bits >= 0, "just checking");
51 }
54 BitMap::BitMap(idx_t size_in_bits, bool in_resource_area) :
55 _map(NULL), _size(0), _map_allocator(false)
56 {
57 assert(sizeof(bm_word_t) == BytesPerWord, "Implementation assumption.");
58 resize(size_in_bits, in_resource_area);
59 }
61 void BitMap::resize(idx_t size_in_bits, bool in_resource_area) {
62 assert(size_in_bits >= 0, "just checking");
63 idx_t old_size_in_words = size_in_words();
64 bm_word_t* old_map = map();
66 _size = size_in_bits;
67 idx_t new_size_in_words = size_in_words();
68 if (in_resource_area) {
69 _map = NEW_RESOURCE_ARRAY(bm_word_t, new_size_in_words);
70 } else {
71 if (old_map != NULL) {
72 _map_allocator.free();
73 }
74 _map = _map_allocator.allocate(new_size_in_words);
75 }
76 Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) _map,
77 MIN2(old_size_in_words, new_size_in_words));
78 if (new_size_in_words > old_size_in_words) {
79 clear_range_of_words(old_size_in_words, size_in_words());
80 }
81 }
83 void BitMap::set_range_within_word(idx_t beg, idx_t end) {
84 // With a valid range (beg <= end), this test ensures that end != 0, as
85 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
86 if (beg != end) {
87 bm_word_t mask = inverted_bit_mask_for_range(beg, end);
88 *word_addr(beg) |= ~mask;
89 }
90 }
92 void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
93 // With a valid range (beg <= end), this test ensures that end != 0, as
94 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
95 if (beg != end) {
96 bm_word_t mask = inverted_bit_mask_for_range(beg, end);
97 *word_addr(beg) &= mask;
98 }
99 }
101 void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
102 assert(value == 0 || value == 1, "0 for clear, 1 for set");
103 // With a valid range (beg <= end), this test ensures that end != 0, as
104 // required by inverted_bit_mask_for_range. Also avoids an unnecessary write.
105 if (beg != end) {
106 intptr_t* pw = (intptr_t*)word_addr(beg);
107 intptr_t w = *pw;
108 intptr_t mr = (intptr_t)inverted_bit_mask_for_range(beg, end);
109 intptr_t nw = value ? (w | ~mr) : (w & mr);
110 while (true) {
111 intptr_t res = Atomic::cmpxchg_ptr(nw, pw, w);
112 if (res == w) break;
113 w = res;
114 nw = value ? (w | ~mr) : (w & mr);
115 }
116 }
117 }
119 void BitMap::set_range(idx_t beg, idx_t end) {
120 verify_range(beg, end);
122 idx_t beg_full_word = word_index_round_up(beg);
123 idx_t end_full_word = word_index(end);
125 if (beg_full_word < end_full_word) {
126 // The range includes at least one full word.
127 set_range_within_word(beg, bit_index(beg_full_word));
128 set_range_of_words(beg_full_word, end_full_word);
129 set_range_within_word(bit_index(end_full_word), end);
130 } else {
131 // The range spans at most 2 partial words.
132 idx_t boundary = MIN2(bit_index(beg_full_word), end);
133 set_range_within_word(beg, boundary);
134 set_range_within_word(boundary, end);
135 }
136 }
138 void BitMap::clear_range(idx_t beg, idx_t end) {
139 verify_range(beg, end);
141 idx_t beg_full_word = word_index_round_up(beg);
142 idx_t end_full_word = word_index(end);
144 if (beg_full_word < end_full_word) {
145 // The range includes at least one full word.
146 clear_range_within_word(beg, bit_index(beg_full_word));
147 clear_range_of_words(beg_full_word, end_full_word);
148 clear_range_within_word(bit_index(end_full_word), end);
149 } else {
150 // The range spans at most 2 partial words.
151 idx_t boundary = MIN2(bit_index(beg_full_word), end);
152 clear_range_within_word(beg, boundary);
153 clear_range_within_word(boundary, end);
154 }
155 }
157 bool BitMap::is_small_range_of_words(idx_t beg_full_word, idx_t end_full_word) {
158 // There is little point to call large version on small ranges.
159 // Need to check carefully, keeping potential idx_t underflow in mind.
160 // The threshold should be at least one word.
161 STATIC_ASSERT(small_range_words >= 1);
162 return (beg_full_word + small_range_words >= end_full_word);
163 }
165 void BitMap::set_large_range(idx_t beg, idx_t end) {
166 verify_range(beg, end);
168 idx_t beg_full_word = word_index_round_up(beg);
169 idx_t end_full_word = word_index(end);
171 if (is_small_range_of_words(beg_full_word, end_full_word)) {
172 set_range(beg, end);
173 return;
174 }
176 // The range includes at least one full word.
177 set_range_within_word(beg, bit_index(beg_full_word));
178 set_large_range_of_words(beg_full_word, end_full_word);
179 set_range_within_word(bit_index(end_full_word), end);
180 }
182 void BitMap::clear_large_range(idx_t beg, idx_t end) {
183 verify_range(beg, end);
185 idx_t beg_full_word = word_index_round_up(beg);
186 idx_t end_full_word = word_index(end);
188 if (is_small_range_of_words(beg_full_word, end_full_word)) {
189 clear_range(beg, end);
190 return;
191 }
193 // The range includes at least one full word.
194 clear_range_within_word(beg, bit_index(beg_full_word));
195 clear_large_range_of_words(beg_full_word, end_full_word);
196 clear_range_within_word(bit_index(end_full_word), end);
197 }
199 void BitMap::at_put(idx_t offset, bool value) {
200 if (value) {
201 set_bit(offset);
202 } else {
203 clear_bit(offset);
204 }
205 }
207 // Return true to indicate that this thread changed
208 // the bit, false to indicate that someone else did.
209 // In either case, the requested bit is in the
210 // requested state some time during the period that
211 // this thread is executing this call. More importantly,
212 // if no other thread is executing an action to
213 // change the requested bit to a state other than
214 // the one that this thread is trying to set it to,
215 // then the the bit is in the expected state
216 // at exit from this method. However, rather than
217 // make such a strong assertion here, based on
218 // assuming such constrained use (which though true
219 // today, could change in the future to service some
220 // funky parallel algorithm), we encourage callers
221 // to do such verification, as and when appropriate.
222 bool BitMap::par_at_put(idx_t bit, bool value) {
223 return value ? par_set_bit(bit) : par_clear_bit(bit);
224 }
226 void BitMap::at_put_grow(idx_t offset, bool value) {
227 if (offset >= size()) {
228 resize(2 * MAX2(size(), offset));
229 }
230 at_put(offset, value);
231 }
233 void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
234 if (value) {
235 set_range(start_offset, end_offset);
236 } else {
237 clear_range(start_offset, end_offset);
238 }
239 }
241 void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
242 verify_range(beg, end);
244 idx_t beg_full_word = word_index_round_up(beg);
245 idx_t end_full_word = word_index(end);
247 if (beg_full_word < end_full_word) {
248 // The range includes at least one full word.
249 par_put_range_within_word(beg, bit_index(beg_full_word), value);
250 if (value) {
251 set_range_of_words(beg_full_word, end_full_word);
252 } else {
253 clear_range_of_words(beg_full_word, end_full_word);
254 }
255 par_put_range_within_word(bit_index(end_full_word), end, value);
256 } else {
257 // The range spans at most 2 partial words.
258 idx_t boundary = MIN2(bit_index(beg_full_word), end);
259 par_put_range_within_word(beg, boundary, value);
260 par_put_range_within_word(boundary, end, value);
261 }
263 }
265 void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
266 if (value) {
267 set_large_range(beg, end);
268 } else {
269 clear_large_range(beg, end);
270 }
271 }
273 void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
274 verify_range(beg, end);
276 idx_t beg_full_word = word_index_round_up(beg);
277 idx_t end_full_word = word_index(end);
279 if (is_small_range_of_words(beg_full_word, end_full_word)) {
280 par_at_put_range(beg, end, value);
281 return;
282 }
284 // The range includes at least one full word.
285 par_put_range_within_word(beg, bit_index(beg_full_word), value);
286 if (value) {
287 set_large_range_of_words(beg_full_word, end_full_word);
288 } else {
289 clear_large_range_of_words(beg_full_word, end_full_word);
290 }
291 par_put_range_within_word(bit_index(end_full_word), end, value);
292 }
294 bool BitMap::contains(const BitMap other) const {
295 assert(size() == other.size(), "must have same size");
296 bm_word_t* dest_map = map();
297 bm_word_t* other_map = other.map();
298 idx_t size = size_in_words();
299 for (idx_t index = 0; index < size_in_words(); index++) {
300 bm_word_t word_union = dest_map[index] | other_map[index];
301 // If this has more bits set than dest_map[index], then other is not a
302 // subset.
303 if (word_union != dest_map[index]) return false;
304 }
305 return true;
306 }
308 bool BitMap::intersects(const BitMap other) const {
309 assert(size() == other.size(), "must have same size");
310 bm_word_t* dest_map = map();
311 bm_word_t* other_map = other.map();
312 idx_t size = size_in_words();
313 for (idx_t index = 0; index < size_in_words(); index++) {
314 if ((dest_map[index] & other_map[index]) != 0) return true;
315 }
316 // Otherwise, no intersection.
317 return false;
318 }
320 void BitMap::set_union(BitMap other) {
321 assert(size() == other.size(), "must have same size");
322 bm_word_t* dest_map = map();
323 bm_word_t* other_map = other.map();
324 idx_t size = size_in_words();
325 for (idx_t index = 0; index < size_in_words(); index++) {
326 dest_map[index] = dest_map[index] | other_map[index];
327 }
328 }
331 void BitMap::set_difference(BitMap other) {
332 assert(size() == other.size(), "must have same size");
333 bm_word_t* dest_map = map();
334 bm_word_t* other_map = other.map();
335 idx_t size = size_in_words();
336 for (idx_t index = 0; index < size_in_words(); index++) {
337 dest_map[index] = dest_map[index] & ~(other_map[index]);
338 }
339 }
342 void BitMap::set_intersection(BitMap other) {
343 assert(size() == other.size(), "must have same size");
344 bm_word_t* dest_map = map();
345 bm_word_t* other_map = other.map();
346 idx_t size = size_in_words();
347 for (idx_t index = 0; index < size; index++) {
348 dest_map[index] = dest_map[index] & other_map[index];
349 }
350 }
353 void BitMap::set_intersection_at_offset(BitMap other, idx_t offset) {
354 assert(other.size() >= offset, "offset not in range");
355 assert(other.size() - offset >= size(), "other not large enough");
356 // XXX Ideally, we would remove this restriction.
357 guarantee((offset % (sizeof(bm_word_t) * BitsPerByte)) == 0,
358 "Only handle aligned cases so far.");
359 bm_word_t* dest_map = map();
360 bm_word_t* other_map = other.map();
361 idx_t offset_word_ind = word_index(offset);
362 idx_t size = size_in_words();
363 for (idx_t index = 0; index < size; index++) {
364 dest_map[index] = dest_map[index] & other_map[offset_word_ind + index];
365 }
366 }
368 bool BitMap::set_union_with_result(BitMap other) {
369 assert(size() == other.size(), "must have same size");
370 bool changed = false;
371 bm_word_t* dest_map = map();
372 bm_word_t* other_map = other.map();
373 idx_t size = size_in_words();
374 for (idx_t index = 0; index < size; index++) {
375 idx_t temp = map(index) | other_map[index];
376 changed = changed || (temp != map(index));
377 map()[index] = temp;
378 }
379 return changed;
380 }
383 bool BitMap::set_difference_with_result(BitMap other) {
384 assert(size() == other.size(), "must have same size");
385 bool changed = false;
386 bm_word_t* dest_map = map();
387 bm_word_t* other_map = other.map();
388 idx_t size = size_in_words();
389 for (idx_t index = 0; index < size; index++) {
390 bm_word_t temp = dest_map[index] & ~(other_map[index]);
391 changed = changed || (temp != dest_map[index]);
392 dest_map[index] = temp;
393 }
394 return changed;
395 }
398 bool BitMap::set_intersection_with_result(BitMap other) {
399 assert(size() == other.size(), "must have same size");
400 bool changed = false;
401 bm_word_t* dest_map = map();
402 bm_word_t* other_map = other.map();
403 idx_t size = size_in_words();
404 for (idx_t index = 0; index < size; index++) {
405 bm_word_t orig = dest_map[index];
406 bm_word_t temp = orig & other_map[index];
407 changed = changed || (temp != orig);
408 dest_map[index] = temp;
409 }
410 return changed;
411 }
414 void BitMap::set_from(BitMap other) {
415 assert(size() == other.size(), "must have same size");
416 bm_word_t* dest_map = map();
417 bm_word_t* other_map = other.map();
418 idx_t size = size_in_words();
419 for (idx_t index = 0; index < size; index++) {
420 dest_map[index] = other_map[index];
421 }
422 }
425 bool BitMap::is_same(BitMap other) {
426 assert(size() == other.size(), "must have same size");
427 bm_word_t* dest_map = map();
428 bm_word_t* other_map = other.map();
429 idx_t size = size_in_words();
430 for (idx_t index = 0; index < size; index++) {
431 if (dest_map[index] != other_map[index]) return false;
432 }
433 return true;
434 }
436 bool BitMap::is_full() const {
437 bm_word_t* word = map();
438 idx_t rest = size();
439 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
440 if (*word != (bm_word_t) AllBits) return false;
441 word++;
442 }
443 return rest == 0 || (*word | ~right_n_bits((int)rest)) == (bm_word_t) AllBits;
444 }
447 bool BitMap::is_empty() const {
448 bm_word_t* word = map();
449 idx_t rest = size();
450 for (; rest >= (idx_t) BitsPerWord; rest -= BitsPerWord) {
451 if (*word != (bm_word_t) NoBits) return false;
452 word++;
453 }
454 return rest == 0 || (*word & right_n_bits((int)rest)) == (bm_word_t) NoBits;
455 }
457 void BitMap::clear_large() {
458 clear_large_range_of_words(0, size_in_words());
459 }
461 // Note that if the closure itself modifies the bitmap
462 // then modifications in and to the left of the _bit_ being
463 // currently sampled will not be seen. Note also that the
464 // interval [leftOffset, rightOffset) is right open.
465 bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
466 verify_range(leftOffset, rightOffset);
468 idx_t startIndex = word_index(leftOffset);
469 idx_t endIndex = MIN2(word_index(rightOffset) + 1, size_in_words());
470 for (idx_t index = startIndex, offset = leftOffset;
471 offset < rightOffset && index < endIndex;
472 offset = (++index) << LogBitsPerWord) {
473 idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
474 for (; offset < rightOffset && rest != (bm_word_t)NoBits; offset++) {
475 if (rest & 1) {
476 if (!blk->do_bit(offset)) return false;
477 // resample at each closure application
478 // (see, for instance, CMS bug 4525989)
479 rest = map(index) >> (offset & (BitsPerWord -1));
480 }
481 rest = rest >> 1;
482 }
483 }
484 return true;
485 }
487 BitMap::idx_t* BitMap::_pop_count_table = NULL;
489 void BitMap::init_pop_count_table() {
490 if (_pop_count_table == NULL) {
491 BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256, mtInternal);
492 for (uint i = 0; i < 256; i++) {
493 table[i] = num_set_bits(i);
494 }
496 intptr_t res = Atomic::cmpxchg_ptr((intptr_t) table,
497 (intptr_t*) &_pop_count_table,
498 (intptr_t) NULL_WORD);
499 if (res != NULL_WORD) {
500 guarantee( _pop_count_table == (void*) res, "invariant" );
501 FREE_C_HEAP_ARRAY(bm_word_t, table, mtInternal);
502 }
503 }
504 }
506 BitMap::idx_t BitMap::num_set_bits(bm_word_t w) {
507 idx_t bits = 0;
509 while (w != 0) {
510 while ((w & 1) == 0) {
511 w >>= 1;
512 }
513 bits++;
514 w >>= 1;
515 }
516 return bits;
517 }
519 BitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) {
520 assert(_pop_count_table != NULL, "precondition");
521 return _pop_count_table[c];
522 }
524 BitMap::idx_t BitMap::count_one_bits() const {
525 init_pop_count_table(); // If necessary.
526 idx_t sum = 0;
527 typedef unsigned char uchar;
528 for (idx_t i = 0; i < size_in_words(); i++) {
529 bm_word_t w = map()[i];
530 for (size_t j = 0; j < sizeof(bm_word_t); j++) {
531 sum += num_set_bits_from_table(uchar(w & 255));
532 w >>= 8;
533 }
534 }
535 return sum;
536 }
538 void BitMap::print_on_error(outputStream* st, const char* prefix) const {
539 st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")",
540 prefix, p2i(map()), p2i((char*)map() + (size() >> LogBitsPerByte)));
541 }
543 #ifndef PRODUCT
545 void BitMap::print_on(outputStream* st) const {
546 tty->print("Bitmap(" SIZE_FORMAT "):", size());
547 for (idx_t index = 0; index < size(); index++) {
548 tty->print("%c", at(index) ? '1' : '0');
549 }
550 tty->cr();
551 }
553 #endif
556 BitMap2D::BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot)
557 : _bits_per_slot(bits_per_slot)
558 , _map(map, size_in_slots * bits_per_slot)
559 {
560 }
563 BitMap2D::BitMap2D(idx_t size_in_slots, idx_t bits_per_slot)
564 : _bits_per_slot(bits_per_slot)
565 , _map(size_in_slots * bits_per_slot)
566 {
567 }