src/hotspot/share/utilities/bitMap.inline.hpp
author stefank
Mon, 25 Nov 2019 12:22:13 +0100
changeset 59247 56bf71d64d51
parent 58961 5d462d4b7a8b
child 59252 623722a6aeb9
permissions -rw-r--r--
8234562: Move OrderAccess::release_store*/load_acquire to Atomic Reviewed-by: rehn, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52485
diff changeset
     2
 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
1
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
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52485
diff changeset
    25
#ifndef SHARE_UTILITIES_BITMAP_INLINE_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52485
diff changeset
    26
#define SHARE_UTILITIES_BITMAP_INLINE_HPP
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 39219
diff changeset
    28
#include "runtime/atomic.hpp"
58961
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    29
#include "runtime/orderAccess.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "utilities/bitMap.hpp"
52394
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
    31
#include "utilities/count_trailing_zeros.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    33
inline void BitMap::set_bit(idx_t bit) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    34
  verify_index(bit);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    35
  *word_addr(bit) |= bit_mask(bit);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    36
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    37
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    38
inline void BitMap::clear_bit(idx_t bit) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    39
  verify_index(bit);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    40
  *word_addr(bit) &= ~bit_mask(bit);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    41
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    42
58961
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    43
inline const BitMap::bm_word_t BitMap::load_word_ordered(const volatile bm_word_t* const addr, atomic_memory_order memory_order) {
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    44
  if (memory_order == memory_order_relaxed || memory_order == memory_order_release) {
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    45
    return Atomic::load(addr);
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    46
  } else {
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    47
    assert(memory_order == memory_order_acq_rel ||
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    48
           memory_order == memory_order_acquire ||
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    49
           memory_order == memory_order_conservative,
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    50
           "unexpected memory ordering");
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 58961
diff changeset
    51
    return Atomic::load_acquire(addr);
58961
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    52
  }
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    53
}
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    54
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    55
inline bool BitMap::par_at(idx_t index, atomic_memory_order memory_order) const {
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    56
  verify_index(index);
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    57
  assert(memory_order == memory_order_acquire ||
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    58
         memory_order == memory_order_relaxed,
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    59
         "unexpected memory ordering");
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    60
  const volatile bm_word_t* const addr = word_addr(index);
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    61
  return (load_word_ordered(addr, memory_order) & bit_mask(index)) != 0;
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    62
}
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    63
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    64
inline bool BitMap::par_set_bit(idx_t bit, atomic_memory_order memory_order) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  verify_index(bit);
19998
c18a7b05127a 8024914: Swapped usage of idx_t and bm_word_t types in bitMap.inline.hpp
tschatzl
parents: 11574
diff changeset
    66
  volatile bm_word_t* const addr = word_addr(bit);
c18a7b05127a 8024914: Swapped usage of idx_t and bm_word_t types in bitMap.inline.hpp
tschatzl
parents: 11574
diff changeset
    67
  const bm_word_t mask = bit_mask(bit);
