src/hotspot/share/utilities/bitMap.hpp
author stuefe
Tue, 10 Sep 2019 09:24:05 +0200
branchstuefe-new-metaspace-branch
changeset 58063 bdf136b8ae0e
parent 57561 1fec2d70fa13
permissions -rw-r--r--
Initial changes for new metaspace. Only tested for Linux x64.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52485
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52485
diff changeset
    25
#ifndef SHARE_UTILITIES_BITMAP_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52485
diff changeset
    26
#define SHARE_UTILITIES_BITMAP_HPP
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
49670
1a1a69219637 8200607: Add missing include dependency in bitMap.hpp
pliden
parents: 49621
diff changeset
    28
#include "memory/allocation.hpp"
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents: 46619
diff changeset
    29
#include "utilities/align.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    31
// Forward decl;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    32
class BitMapClosure;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    34
// Operations for bitmaps represented as arrays of unsigned integers.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    35
// Bit offsets are numbered from 0 to size-1.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    37
// The "abstract" base BitMap class.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    38
//
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    39
// The constructor and destructor are protected to prevent
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    40
// creation of BitMap instances outside of the BitMap class.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    41
//
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    42
// The BitMap class doesn't use virtual calls on purpose,
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    43
// this ensures that we don't get a vtable unnecessarily.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    44
//
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    45
// The allocation of the backing storage for the BitMap are handled by
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    46
// the subclasses. BitMap doesn't allocate or delete backing storage.
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 47216
diff changeset
    47
class BitMap {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  friend class BitMap2D;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  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
    52
  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
    53
                                // the bitmap.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  // Hints for range sizes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  typedef enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    unknown_range, small_range, large_range
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  } RangeSizeHint;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
 private:
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    61
  bm_word_t* _map;     // First word in bitmap
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    62
  idx_t      _size;    // Size of bitmap (in bits)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
52394
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    64
  // Helper for get_next_{zero,one}_bit variants.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    65
  // - flip designates whether searching for 1s or 0s.  Must be one of
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    66
  //   find_{zeros,ones}_flip.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    67
  // - aligned_right is true if r_index is a priori on a bm_word_t boundary.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    68
  template<bm_word_t flip, bool aligned_right>
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    69
  inline idx_t get_next_bit_impl(idx_t l_index, idx_t r_index) const;
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    70
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    71
  // Values for get_next_bit_impl flip parameter.
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    72
  static const bm_word_t find_ones_flip = 0;
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    73
  static const bm_word_t find_zeros_flip = ~(bm_word_t)0;
96bd0f70ef99 6735527: Bitmap - speed up searches
kbarrett
parents: 51491
diff changeset
    74
52485
e5534cc91a10 8211926: Catastrophic size_t underflow in BitMap::*_large methods
shade
parents: 52394
diff changeset
    75
  // Threshold for performing small range operation, even when large range
e5534cc91a10 8211926: Catastrophic size_t underflow in BitMap::*_large methods
shade
parents: 52394
diff changeset
    76
  // operation was requested. Measured in words.
e5534cc91a10 8211926: Catastrophic size_t underflow in BitMap::*_large methods
shade
parents: 52394
diff changeset
    77
  static const size_t small_range_words = 32;
