src/share/vm/utilities/bitMap.inline.hpp

changeset 435
a61af66fc99e
child 777
37f87013dfd8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/bitMap.inline.hpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,105 @@
     1.4 +/*
     1.5 + * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +inline bool BitMap::par_set_bit(idx_t bit) {
    1.29 +  verify_index(bit);
    1.30 +  volatile idx_t* const addr = word_addr(bit);
    1.31 +  const idx_t mask = bit_mask(bit);
    1.32 +  idx_t old_val = *addr;
    1.33 +
    1.34 +  do {
    1.35 +    const idx_t new_val = old_val | mask;
    1.36 +    if (new_val == old_val) {
    1.37 +      return false;     // Someone else beat us to it.
    1.38 +    }
    1.39 +    const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
    1.40 +                                                      (volatile void*) addr,
    1.41 +                                                      (void*) old_val);
    1.42 +    if (cur_val == old_val) {
    1.43 +      return true;      // Success.
    1.44 +    }
    1.45 +    old_val = cur_val;  // The value changed, try again.
    1.46 +  } while (true);
    1.47 +}
    1.48 +
    1.49 +inline bool BitMap::par_clear_bit(idx_t bit) {
    1.50 +  verify_index(bit);
    1.51 +  volatile idx_t* const addr = word_addr(bit);
    1.52 +  const idx_t mask = ~bit_mask(bit);
    1.53 +  idx_t old_val = *addr;
    1.54 +
    1.55 +  do {
    1.56 +    const idx_t new_val = old_val & mask;
    1.57 +    if (new_val == old_val) {
    1.58 +      return false;     // Someone else beat us to it.
    1.59 +    }
    1.60 +    const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
    1.61 +                                                      (volatile void*) addr,
    1.62 +                                                      (void*) old_val);
    1.63 +    if (cur_val == old_val) {
    1.64 +      return true;      // Success.
    1.65 +    }
    1.66 +    old_val = cur_val;  // The value changed, try again.
    1.67 +  } while (true);
    1.68 +}
    1.69 +
    1.70 +inline BitMap::idx_t
    1.71 +BitMap::find_next_one_bit(idx_t beg_bit, idx_t end_bit) const
    1.72 +{
    1.73 +  verify_range(beg_bit, end_bit);
    1.74 +  assert(bit_in_word(end_bit) == 0, "end_bit not word-aligned");
    1.75 +
    1.76 +  if (beg_bit == end_bit) {
    1.77 +    return beg_bit;
    1.78 +  }
    1.79 +
    1.80 +  idx_t   index = word_index(beg_bit);
    1.81 +  idx_t r_index = word_index(end_bit);
    1.82 +  idx_t res_bit = beg_bit;
    1.83 +
    1.84 +  // check bits including and to the _left_ of offset's position
    1.85 +  idx_t res = map(index) >> bit_in_word(res_bit);
    1.86 +  if (res != (uintptr_t) NoBits) {
    1.87 +    // find the position of the 1-bit
    1.88 +    for (; !(res & 1); res_bit++) {
    1.89 +      res = res >> 1;
    1.90 +    }
    1.91 +    assert(res_bit >= beg_bit && res_bit < end_bit, "just checking");
    1.92 +    return res_bit;
    1.93 +  }
    1.94 +  // skip over all word length 0-bit runs
    1.95 +  for (index++; index < r_index; index++) {
    1.96 +    res = map(index);
    1.97 +    if (res != (uintptr_t) NoBits) {
    1.98 +      // found a 1, return the offset
    1.99 +      for (res_bit = bit_index(index); !(res & 1); res_bit++) {
   1.100 +        res = res >> 1;
   1.101 +      }
   1.102 +      assert(res & 1, "tautology; see loop condition");
   1.103 +      assert(res_bit >= beg_bit && res_bit < end_bit, "just checking");
   1.104 +      return res_bit;
   1.105 +    }
   1.106 +  }
   1.107 +  return end_bit;
   1.108 +}

mercurial