annotate src/share/vm/utilities/bitMap.hpp @ 13654:503ddcb96bb0

Merge
author dholmes
date Wed, 27 Sep 2017 01:20:56 -0400
parents b69f277b24f4
children
rev   line source
duke@0 1 /*
kbarrett@12889 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #ifndef SHARE_VM_UTILITIES_BITMAP_HPP
stefank@1879 26 #define SHARE_VM_UTILITIES_BITMAP_HPP
stefank@1879 27
stefank@1879 28 #include "memory/allocation.hpp"
stefank@13269 29 #include "utilities/align.hpp"
stefank@1879 30
ysr@342 31 // Forward decl;
ysr@342 32 class BitMapClosure;
duke@0 33
ysr@342 34 // Operations for bitmaps represented as arrays of unsigned integers.
ysr@342 35 // Bit offsets are numbered from 0 to size-1.
duke@0 36
stefank@11123 37 // The "abstract" base BitMap class.
stefank@11123 38 //
stefank@11123 39 // The constructor and destructor are protected to prevent
stefank@11123 40 // creation of BitMap instances outside of the BitMap class.
stefank@11123 41 //
stefank@11123 42 // The BitMap class doesn't use virtual calls on purpose,
stefank@11123 43 // this ensures that we don't get a vtable unnecessarily.
stefank@11123 44 //
stefank@11123 45 // The allocation of the backing storage for the BitMap are handled by
stefank@11123 46 // the subclasses. BitMap doesn't allocate or delete backing storage.
duke@0 47 class BitMap VALUE_OBJ_CLASS_SPEC {
duke@0 48 friend class BitMap2D;
duke@0 49
duke@0 50 public:
duke@0 51 typedef size_t idx_t; // Type used for bit and word indices.
ysr@342 52 typedef uintptr_t bm_word_t; // Element type of array that represents
ysr@342 53 // the bitmap.
duke@0 54
duke@0 55 // Hints for range sizes.
duke@0 56 typedef enum {
duke@0 57 unknown_range, small_range, large_range
duke@0 58 } RangeSizeHint;
duke@0 59
duke@0 60 private:
ysr@342 61 bm_word_t* _map; // First word in bitmap
ysr@342 62 idx_t _size; // Size of bitmap (in bits)
duke@0 63
duke@0 64 protected:
duke@0 65 // Return the position of bit within the word that contains it (e.g., if
duke@0 66 // bitmap words are 32 bits, return a number 0 <= n <= 31).
duke@0 67 static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); }
duke@0 68
duke@0 69 // Return a mask that will select the specified bit, when applied to the word
duke@0 70 // containing the bit.
ysr@342 71 static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); }
duke@0 72
duke@0 73 // Return the index of the word containing the specified bit.
duke@0 74 static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; }
duke@0 75
duke@0 76 // Return the bit number of the first bit in the specified word.
duke@0 77 static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; }
duke@0 78
duke@0 79 // Return the array of bitmap words, or a specific word from it.
stefank@11048 80 bm_word_t* map() { return _map; }
stefank@11048 81 const bm_word_t* map() const { return _map; }
ysr@342 82 bm_word_t map(idx_t word) const { return _map[word]; }
duke@0 83
duke@0 84 // Return a pointer to the word containing the specified bit.
stefank@11048 85 bm_word_t* word_addr(idx_t bit) { return map() + word_index(bit); }
stefank@11048 86 const bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); }
duke@0 87
duke@0 88 // Set a word to a specified value or to all ones; clear a word.
ysr@342 89 void set_word (idx_t word, bm_word_t val) { _map[word] = val; }
shade@7169 90 void set_word (idx_t word) { set_word(word, ~(bm_word_t)0); }
duke@0 91 void clear_word(idx_t word) { _map[word] = 0; }
duke@0 92
duke@0 93 // Utilities for ranges of bits. Ranges are half-open [beg, end).
duke@0 94
duke@0 95 // Ranges within a single word.
ysr@342 96 bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const;
ysr@342 97 void set_range_within_word (idx_t beg, idx_t end);
ysr@342 98 void clear_range_within_word (idx_t beg, idx_t end);
ysr@342 99 void par_put_range_within_word (idx_t beg, idx_t end, bool value);
duke@0 100
duke@0 101 // Ranges spanning entire words.
ysr@342 102 void set_range_of_words (idx_t beg, idx_t end);
ysr@342 103 void clear_range_of_words (idx_t beg, idx_t end);
ysr@342 104 void set_large_range_of_words (idx_t beg, idx_t end);
ysr@342 105 void clear_large_range_of_words (idx_t beg, idx_t end);
duke@0 106
stefank@11123 107 static void clear_range_of_words(bm_word_t* map, idx_t beg, idx_t end);
stefank@11123 108
duke@0 109 // The index of the first full word in a range.
ysr@342 110 idx_t word_index_round_up(idx_t bit) const;
duke@0 111
jcoomes@809 112 // Verification.
stefank@10573 113 void verify_index(idx_t index) const NOT_DEBUG_RETURN;
stefank@10573 114 void verify_range(idx_t beg_index, idx_t end_index) const NOT_DEBUG_RETURN;
duke@0 115
jcoomes@809 116 // Statistics.
eosterlund@13558 117 static const idx_t* _pop_count_table;
ysr@342 118 static void init_pop_count_table();
ysr@342 119 static idx_t num_set_bits(bm_word_t w);
ysr@342 120 static idx_t num_set_bits_from_table(unsigned char c);
duke@0 121
stefank@11123 122 // Allocation Helpers.
duke@0 123
stefank@11123 124 // Allocates and clears the bitmap memory.
stefank@11123 125 template <class Allocator>
stefank@11123 126 static bm_word_t* allocate(const Allocator&, idx_t size_in_bits);
duke@0 127
stefank@11123 128 // Reallocates and clears the new bitmap memory.
stefank@11123 129 template <class Allocator>
stefank@11123 130 static bm_word_t* reallocate(const Allocator&, bm_word_t* map, idx_t old_size_in_bits, idx_t new_size_in_bits);
duke@0 131
stefank@11123 132 // Free the bitmap memory.
stefank@11123 133 template <class Allocator>
stefank@11123 134 static void free(const Allocator&, bm_word_t* map, idx_t size_in_bits);
stefank@11123 135
stefank@11123 136 // Protected functions, that are used by BitMap sub-classes that support them.
stefank@11123 137
stefank@11123 138 // Resize the backing bitmap memory.
stefank@11123 139 //
stefank@11123 140 // Old bits are transfered to the new memory
stefank@11123 141 // and the extended memory is cleared.
stefank@11123 142 template <class Allocator>
stefank@11123 143 void resize(const Allocator& allocator, idx_t new_size_in_bits);
stefank@11123 144
stefank@11123 145 // Set up and clear the bitmap memory.
stefank@11123 146 //
stefank@11123 147 // Precondition: The bitmap was default constructed and has
stefank@11123 148 // not yet had memory allocated via resize or (re)initialize.
stefank@11123 149 template <class Allocator>
stefank@11123 150 void initialize(const Allocator& allocator, idx_t size_in_bits);
stefank@11123 151
stefank@11123 152 // Set up and clear the bitmap memory.
stefank@11123 153 //
stefank@11123 154 // Can be called on previously initialized bitmaps.
stefank@11123 155 template <class Allocator>
stefank@11123 156 void reinitialize(const Allocator& allocator, idx_t new_size_in_bits);
duke@0 157
ysr@342 158 // Set the map and size.
stefank@11123 159 void update(bm_word_t* map, idx_t size) {
stefank@11123 160 _map = map;
stefank@11123 161 _size = size;
stefank@11123 162 }
duke@0 163
stefank@11123 164 // Protected constructor and destructor.
stefank@11123 165 BitMap(bm_word_t* map, idx_t size_in_bits) : _map(map), _size(size_in_bits) {}
stefank@11123 166 ~BitMap() {}
duke@0 167
stefank@11123 168 public:
tschatzl@10818 169 // Pretouch the entire range of memory this BitMap covers.
tschatzl@10818 170 void pretouch();
tschatzl@10818 171
duke@0 172 // Accessing
tschatzl@10819 173 static idx_t calc_size_in_words(size_t size_in_bits) {
tschatzl@10818 174 return word_index(size_in_bits + BitsPerWord - 1);
tschatzl@10818 175 }
tschatzl@10818 176
stefank@11123 177 static idx_t calc_size_in_bytes(size_t size_in_bits) {
stefank@11123 178 return calc_size_in_words(size_in_bits) * BytesPerWord;
stefank@11123 179 }
stefank@11123 180
stefank@11123 181 idx_t size() const { return _size; }
stefank@11123 182 idx_t size_in_words() const { return calc_size_in_words(size()); }
stefank@11123 183 idx_t size_in_bytes() const { return calc_size_in_bytes(size()); }
stefank@11123 184
duke@0 185 bool at(idx_t index) const {
duke@0 186 verify_index(index);
duke@0 187 return (*word_addr(index) & bit_mask(index)) != 0;
duke@0 188 }
duke@0 189
duke@0 190 // Align bit index up or down to the next bitmap word boundary, or check
duke@0 191 // alignment.
duke@0 192 static idx_t word_align_up(idx_t bit) {
stefank@13263 193 return align_up(bit, BitsPerWord);
duke@0 194 }
duke@0 195 static idx_t word_align_down(idx_t bit) {
stefank@13263 196 return align_down(bit, BitsPerWord);
duke@0 197 }
duke@0 198 static bool is_word_aligned(idx_t bit) {
duke@0 199 return word_align_up(bit) == bit;
duke@0 200 }
duke@0 201
duke@0 202 // Set or clear the specified bit.
duke@0 203 inline void set_bit(idx_t bit);
tonyp@2533 204 inline void clear_bit(idx_t bit);
duke@0 205
duke@0 206 // Atomically set or clear the specified bit.
tonyp@2533 207 inline bool par_set_bit(idx_t bit);
tonyp@2533 208 inline bool par_clear_bit(idx_t bit);
duke@0 209
duke@0 210 // Put the given value at the given offset. The parallel version
duke@0 211 // will CAS the value into the bitmap and is quite a bit slower.
duke@0 212 // The parallel version also returns a value indicating if the
duke@0 213 // calling thread was the one that changed the value of the bit.
duke@0 214 void at_put(idx_t index, bool value);
duke@0 215 bool par_at_put(idx_t index, bool value);
duke@0 216
duke@0 217 // Update a range of bits. Ranges are half-open [beg, end).
duke@0 218 void set_range (idx_t beg, idx_t end);
duke@0 219 void clear_range (idx_t beg, idx_t end);
duke@0 220 void set_large_range (idx_t beg, idx_t end);
duke@0 221 void clear_large_range (idx_t beg, idx_t end);
duke@0 222 void at_put_range(idx_t beg, idx_t end, bool value);
duke@0 223 void par_at_put_range(idx_t beg, idx_t end, bool value);
duke@0 224 void at_put_large_range(idx_t beg, idx_t end, bool value);
duke@0 225 void par_at_put_large_range(idx_t beg, idx_t end, bool value);
duke@0 226
duke@0 227 // Update a range of bits, using a hint about the size. Currently only
duke@0 228 // inlines the predominant case of a 1-bit range. Works best when hint is a
duke@0 229 // compile-time constant.
ysr@342 230 void set_range(idx_t beg, idx_t end, RangeSizeHint hint);
ysr@342 231 void clear_range(idx_t beg, idx_t end, RangeSizeHint hint);
ysr@342 232 void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint);
ysr@342 233 void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint);
ysr@342 234
duke@0 235 // Clearing
duke@0 236 void clear_large();
ysr@342 237 inline void clear();
duke@0 238
ysr@342 239 // Iteration support. Returns "true" if the iteration completed, false
ysr@342 240 // if the iteration terminated early (because the closure "blk" returned
ysr@342 241 // false).
ysr@342 242 bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex);
ysr@342 243 bool iterate(BitMapClosure* blk) {
duke@0 244 // call the version that takes an interval
ysr@342 245 return iterate(blk, 0, size());
duke@0 246 }
duke@0 247
ysr@342 248 // Looking for 1's and 0's at indices equal to or greater than "l_index",
ysr@342 249 // stopping if none has been found before "r_index", and returning
ysr@342 250 // "r_index" (which must be at most "size") in that case.
duke@0 251 idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const;
duke@0 252 idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const;
duke@0 253
duke@0 254 idx_t get_next_one_offset(idx_t offset) const {
duke@0 255 return get_next_one_offset(offset, size());
duke@0 256 }
duke@0 257 idx_t get_next_zero_offset(idx_t offset) const {
duke@0 258 return get_next_zero_offset(offset, size());
duke@0 259 }
duke@0 260
kbarrett@12889 261 // Like "get_next_one_offset", except requires that "r_index" is
kbarrett@12889 262 // aligned to bitsizeof(bm_word_t).
kbarrett@12889 263 idx_t get_next_one_offset_aligned_right(idx_t l_index, idx_t r_index) const;
kbarrett@12889 264
ysr@342 265 // Returns the number of bits set in the bitmap.
ysr@342 266 idx_t count_one_bits() const;
duke@0 267
duke@0 268 // Set operations.
stefank@11048 269 void set_union(const BitMap& bits);
stefank@11048 270 void set_difference(const BitMap& bits);
stefank@11048 271 void set_intersection(const BitMap& bits);
duke@0 272 // Returns true iff "this" is a superset of "bits".
stefank@11048 273 bool contains(const BitMap& bits) const;
duke@0 274 // Returns true iff "this and "bits" have a non-empty intersection.
stefank@11048 275 bool intersects(const BitMap& bits) const;
duke@0 276
duke@0 277 // Returns result of whether this map changed
duke@0 278 // during the operation
stefank@11048 279 bool set_union_with_result(const BitMap& bits);
stefank@11048 280 bool set_difference_with_result(const BitMap& bits);
stefank@11048 281 bool set_intersection_with_result(const BitMap& bits);
duke@0 282
stefank@11048 283 void set_from(const BitMap& bits);
duke@0 284
kbarrett@11841 285 bool is_same(const BitMap& bits) const;
duke@0 286
duke@0 287 // Test if all bits are set or cleared
duke@0 288 bool is_full() const;
duke@0 289 bool is_empty() const;
duke@0 290
stefank@4469 291 void print_on_error(outputStream* st, const char* prefix) const;
duke@0 292
duke@0 293 #ifndef PRODUCT
duke@0 294 public:
duke@0 295 // Printing
duke@0 296 void print_on(outputStream* st) const;
duke@0 297 #endif
duke@0 298 };
duke@0 299
stefank@11123 300 // A concrete implementation of the the "abstract" BitMap class.
stefank@11123 301 //
stefank@11123 302 // The BitMapView is used when the backing storage is managed externally.
stefank@11123 303 class BitMapView : public BitMap {
stefank@11123 304 public:
stefank@11123 305 BitMapView() : BitMap(NULL, 0) {}
stefank@11123 306 BitMapView(bm_word_t* map, idx_t size_in_bits) : BitMap(map, size_in_bits) {}
stefank@11123 307 };
stefank@11123 308
stefank@11123 309 // A BitMap with storage in a ResourceArea.
stefank@11123 310 class ResourceBitMap : public BitMap {
stefank@11123 311
stefank@11123 312 public:
stefank@11123 313 ResourceBitMap() : BitMap(NULL, 0) {}
stefank@11123 314 // Clears the bitmap memory.
stefank@11123 315 ResourceBitMap(idx_t size_in_bits);
stefank@11123 316
stefank@11123 317 // Resize the backing bitmap memory.
stefank@11123 318 //
stefank@11123 319 // Old bits are transfered to the new memory
stefank@11123 320 // and the extended memory is cleared.
stefank@11123 321 void resize(idx_t new_size_in_bits);
stefank@11123 322
stefank@11123 323 // Set up and clear the bitmap memory.
stefank@11123 324 //
stefank@11123 325 // Precondition: The bitmap was default constructed and has
stefank@11123 326 // not yet had memory allocated via resize or initialize.
stefank@11123 327 void initialize(idx_t size_in_bits);
stefank@11123 328
stefank@11123 329 // Set up and clear the bitmap memory.
stefank@11123 330 //
stefank@11123 331 // Can be called on previously initialized bitmaps.
stefank@11123 332 void reinitialize(idx_t size_in_bits);
stefank@11123 333 };
stefank@11123 334
stefank@11123 335 // A BitMap with storage in a specific Arena.
stefank@11123 336 class ArenaBitMap : public BitMap {
stefank@11123 337 public:
stefank@11123 338 // Clears the bitmap memory.
stefank@11123 339 ArenaBitMap(Arena* arena, idx_t size_in_bits);
stefank@11123 340
stefank@11123 341 private:
stefank@11123 342 // Don't allow copy or assignment.
stefank@11123 343 ArenaBitMap(const ArenaBitMap&);
stefank@11123 344 ArenaBitMap& operator=(const ArenaBitMap&);
stefank@11123 345 };
stefank@11123 346
stefank@11123 347 // A BitMap with storage in the CHeap.
stefank@11123 348 class CHeapBitMap : public BitMap {
stefank@11123 349
stefank@11123 350 private:
stefank@11123 351 // Don't allow copy or assignment, to prevent the
stefank@11123 352 // allocated memory from leaking out to other instances.
stefank@11123 353 CHeapBitMap(const CHeapBitMap&);
stefank@11123 354 CHeapBitMap& operator=(const CHeapBitMap&);
stefank@11123 355
kbarrett@13441 356 // NMT memory type
kbarrett@13441 357 MEMFLAGS _flags;
kbarrett@13441 358
stefank@11123 359 public:
kbarrett@13441 360 CHeapBitMap(MEMFLAGS flags = mtInternal) : BitMap(NULL, 0), _flags(flags) {}
stefank@11123 361 // Clears the bitmap memory.
kbarrett@13441 362 CHeapBitMap(idx_t size_in_bits, MEMFLAGS flags = mtInternal);
stefank@11123 363 ~CHeapBitMap();
stefank@11123 364
stefank@11123 365 // Resize the backing bitmap memory.
stefank@11123 366 //
stefank@11123 367 // Old bits are transfered to the new memory
stefank@11123 368 // and the extended memory is cleared.
stefank@11123 369 void resize(idx_t new_size_in_bits);
stefank@11123 370
stefank@11123 371 // Set up and clear the bitmap memory.
stefank@11123 372 //
stefank@11123 373 // Precondition: The bitmap was default constructed and has
stefank@11123 374 // not yet had memory allocated via resize or initialize.
stefank@11123 375 void initialize(idx_t size_in_bits);
stefank@11123 376
stefank@11123 377 // Set up and clear the bitmap memory.
stefank@11123 378 //
stefank@11123 379 // Can be called on previously initialized bitmaps.
stefank@11123 380 void reinitialize(idx_t size_in_bits);
stefank@11123 381 };
stefank@11123 382
duke@0 383 // Convenience class wrapping BitMap which provides multiple bits per slot.
duke@0 384 class BitMap2D VALUE_OBJ_CLASS_SPEC {
duke@0 385 public:
ysr@342 386 typedef BitMap::idx_t idx_t; // Type used for bit and word indices.
ysr@342 387 typedef BitMap::bm_word_t bm_word_t; // Element type of array that
ysr@342 388 // represents the bitmap.
duke@0 389 private:
stefank@11123 390 ResourceBitMap _map;
stefank@11123 391 idx_t _bits_per_slot;
duke@0 392
duke@0 393 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const {
duke@0 394 return slot_index * _bits_per_slot + bit_within_slot_index;
duke@0 395 }
duke@0 396
duke@0 397 void verify_bit_within_slot_index(idx_t index) const {
duke@0 398 assert(index < _bits_per_slot, "bit_within_slot index out of bounds");
duke@0 399 }
duke@0 400
duke@0 401 public:
duke@0 402 // Construction. bits_per_slot must be greater than 0.
stefank@11123 403 BitMap2D(idx_t bits_per_slot) :
stefank@11123 404 _map(), _bits_per_slot(bits_per_slot) {}
duke@0 405
duke@0 406 // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0.
stefank@11123 407 BitMap2D(idx_t size_in_slots, idx_t bits_per_slot) :
stefank@11123 408 _map(size_in_slots * bits_per_slot), _bits_per_slot(bits_per_slot) {}
duke@0 409
duke@0 410 idx_t size_in_bits() {
duke@0 411 return _map.size();
duke@0 412 }
duke@0 413
duke@0 414 // Returns number of full slots that have been allocated
duke@0 415 idx_t size_in_slots() {
duke@0 416 // Round down
duke@0 417 return _map.size() / _bits_per_slot;
duke@0 418 }
duke@0 419
stefank@10573 420 bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index);
stefank@10573 421 bool at(idx_t slot_index, idx_t bit_within_slot_index) const;
stefank@10573 422 void set_bit(idx_t slot_index, idx_t bit_within_slot_index);
stefank@10573 423 void clear_bit(idx_t slot_index, idx_t bit_within_slot_index);
stefank@10573 424 void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value);
stefank@10573 425 void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value);
duke@0 426 };
duke@0 427
ysr@342 428 // Closure for iterating over BitMaps
duke@0 429
ysr@342 430 class BitMapClosure VALUE_OBJ_CLASS_SPEC {
ysr@342 431 public:
ysr@342 432 // Callback when bit in map is set. Should normally return "true";
ysr@342 433 // return of false indicates that the bitmap iteration should terminate.
ysr@342 434 virtual bool do_bit(BitMap::idx_t offset) = 0;
ysr@342 435 };
stefank@1879 436
stefank@1879 437 #endif // SHARE_VM_UTILITIES_BITMAP_HPP