e5534cc91a10 8211926: Catastrophic size_t underflow in BitMap::*_large methods
shade
parents: 52394
diff changeset
    78
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  // Return the position of bit within the word that contains it (e.g., if
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  // bitmap words are 32 bits, return a number 0 <= n <= 31).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  // Return a mask that will select the specified bit, when applied to the word
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  // containing the bit.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    86
  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
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  // Return the index of the word containing the specified bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  static idx_t word_index(idx_t bit)  { return bit >> LogBitsPerWord; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  // Return the bit number of the first bit in the specified word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  static idx_t bit_index(idx_t word)  { return word << LogBitsPerWord; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  // 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
    95
  bm_word_t* map()                 { return _map; }
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
    96
  const bm_word_t* map() const     { return _map; }
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    97
  bm_word_t  map(idx_t word) const { return _map[word]; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // 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
   100
  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
   101
  const bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // 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
   104
  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
   105
  void set_word  (idx_t word)            { set_word(word, ~(bm_word_t)0); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  void clear_word(idx_t word)            { _map[word] = 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  // Utilities for ranges of bits.  Ranges are half-open [beg, end).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  // Ranges within a single word.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   111
  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
   112
  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
   113
  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
   114
  void  par_put_range_within_word  (idx_t beg, idx_t end, bool value);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  // Ranges spanning entire words.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   117
  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
   118
  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
   119
  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
   120
  void      clear_large_range_of_words (idx_t beg, idx_t end);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   122
  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
   123
52485
e5534cc91a10 8211926: Catastrophic size_t underflow in BitMap::*_large methods
shade
parents: 52394
diff changeset
   124
  static bool is_small_range_of_words(idx_t beg_full_word, idx_t end_full_word);
e5534cc91a10 8211926: Catastrophic size_t underflow in BitMap::*_large methods
shade
parents: 52394
diff changeset
   125
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  // 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
   127
  idx_t word_index_round_up(idx_t bit) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
2998
b501bd305780 6849716: BitMap - performance regression introduced with G1
jcoomes
parents: 1374
diff changeset
   129
  // Verification.
37059
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   130
  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
   131
  void verify_range(idx_t beg_index, idx_t end_index) const NOT_DEBUG_RETURN;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
2998
b501bd305780 6849716: BitMap - performance regression introduced with G1
jcoomes
parents: 1374
diff changeset
   133
  // Statistics.
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46745
diff changeset
   134
  static const idx_t* _pop_count_table;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   135
  static void init_pop_count_table();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   136
  static idx_t num_set_bits(bm_word_t w);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   137
  static idx_t num_set_bits_from_table(unsigned char c);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   139
  // Allocation Helpers.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   140
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   141
  // Allocates and clears the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   142
  template <class Allocator>
49828
ee3555b4a130 8201647: Make initial clearing of CHeapBitMap optional
pliden
parents: 49670
diff changeset
   143
  static bm_word_t* allocate(const Allocator&, idx_t size_in_bits, bool clear = true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   145
  // Reallocates and clears the new bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   146
  template <class Allocator>
49828
ee3555b4a130 8201647: Make initial clearing of CHeapBitMap optional
pliden
parents: 49670
diff changeset
   147
  static bm_word_t* reallocate(const Allocator&, bm_word_t* map, idx_t old_size_in_bits, idx_t new_size_in_bits, bool clear = true);
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   148
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   149
  // Free the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   150
  template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   151
  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
   152
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   153
  // Protected functions, that are used by BitMap sub-classes that support them.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   155
  // Resize the backing bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   156
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   157
  // Old bits are transfered to the new memory
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   158
  // and the extended memory is cleared.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   159
  template <class Allocator>
54679
3a2e45820d77 8222986: Add parameter to skip clearing CHeapBitMaps when resizing
stefank
parents: 53546
diff changeset
   160
  void resize(const Allocator& allocator, idx_t new_size_in_bits, bool clear);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   162
  // Set up and clear the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   163
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   164
  // Precondition: The bitmap was default constructed and has
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   165
  // not yet had memory allocated via resize or (re)initialize.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   166
  template <class Allocator>
54679
3a2e45820d77 8222986: Add parameter to skip clearing CHeapBitMaps when resizing
stefank
parents: 53546
diff changeset
   167
  void initialize(const Allocator& allocator, idx_t size_in_bits, bool clear);
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   168
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   169
  // Set up and clear the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   170
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   171
  // Can be called on previously initialized bitmaps.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   172
  template <class Allocator>
54679
3a2e45820d77 8222986: Add parameter to skip clearing CHeapBitMaps when resizing
stefank
parents: 53546
diff changeset
   173
  void reinitialize(const Allocator& allocator, idx_t new_size_in_bits, bool clear);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   175
  // Set the map and size.
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   176
  void update(bm_word_t* map, idx_t size) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   177
    _map = map;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   178
    _size = size;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   179
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   181
  // Protected constructor and destructor.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   182
  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
   183
  ~BitMap() {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   185
 public:
37413
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   186
  // Pretouch the entire range of memory this BitMap covers.
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   187
  void pretouch();
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   188
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  // Accessing
37414
2672ba9af0dc 8151386: Extract card live data out of G1ConcurrentMark
tschatzl
parents: 37413
diff changeset
   190
  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
   191
    return word_index(size_in_bits + BitsPerWord - 1);
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   192
  }
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37059
diff changeset
   193
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   194
  static idx_t calc_size_in_bytes(size_t size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   195
    return calc_size_in_words(size_in_bits) * BytesPerWord;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   196
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   197
58063
bdf136b8ae0e Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents: 57561
diff changeset
   198
  // Size, in number of bits, of this map.
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   199
  idx_t size() const          { return _size; }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   200
  idx_t size_in_words() const { return calc_size_in_words(size()); }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   201
  idx_t size_in_bytes() const { return calc_size_in_bytes(size()); }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   202
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  bool at(idx_t index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    verify_index(index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    return (*word_addr(index) & bit_mask(index)) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  // Align bit index up or down to the next bitmap word boundary, or check
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  // alignment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  static idx_t word_align_up(idx_t bit) {
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46402
diff changeset
   211
    return align_up(bit, BitsPerWord);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  static idx_t word_align_down(idx_t bit) {
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 46402
diff changeset
   214
    return align_down(bit, BitsPerWord);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  static bool is_word_aligned(idx_t bit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    return word_align_up(bit) == bit;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  // Set or clear the specified bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  inline void set_bit(idx_t bit);
9994
efb74fdbd46e 7046558: G1: concurrent marking optimizations
tonyp
parents: 7397
diff changeset
   222
  inline void clear_bit(idx_t bit);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  // Atomically set or clear the specified bit.
9994
efb74fdbd46e 7046558: G1: concurrent marking optimizations
tonyp
parents: 7397
diff changeset
   225
  inline bool par_set_bit(idx_t bit);
efb74fdbd46e 7046558: G1: concurrent marking optimizations
tonyp
parents: 7397
diff changeset
   226
  inline bool par_clear_bit(idx_t bit);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  // Put the given value at the given offset. The parallel version
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  // will CAS the value into the bitmap and is quite a bit slower.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  // The parallel version also returns a value indicating if the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  // calling thread was the one that changed the value of the bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  void at_put(idx_t index, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  bool par_at_put(idx_t index, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  // Update a range of bits.  Ranges are half-open [beg, end).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  void set_range   (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  void clear_range (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  void set_large_range   (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  void clear_large_range (idx_t beg, idx_t end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  void at_put_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
  void par_at_put_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  void at_put_large_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  void par_at_put_large_range(idx_t beg, idx_t end, bool value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  // Update a range of bits, using a hint about the size.  Currently only
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  // inlines the predominant case of a 1-bit range.  Works best when hint is a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  // compile-time constant.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   248
  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
   249
  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
   250
  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
   251
  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
   252
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  // Clearing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  void clear_large();
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   255
  inline void clear();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
58063
bdf136b8ae0e Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents: 57561
diff changeset
   257
  // Iteration support [leftIndex, rightIndex).  Returns "true" if the iteration completed, false
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   258
  // if the iteration terminated early (because the closure "blk" returned
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   259
  // false).
58063
bdf136b8ae0e Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents: 57561
diff changeset
   260
  bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex) const;
bdf136b8ae0e Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents: 57561
diff changeset
   261
  bool iterate(BitMapClosure* blk) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    // call the version that takes an interval
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   263
    return iterate(blk, 0, size());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   266
  // 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
   267
  // 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
   268
  // "r_index" (which must be at most "size") in that case.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  idx_t get_next_one_offset(idx_t offset) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
    return get_next_one_offset(offset, size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  idx_t get_next_zero_offset(idx_t offset) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    return get_next_zero_offset(offset, size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
46402
8147e17ad6fa 8179181: Cleanup BitMap search API
kbarrett
parents: 42591
diff changeset
   279
  // Like "get_next_one_offset", except requires that "r_index" is
8147e17ad6fa 8179181: Cleanup BitMap search API
kbarrett
parents: 42591
diff changeset
   280
  // aligned to bitsizeof(bm_word_t).
8147e17ad6fa 8179181: Cleanup BitMap search API
kbarrett
parents: 42591
diff changeset
   281
  idx_t get_next_one_offset_aligned_right(idx_t l_index, idx_t r_index) const;
8147e17ad6fa 8179181: Cleanup BitMap search API
kbarrett
parents: 42591
diff changeset
   282
58063
bdf136b8ae0e Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents: 57561
diff changeset
   283
  // Returns the number of bits set between [l_index, r_index) in the bitmap.
bdf136b8ae0e Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents: 57561
diff changeset
   284
  idx_t count_one_bits(idx_t l_index, idx_t r_index) const;
bdf136b8ae0e Initial changes for new metaspace. Only tested for Linux x64.
stuefe
parents: 57561
diff changeset
   285
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   286
  // Returns the number of bits set in the bitmap.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   287
  idx_t count_one_bits() const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  // Set operations.
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   290
  void set_union(const BitMap& bits);
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   291
  void set_difference(const BitMap& bits);
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   292
  void set_intersection(const BitMap& bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  // 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
   294
  bool contains(const BitMap& bits) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  // 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
   296
  bool intersects(const BitMap& bits) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  // Returns result of whether this map changed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  // during the operation
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   300
  bool set_union_with_result(const BitMap& bits);
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   301
  bool set_difference_with_result(const BitMap& bits);
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   302
  bool set_intersection_with_result(const BitMap& bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37466
diff changeset
   304
  void set_from(const BitMap& bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 39219
diff changeset
   306
  bool is_same(const BitMap& bits) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  // Test if all bits are set or cleared
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  bool is_full() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  bool is_empty() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
51491
187c84a5efe1 8208658: Make CDS archived heap regions usable even if compressed oop encoding has changed
iklam
parents: 49828
diff changeset
   312
  void write_to(bm_word_t* buffer, size_t buffer_size_in_bytes) const;
16685
41c34debcde0 8011872: Include Bit Map addresses in the hs_err files
stefank
parents: 12272
diff changeset
   313
  void print_on_error(outputStream* st, const char* prefix) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  // Printing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  void print_on(outputStream* st) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   322
// A concrete implementation of the the "abstract" BitMap class.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   323
//
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   324
// The BitMapView is used when the backing storage is managed externally.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   325
class BitMapView : public BitMap {
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
  BitMapView() : BitMap(NULL, 0) {}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   328
  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
   329
};
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
// A BitMap with storage in a ResourceArea.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   332
class ResourceBitMap : public BitMap {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   333
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   334
 public:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   335
  ResourceBitMap() : BitMap(NULL, 0) {}
57561
1fec2d70fa13 8156207: Resource allocated BitMaps are often cleared unnecessarily
thartmann
parents: 55075
diff changeset
   336
  // Conditionally clears the bitmap memory.
1fec2d70fa13 8156207: Resource allocated BitMaps are often cleared unnecessarily
thartmann
parents: 55075
diff changeset
   337
  ResourceBitMap(idx_t size_in_bits, bool clear = true);
38177
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
  // Resize the backing bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   340
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   341
  // Old bits are transfered to the new memory
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   342
  // and the extended memory is cleared.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   343
  void resize(idx_t new_size_in_bits);
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
  // Set up and clear the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   346
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   347
  // Precondition: The bitmap was default constructed and has
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   348
  // not yet had memory allocated via resize or initialize.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   349
  void initialize(idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   350
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   351
  // Set up and clear the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   352
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   353
  // Can be called on previously initialized bitmaps.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   354
  void reinitialize(idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   355
};
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   356
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   357
// A BitMap with storage in a specific Arena.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   358
class ArenaBitMap : public BitMap {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   359
 public:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   360
  // Clears the bitmap memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   361
  ArenaBitMap(Arena* arena, idx_t size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   362
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   363
 private:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   364
  // Don't allow copy or assignment.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   365
  ArenaBitMap(const ArenaBitMap&);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   366
  ArenaBitMap& operator=(const ArenaBitMap&);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   367
};
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   368
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   369
// A BitMap with storage in the CHeap.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   370
class CHeapBitMap : public BitMap {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   371
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   372
 private:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   373
  // Don't allow copy or assignment, to prevent the
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   374
  // allocated memory from leaking out to other instances.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   375
  CHeapBitMap(const CHeapBitMap&);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   376
  CHeapBitMap& operator=(const CHeapBitMap&);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   377
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46625
diff changeset
   378
  // NMT memory type
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46625
diff changeset
   379
  MEMFLAGS _flags;
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46625
diff changeset
   380
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   381
 public:
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46625
diff changeset
   382
  CHeapBitMap(MEMFLAGS flags = mtInternal) : BitMap(NULL, 0), _flags(flags) {}
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   383
  // Clears the bitmap memory.
49828
ee3555b4a130 8201647: Make initial clearing of CHeapBitMap optional
pliden
parents: 49670
diff changeset
   384
  CHeapBitMap(idx_t size_in_bits, MEMFLAGS flags = mtInternal, bool clear = true);
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   385
  ~CHeapBitMap();
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   386
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   387
  // Resize the backing bitmap memory.
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
  // Old bits are transfered to the new memory
55075
044f2ca6ce22 8223392: Add missing BitMap comments for JDK-8222986
stefank
parents: 54679
diff changeset
   390
  // and the extended memory is (optionally) cleared.
54679
3a2e45820d77 8222986: Add parameter to skip clearing CHeapBitMaps when resizing
stefank
parents: 53546
diff changeset
   391
  void resize(idx_t new_size_in_bits, bool clear = true);
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   392
55075
044f2ca6ce22 8223392: Add missing BitMap comments for JDK-8222986
stefank
parents: 54679
diff changeset
   393
  // Set up and (optionally) clear the bitmap memory.
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   394
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   395
  // Precondition: The bitmap was default constructed and has
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   396
  // not yet had memory allocated via resize or initialize.
54679
3a2e45820d77 8222986: Add parameter to skip clearing CHeapBitMaps when resizing
stefank
parents: 53546
diff changeset
   397
  void initialize(idx_t size_in_bits, bool clear = true);
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   398
55075
044f2ca6ce22 8223392: Add missing BitMap comments for JDK-8222986
stefank
parents: 54679
diff changeset
   399
  // Set up and (optionally) clear the bitmap memory.
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   400
  //
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   401
  // Can be called on previously initialized bitmaps.
54679
3a2e45820d77 8222986: Add parameter to skip clearing CHeapBitMaps when resizing
stefank
parents: 53546
diff changeset
   402
  void reinitialize(idx_t size_in_bits, bool clear = true);
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   403
};
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   404
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
// Convenience class wrapping BitMap which provides multiple bits per slot.
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 47216
diff changeset
   406
class BitMap2D {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
 public:
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   408
  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
   409
  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
   410
                                        // represents the bitmap.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
 private:
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   412
  ResourceBitMap _map;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   413
  idx_t          _bits_per_slot;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
  idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
    return slot_index * _bits_per_slot + bit_within_slot_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  void verify_bit_within_slot_index(idx_t index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
    assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  // Construction. bits_per_slot must be greater than 0.
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   425
  BitMap2D(idx_t bits_per_slot) :
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   426
      _map(), _bits_per_slot(bits_per_slot) {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  // 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
   429
  BitMap2D(idx_t size_in_slots, idx_t bits_per_slot) :
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   430
      _map(size_in_slots * bits_per_slot), _bits_per_slot(bits_per_slot) {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  idx_t size_in_bits() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
    return _map.size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
37059
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   436
  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
   437
  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
   438
  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
   439
  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
   440
  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
   441
  void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   444
// Closure for iterating over BitMaps
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
49364
601146c66cad 8173070: Remove ValueObj class for allocation subclassing for runtime code
coleenp
parents: 47216
diff changeset
   446
class BitMapClosure {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   447
 public:
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   448
  // 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
   449
  // return of false indicates that the bitmap iteration should terminate.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   450
  virtual bool do_bit(BitMap::idx_t offset) = 0;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   451
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   452
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 52485
diff changeset
   453
#endif // SHARE_UTILITIES_BITMAP_HPP