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