src/hotspot/share/utilities/bitMap.cpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 46958 hotspot/src/share/vm/utilities/bitMap.cpp@a13bd8c6b7a2
child 47616 4f26db3c02af
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
     2
 * Copyright (c) 1997, 2017, 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
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "memory/allocation.inline.hpp"
23545
2c14fb03a06d 8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents: 22876
diff changeset
    27
#include "memory/resourceArea.hpp"
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 40639
diff changeset
    28
#include "runtime/atomic.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "utilities/bitMap.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "utilities/copy.hpp"
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    31
#include "utilities/debug.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
37058
47faedc8085b 8151439: Inline the BitMap constructor
stefank
parents: 37057
diff changeset
    33
STATIC_ASSERT(sizeof(BitMap::bm_word_t) == BytesPerWord); // "Implementation assumption."
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    35
typedef BitMap::bm_word_t bm_word_t;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    36
typedef BitMap::idx_t     idx_t;
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
class ResourceBitMapAllocator : StackObj {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    39
 public:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    40
  bm_word_t* allocate(idx_t size_in_words) const {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    41
    return NEW_RESOURCE_ARRAY(bm_word_t, size_in_words);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    42
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    43
  void free(bm_word_t* map, idx_t size_in_words) const {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    44
    // Don't free resource allocated arrays.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    45
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    46
};
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    47
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    48
class CHeapBitMapAllocator : StackObj {
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
    49
  MEMFLAGS _flags;
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
    50
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    51
 public:
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
    52
  CHeapBitMapAllocator(MEMFLAGS flags) : _flags(flags) {}
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    53
  bm_word_t* allocate(size_t size_in_words) const {
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
    54
    return ArrayAllocator<bm_word_t>::allocate(size_in_words, _flags);
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    55
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    56
  void free(bm_word_t* map, idx_t size_in_words) const {
46704
211b3f6b75ef 8182169: ArrayAllocator should take MEMFLAGS as regular parameter
kbarrett
parents: 42591
diff changeset
    57
    ArrayAllocator<bm_word_t>::free(map, size_in_words);
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    58
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    59
};
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    60
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    61
class ArenaBitMapAllocator : StackObj {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    62
  Arena* _arena;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    63
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    64
 public:
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    65
  ArenaBitMapAllocator(Arena* arena) : _arena(arena) {}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    66
  bm_word_t* allocate(idx_t size_in_words) const {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    67
    return (bm_word_t*)_arena->Amalloc(size_in_words * BytesPerWord);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    68
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    69
  void free(bm_word_t* map, idx_t size_in_words) const {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    70
    // ArenaBitMaps currently don't free memory.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    71
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    72
};
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    73
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    74
template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    75
BitMap::bm_word_t* BitMap::reallocate(const Allocator& allocator, bm_word_t* old_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
    76
  size_t old_size_in_words = calc_size_in_words(old_size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    77
  size_t new_size_in_words = calc_size_in_words(new_size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    78
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    79
  bm_word_t* map = NULL;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    80
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    81
  if (new_size_in_words > 0) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    82
    map = allocator.allocate(new_size_in_words);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    83
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    84
    Copy::disjoint_words((HeapWord*)old_map, (HeapWord*) map,
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    85
                         MIN2(old_size_in_words, new_size_in_words));
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    86
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    87
    if (new_size_in_words > old_size_in_words) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    88
      clear_range_of_words(map, old_size_in_words, new_size_in_words);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    89
    }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    90
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    91
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    92
  if (old_map != NULL) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    93
    allocator.free(old_map, old_size_in_words);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    94
  }
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    95
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    96
  return map;
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    97
}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    98
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
    99
template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   100
bm_word_t* BitMap::allocate(const Allocator& allocator, idx_t size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   101
  // Reuse reallocate to ensure that the new memory is cleared.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   102
  return reallocate(allocator, NULL, 0, size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   103
}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   104
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   105
template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   106
void BitMap::free(const Allocator& allocator, bm_word_t* map, idx_t  size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   107
  bm_word_t* ret = reallocate(allocator, map, size_in_bits, 0);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   108
  assert(ret == NULL, "Reallocate shouldn't have allocated");
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   109
}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   110
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   111
template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   112
void BitMap::resize(const Allocator& allocator, idx_t new_size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   113
  bm_word_t* new_map = reallocate(allocator, map(), size(), new_size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   114
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   115
  update(new_map, new_size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   116
}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   117
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   118
template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   119
void BitMap::initialize(const Allocator& allocator, idx_t size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   120
  assert(map() == NULL, "precondition");
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   121
  assert(size() == 0,   "precondition");
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
  resize(allocator, size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   124
}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   125
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   126
template <class Allocator>
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   127
void BitMap::reinitialize(const Allocator& allocator, idx_t new_size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   128
  // Remove previous bits.
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   129
  resize(allocator, 0);
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
  initialize(allocator, new_size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   132
}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   133
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   134
ResourceBitMap::ResourceBitMap(idx_t size_in_bits)
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   135
    : BitMap(allocate(ResourceBitMapAllocator(), size_in_bits), size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   136
}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   137
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   138
void ResourceBitMap::resize(idx_t new_size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   139
  BitMap::resize(ResourceBitMapAllocator(), new_size_in_bits);
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
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   142
void ResourceBitMap::initialize(idx_t size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   143
  BitMap::initialize(ResourceBitMapAllocator(), size_in_bits);
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   144
}
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
void ResourceBitMap::reinitialize(idx_t size_in_bits) {
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   147
  BitMap::reinitialize(ResourceBitMapAllocator(), size_in_bits);
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
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   150
ArenaBitMap::ArenaBitMap(Arena* arena, idx_t size_in_bits)
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   151
    : BitMap(allocate(ArenaBitMapAllocator(arena), size_in_bits), 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
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
   154
CHeapBitMap::CHeapBitMap(idx_t size_in_bits, MEMFLAGS flags)
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
   155
    : BitMap(allocate(CHeapBitMapAllocator(flags), size_in_bits), size_in_bits), _flags(flags) {
38177
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
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   158
CHeapBitMap::~CHeapBitMap() {
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
   159
  free(CHeapBitMapAllocator(_flags), map(), size());
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   160
}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   161
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   162
void CHeapBitMap::resize(idx_t new_size_in_bits) {
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
   163
  BitMap::resize(CHeapBitMapAllocator(_flags), new_size_in_bits);
38177
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   164
}
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   165
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   166
void CHeapBitMap::initialize(idx_t size_in_bits) {
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
   167
  BitMap::initialize(CHeapBitMapAllocator(_flags), size_in_bits);
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
b0c9cb06506b 8141501: Problems with BitMap buffer management
stefank
parents: 38102
diff changeset
   170
void CHeapBitMap::reinitialize(idx_t size_in_bits) {
46745
f7b9bb98bb72 8176571: Fine bitmaps should be allocated as belonging to mtGC, not mtInternal
kbarrett
parents: 46704
diff changeset
   171
  BitMap::reinitialize(CHeapBitMapAllocator(_flags), size_in_bits);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
37059
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   174
#ifdef ASSERT
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   175
void BitMap::verify_index(idx_t index) const {
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   176
  assert(index < _size, "BitMap index out of bounds");
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   177
}
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   178
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   179
void BitMap::verify_range(idx_t beg_index, idx_t end_index) const {
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   180
  assert(beg_index <= end_index, "BitMap range error");
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   181
  // Note that [0,0) and [size,size) are both valid ranges.
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   182
  if (end_index != _size) verify_index(end_index);
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   183
}
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   184
#endif // #ifdef ASSERT
c482915a21aa 8151440: Move BitMap verfication inline functions out from bitMap.hpp
stefank
parents: 37058
diff changeset
   185
37413
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37264
diff changeset
   186
void BitMap::pretouch() {
37414
2672ba9af0dc 8151386: Extract card live data out of G1ConcurrentMark
tschatzl
parents: 37413
diff changeset
   187
  os::pretouch_memory(word_addr(0), word_addr(size()));
37413
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37264
diff changeset
   188
}
2f71679d06dd 8077144: Concurrent mark initialization takes too long
tschatzl
parents: 37264
diff changeset
   189
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
void BitMap::set_range_within_word(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  // With a valid range (beg <= end), this test ensures that end != 0, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  if (beg != end) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   194
    bm_word_t mask = inverted_bit_mask_for_range(beg, end);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    *word_addr(beg) |= ~mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
void BitMap::clear_range_within_word(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  // With a valid range (beg <= end), this test ensures that end != 0, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  if (beg != end) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   203
    bm_word_t mask = inverted_bit_mask_for_range(beg, end);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    *word_addr(beg) &= mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
void BitMap::par_put_range_within_word(idx_t beg, idx_t end, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  assert(value == 0 || value == 1, "0 for clear, 1 for set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  // With a valid range (beg <= end), this test ensures that end != 0, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  // required by inverted_bit_mask_for_range.  Also avoids an unnecessary write.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  if (beg != end) {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46745
diff changeset
   213
    bm_word_t* pw = word_addr(beg);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46745
diff changeset
   214
    bm_word_t  w  = *pw;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46745
diff changeset
   215
    bm_word_t  mr = inverted_bit_mask_for_range(beg, end);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46745
diff changeset
   216
    bm_word_t  nw = value ? (w | ~mr) : (w & mr);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    while (true) {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46745
diff changeset
   218
      bm_word_t res = Atomic::cmpxchg(nw, pw, w);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
      if (res == w) break;
22769
516e9b159656 8033545: Missing volatile specifier in Bitmap::par_put_range_within_word
tschatzl
parents: 22234
diff changeset
   220
      w  = res;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
      nw = value ? (w | ~mr) : (w & mr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
void BitMap::set_range(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  if (beg_full_word < end_full_word) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    set_range_within_word(beg, bit_index(beg_full_word));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    set_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    set_range_within_word(bit_index(end_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
    // The range spans at most 2 partial words.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    idx_t boundary = MIN2(bit_index(beg_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    set_range_within_word(beg, boundary);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    set_range_within_word(boundary, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
void BitMap::clear_range(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  if (beg_full_word < end_full_word) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
    // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    clear_range_within_word(beg, bit_index(beg_full_word));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    clear_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
    clear_range_within_word(bit_index(end_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
    // The range spans at most 2 partial words.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    idx_t boundary = MIN2(bit_index(beg_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    clear_range_within_word(beg, boundary);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    clear_range_within_word(boundary, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
void BitMap::set_large_range(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  assert(end_full_word - beg_full_word >= 32,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
         "the range must include at least 32 bytes");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  set_range_within_word(beg, bit_index(beg_full_word));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  set_large_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  set_range_within_word(bit_index(end_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
void BitMap::clear_large_range(idx_t beg, idx_t end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
37264
d9b0f2b53b73 8152188: Allow CMSBitMapYieldQuantum for BitMap::clear_range and clear_large_range
sangheki
parents: 37059
diff changeset
   285
  if (end_full_word - beg_full_word < 32) {
d9b0f2b53b73 8152188: Allow CMSBitMapYieldQuantum for BitMap::clear_range and clear_large_range
sangheki
parents: 37059
diff changeset
   286
    clear_range(beg, end);
d9b0f2b53b73 8152188: Allow CMSBitMapYieldQuantum for BitMap::clear_range and clear_large_range
sangheki
parents: 37059
diff changeset
   287
    return;
d9b0f2b53b73 8152188: Allow CMSBitMapYieldQuantum for BitMap::clear_range and clear_large_range
sangheki
parents: 37059
diff changeset
   288
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  clear_range_within_word(beg, bit_index(beg_full_word));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  clear_large_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  clear_range_within_word(bit_index(end_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
void BitMap::at_put(idx_t offset, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  if (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    set_bit(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    clear_bit(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
// Return true to indicate that this thread changed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
// the bit, false to indicate that someone else did.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
// In either case, the requested bit is in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
// requested state some time during the period that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
// this thread is executing this call. More importantly,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
// if no other thread is executing an action to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
// change the requested bit to a state other than
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
// the one that this thread is trying to set it to,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
// then the the bit is in the expected state
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
// at exit from this method. However, rather than
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
// make such a strong assertion here, based on
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
// assuming such constrained use (which though true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
// today, could change in the future to service some
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
// funky parallel algorithm), we encourage callers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
// to do such verification, as and when appropriate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
bool BitMap::par_at_put(idx_t bit, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  return value ? par_set_bit(bit) : par_clear_bit(bit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
void BitMap::at_put_range(idx_t start_offset, idx_t end_offset, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  if (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    set_range(start_offset, end_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    clear_range(start_offset, end_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
void BitMap::par_at_put_range(idx_t beg, idx_t end, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  if (beg_full_word < end_full_word) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
    // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
    par_put_range_within_word(beg, bit_index(beg_full_word), value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
    if (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
      set_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
      clear_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    par_put_range_within_word(bit_index(end_full_word), end, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
    // The range spans at most 2 partial words.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    idx_t boundary = MIN2(bit_index(beg_full_word), end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
    par_put_range_within_word(beg, boundary, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    par_put_range_within_word(boundary, end, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
void BitMap::at_put_large_range(idx_t beg, idx_t end, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  if (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    set_large_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    clear_large_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
void BitMap::par_at_put_large_range(idx_t beg, idx_t end, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  verify_range(beg, end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  idx_t beg_full_word = word_index_round_up(beg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  idx_t end_full_word = word_index(end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  assert(end_full_word - beg_full_word >= 32,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
         "the range must include at least 32 bytes");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  // The range includes at least one full word.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  par_put_range_within_word(beg, bit_index(beg_full_word), value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  if (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    set_large_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    clear_large_range_of_words(beg_full_word, end_full_word);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  par_put_range_within_word(bit_index(end_full_word), end, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   382
inline bm_word_t tail_mask(idx_t tail_bits) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   383
  assert(tail_bits != 0, "precondition"); // Works, but shouldn't be called.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   384
  assert(tail_bits < (idx_t)BitsPerWord, "precondition");
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   385
  return (bm_word_t(1) << tail_bits) - 1;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   386
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   387
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   388
// Get the low tail_bits of value, which is the last partial word of a map.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   389
inline bm_word_t tail_of_map(bm_word_t value, idx_t tail_bits) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   390
  return value & tail_mask(tail_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   391
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   392
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   393
// Compute the new last word of a map with a non-aligned length.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   394
// new_value has the new trailing bits of the map in the low tail_bits.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   395
// old_value is the last word of the map, including bits beyond the end.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   396
// Returns old_value with the low tail_bits replaced by the corresponding
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   397
// bits in new_value.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   398
inline bm_word_t merge_tail_of_map(bm_word_t new_value,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   399
                                   bm_word_t old_value,
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   400
                                   idx_t tail_bits) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   401
  bm_word_t mask = tail_mask(tail_bits);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   402
  return (new_value & mask) | (old_value & ~mask);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   403
}
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   404
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   405
bool BitMap::contains(const BitMap& other) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  assert(size() == other.size(), "must have same size");
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   407
  const bm_word_t* dest_map = map();
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   408
  const bm_word_t* other_map = other.map();
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   409
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   410
  for (idx_t index = 0; index < limit; ++index) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   411
    // false if other bitmap has bits set which are clear in this bitmap.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   412
    if ((~dest_map[index] & other_map[index]) != 0) return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  }
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   414
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   415
  // true unless there is a partial-word tail in which the other
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   416
  // bitmap has bits set which are clear in this bitmap.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   417
  return (rest == 0) || tail_of_map(~dest_map[limit] & other_map[limit], rest) == 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   420
bool BitMap::intersects(const BitMap& other) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  assert(size() == other.size(), "must have same size");
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   422
  const bm_word_t* dest_map = map();
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   423
  const bm_word_t* other_map = other.map();
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   424
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   425
  for (idx_t index = 0; index < limit; ++index) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    if ((dest_map[index] & other_map[index]) != 0) return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  }
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   428
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   429
  // false unless there is a partial-word tail with non-empty intersection.
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   430
  return (rest > 0) && tail_of_map(dest_map[limit] & other_map[limit], rest) != 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   433
void BitMap::set_union(const BitMap& other) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  assert(size() == other.size(), "must have same size");
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   435
  bm_word_t* dest_map = map();
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   436
  const bm_word_t* other_map = other.map();
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   437
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   438
  for (idx_t index = 0; index < limit; ++index) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   439
    dest_map[index] |= other_map[index];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   440
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   441
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   442
  if (rest > 0) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   443
    bm_word_t orig = dest_map[limit];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   444
    dest_map[limit] = merge_tail_of_map(orig | other_map[limit], orig, rest);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   448
void BitMap::set_difference(const BitMap& other) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  assert(size() == other.size(), "must have same size");
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   450
  bm_word_t* dest_map = map();
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   451
  const bm_word_t* other_map = other.map();
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   452
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   453
  for (idx_t index = 0; index < limit; ++index) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   454
    dest_map[index] &= ~other_map[index];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   455
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   456
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   457
  if (rest > 0) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   458
    bm_word_t orig = dest_map[limit];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   459
    dest_map[limit] = merge_tail_of_map(orig & ~other_map[limit], orig, rest);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   463
void BitMap::set_intersection(const BitMap& other) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  assert(size() == other.size(), "must have same size");
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   465
  bm_word_t* dest_map = map();
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   466
  const bm_word_t* other_map = other.map();
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   467
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   468
  for (idx_t index = 0; index < limit; ++index) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   469
    dest_map[index] &= other_map[index];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  }
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   471
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   472
  if (rest > 0) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   473
    bm_word_t orig = dest_map[limit];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   474
    dest_map[limit] = merge_tail_of_map(orig & other_map[limit], orig, rest);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   475
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   476
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   477
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   478
bool BitMap::set_union_with_result(const BitMap& other) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
  bool changed = false;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   481
  bm_word_t* dest_map = map();
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   482
  const bm_word_t* other_map = other.map();
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   483
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   484
  for (idx_t index = 0; index < limit; ++index) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   485
    bm_word_t orig = dest_map[index];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   486
    bm_word_t temp = orig | other_map[index];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   487
    changed = changed || (temp != orig);
26937
dd2b0f6de283 8059474: Clean up vm/utilities/Bitmap type uses
shade
parents: 25959
diff changeset
   488
    dest_map[index] = temp;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  }
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   490
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   491
  if (rest > 0) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   492
    bm_word_t orig = dest_map[limit];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   493
    bm_word_t temp = merge_tail_of_map(orig | other_map[limit], orig, rest);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   494
    changed = changed || (temp != orig);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   495
    dest_map[limit] = temp;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   496
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  return changed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   500
bool BitMap::set_difference_with_result(const BitMap& other) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  bool changed = false;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   503
  bm_word_t* dest_map = map();
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   504
  const bm_word_t* other_map = other.map();
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   505
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   506
  for (idx_t index = 0; index < limit; ++index) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   507
    bm_word_t orig = dest_map[index];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   508
    bm_word_t temp = orig & ~other_map[index];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   509
    changed = changed || (temp != orig);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
    dest_map[index] = temp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  }
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   512
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   513
  if (rest > 0) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   514
    bm_word_t orig = dest_map[limit];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   515
    bm_word_t temp = merge_tail_of_map(orig & ~other_map[limit], orig, rest);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   516
    changed = changed || (temp != orig);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   517
    dest_map[limit] = temp;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   518
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  return changed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   522
bool BitMap::set_intersection_with_result(const BitMap& other) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
  assert(size() == other.size(), "must have same size");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
  bool changed = false;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   525
  bm_word_t* dest_map = map();
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   526
  const bm_word_t* other_map = other.map();
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   527
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   528
  for (idx_t index = 0; index < limit; ++index) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   529
    bm_word_t orig = dest_map[index];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   530
    bm_word_t temp = orig & other_map[index];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
    changed = changed || (temp != orig);
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   532
    dest_map[index] = temp;
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   533
  }
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   534
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   535
  if (rest > 0) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   536
    bm_word_t orig = dest_map[limit];
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   537
    bm_word_t temp = merge_tail_of_map(orig & other_map[limit], orig, rest);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   538
    changed = changed || (temp != orig);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   539
    dest_map[limit] = temp;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
  return changed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   544
void BitMap::set_from(const BitMap& other) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
  assert(size() == other.size(), "must have same size");
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   546
  bm_word_t* dest_map = map();
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   547
  const bm_word_t* other_map = other.map();
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   548
  idx_t copy_words = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   549
  Copy::disjoint_words((HeapWord*)other_map, (HeapWord*)dest_map, copy_words);
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   550
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   551
  if (rest > 0) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   552
    dest_map[copy_words] = merge_tail_of_map(other_map[copy_words],
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   553
                                             dest_map[copy_words],
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   554
                                             rest);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   558
bool BitMap::is_same(const BitMap& other) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
  assert(size() == other.size(), "must have same size");
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   560
  const bm_word_t* dest_map = map();
38102
839593075df1 8141496: BitMap set operations copy their other BitMap argument
stefank
parents: 37414
diff changeset
   561
  const bm_word_t* other_map = other.map();
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   562
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   563
  for (idx_t index = 0; index < limit; ++index) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
    if (dest_map[index] != other_map[index]) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
  }
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   566
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   567
  return (rest == 0) || (tail_of_map(dest_map[limit] ^ other_map[limit], rest) == 0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
bool BitMap::is_full() const {
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   571
  const bm_word_t* words = map();
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   572
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   573
  for (idx_t index = 0; index < limit; ++index) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   574
    if (~words[index] != 0) return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
  }
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   576
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   577
  return (rest == 0) || (tail_of_map(~words[limit], rest) == 0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
bool BitMap::is_empty() const {
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   581
  const bm_word_t* words = map();
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   582
  idx_t limit = word_index(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   583
  for (idx_t index = 0; index < limit; ++index) {
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   584
    if (words[index] != 0) return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
  }
40639
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   586
  idx_t rest = bit_in_word(size());
83c879c8db57 8155043: BitMap set operations assume clear bits beyond unaligned end
kbarrett
parents: 38177
diff changeset
   587
  return (rest == 0) || (tail_of_map(words[limit], rest) == 0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
void BitMap::clear_large() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
  clear_large_range_of_words(0, size_in_words());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
// Note that if the closure itself modifies the bitmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
// then modifications in and to the left of the _bit_ being
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
// currently sampled will not be seen. Note also that the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
// interval [leftOffset, rightOffset) is right open.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   598
bool BitMap::iterate(BitMapClosure* blk, idx_t leftOffset, idx_t rightOffset) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
  verify_range(leftOffset, rightOffset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
  idx_t startIndex = word_index(leftOffset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
  idx_t endIndex   = MIN2(word_index(rightOffset) + 1, size_in_words());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
  for (idx_t index = startIndex, offset = leftOffset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
       offset < rightOffset && index < endIndex;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
       offset = (++index) << LogBitsPerWord) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
    idx_t rest = map(index) >> (offset & (BitsPerWord - 1));
26937
dd2b0f6de283 8059474: Clean up vm/utilities/Bitmap type uses
shade
parents: 25959
diff changeset
   607
    for (; offset < rightOffset && rest != 0; offset++) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
      if (rest & 1) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   609
        if (!blk->do_bit(offset)) return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
        //  resample at each closure application
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
        // (see, for instance, CMS bug 4525989)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
        rest = map(index) >> (offset & (BitsPerWord -1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
      rest = rest >> 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
  }
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   617
  return true;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   618
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   619
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46745
diff changeset
   620
const BitMap::idx_t* BitMap::_pop_count_table = NULL;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   621
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   622
void BitMap::init_pop_count_table() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   623
  if (_pop_count_table == NULL) {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12272
diff changeset
   624
    BitMap::idx_t *table = NEW_C_HEAP_ARRAY(idx_t, 256, mtInternal);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   625
    for (uint i = 0; i < 256; i++) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   626
      table[i] = num_set_bits(i);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   627
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   628
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46745
diff changeset
   629
    if (!Atomic::replace_if_null(table, &_pop_count_table)) {
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 46745
diff changeset
   630
      guarantee(_pop_count_table != NULL, "invariant");
27880
afb974a04396 8060074: os::free() takes MemoryTrackingLevel but doesn't need it
coleenp
parents: 26937
diff changeset
   631
      FREE_C_HEAP_ARRAY(idx_t, table);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   632
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   633
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   636
BitMap::idx_t BitMap::num_set_bits(bm_word_t w) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   637
  idx_t bits = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   639
  while (w != 0) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   640
    while ((w & 1) == 0) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   641
      w >>= 1;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
    }
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   643
    bits++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   644
    w >>= 1;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
  }
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   646
  return bits;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   649
BitMap::idx_t BitMap::num_set_bits_from_table(unsigned char c) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   650
  assert(_pop_count_table != NULL, "precondition");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   651
  return _pop_count_table[c];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   652
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   654
BitMap::idx_t BitMap::count_one_bits() const {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   655
  init_pop_count_table(); // If necessary.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   656
  idx_t sum = 0;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   657
  typedef unsigned char uchar;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   658
  for (idx_t i = 0; i < size_in_words(); i++) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   659
    bm_word_t w = map()[i];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   660
    for (size_t j = 0; j < sizeof(bm_word_t); j++) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   661
      sum += num_set_bits_from_table(uchar(w & 255));
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   662
      w >>= 8;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
  }
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   665
  return sum;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
16685
41c34debcde0 8011872: Include Bit Map addresses in the hs_err files
stefank
parents: 13195
diff changeset
   668
void BitMap::print_on_error(outputStream* st, const char* prefix) const {
41c34debcde0 8011872: Include Bit Map addresses in the hs_err files
stefank
parents: 13195
diff changeset
   669
  st->print_cr("%s[" PTR_FORMAT ", " PTR_FORMAT ")",
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 23545
diff changeset
   670
      prefix, p2i(map()), p2i((char*)map() + (size() >> LogBitsPerByte)));
16685
41c34debcde0 8011872: Include Bit Map addresses in the hs_err files
stefank
parents: 13195
diff changeset
   671
}
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   672
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
void BitMap::print_on(outputStream* st) const {
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 23545
diff changeset
   676
  tty->print("Bitmap(" SIZE_FORMAT "):", size());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
  for (idx_t index = 0; index < size(); index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
    tty->print("%c", at(index) ? '1' : '0');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
  tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
#endif