hotspot/src/share/vm/utilities/bitMap.hpp
author stefank
Tue, 03 May 2016 22:45:27 +0200
changeset 38177 b0c9cb06506b
parent 38102 839593075df1
child 39219 1b33aa56ed18
permissions -rw-r--r--
8141501: Problems with BitMap buffer management Reviewed-by: pliden, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
37414
2672ba9af0dc 8151386: Extract card live data out of G1ConcurrentMark
tschatzl
parents: 37413
diff changeset
     2
 * Copyright (c) 1997, 2016, 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
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_UTILITIES_BITMAP_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_UTILITIES_BITMAP_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    30
// Forward decl;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    31
class BitMapClosure;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    33
// Operations for bitmaps represented as arrays of unsigned integers.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    34
// Bit offsets are numbered from 0 to size-1.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    36
// The "abstract" base BitMap class.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    37
//
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    38
// The constructor and destructor are protected to prevent
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    39
// creation of BitMap instances outside of the BitMap class.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    40
//
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    41
// The BitMap class doesn't use virtual calls on purpose,
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    42
// this ensures that we don't get a vtable unnecessarily.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    43
//
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    44
// The allocation of the backing storage for the BitMap are handled by
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    45
// the subclasses. BitMap doesn't allocate or delete backing storage.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
class BitMap VALUE_OBJ_CLASS_SPEC {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  friend class BitMap2D;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  typedef size_t idx_t;         // Type used for bit and word indices.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    51
  typedef uintptr_t bm_word_t;  // Element type of array that represents
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    52
                                // the bitmap.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  // Hints for range sizes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  typedef enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    unknown_range, small_range, large_range
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  } RangeSizeHint;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
 private:
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    60
  bm_word_t* _map;     // First word in bitmap
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    61
  idx_t      _size;    // Size of bitmap (in bits)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  // Return the position of bit within the word that contains it (e.g., if
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  // bitmap words are 32 bits, return a number 0 <= n <= 31).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  // Return a mask that will select the specified bit, when applied to the word
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  // containing the bit.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    70
  static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  // Return the index of the word containing the specified bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  static idx_t word_index(idx_t bit)  { return bit >> LogBitsPerWord; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  // Return the bit number of the first bit in the specified word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  static idx_t bit_index(idx_t word)  { return word << LogBitsPerWord; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  // Return the array of bitmap words, or a specific word from it.
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
    79
  bm_word_t* map()                 { return _map; }
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
    80
  const bm_word_t* map() const     { return _map; }
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    81
  bm_word_t  map(idx_t word) const { return _map[word]; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  // Return a pointer to the word containing the specified bit.
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
    84
  bm_word_t* word_addr(idx_t bit)             { return map() + word_index(bit); }
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
    85
  const bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  // Set a word to a specified value or to all ones; clear a word.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    88
  void set_word  (idx_t word, bm_word_t val) { _map[word] = val; }
26937
dd2b0f6de283 8059474: Clean up vm/utilities/Bitmap type uses
shade
parents: 22234
diff changeset
    89
  void set_word  (idx_t word)            { set_word(word, ~(bm_word_t)0); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  void clear_word(idx_t word)            { _map[word] = 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  // Utilities for ranges of bits.  Ranges are half-open [beg, end).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  // Ranges within a single word.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    95
  bm_word_t 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
    96
  void  set_range_within_word      (idx_t beg, idx_t end);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    97
  void  clear_range_within_word    (idx_t beg, idx_t end);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    98
  void  par_put_range_within_word  (idx_t beg, idx_t end, bool value);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  // Ranges spanning entire words.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   101
  void      set_range_of_words         (idx_t beg, idx_t end);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   102
  void      clear_range_of_words       (idx_t beg, idx_t end);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   103
  void      set_large_range_of_words   (idx_t beg, idx_t end);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   104
  void      clear_large_range_of_words (idx_t beg, idx_t end);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   106
  static void clear_range_of_words(bm_word_t* map, idx_t beg, idx_t end);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   107
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  // The index of the first full word in a range.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   109
  idx_t word_index_round_up(idx_t bit) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
2998
b501bd305780 6849716: BitMap - performance regression introduced with G1
jcoomes
parents: 1374
diff changeset
   111
  // Verification.
37059
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   112
  void verify_index(idx_t index) const NOT_DEBUG_RETURN;
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   113
  void verify_range(idx_t beg_index, idx_t end_index) const NOT_DEBUG_RETURN;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
2998
b501bd305780 6849716: BitMap - performance regression introduced with G1
jcoomes
parents: 1374
diff changeset
   115
  // Statistics.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   116
  static idx_t* _pop_count_table;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   117
  static void init_pop_count_table();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   118
  static idx_t num_set_bits(bm_word_t w);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   119
  static idx_t num_set_bits_from_table(unsigned char c);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   121
  // Allocation Helpers.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   122
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   123
  // Allocates and clears the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   124
  template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   125
  static bm_word_t* allocate(const Allocator&, idx_t size_in_bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   127
  // Reallocates and clears the new bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   128
  template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   129
  static bm_word_t* reallocate(const Allocator&, bm_word_t* map, idx_t old_size_in_bits, idx_t new_size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   130
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   131
  // Free the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   132
  template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   133
  static void free(const Allocator&, bm_word_t* map, idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   134
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   135
  // Protected functions, that are used by BitMap sub-classes that support them.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   137
  // Resize the backing bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   138
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   139
  // Old bits are transfered to the new memory
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   140
  // and the extended memory is cleared.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   141
  template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   142
  void resize(const Allocator& allocator, idx_t new_size_in_bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   144
  // Set up and clear the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   145
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   146
  // Precondition: The bitmap was default constructed and has
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   147
  // not yet had memory allocated via resize or (re)initialize.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   148
  template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   149
  void initialize(const Allocator& allocator, idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   150
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   151
  // Set up and clear the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   152
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   153
  // Can be called on previously initialized bitmaps.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   154
  template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   155
  void reinitialize(const Allocator& allocator, idx_t new_size_in_bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   157
  // Set the map and size.
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   158
  void update(bm_word_t* map, idx_t size) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   159
    _map = map;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   160
    _size = size;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   161
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   163
  // Protected constructor and destructor.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   164
  BitMap(bm_word_t* map, idx_t size_in_bits) : _map(map), _size(size_in_bits) {}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   165
  ~BitMap() {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   167
 public:
37413
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   168
  // Pretouch the entire range of memory this BitMap covers.
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   169
  void pretouch();
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   170
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  // Accessing
37414
2672ba9af0dc 8151386: Extract card live data out of G1ConcurrentMark
tschatzl
parents: 37413
diff changeset
   172
  static idx_t calc_size_in_words(size_t size_in_bits) {
37413
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   173
    return word_index(size_in_bits + BitsPerWord - 1);
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   174
  }
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   175
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   176
  static idx_t calc_size_in_bytes(size_t size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   177
    return calc_size_in_words(size_in_bits) * BytesPerWord;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   178
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   179
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   180
  idx_t size() const          { return _size; }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   181
  idx_t size_in_words() const { return calc_size_in_words(size()); }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   182
  idx_t size_in_bytes() const { return calc_size_in_bytes(size()); }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   183
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  bool at(idx_t index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    verify_index(index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    return (*word_addr(index) & bit_mask(index)) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  // Align bit index up or down to the next bitmap word boundary, or check
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  // alignment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  static idx_t word_align_up(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    return align_size_up(bit, BitsPerWord);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  static idx_t word_align_down(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    return align_size_down(bit, BitsPerWord);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  static bool is_word_aligned(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    return word_align_up(bit) == bit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  // Set or clear the specified bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  inline void set_bit(idx_t bit);
9994
efb74fdbd46e 7046558: G1: concurrent marking optimizations
tonyp
parents: 7397
diff changeset
   203
  inline void clear_bit(idx_t bit);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  // Atomically set or clear the specified bit.
9994
efb74fdbd46e 7046558: G1: concurrent marking optimizations
tonyp
parents: 7397
diff changeset
   206
  inline bool par_set_bit(idx_t bit);
efb74fdbd46e 7046558: G1: concurrent marking optimizations
tonyp
parents: 7397
diff changeset
   207
  inline bool par_clear_bit(idx_t bit);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  // Put the given value at the given offset. The parallel version
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  // will CAS the value into the bitmap and is quite a bit slower.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  // The parallel version also returns a value indicating if the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  // calling thread was the one that changed the value of the bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  void at_put(idx_t index, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  bool par_at_put(idx_t index, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  // Update a range of bits.  Ranges are half-open [beg, end).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  void set_range   (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  void clear_range (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  void set_large_range   (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  void clear_large_range (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  void at_put_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  void par_at_put_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  void at_put_large_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  void par_at_put_large_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  // Update a range of bits, using a hint about the size.  Currently only
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  // inlines the predominant case of a 1-bit range.  Works best when hint is a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  // compile-time constant.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   229
  void set_range(idx_t beg, idx_t end, RangeSizeHint hint);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   230
  void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   231
  void 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
   232
  void 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
   233
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  // Clearing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  void clear_large();
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   236
  inline void clear();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   238
  // Iteration support.  Returns "true" if the iteration completed, false
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   239
  // if the iteration terminated early (because the closure "blk" returned
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   240
  // false).
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   241
  bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   242
  bool iterate(BitMapClosure* blk) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
    // call the version that takes an interval
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   244
    return iterate(blk, 0, size());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   247
  // Looking for 1's and 0's at indices equal to or greater than "l_index",
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   248
  // stopping if none has been found before "r_index", and returning
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   249
  // "r_index" (which must be at most "size") in that case.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   250
  idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   251
  idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   252
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   253
  // Like "get_next_one_offset_inline", except requires that "r_index" is
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   254
  // aligned to bitsizeof(bm_word_t).
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   255
  idx_t get_next_one_offset_inline_aligned_right(idx_t l_index,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   256
                                                        idx_t r_index) const;
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
  // Non-inline versionsof the above.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  idx_t get_next_one_offset(idx_t offset) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    return get_next_one_offset(offset, size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  idx_t get_next_zero_offset(idx_t offset) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    return get_next_zero_offset(offset, size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   269
  // Returns the number of bits set in the bitmap.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   270
  idx_t count_one_bits() const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  // Set operations.
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   273
  void set_union(const BitMap& bits);
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   274
  void set_difference(const BitMap& bits);
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   275
  void set_intersection(const BitMap& bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  // Returns true iff "this" is a superset of "bits".
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   277
  bool contains(const BitMap& bits) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  // Returns true iff "this and "bits" have a non-empty intersection.
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   279
  bool intersects(const BitMap& bits) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  // Returns result of whether this map changed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  // during the operation
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   283
  bool set_union_with_result(const BitMap& bits);
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   284
  bool set_difference_with_result(const BitMap& bits);
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   285
  bool set_intersection_with_result(const BitMap& bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   287
  // Requires the submap of "bits" starting at offset to be at least as
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   288
  // large as "this".  Modifies "this" to be the intersection of its
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   289
  // current contents and the submap of "bits" starting at "offset" of the
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   290
  // same length as "this."
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   291
  // (For expedience, currently requires the offset to be aligned to the
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   292
  // bitsize of a uintptr_t.  This should go away in the future though it
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   293
  // will probably remain a good case to optimize.)
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   294
  void set_intersection_at_offset(const BitMap& bits, idx_t offset);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   295
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   296
  void set_from(const BitMap& bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   298
  bool is_same(const BitMap& bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  // Test if all bits are set or cleared
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  bool is_full() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  bool is_empty() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
16685
41c34debcde0 8011872: Include Bit Map addresses in the hs_err files
stefank
parents: 12272
diff changeset
   304
  void print_on_error(outputStream* st, const char* prefix) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  // Printing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  void print_on(outputStream* st) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   313
// A concrete implementation of the the "abstract" BitMap class.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   314
//
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   315
// The BitMapView is used when the backing storage is managed externally.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   316
class BitMapView : public BitMap {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   317
 public:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   318
  BitMapView() : BitMap(NULL, 0) {}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   319
  BitMapView(bm_word_t* map, idx_t size_in_bits) : BitMap(map, size_in_bits) {}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   320
};
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   321
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   322
// A BitMap with storage in a ResourceArea.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   323
class ResourceBitMap : public BitMap {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   324
  friend class TestBitMap;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   325
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   326
 public:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   327
  ResourceBitMap() : BitMap(NULL, 0) {}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   328
  // Clears the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   329
  ResourceBitMap(idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   330
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   331
  // Resize the backing bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   332
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   333
  // Old bits are transfered to the new memory
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   334
  // and the extended memory is cleared.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   335
  void resize(idx_t new_size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   336
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   337
  // Set up and clear the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   338
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   339
  // Precondition: The bitmap was default constructed and has
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   340
  // not yet had memory allocated via resize or initialize.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   341
  void initialize(idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   342
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   343
  // Set up and clear the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   344
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   345
  // Can be called on previously initialized bitmaps.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   346
  void reinitialize(idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   347
};
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   348
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   349
// A BitMap with storage in a specific Arena.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   350
class ArenaBitMap : public BitMap {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   351
 public:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   352
  // Clears the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   353
  ArenaBitMap(Arena* arena, idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   354
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   355
 private:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   356
  // Don't allow copy or assignment.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   357
  ArenaBitMap(const ArenaBitMap&);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   358
  ArenaBitMap& operator=(const ArenaBitMap&);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   359
};
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   360
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   361
// A BitMap with storage in the CHeap.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   362
class CHeapBitMap : public BitMap {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   363
  friend class TestBitMap;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   364
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   365
 private:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   366
  // Don't allow copy or assignment, to prevent the
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   367
  // allocated memory from leaking out to other instances.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   368
  CHeapBitMap(const CHeapBitMap&);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   369
  CHeapBitMap& operator=(const CHeapBitMap&);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   370
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   371
 public:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   372
  CHeapBitMap() : BitMap(NULL, 0) {}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   373
  // Clears the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   374
  CHeapBitMap(idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   375
  ~CHeapBitMap();
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   376
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   377
  // Resize the backing bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   378
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   379
  // Old bits are transfered to the new memory
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   380
  // and the extended memory is cleared.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   381
  void resize(idx_t new_size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   382
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   383
  // Set up and clear the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   384
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   385
  // Precondition: The bitmap was default constructed and has
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   386
  // not yet had memory allocated via resize or initialize.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   387
  void initialize(idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   388
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   389
  // Set up and clear the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   390
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   391
  // Can be called on previously initialized bitmaps.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   392
  void reinitialize(idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   393
};
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   394
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
// Convenience class wrapping BitMap which provides multiple bits per slot.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
class BitMap2D VALUE_OBJ_CLASS_SPEC {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
 public:
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   398
  typedef BitMap::idx_t idx_t;          // Type used for bit and word indices.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   399
  typedef BitMap::bm_word_t bm_word_t;  // Element type of array that
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   400
                                        // represents the bitmap.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
 private:
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   402
  ResourceBitMap _map;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   403
  idx_t          _bits_per_slot;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
    return slot_index * _bits_per_slot + bit_within_slot_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  void verify_bit_within_slot_index(idx_t index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
    assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
  // Construction. bits_per_slot must be greater than 0.
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   415
  BitMap2D(idx_t bits_per_slot) :
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   416
      _map(), _bits_per_slot(bits_per_slot) {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   419
  BitMap2D(idx_t size_in_slots, idx_t bits_per_slot) :
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   420
      _map(size_in_slots * bits_per_slot), _bits_per_slot(bits_per_slot) {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
  idx_t size_in_bits() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
    return _map.size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  // Returns number of full slots that have been allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  idx_t size_in_slots() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
    // Round down
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
    return _map.size() / _bits_per_slot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
37059
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   432
  bool 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: 37058
diff changeset
   433
  bool 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: 37058
diff changeset
   434
  void 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: 37058
diff changeset
   435
  void 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: 37058
diff changeset
   436
  void 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: 37058
diff changeset
   437
  void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   438
  void clear();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   441
// Closure for iterating over BitMaps
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   443
class BitMapClosure VALUE_OBJ_CLASS_SPEC {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   444
 public:
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   445
  // Callback when bit in map is set.  Should normally return "true";
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   446
  // return of false indicates that the bitmap iteration should terminate.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   447
  virtual bool do_bit(BitMap::idx_t offset) = 0;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   448
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   449
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   450
#endif // SHARE_VM_UTILITIES_BITMAP_HPP