hotspot/src/share/vm/utilities/bitMap.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 1997-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
// Closure for iterating over BitMaps
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
class BitMapClosure VALUE_OBJ_CLASS_SPEC {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
  // Callback when bit in map is set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
  virtual void do_bit(size_t offset) = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// Operations for bitmaps represented as arrays of unsigned 32- or 64-bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// integers (uintptr_t).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// Bit offsets are numbered from 0 to size-1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
class BitMap VALUE_OBJ_CLASS_SPEC {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  friend class BitMap2D;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  typedef size_t idx_t;         // Type used for bit and word indices.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  // Hints for range sizes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  typedef enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    unknown_range, small_range, large_range
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  } RangeSizeHint;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  idx_t* _map;     // First word in bitmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  idx_t  _size;    // Size of bitmap (in bits)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  // Puts the given value at the given offset, using resize() to size
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  // the bitmap appropriately if needed using factor-of-two expansion.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  void at_put_grow(idx_t index, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  // Return the position of bit within the word that contains it (e.g., if
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  // bitmap words are 32 bits, return a number 0 <= n <= 31).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  // Return a mask that will select the specified bit, when applied to the word
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  // containing the bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  static idx_t bit_mask(idx_t bit)    { return (idx_t)1 << bit_in_word(bit); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  // Return the index of the word containing the specified bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  static idx_t word_index(idx_t bit)  { return bit >> LogBitsPerWord; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  // Return the bit number of the first bit in the specified word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  static idx_t bit_index(idx_t word)  { return word << LogBitsPerWord; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // Return the array of bitmap words, or a specific word from it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  idx_t* map() const           { return _map; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  idx_t  map(idx_t word) const { return _map[word]; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  // Return a pointer to the word containing the specified bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  idx_t* word_addr(idx_t bit) const { return map() + word_index(bit); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  // Set a word to a specified value or to all ones; clear a word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  void set_word  (idx_t word, idx_t val) { _map[word] = val; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  void set_word  (idx_t word)            { set_word(word, ~(uintptr_t)0); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  void clear_word(idx_t word)            { _map[word] = 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  // Utilities for ranges of bits.  Ranges are half-open [beg, end).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  // Ranges within a single word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  inline idx_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  inline void  set_range_within_word      (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  inline void  clear_range_within_word    (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  inline void  par_put_range_within_word  (idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  // Ranges spanning entire words.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  inline void      set_range_of_words         (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  inline void      clear_range_of_words       (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  inline void      set_large_range_of_words   (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  inline void      clear_large_range_of_words (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // The index of the first full word in a range.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  inline idx_t word_index_round_up(idx_t bit) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  // Verification, statistics.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  void verify_index(idx_t index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    assert(index < _size, "BitMap index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  void verify_range(idx_t beg_index, idx_t end_index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    assert(beg_index <= end_index, "BitMap range error");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    // Note that [0,0) and [size,size) are both valid ranges.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    if (end_index != _size)  verify_index(end_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  // Constructs a bitmap with no map, and size 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  BitMap() : _map(NULL), _size(0) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  // Construction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  BitMap(idx_t* map, idx_t size_in_bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  // Allocates necessary data structure in resource area
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  BitMap(idx_t size_in_bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  void set_map(idx_t* map)          { _map = map; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  void set_size(idx_t size_in_bits) { _size = size_in_bits; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  // Allocates necessary data structure in resource area.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  // Preserves state currently in bit map by copying data.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  // Zeros any newly-addressable bits.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  // Does not perform any frees (i.e., of current _map).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  void resize(idx_t size_in_bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  // Accessing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  idx_t size() const                    { return _size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  idx_t size_in_words() const           {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    return word_index(size() + BitsPerWord - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  bool at(idx_t index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    verify_index(index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    return (*word_addr(index) & bit_mask(index)) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  // Align bit index up or down to the next bitmap word boundary, or check
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  // alignment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  static idx_t word_align_up(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    return align_size_up(bit, BitsPerWord);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  static idx_t word_align_down(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    return align_size_down(bit, BitsPerWord);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  static bool is_word_aligned(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    return word_align_up(bit) == bit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  // Set or clear the specified bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  inline void set_bit(idx_t bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  inline void clear_bit(idx_t bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  // Atomically set or clear the specified bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  inline bool par_set_bit(idx_t bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  inline bool par_clear_bit(idx_t bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  // Put the given value at the given offset. The parallel version
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  // will CAS the value into the bitmap and is quite a bit slower.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  // The parallel version also returns a value indicating if the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  // calling thread was the one that changed the value of the bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  void at_put(idx_t index, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  bool par_at_put(idx_t index, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  // Update a range of bits.  Ranges are half-open [beg, end).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  void set_range   (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  void clear_range (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  void set_large_range   (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  void clear_large_range (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  void at_put_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  void par_at_put_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  void at_put_large_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  void par_at_put_large_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  // Update a range of bits, using a hint about the size.  Currently only
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  // inlines the predominant case of a 1-bit range.  Works best when hint is a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  // compile-time constant.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  inline void set_range(idx_t beg, idx_t end, RangeSizeHint hint);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  inline void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  inline void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  inline void par_clear_range  (idx_t beg, idx_t end, RangeSizeHint hint);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  // Clearing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  void clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  void clear_large();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  // Iteration support
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  void iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  inline void iterate(BitMapClosure* blk) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    // call the version that takes an interval
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    iterate(blk, 0, size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  // Looking for 1's and 0's to the "right"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  idx_t get_next_one_offset(idx_t offset) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    return get_next_one_offset(offset, size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  idx_t get_next_zero_offset(idx_t offset) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
    return get_next_zero_offset(offset, size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  // Find the next one bit in the range [beg_bit, end_bit), or return end_bit if
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  // no one bit is found.  Equivalent to get_next_one_offset(), but inline for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  // use in performance-critical code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  inline idx_t find_next_one_bit(idx_t beg_bit, idx_t end_bit) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  // Set operations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  void set_union(BitMap bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  void set_difference(BitMap bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  void set_intersection(BitMap bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  // Returns true iff "this" is a superset of "bits".
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  bool contains(const BitMap bits) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  // Returns true iff "this and "bits" have a non-empty intersection.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  bool intersects(const BitMap bits) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  // Returns result of whether this map changed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  // during the operation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  bool set_union_with_result(BitMap bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  bool set_difference_with_result(BitMap bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  bool set_intersection_with_result(BitMap bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  void set_from(BitMap bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  bool is_same(BitMap bits);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  // Test if all bits are set or cleared
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  bool is_full() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
  bool is_empty() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  // Printing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  void print_on(outputStream* st) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
inline void BitMap::set_bit(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  verify_index(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  *word_addr(bit) |= bit_mask(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
inline void BitMap::clear_bit(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  verify_index(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  *word_addr(bit) &= ~bit_mask(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
inline void BitMap::set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  if (hint == small_range && end - beg == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    set_bit(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    if (hint == large_range) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
      set_large_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
      set_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
inline void BitMap::clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  if (hint == small_range && end - beg == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
    clear_bit(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
    if (hint == large_range) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
      clear_large_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
      clear_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
inline void BitMap::par_set_range(idx_t beg, idx_t end, RangeSizeHint hint) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  if (hint == small_range && end - beg == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
    par_at_put(beg, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    if (hint == large_range) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
      par_at_put_large_range(beg, end, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
      par_at_put_range(beg, end, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
// Convenience class wrapping BitMap which provides multiple bits per slot.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
class BitMap2D VALUE_OBJ_CLASS_SPEC {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  typedef size_t idx_t;         // Type used for bit and word indices.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  BitMap _map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  idx_t  _bits_per_slot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    return slot_index * _bits_per_slot + bit_within_slot_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  void verify_bit_within_slot_index(idx_t index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  // Construction. bits_per_slot must be greater than 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  BitMap2D(uintptr_t* map, idx_t size_in_slots, idx_t bits_per_slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  BitMap2D(idx_t size_in_slots, idx_t bits_per_slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  idx_t size_in_bits() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
    return _map.size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  // Returns number of full slots that have been allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  idx_t size_in_slots() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
    // Round down
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    return _map.size() / _bits_per_slot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    verify_bit_within_slot_index(bit_within_slot_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    return (bit_index(slot_index, bit_within_slot_index) < size_in_bits());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  bool at(idx_t slot_index, idx_t bit_within_slot_index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
    verify_bit_within_slot_index(bit_within_slot_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
    return _map.at(bit_index(slot_index, bit_within_slot_index));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  void set_bit(idx_t slot_index, idx_t bit_within_slot_index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    verify_bit_within_slot_index(bit_within_slot_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
    _map.set_bit(bit_index(slot_index, bit_within_slot_index));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    verify_bit_within_slot_index(bit_within_slot_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
    _map.clear_bit(bit_index(slot_index, bit_within_slot_index));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    verify_bit_within_slot_index(bit_within_slot_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
    _map.at_put(bit_index(slot_index, bit_within_slot_index), value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    verify_bit_within_slot_index(bit_within_slot_index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  void clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    _map.clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
inline void BitMap::set_range_of_words(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  uintptr_t* map = _map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
  for (idx_t i = beg; i < end; ++i) map[i] = ~(uintptr_t)0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
inline void BitMap::clear_range_of_words(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  uintptr_t* map = _map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  for (idx_t i = beg; i < end; ++i) map[i] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
inline void BitMap::clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  clear_range_of_words(0, size_in_words());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
inline void BitMap::par_clear_range(idx_t beg, idx_t end, RangeSizeHint hint) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  if (hint == small_range && end - beg == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    par_at_put(beg, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    if (hint == large_range) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
      par_at_put_large_range(beg, end, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
      par_at_put_range(beg, end, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
}