src/share/vm/utilities/bitMap.hpp

Tue, 24 Jul 2018 13:22:11 +0800

author
aoqi
date
Tue, 24 Jul 2018 13:22:11 +0800
changeset 9137
dc1769738300
parent 6876
710a3c8b516e
child 9637
eef07cd490d4
permissions
-rw-r--r--

#7048 added Loongson release info to hs_err crash files

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_UTILITIES_BITMAP_HPP
aoqi@0 26 #define SHARE_VM_UTILITIES_BITMAP_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "utilities/top.hpp"
aoqi@0 30
aoqi@0 31 // Forward decl;
aoqi@0 32 class BitMapClosure;
aoqi@0 33
aoqi@0 34 // Operations for bitmaps represented as arrays of unsigned integers.
aoqi@0 35 // Bit offsets are numbered from 0 to size-1.
aoqi@0 36
aoqi@0 37 class BitMap VALUE_OBJ_CLASS_SPEC {
aoqi@0 38 friend class BitMap2D;
aoqi@0 39
aoqi@0 40 public:
aoqi@0 41 typedef size_t idx_t; // Type used for bit and word indices.
aoqi@0 42 typedef uintptr_t bm_word_t; // Element type of array that represents
aoqi@0 43 // the bitmap.
aoqi@0 44
aoqi@0 45 // Hints for range sizes.
aoqi@0 46 typedef enum {
aoqi@0 47 unknown_range, small_range, large_range
aoqi@0 48 } RangeSizeHint;
aoqi@0 49
aoqi@0 50 private:
aoqi@0 51 ArrayAllocator<bm_word_t, mtInternal> _map_allocator;
aoqi@0 52 bm_word_t* _map; // First word in bitmap
aoqi@0 53 idx_t _size; // Size of bitmap (in bits)
aoqi@0 54
aoqi@0 55 // Puts the given value at the given offset, using resize() to size
aoqi@0 56 // the bitmap appropriately if needed using factor-of-two expansion.
aoqi@0 57 void at_put_grow(idx_t index, bool value);
aoqi@0 58
aoqi@0 59 protected:
aoqi@0 60 // Return the position of bit within the word that contains it (e.g., if
aoqi@0 61 // bitmap words are 32 bits, return a number 0 <= n <= 31).
aoqi@0 62 static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }
aoqi@0 63
aoqi@0 64 // Return a mask that will select the specified bit, when applied to the word
aoqi@0 65 // containing the bit.
aoqi@0 66 static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); }
aoqi@0 67
aoqi@0 68 // Return the index of the word containing the specified bit.
aoqi@0 69 static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; }
aoqi@0 70
aoqi@0 71 // Return the bit number of the first bit in the specified word.
aoqi@0 72 static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; }
aoqi@0 73
aoqi@0 74 // Return the array of bitmap words, or a specific word from it.
aoqi@0 75 bm_word_t* map() const { return _map; }
aoqi@0 76 bm_word_t map(idx_t word) const { return _map[word]; }
aoqi@0 77
aoqi@0 78 // Return a pointer to the word containing the specified bit.
aoqi@0 79 bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); }
aoqi@0 80
aoqi@0 81 // Set a word to a specified value or to all ones; clear a word.
aoqi@0 82 void set_word (idx_t word, bm_word_t val) { _map[word] = val; }
aoqi@0 83 void set_word (idx_t word) { set_word(word, ~(uintptr_t)0); }
aoqi@0 84 void clear_word(idx_t word) { _map[word] = 0; }
aoqi@0 85
aoqi@0 86 // Utilities for ranges of bits. Ranges are half-open [beg, end).
aoqi@0 87
aoqi@0 88 // Ranges within a single word.
aoqi@0 89 bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const;
aoqi@0 90 void set_range_within_word (idx_t beg, idx_t end);
aoqi@0 91 void clear_range_within_word (idx_t beg, idx_t end);
aoqi@0 92 void par_put_range_within_word (idx_t beg, idx_t end, bool value);
aoqi@0 93
aoqi@0 94 // Ranges spanning entire words.
aoqi@0 95 void set_range_of_words (idx_t beg, idx_t end);
aoqi@0 96 void clear_range_of_words (idx_t beg, idx_t end);
aoqi@0 97 void set_large_range_of_words (idx_t beg, idx_t end);
aoqi@0 98 void clear_large_range_of_words (idx_t beg, idx_t end);
aoqi@0 99
aoqi@0 100 // The index of the first full word in a range.
aoqi@0 101 idx_t word_index_round_up(idx_t bit) const;
aoqi@0 102
aoqi@0 103 // Verification.
aoqi@0 104 inline void verify_index(idx_t index) const NOT_DEBUG_RETURN;
aoqi@0 105 inline void verify_range(idx_t beg_index, idx_t end_index) const
aoqi@0 106 NOT_DEBUG_RETURN;
aoqi@0 107
aoqi@0 108 // Statistics.
aoqi@0 109 static idx_t* _pop_count_table;
aoqi@0 110 static void init_pop_count_table();
aoqi@0 111 static idx_t num_set_bits(bm_word_t w);
aoqi@0 112 static idx_t num_set_bits_from_table(unsigned char c);
aoqi@0 113
aoqi@0 114 public:
aoqi@0 115
aoqi@0 116 // Constructs a bitmap with no map, and size 0.
aoqi@0 117 BitMap() : _map(NULL), _size(0), _map_allocator(false) {}
aoqi@0 118
aoqi@0 119 // Constructs a bitmap with the given map and size.
aoqi@0 120 BitMap(bm_word_t* map, idx_t size_in_bits);
aoqi@0 121
aoqi@0 122 // Constructs an empty bitmap of the given size (that is, this clears the
aoqi@0 123 // new bitmap). Allocates the map array in resource area if
aoqi@0 124 // "in_resource_area" is true, else in the C heap.
aoqi@0 125 BitMap(idx_t size_in_bits, bool in_resource_area = true);
aoqi@0 126
aoqi@0 127 // Set the map and size.
aoqi@0 128 void set_map(bm_word_t* map) { _map = map; }
aoqi@0 129 void set_size(idx_t size_in_bits) { _size = size_in_bits; }
aoqi@0 130
aoqi@0 131 // Allocates necessary data structure, either in the resource area
aoqi@0 132 // or in the C heap, as indicated by "in_resource_area."
aoqi@0 133 // Preserves state currently in bit map by copying data.
aoqi@0 134 // Zeros any newly-addressable bits.
aoqi@0 135 // If "in_resource_area" is false, frees the current map.
aoqi@0 136 // (Note that this assumes that all calls to "resize" on the same BitMap
aoqi@0 137 // use the same value for "in_resource_area".)
aoqi@0 138 void resize(idx_t size_in_bits, bool in_resource_area = true);
aoqi@0 139
aoqi@0 140 // Accessing
aoqi@0 141 idx_t size() const { return _size; }
aoqi@0 142 idx_t size_in_words() const {
aoqi@0 143 return word_index(size() + BitsPerWord - 1);
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 bool at(idx_t index) const {
aoqi@0 147 verify_index(index);
aoqi@0 148 return (*word_addr(index) & bit_mask(index)) != 0;
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 // Align bit index up or down to the next bitmap word boundary, or check
aoqi@0 152 // alignment.
aoqi@0 153 static idx_t word_align_up(idx_t bit) {
aoqi@0 154 return align_size_up(bit, BitsPerWord);
aoqi@0 155 }
aoqi@0 156 static idx_t word_align_down(idx_t bit) {
aoqi@0 157 return align_size_down(bit, BitsPerWord);
aoqi@0 158 }
aoqi@0 159 static bool is_word_aligned(idx_t bit) {
aoqi@0 160 return word_align_up(bit) == bit;
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 // Set or clear the specified bit.
aoqi@0 164 inline void set_bit(idx_t bit);
aoqi@0 165 inline void clear_bit(idx_t bit);
aoqi@0 166
aoqi@0 167 // Atomically set or clear the specified bit.
aoqi@0 168 inline bool par_set_bit(idx_t bit);
aoqi@0 169 inline bool par_clear_bit(idx_t bit);
aoqi@0 170
aoqi@0 171 // Put the given value at the given offset. The parallel version
aoqi@0 172 // will CAS the value into the bitmap and is quite a bit slower.
aoqi@0 173 // The parallel version also returns a value indicating if the
aoqi@0 174 // calling thread was the one that changed the value of the bit.
aoqi@0 175 void at_put(idx_t index, bool value);
aoqi@0 176 bool par_at_put(idx_t index, bool value);
aoqi@0 177
aoqi@0 178 // Update a range of bits. Ranges are half-open [beg, end).
aoqi@0 179 void set_range (idx_t beg, idx_t end);
aoqi@0 180 void clear_range (idx_t beg, idx_t end);
aoqi@0 181 void set_large_range (idx_t beg, idx_t end);
aoqi@0 182 void clear_large_range (idx_t beg, idx_t end);
aoqi@0 183 void at_put_range(idx_t beg, idx_t end, bool value);
aoqi@0 184 void par_at_put_range(idx_t beg, idx_t end, bool value);
aoqi@0 185 void at_put_large_range(idx_t beg, idx_t end, bool value);
aoqi@0 186 void par_at_put_large_range(idx_t beg, idx_t end, bool value);
aoqi@0 187
aoqi@0 188 // Update a range of bits, using a hint about the size. Currently only
aoqi@0 189 // inlines the predominant case of a 1-bit range. Works best when hint is a
aoqi@0 190 // compile-time constant.
aoqi@0 191 void set_range(idx_t beg, idx_t end, RangeSizeHint hint);
aoqi@0 192 void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);
aoqi@0 193 void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint);
aoqi@0 194 void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint);
aoqi@0 195
aoqi@0 196 // Clearing
aoqi@0 197 void clear_large();
aoqi@0 198 inline void clear();
aoqi@0 199
aoqi@0 200 // Iteration support. Returns "true" if the iteration completed, false
aoqi@0 201 // if the iteration terminated early (because the closure "blk" returned
aoqi@0 202 // false).
aoqi@0 203 bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex);
aoqi@0 204 bool iterate(BitMapClosure* blk) {
aoqi@0 205 // call the version that takes an interval
aoqi@0 206 return iterate(blk, 0, size());
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 // Looking for 1's and 0's at indices equal to or greater than "l_index",
aoqi@0 210 // stopping if none has been found before "r_index", and returning
aoqi@0 211 // "r_index" (which must be at most "size") in that case.
aoqi@0 212 idx_t get_next_one_offset_inline (idx_t l_index, idx_t r_index) const;
aoqi@0 213 idx_t get_next_zero_offset_inline(idx_t l_index, idx_t r_index) const;
aoqi@0 214
aoqi@0 215 // Like "get_next_one_offset_inline", except requires that "r_index" is
aoqi@0 216 // aligned to bitsizeof(bm_word_t).
aoqi@0 217 idx_t get_next_one_offset_inline_aligned_right(idx_t l_index,
aoqi@0 218 idx_t r_index) const;
aoqi@0 219
aoqi@0 220 // Non-inline versionsof the above.
aoqi@0 221 idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
aoqi@0 222 idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
aoqi@0 223
aoqi@0 224 idx_t get_next_one_offset(idx_t offset) const {
aoqi@0 225 return get_next_one_offset(offset, size());
aoqi@0 226 }
aoqi@0 227 idx_t get_next_zero_offset(idx_t offset) const {
aoqi@0 228 return get_next_zero_offset(offset, size());
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 // Returns the number of bits set in the bitmap.
aoqi@0 232 idx_t count_one_bits() const;
aoqi@0 233
aoqi@0 234 // Set operations.
aoqi@0 235 void set_union(BitMap bits);
aoqi@0 236 void set_difference(BitMap bits);
aoqi@0 237 void set_intersection(BitMap bits);
aoqi@0 238 // Returns true iff "this" is a superset of "bits".
aoqi@0 239 bool contains(const BitMap bits) const;
aoqi@0 240 // Returns true iff "this and "bits" have a non-empty intersection.
aoqi@0 241 bool intersects(const BitMap bits) const;
aoqi@0 242
aoqi@0 243 // Returns result of whether this map changed
aoqi@0 244 // during the operation
aoqi@0 245 bool set_union_with_result(BitMap bits);
aoqi@0 246 bool set_difference_with_result(BitMap bits);
aoqi@0 247 bool set_intersection_with_result(BitMap bits);
aoqi@0 248
aoqi@0 249 // Requires the submap of "bits" starting at offset to be at least as
aoqi@0 250 // large as "this". Modifies "this" to be the intersection of its
aoqi@0 251 // current contents and the submap of "bits" starting at "offset" of the
aoqi@0 252 // same length as "this."
aoqi@0 253 // (For expedience, currently requires the offset to be aligned to the
aoqi@0 254 // bitsize of a uintptr_t. This should go away in the future though it
aoqi@0 255 // will probably remain a good case to optimize.)
aoqi@0 256 void set_intersection_at_offset(BitMap bits, idx_t offset);
aoqi@0 257
aoqi@0 258 void set_from(BitMap bits);
aoqi@0 259
aoqi@0 260 bool is_same(BitMap bits);
aoqi@0 261
aoqi@0 262 // Test if all bits are set or cleared
aoqi@0 263 bool is_full() const;
aoqi@0 264 bool is_empty() const;
aoqi@0 265
aoqi@0 266 void print_on_error(outputStream* st, const char* prefix) const;
aoqi@0 267
aoqi@0 268 #ifndef PRODUCT
aoqi@0 269 public:
aoqi@0 270 // Printing
aoqi@0 271 void print_on(outputStream* st) const;
aoqi@0 272 #endif
aoqi@0 273 };
aoqi@0 274
aoqi@0 275 // Convenience class wrapping BitMap which provides multiple bits per slot.
aoqi@0 276 class BitMap2D VALUE_OBJ_CLASS_SPEC {
aoqi@0 277 public:
aoqi@0 278 typedef BitMap::idx_t idx_t; // Type used for bit and word indices.
aoqi@0 279 typedef BitMap::bm_word_t bm_word_t; // Element type of array that
aoqi@0 280 // represents the bitmap.
aoqi@0 281 private:
aoqi@0 282 BitMap _map;
aoqi@0 283 idx_t _bits_per_slot;
aoqi@0 284
aoqi@0 285 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
aoqi@0 286 return slot_index * _bits_per_slot + bit_within_slot_index;
aoqi@0 287 }
aoqi@0 288
aoqi@0 289 void verify_bit_within_slot_index(idx_t index) const {
aoqi@0 290 assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
aoqi@0 291 }
aoqi@0 292
aoqi@0 293 public:
aoqi@0 294 // Construction. bits_per_slot must be greater than 0.
aoqi@0 295 BitMap2D(bm_word_t* map, idx_t size_in_slots, idx_t bits_per_slot);
aoqi@0 296
aoqi@0 297 // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.
aoqi@0 298 BitMap2D(idx_t size_in_slots, idx_t bits_per_slot);
aoqi@0 299
aoqi@0 300 idx_t size_in_bits() {
aoqi@0 301 return _map.size();
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 // Returns number of full slots that have been allocated
aoqi@0 305 idx_t size_in_slots() {
aoqi@0 306 // Round down
aoqi@0 307 return _map.size() / _bits_per_slot;
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index) {
aoqi@0 311 verify_bit_within_slot_index(bit_within_slot_index);
aoqi@0 312 return (bit_index(slot_index, bit_within_slot_index) < size_in_bits());
aoqi@0 313 }
aoqi@0 314
aoqi@0 315 bool at(idx_t slot_index, idx_t bit_within_slot_index) const {
aoqi@0 316 verify_bit_within_slot_index(bit_within_slot_index);
aoqi@0 317 return _map.at(bit_index(slot_index, bit_within_slot_index));
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 void set_bit(idx_t slot_index, idx_t bit_within_slot_index) {
aoqi@0 321 verify_bit_within_slot_index(bit_within_slot_index);
aoqi@0 322 _map.set_bit(bit_index(slot_index, bit_within_slot_index));
aoqi@0 323 }
aoqi@0 324
aoqi@0 325 void clear_bit(idx_t slot_index, idx_t bit_within_slot_index) {
aoqi@0 326 verify_bit_within_slot_index(bit_within_slot_index);
aoqi@0 327 _map.clear_bit(bit_index(slot_index, bit_within_slot_index));
aoqi@0 328 }
aoqi@0 329
aoqi@0 330 void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
aoqi@0 331 verify_bit_within_slot_index(bit_within_slot_index);
aoqi@0 332 _map.at_put(bit_index(slot_index, bit_within_slot_index), value);
aoqi@0 333 }
aoqi@0 334
aoqi@0 335 void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value) {
aoqi@0 336 verify_bit_within_slot_index(bit_within_slot_index);
aoqi@0 337 _map.at_put_grow(bit_index(slot_index, bit_within_slot_index), value);
aoqi@0 338 }
aoqi@0 339
aoqi@0 340 void clear();
aoqi@0 341 };
aoqi@0 342
aoqi@0 343 // Closure for iterating over BitMaps
aoqi@0 344
aoqi@0 345 class BitMapClosure VALUE_OBJ_CLASS_SPEC {
aoqi@0 346 public:
aoqi@0 347 // Callback when bit in map is set. Should normally return "true";
aoqi@0 348 // return of false indicates that the bitmap iteration should terminate.
aoqi@0 349 virtual bool do_bit(BitMap::idx_t offset) = 0;
aoqi@0 350 };
aoqi@0 351
aoqi@0 352 #endif // SHARE_VM_UTILITIES_BITMAP_HPP

mercurial