hotspot/src/share/vm/utilities/bitMap.inline.hpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 1374 4c24294029a9
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
inline bool BitMap::par_set_bit(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
  verify_index(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
  volatile idx_t* const addr = word_addr(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
  const idx_t mask = bit_mask(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
  idx_t old_val = *addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
    const idx_t new_val = old_val | mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    if (new_val == old_val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
      return false;     // Someone else beat us to it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
                                                      (volatile void*) addr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
                                                      (void*) old_val);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    if (cur_val == old_val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
      return true;      // Success.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    old_val = cur_val;  // The value changed, try again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  } while (true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
inline bool BitMap::par_clear_bit(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  verify_index(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  volatile idx_t* const addr = word_addr(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  const idx_t mask = ~bit_mask(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  idx_t old_val = *addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    const idx_t new_val = old_val & mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    if (new_val == old_val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
      return false;     // Someone else beat us to it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    const idx_t cur_val = (idx_t) Atomic::cmpxchg_ptr((void*) new_val,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
                                                      (volatile void*) addr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
                                                      (void*) old_val);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    if (cur_val == old_val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
      return true;      // Success.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    old_val = cur_val;  // The value changed, try again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  } while (true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
inline BitMap::idx_t
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
BitMap::find_next_one_bit(idx_t beg_bit, idx_t end_bit) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  verify_range(beg_bit, end_bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  assert(bit_in_word(end_bit) == 0, "end_bit not word-aligned");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  if (beg_bit == end_bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    return beg_bit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  idx_t   index = word_index(beg_bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  idx_t r_index = word_index(end_bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  idx_t res_bit = beg_bit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  // check bits including and to the _left_ of offset's position
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  idx_t res = map(index) >> bit_in_word(res_bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  if (res != (uintptr_t) NoBits) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    // find the position of the 1-bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    for (; !(res & 1); res_bit++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
      res = res >> 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    assert(res_bit >= beg_bit && res_bit < end_bit, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    return res_bit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  // skip over all word length 0-bit runs
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  for (index++; index < r_index; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    res = map(index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    if (res != (uintptr_t) NoBits) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
      // found a 1, return the offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      for (res_bit = bit_index(index); !(res & 1); res_bit++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
        res = res >> 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      assert(res & 1, "tautology; see loop condition");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
      assert(res_bit >= beg_bit && res_bit < end_bit, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
      return res_bit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  return end_bit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
}