58961
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    68
  bm_word_t old_val = load_word_ordered(addr, memory_order);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  do {
19998
c18a7b05127a 8024914: Swapped usage of idx_t and bm_word_t types in bitMap.inline.hpp
tschatzl
parents: 11574
diff changeset
    71
    const bm_word_t new_val = old_val | mask;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    if (new_val == old_val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
      return false;     // Someone else beat us to it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    }
58961
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    75
    const bm_word_t cur_val = Atomic::cmpxchg(new_val, addr, old_val, memory_order);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    if (cur_val == old_val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
      return true;      // Success.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    old_val = cur_val;  // The value changed, try again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  } while (true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
58961
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    83
inline bool BitMap::par_clear_bit(idx_t bit, atomic_memory_order memory_order) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  verify_index(bit);
19998
c18a7b05127a 8024914: Swapped usage of idx_t and bm_word_t types in bitMap.inline.hpp
tschatzl
parents: 11574
diff changeset
    85
  volatile bm_word_t* const addr = word_addr(bit);
c18a7b05127a 8024914: Swapped usage of idx_t and bm_word_t types in bitMap.inline.hpp
tschatzl
parents: 11574
diff changeset
    86
  const bm_word_t mask = ~bit_mask(bit);
58961
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    87
  bm_word_t old_val = load_word_ordered(addr, memory_order);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  do {
19998
c18a7b05127a 8024914: Swapped usage of idx_t and bm_word_t types in bitMap.inline.hpp
tschatzl
parents: 11574
diff changeset
    90
    const bm_word_t new_val = old_val & mask;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    if (new_val == old_val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
      return false;     // Someone else beat us to it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    }
58961
5d462d4b7a8b 8233073: Make BitMap accessors more memory ordering friendly
eosterlund
parents: 53244
diff changeset
    94
    const bm_word_t cur_val = Atomic::cmpxchg(new_val, addr, old_val, memory_order);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    if (cur_val == old_val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      return true;      // Success.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    old_val = cur_val;  // The value changed, try again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  } while (true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   102
inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   103
  if (hint == small_range && end - beg == 1) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   104
    set_bit(beg);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   105
  } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   106
    if (hint == large_range) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   107
      set_large_range(beg, end);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   108
    } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   109
      set_range(beg, end);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   110
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   111
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   112
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   113
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   114
inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
37264
d9b0f2b53b73 8152188: Allow CMSBitMapYieldQuantum for BitMap::clear_range and clear_large_range
sangheki
parents: 37059
diff changeset
   115
  if (end - beg == 1) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   116
    clear_bit(beg);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   117
  } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   118
    if (hint == large_range) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   119
      clear_large_range(beg, end);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   120
    } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   121
      clear_range(beg, end);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   122
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   123
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   124
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   125
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   126
inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   127
  if (hint == small_range && end - beg == 1) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   128
    par_at_put(beg, true);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   129
  } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   130
    if (hint == large_range) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   131
      par_at_put_large_range(beg, end, true);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   132
    } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   133
      par_at_put_range(beg, end, true);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   134
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   135
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   136
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   138
inline void BitMap::set_range_of_words(idx_t beg, idx_t end) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   139
  bm_word_t* map = _map;
26937
dd2b0f6de283 8059474: Clean up vm/utilities/Bitmap type uses
shade
parents: 25351
diff changeset
   140
  for (idx_t i = beg; i < end; ++i) map[i] = ~(bm_word_t)0;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   141
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   142
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 37264
diff changeset
   143
inline void BitMap::clear_range_of_words(bm_word_t* map, idx_t beg, idx_t end) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   144
  for (idx_t i = beg; i < end; ++i) map[i] = 0;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   145
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   146
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 37264
diff changeset
   147
inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 37264
diff changeset
   148
  clear_range_of_words(_map, beg, end);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 37264
