--- old/src/hotspot/share/utilities/bitMap.cpp 2019-06-10 20:16:24.563591595 -0400 +++ new/src/hotspot/share/utilities/bitMap.cpp 2019-06-10 20:16:23.987560805 -0400 @@ -73,7 +73,6 @@ template BitMap::bm_word_t* BitMap::reallocate(const Allocator& allocator, bm_word_t* old_map, idx_t old_size_in_bits, idx_t new_size_in_bits, bool clear) { - verify_max_size_limited(new_size_in_bits); size_t old_size_in_words = calc_size_in_words(old_size_in_bits); size_t new_size_in_words = calc_size_in_words(new_size_in_bits); @@ -175,8 +174,9 @@ } #ifdef ASSERT -void BitMap::verify_max_size_limited(idx_t bit) { - assert(bit <= max_size_in_bits(), "out of bounds: " SIZE_FORMAT, bit); +void BitMap::verify_valid_size(idx_t size_in_bits) { + assert(size_in_bits <= max_size_in_bits(), + "out of bounds: " SIZE_FORMAT, size_in_bits); } void BitMap::verify_index(idx_t index) const { @@ -233,17 +233,17 @@ void BitMap::set_range(idx_t beg, idx_t end) { verify_range(beg, end); - idx_t beg_full_word = word_index_round_up(beg); - idx_t end_full_word = word_index(end); + idx_t beg_aligned = range_begin_align_up(beg); + idx_t end_aligned = range_end_align_down(end); - if (beg_full_word < end_full_word) { + if (beg_aligned < end_aligned) { // The range includes at least one full word. - set_range_within_word(beg, bit_index(beg_full_word)); - set_range_of_words(beg_full_word, end_full_word); - set_range_within_word(bit_index(end_full_word), end); + set_range_within_word(beg, beg_aligned); + set_range_of_words(word_index(beg_aligned), word_index(end_aligned)); + set_range_within_word(end_aligned, end); } else { // The range spans at most 2 partial words. - idx_t boundary = MIN2(bit_index(beg_full_word), end); + idx_t boundary = MIN2(beg_aligned, end); set_range_within_word(beg, boundary); set_range_within_word(boundary, end); } @@ -252,62 +252,64 @@ void BitMap::clear_range(idx_t beg, idx_t end) { verify_range(beg, end); - idx_t beg_full_word = word_index_round_up(beg); - idx_t end_full_word = word_index(end); + idx_t beg_aligned = range_begin_align_up(beg); + idx_t end_aligned = range_end_align_down(end); - if (beg_full_word < end_full_word) { + if (beg_aligned < end_aligned) { // The range includes at least one full word. - clear_range_within_word(beg, bit_index(beg_full_word)); - clear_range_of_words(beg_full_word, end_full_word); - clear_range_within_word(bit_index(end_full_word), end); + clear_range_within_word(beg, beg_aligned); + clear_range_of_words(word_index(beg_aligned), word_index(end_aligned)); + clear_range_within_word(end_aligned, end); } else { // The range spans at most 2 partial words. - idx_t boundary = MIN2(bit_index(beg_full_word), end); + idx_t boundary = MIN2(beg_aligned, end); clear_range_within_word(beg, boundary); clear_range_within_word(boundary, end); } } -bool BitMap::is_small_range_of_words(idx_t beg_full_word, idx_t end_full_word) { +bool BitMap::is_small_aligned_range(idx_t beg_aligned, idx_t end_aligned) { // There is little point to call large version on small ranges. - // Need to check carefully, keeping potential idx_t underflow in mind. + // Need to check carefully, keeping potential idx_t over/underflow in mind, + // because beg_aligned > end_aligned can occur when beg and end are in the + // same word. // The threshold should be at least one word. STATIC_ASSERT(small_range_words >= 1); - return (beg_full_word + small_range_words >= end_full_word); + return word_index(beg_aligned) + small_range_words >= word_index(end_aligned); } void BitMap::set_large_range(idx_t beg, idx_t end) { verify_range(beg, end); - idx_t beg_full_word = word_index_round_up(beg); - idx_t end_full_word = word_index(end); + idx_t beg_aligned = range_begin_align_up(beg); + idx_t end_aligned = range_end_align_down(end); - if (is_small_range_of_words(beg_full_word, end_full_word)) { + if (is_small_aligned_range(beg_aligned, end_aligned)) { set_range(beg, end); return; } // The range includes at least one full word. - set_range_within_word(beg, bit_index(beg_full_word)); - set_large_range_of_words(beg_full_word, end_full_word); - set_range_within_word(bit_index(end_full_word), end); + set_range_within_word(beg, beg_aligned); + set_large_range_of_words(word_index(beg_aligned), word_index(end_aligned)); + set_range_within_word(end_aligned, end); } void BitMap::clear_large_range(idx_t beg, idx_t end) { verify_range(beg, end); - idx_t beg_full_word = word_index_round_up(beg); - idx_t end_full_word = word_index(end); + idx_t beg_aligned = range_begin_align_up(beg); + idx_t end_aligned = range_end_align_down(end); - if (is_small_range_of_words(beg_full_word, end_full_word)) { + if (is_small_aligned_range(beg_aligned, end_aligned)) { clear_range(beg, end); return; } // The range includes at least one full word. - clear_range_within_word(beg, bit_index(beg_full_word)); - clear_large_range_of_words(beg_full_word, end_full_word); - clear_range_within_word(bit_index(end_full_word), end); + clear_range_within_word(beg, beg_aligned); + clear_large_range_of_words(word_index(beg_aligned), word_index(end_aligned)); + clear_range_within_word(end_aligned, end); } void BitMap::at_put(idx_t offset, bool value) { @@ -348,21 +350,23 @@ void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) { verify_range(beg, end); - idx_t beg_full_word = word_index_round_up(beg); - idx_t end_full_word = word_index(end); + idx_t beg_aligned = range_begin_align_up(beg); + idx_t end_aligned = range_end_align_down(end); - if (beg_full_word < end_full_word) { + if (beg_aligned < end_aligned) { // The range includes at least one full word. - par_put_range_within_word(beg, bit_index(beg_full_word), value); + par_put_range_within_word(beg, beg_aligned, value); + idx_t beg_full_word = word_index(beg_aligned); + idx_t end_full_word = word_index(end_aligned); if (value) { set_range_of_words(beg_full_word, end_full_word); } else { clear_range_of_words(beg_full_word, end_full_word); } - par_put_range_within_word(bit_index(end_full_word), end, value); + par_put_range_within_word(end_aligned, end, value); } else { // The range spans at most 2 partial words. - idx_t boundary = MIN2(bit_index(beg_full_word), end); + idx_t boundary = MIN2(beg_aligned, end); par_put_range_within_word(beg, boundary, value); par_put_range_within_word(boundary, end, value); } @@ -380,22 +384,24 @@ void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) { verify_range(beg, end); - idx_t beg_full_word = word_index_round_up(beg); - idx_t end_full_word = word_index(end); + idx_t beg_aligned = range_begin_align_up(beg); + idx_t end_aligned = range_end_align_down(end); - if (is_small_range_of_words(beg_full_word, end_full_word)) { + if (is_small_aligned_range(beg_aligned, end_aligned)) { par_at_put_range(beg, end, value); return; } // The range includes at least one full word. - par_put_range_within_word(beg, bit_index(beg_full_word), value); + par_put_range_within_word(beg, beg_aligned, value); + idx_t beg_full_word = word_index(beg_aligned); + idx_t end_full_word = word_index(end_aligned); if (value) { set_large_range_of_words(beg_full_word, end_full_word); } else { clear_large_range_of_words(beg_full_word, end_full_word); } - par_put_range_within_word(bit_index(end_full_word), end, value); + par_put_range_within_word(end_aligned, end, value); } inline bm_word_t tail_mask(idx_t tail_bits) { --- old/src/hotspot/share/utilities/bitMap.hpp 2019-06-10 20:16:26.223680331 -0400 +++ new/src/hotspot/share/utilities/bitMap.hpp 2019-06-10 20:16:25.635648899 -0400 @@ -48,8 +48,10 @@ public: typedef size_t idx_t; // Type used for bit and word indices. - typedef uintptr_t bm_word_t; // Element type of array that represents - // the bitmap. BitsPerWord bits per element. + typedef uintptr_t bm_word_t; // Element type of array that represents the + // bitmap, with BitsPerWord bits per element. + // If this were to fail, there are lots of places that would need repair. + STATIC_ASSERT((sizeof(bm_word_t) * BitsPerByte) == BitsPerWord); // Hints for range sizes. typedef enum { @@ -60,10 +62,14 @@ bm_word_t* _map; // First word in bitmap idx_t _size; // Size of bitmap (in bits) - // Limit max_size_in_bits so roundup in calc_size_in_words can't overflow. + // Limit max_size_in_bits so aligning up to a word never overflows. static idx_t max_size_in_words() { return word_index(~idx_t(0)); } static idx_t max_size_in_bits() { return max_size_in_words() * BitsPerWord; } + // bit must be the begin/end of a validated range. + static inline idx_t range_begin_align_up(idx_t bit); + static inline idx_t range_end_align_down(idx_t bit); + // Helper for get_next_{zero,one}_bit variants. // - flip designates whether searching for 1s or 0s. Must be one of // find_{zeros,ones}_flip. @@ -79,6 +85,9 @@ // operation was requested. Measured in words. static const size_t small_range_words = 32; + // beg/end_aligned must be from range_begin/end aligners. + static bool is_small_aligned_range(idx_t beg_aligned, idx_t end_aligned); + protected: // Return the position of bit within the word that contains it (e.g., if // bitmap words are 32 bits, return a number 0 <= n <= 31). @@ -124,14 +133,13 @@ static void clear_range_of_words(bm_word_t* map, idx_t beg, idx_t end); - static bool is_small_range_of_words(idx_t beg_full_word, idx_t end_full_word); - - // The index of the first full word in a range. - idx_t word_index_round_up(idx_t bit) const; - // Verification. - static void verify_max_size_limited(idx_t bit) NOT_DEBUG_RETURN; + + // Verify size_in_bits does not exceed maximum size. + static void verify_valid_size(idx_t size_in_bits) NOT_DEBUG_RETURN; + // Verify index is less than size. void verify_index(idx_t index) const NOT_DEBUG_RETURN; + // Verify [beg,end) is a valid range. void verify_range(idx_t beg_index, idx_t end_index) const NOT_DEBUG_RETURN; // Statistics. @@ -191,7 +199,7 @@ void pretouch(); static idx_t calc_size_in_words(idx_t size_in_bits) { - verify_max_size_limited(size_in_bits); + verify_valid_size(size_in_bits); return word_index(size_in_bits + (BitsPerWord - 1)); } --- old/src/hotspot/share/utilities/bitMap.inline.hpp 2019-06-10 20:16:28.063778688 -0400 +++ new/src/hotspot/share/utilities/bitMap.inline.hpp 2019-06-10 20:16:27.483747684 -0400 @@ -247,8 +247,12 @@ memset(_map + beg, 0, (end - beg) * sizeof(bm_word_t)); } -inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const { - return calc_size_in_words(bit); +inline BitMap::idx_t BitMap::range_begin_align_up(idx_t bit) { + return align_up(bit, BitsPerWord); +} + +inline BitMap::idx_t BitMap::range_end_align_down(idx_t bit) { + return align_down(bit, BitsPerWord); } inline bool BitMap2D::is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) {