1 /*
   2  * Copyright (c) 2005, 2010, 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  */
  24 
  25 #ifndef SHARE_VM_UTILITIES_BITMAP_INLINE_HPP
  26 #define SHARE_VM_UTILITIES_BITMAP_INLINE_HPP
  27 
  28 #include "runtime/atomic.hpp"
  29 #include "utilities/bitMap.hpp"
  30 
  31 #ifdef ASSERT
  32 inline void BitMap::verify_index(idx_t index) const {
  33   assert(index < _size, "BitMap index out of bounds");
  34 }
  35 
  36 inline void BitMap::verify_range(idx_t beg_index, idx_t end_index) const {
  37   assert(beg_index <= end_index, "BitMap range error");
  38   // Note that [0,0) and [size,size) are both valid ranges.
  39   if (end_index != _size) verify_index(end_index);
  40 }
  41 #endif // #ifdef ASSERT
  42 
  43 inline void BitMap::set_bit(idx_t bit) {
  44   verify_index(bit);
  45   *word_addr(bit) |= bit_mask(bit);
  46 }
  47 
  48 inline void BitMap::clear_bit(idx_t bit) {
  49   verify_index(bit);
  50   *word_addr(bit) &= ~bit_mask(bit);
  51 }
  52 
  53 inline bool BitMap::par_set_bit(idx_t bit) {
  54   verify_index(bit);
  55   volatile idx_t* const addr = word_addr(bit);
  56   const idx_t mask = bit_mask(bit);
  57   idx_t old_val = *addr;
  58 
  59   do {
  60     const idx_t new_val = old_val | mask;
  61     if (new_val == old_val) {
  62       return false;     // Someone else beat us to it.
  63     }
  64     const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
  65                                                       (volatile void*) addr,
  66                                                       (void*) old_val);
  67     if (cur_val == old_val) {
  68       return true;      // Success.
  69     }
  70     old_val = cur_val;  // The value changed, try again.
  71   } while (true);
  72 }
  73 
  74 inline bool BitMap::par_clear_bit(idx_t bit) {
  75   verify_index(bit);
  76   volatile idx_t* const addr = word_addr(bit);
  77   const idx_t mask = ~bit_mask(bit);
  78   idx_t old_val = *addr;
  79 
  80   do {
  81     const idx_t new_val = old_val & mask;
  82     if (new_val == old_val) {
  83       return false;     // Someone else beat us to it.
  84     }
  85     const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
  86                                                       (volatile void*) addr,
  87                                                       (void*) old_val);
  88     if (cur_val == old_val) {
  89       return true;      // Success.
  90     }
  91     old_val = cur_val;  // The value changed, try again.
  92   } while (true);
  93 }
  94 
  95 inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
  96   if (hint == small_range && end - beg == 1) {
  97     set_bit(beg);
  98   } else {
  99     if (hint == large_range) {
 100       set_large_range(beg, end);
 101     } else {
 102       set_range(beg, end);
 103     }
 104   }
 105 }
 106 
 107 inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
 108   if (hint == small_range && end - beg == 1) {
 109     clear_bit(beg);
 110   } else {
 111     if (hint == large_range) {
 112       clear_large_range(beg, end);
 113     } else {
 114       clear_range(beg, end);
 115     }
 116   }
 117 }
 118 
 119 inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
 120   if (hint == small_range && end - beg == 1) {
 121     par_at_put(beg, true);
 122   } else {
 123     if (hint == large_range) {
 124       par_at_put_large_range(beg, end, true);
 125     } else {
 126       par_at_put_range(beg, end, true);
 127     }
 128   }
 129 }
 130 
 131 inline void BitMap::set_range_of_words(idx_t beg, idx_t end) {
 132   bm_word_t* map = _map;
 133   for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0;
 134 }
 135 
 136 
 137 inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) {
 138   bm_word_t* map = _map;
 139   for (idx_t i = beg; i < end; ++i) map[i] = 0;
 140 }
 141 
 142 
 143 inline void BitMap::clear() {
 144   clear_range_of_words(0, size_in_words());
 145 }
 146 
 147 
 148 inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
 149   if (hint == small_range && end - beg == 1) {
 150     par_at_put(beg, false);
 151   } else {
 152     if (hint == large_range) {
 153       par_at_put_large_range(beg, end, false);
 154     } else {
 155       par_at_put_range(beg, end, false);
 156     }
 157   }
 158 }
 159 
 160 inline BitMap::idx_t
 161 BitMap::get_next_one_offset_inline(idx_t l_offset, idx_t r_offset) const {
 162   assert(l_offset <= size(), "BitMap index out of bounds");
 163   assert(r_offset <= size(), "BitMap index out of bounds");
 164   assert(l_offset <= r_offset, "l_offset > r_offset ?");
 165 
 166   if (l_offset == r_offset) {
 167     return l_offset;
 168   }
 169   idx_t   index = word_index(l_offset);
 170   idx_t r_index = word_index(r_offset-1) + 1;
 171   idx_t res_offset = l_offset;
 172 
 173   // check bits including and to the _left_ of offset's position
 174   idx_t pos = bit_in_word(res_offset);
 175   idx_t res = map(index) >> pos;
 176   if (res != (uintptr_t)NoBits) {
 177     // find the position of the 1-bit
 178     for (; !(res & 1); res_offset++) {
 179       res = res >> 1;
 180     }
 181 
 182     // In the following assert, checking that res_offset is strictly
 183     // less than r_offset is too strong. Consider the case where
 184     // l_offset is bit 15 and r_offset is bit 17 of the same map word,
 185     // and where bits [18:17:16:15] == [01:00:00:00]. The calculation
 186     // above would yield the offset of bit 18. All we can assert is that
 187     // res_offset is strictly less than size() since we know that there
 188     // are set bits at offsets above, but in the same map word as, r_offset
 189     // The bits in the range (r_offset:l_offset] are all 0.
 190     assert(res_offset >= l_offset && res_offset < size(), "just checking");
 191     return MIN2(res_offset, r_offset);
 192   }
 193   // skip over all word length 0-bit runs
 194   for (index++; index < r_index; index++) {
 195     res = map(index);
 196     if (res != (uintptr_t)NoBits) {
 197       // found a 1, return the offset
 198       for (res_offset = bit_index(index); !(res & 1); res_offset++) {
 199         res = res >> 1;
 200       }
 201       assert(res & 1, "tautology; see loop condition");
 202       assert(res_offset >= l_offset, "just checking");
 203       return MIN2(res_offset, r_offset);
 204     }
 205   }
 206   return r_offset;
 207 }
 208 
 209 inline BitMap::idx_t
 210 BitMap::get_next_zero_offset_inline(idx_t l_offset, idx_t r_offset) const {
 211   assert(l_offset <= size(), "BitMap index out of bounds");
 212   assert(r_offset <= size(), "BitMap index out of bounds");
 213   assert(l_offset <= r_offset, "l_offset > r_offset ?");
 214 
 215   if (l_offset == r_offset) {
 216     return l_offset;
 217   }
 218   idx_t   index = word_index(l_offset);
 219   idx_t r_index = word_index(r_offset-1) + 1;
 220   idx_t res_offset = l_offset;
 221 
 222   // check bits including and to the _left_ of offset's position
 223   idx_t pos = res_offset & (BitsPerWord - 1);
 224   idx_t res = (map(index) >> pos) | left_n_bits((int)pos);
 225 
 226   if (res != (uintptr_t)AllBits) {
 227     // find the position of the 0-bit
 228     for (; res & 1; res_offset++) {
 229       res = res >> 1;
 230     }
 231     assert(res_offset >= l_offset, "just checking");
 232     return MIN2(res_offset, r_offset);
 233   }
 234   // skip over all word length 1-bit runs
 235   for (index++; index < r_index; index++) {
 236     res = map(index);
 237     if (res != (uintptr_t)AllBits) {
 238       // found a 0, return the offset
 239       for (res_offset = index << LogBitsPerWord; res & 1;
 240            res_offset++) {
 241         res = res >> 1;
 242       }
 243       assert(!(res & 1), "tautology; see loop condition");
 244       assert(res_offset >= l_offset, "just checking");
 245       return MIN2(res_offset, r_offset);
 246     }
 247   }
 248   return r_offset;
 249 }
 250 
 251 inline BitMap::idx_t
 252 BitMap::get_next_one_offset_inline_aligned_right(idx_t l_offset,
 253                                                  idx_t r_offset) const
 254 {
 255   verify_range(l_offset, r_offset);
 256   assert(bit_in_word(r_offset) == 0, "r_offset not word-aligned");
 257 
 258   if (l_offset == r_offset) {
 259     return l_offset;
 260   }
 261   idx_t   index = word_index(l_offset);
 262   idx_t r_index = word_index(r_offset);
 263   idx_t res_offset = l_offset;
 264 
 265   // check bits including and to the _left_ of offset's position
 266   idx_t res = map(index) >> bit_in_word(res_offset);
 267   if (res != (uintptr_t)NoBits) {
 268     // find the position of the 1-bit
 269     for (; !(res & 1); res_offset++) {
 270       res = res >> 1;
 271     }
 272     assert(res_offset >= l_offset &&
 273            res_offset < r_offset, "just checking");
 274     return res_offset;
 275   }
 276   // skip over all word length 0-bit runs
 277   for (index++; index < r_index; index++) {
 278     res = map(index);
 279     if (res != (uintptr_t)NoBits) {
 280       // found a 1, return the offset
 281       for (res_offset = bit_index(index); !(res & 1); res_offset++) {
 282         res = res >> 1;
 283       }
 284       assert(res & 1, "tautology; see loop condition");
 285       assert(res_offset >= l_offset && res_offset < r_offset, "just checking");
 286       return res_offset;
 287     }
 288   }
 289   return r_offset;
 290 }
 291 
 292 
 293 // Returns a bit mask for a range of bits [beg, end) within a single word.  Each
 294 // bit in the mask is 0 if the bit is in the range, 1 if not in the range.  The
 295 // returned mask can be used directly to clear the range, or inverted to set the
 296 // range.  Note:  end must not be 0.
 297 inline BitMap::bm_word_t
 298 BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
 299   assert(end != 0, "does not work when end == 0");
 300   assert(beg == end || word_index(beg) == word_index(end - 1),
 301          "must be a single-word range");
 302   bm_word_t mask = bit_mask(beg) - 1;   // low (right) bits
 303   if (bit_in_word(end) != 0) {
 304     mask |= ~(bit_mask(end) - 1);       // high (left) bits
 305   }
 306   return mask;
 307 }
 308 
 309 inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
 310   memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(uintptr_t));
 311 }
 312 
 313 inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
 314   memset(_map + beg, 0, (end - beg) * sizeof(uintptr_t));
 315 }
 316 
 317 inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {
 318   idx_t bit_rounded_up = bit + (BitsPerWord - 1);
 319   // Check for integer arithmetic overflow.
 320   return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();
 321 }
 322 
 323 inline BitMap::idx_t BitMap::get_next_one_offset(idx_t l_offset,
 324                                           idx_t r_offset) const {
 325   return get_next_one_offset_inline(l_offset, r_offset);
 326 }
 327 
 328 inline BitMap::idx_t BitMap::get_next_zero_offset(idx_t l_offset,
 329                                            idx_t r_offset) const {
 330   return get_next_zero_offset_inline(l_offset, r_offset);
 331 }
 332 
 333 inline void BitMap2D::clear() {
 334   _map.clear();
 335 }
 336 
 337 #endif // SHARE_VM_UTILITIES_BITMAP_INLINE_HPP