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