diff changeset
   149
}
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   150
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   151
inline void BitMap::clear() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   152
  clear_range_of_words(0, size_in_words());
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   153
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   154
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   155
inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   156
  if (hint == small_range && end - beg == 1) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   157
    par_at_put(beg, false);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   158
  } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   159
    if (hint == large_range) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   160
      par_at_put_large_range(beg, end, false);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   161
    } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   162
      par_at_put_range(beg, end, false);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   163
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   164
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   165
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   166
52394
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   167
template<BitMap::bm_word_t flip, bool aligned_right>
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   168
inline BitMap::idx_t BitMap::get_next_bit_impl(idx_t l_index, idx_t r_index) const {
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   169
  STATIC_ASSERT(flip == find_ones_flip || flip == find_zeros_flip);
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   170
  verify_range(l_index, r_index);
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   171
  assert(!aligned_right || is_word_aligned(r_index), "r_index not aligned");
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   172
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   173
  // The first word often contains an interesting bit, either due to
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   174
  // density or because of features of the calling algorithm.  So it's
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   175
  // important to examine that first word with a minimum of fuss,
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   176
  // minimizing setup time for later words that will be wasted if the
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   177
  // first word is indeed interesting.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   178
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   179
  // The benefit from aligned_right being true is relatively small.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   180
  // It saves a couple instructions in the setup for the word search
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   181
  // loop.  It also eliminates the range check on the final result.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   182
  // However, callers often have a comparison with r_index, and
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   183
  // inlining often allows the two comparisons to be combined; it is
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   184
  // important when !aligned_right that return paths either return
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   185
  // r_index or a value dominated by a comparison with r_index.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   186
  // aligned_right is still helpful when the caller doesn't have a
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   187
  // range check because features of the calling algorithm guarantee
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   188
  // an interesting bit will be present.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   189
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   190
  if (l_index < r_index) {
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   191
    // Get the word containing l_index, and shift out low bits.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   192
    idx_t index = word_index(l_index);
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   193
    bm_word_t cword = (map(index) ^ flip) >> bit_in_word(l_index);
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   194
    if ((cword & 1) != 0) {
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   195
      // The first bit is similarly often interesting. When it matters
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   196
      // (density or features of the calling algorithm make it likely
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   197
      // the first bit is set), going straight to the next clause compares
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   198
      // poorly with doing this check first; count_trailing_zeros can be
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   199
      // relatively expensive, plus there is the additional range check.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   200
      // But when the first bit isn't set, the cost of having tested for
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   201
      // it is relatively small compared to the rest of the search.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   202
      return l_index;
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   203
    } else if (cword != 0) {
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   204
      // Flipped and shifted first word is non-zero.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   205
      idx_t result = l_index + count_trailing_zeros(cword);
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   206
      if (aligned_right || (result < r_index)) return result;
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   207
      // Result is beyond range bound; return r_index.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   208
    } else {
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   209
      // Flipped and shifted first word is zero.  Word search through
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   210
      // aligned up r_index for a non-zero flipped word.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   211
      idx_t limit = aligned_right
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   212
        ? word_index(r_index)
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   213
        : (word_index(r_index - 1) + 1); // Align up, knowing r_index > 0.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   214
      while (++index < limit) {
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   215
        cword = map(index) ^ flip;
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   216
        if (cword != 0) {
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   217
          idx_t result = bit_index(index) + count_trailing_zeros(cword);
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   218
          if (aligned_right || (result < r_index)) return result;
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   219
          // Result is beyond range bound; return r_index.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   220
          assert((index + 1) == limit, "invariant");
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   221
          break;
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   222
        }
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   223
      }
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   224
      // No bits in range; return r_index.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   225
    }
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   226
  }
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   227
  return r_index;
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   228
}
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   229
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   230
inline BitMap::idx_t
46402
8147e17ad6fa 8179181: Cleanup BitMap search API
kbarrett
parents: 40655
diff changeset
   231
BitMap::get_next_one_offset(idx_t l_offset, idx_t r_offset) const {
52394
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   232
  return get_next_bit_impl<find_ones_flip, false>(l_offset, r_offset);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   233
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   234
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   235
inline BitMap::idx_t
46402
8147e17ad6fa 8179181: Cleanup BitMap search API
kbarrett
parents: 40655
diff changeset
   236
BitMap::get_next_zero_offset(idx_t l_offset, idx_t r_offset) const {
52394
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   237
  return get_next_bit_impl<find_zeros_flip, false>(l_offset, r_offset);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   238
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   239
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   240
inline BitMap::idx_t
52394
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   241
BitMap::get_next_one_offset_aligned_right(idx_t l_offset, idx_t r_offset) const {
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 47216
diff changeset
   242
  return get_next_bit_impl<find_ones_flip, true>(l_offset, r_offset);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
}
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   244
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   245
// Returns a bit mask for a range of bits [beg, end) within a single word.  Each
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   246
// bit in the mask is 0 if the bit is in the range, 1 if not in the range.  The
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   247
// returned mask can be used directly to clear the range, or inverted to set the
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   248
// range.  Note:  end must not be 0.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   249
inline BitMap::bm_word_t
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   250
BitMap::inverted_bit_mask_for_range(idx_t beg, idx_t end) const {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   251
  assert(end != 0, "does not work when end == 0");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   252
  assert(beg == end || word_index(beg) == word_index(end - 1),
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   253
         "must be a single-word range");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   254
  bm_word_t mask = bit_mask(beg) - 1;   // low (right) bits
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   255
  if (bit_in_word(end) != 0) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   256
    mask |= ~(bit_mask(end) - 1);       // high (left) bits
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   257
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   258
  return mask;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   259
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   260
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   261
inline void BitMap::set_large_range_of_words(idx_t beg, idx_t end) {
52485
e5534cc91a10 8211926: Catastrophic size_t underflow in BitMap::*_large methods
shade
parents: 52394
diff changeset
   262
  assert(beg <= end, "underflow");
26937
dd2b0f6de283 8059474: Clean up vm/utilities/Bitmap type uses
shade
parents: 25351
diff changeset
   263
  memset(_map + beg, ~(unsigned char)0, (end - beg) * sizeof(bm_word_t));
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   264
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   265
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   266
inline void BitMap::clear_large_range_of_words(idx_t beg, idx_t end) {
52485
e5534cc91a10 8211926: Catastrophic size_t underflow in BitMap::*_large methods
shade
parents: 52394
diff changeset
   267
  assert(beg <= end, "underflow");
26937
dd2b0f6de283 8059474: Clean up vm/utilities/Bitmap type uses
shade
parents: 25351
diff changeset
   268
  memset(_map + beg, 0, (end - beg) * sizeof(bm_word_t));
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   269
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   270
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   271
inline BitMap::idx_t BitMap::word_index_round_up(idx_t bit) const {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   272
  idx_t bit_rounded_up = bit + (BitsPerWord - 1);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   273
  // Check for integer arithmetic overflow.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   274
  return bit_rounded_up > bit ? word_index(bit_rounded_up) : size_in_words();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   275
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   276
37059
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   277
inline bool BitMap2D::is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) {
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   278
  verify_bit_within_slot_index(bit_within_slot_index);
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   279
  return (bit_index(slot_index, bit_within_slot_index) < size_in_bits());
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   280
}
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   281
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   282
inline bool BitMap2D::at(idx_t slot_index, idx_t bit_within_slot_index) const {
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   283
  verify_bit_within_slot_index(bit_within_slot_index);
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   284
  return _map.at(bit_index(slot_index, bit_within_slot_index));
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   285
}
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   286
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   287
inline void BitMap2D::set_bit(idx_t slot_index, idx_t bit_within_slot_index) {
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   288
  verify_bit_within_slot_index(bit_within_slot_index);
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   289
  _map.set_bit(bit_index(slot_index, bit_within_slot_index));
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   290
}
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   291
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   292
inline void BitMap2D::clear_bit(idx_t slot_index, idx_t bit_within_slot_index) {
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   293
  verify_bit_within_slot_index(bit_within_slot_index);
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   294
  _map.clear_bit(bit_index(slot_index, bit_within_slot_index));
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   295
}
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   296
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   297
inline void BitMap2D::at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   298
  verify_bit_within_slot_index(bit_within_slot_index);
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   299
  _map.at_put(bit_index(slot_index, bit_within_slot_index), value);
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   300
}
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   301
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   302
inline void BitMap2D::at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   303
  verify_bit_within_slot_index(bit_within_slot_index);
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 37264
diff changeset
   304
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 37264
diff changeset
   305
  idx_t bit = bit_index(slot_index, bit_within_slot_index);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 37264
diff changeset
   306
  if (bit >= _map.size()) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 37264
diff changeset
   307
    _map.resize(2 * MAX2(_map.size(), bit));
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 37264
diff changeset
   308
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 37264
diff changeset
   309
  _map.at_put(bit, value);
37059
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   310
}
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 26937
diff changeset
   311
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52485
diff changeset
   312
#endif // SHARE_UTILITIES_BITMAP_INLINE_HPP