1 /* 2 * Copyright (c) 1997, 2019, 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_UTILITIES_BITMAP_HPP 26 #define SHARE_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 // Helper for get_next_{zero,one}_bit variants. 65 // - flip designates whether searching for 1s or 0s. Must be one of 66 // find_{zeros,ones}_flip. 67 // - aligned_right is true if r_index is a priori on a bm_word_t boundary. 68 template<bm_word_t flip, bool aligned_right> 69 inline idx_t get_next_bit_impl(idx_t l_index, idx_t r_index) const; 70 71 // Values for get_next_bit_impl flip parameter. 72 static const bm_word_t find_ones_flip = 0; 73 static const bm_word_t find_zeros_flip = ~(bm_word_t)0; 74 75 // Threshold for performing small range operation, even when large range 76 // operation was requested. Measured in words. 77 static const size_t small_range_words = 32; 78 79 protected: 80 // Return the position of bit within the word that contains it (e.g., if 81 // bitmap words are 32 bits, return a number 0 <= n <= 31). 82 static idx_t bit_in_word(idx_t bit) { return bit & (BitsPerWord - 1); } 83 84 // Return a mask that will select the specified bit, when applied to the word 85 // containing the bit. 86 static bm_word_t bit_mask(idx_t bit) { return (bm_word_t)1 << bit_in_word(bit); } 87 88 // Return the index of the word containing the specified bit. 89 static idx_t word_index(idx_t bit) { return bit >> LogBitsPerWord; } 90 91 // Return the bit number of the first bit in the specified word. 92 static idx_t bit_index(idx_t word) { return word << LogBitsPerWord; } 93 94 // Return the array of bitmap words, or a specific word from it. 95 bm_word_t* map() { return _map; } 96 const bm_word_t* map() const { return _map; } 97 bm_word_t map(idx_t word) const { return _map[word]; } 98 99 // Return a pointer to the word containing the specified bit. 100 bm_word_t* word_addr(idx_t bit) { return map() + word_index(bit); } 101 const bm_word_t* word_addr(idx_t bit) const { return map() + word_index(bit); } 102 103 // Set a word to a specified value or to all ones; clear a word. 104 void set_word (idx_t word, bm_word_t val) { _map[word] = val; } 105 void set_word (idx_t word) { set_word(word, ~(bm_word_t)0); } 106 void clear_word(idx_t word) { _map[word] = 0; } 107 108 // Utilities for ranges of bits. Ranges are half-open [beg, end). 109 110 // Ranges within a single word. 111 bm_word_t inverted_bit_mask_for_range(idx_t beg, idx_t end) const; 112 void set_range_within_word (idx_t beg, idx_t end); 113 void clear_range_within_word (idx_t beg, idx_t end); 114 void par_put_range_within_word (idx_t beg, idx_t end, bool value); 115 116 // Ranges spanning entire words. 117 void set_range_of_words (idx_t beg, idx_t end); 118 void clear_range_of_words (idx_t beg, idx_t end); 119 void set_large_range_of_words (idx_t beg, idx_t end); 120 void clear_large_range_of_words (idx_t beg, idx_t end); 121 122 static void clear_range_of_words(bm_word_t* map, idx_t beg, idx_t end); 123 124 static bool is_small_range_of_words(idx_t beg_full_word, idx_t end_full_word); 125 126 // The index of the first full word in a range. 127 idx_t word_index_round_up(idx_t bit) const; 128 129 // Verification. 130 void verify_index(idx_t index) const NOT_DEBUG_RETURN; 131 void verify_range(idx_t beg_index, idx_t end_index) const NOT_DEBUG_RETURN; 132 133 // Statistics. 134 static const idx_t* _pop_count_table; 135 static void init_pop_count_table(); 136 static idx_t num_set_bits(bm_word_t w); 137 static idx_t num_set_bits_from_table(unsigned char c); 138 139 // Allocation Helpers. 140 141 // Allocates and clears the bitmap memory. 142 template <class Allocator> 143 static bm_word_t* allocate(const Allocator&, idx_t size_in_bits, bool clear = true); 144 145 // Reallocates and clears the new bitmap memory. 146 template <class Allocator> 147 static bm_word_t* reallocate(const Allocator&, bm_word_t* map, idx_t old_size_in_bits, idx_t new_size_in_bits, bool clear = true); 148 149 // Free the bitmap memory. 150 template <class Allocator> 151 static void free(const Allocator&, bm_word_t* map, idx_t size_in_bits); 152 153 // Protected functions, that are used by BitMap sub-classes that support them. 154 155 // Resize the backing bitmap memory. 156 // 157 // Old bits are transfered to the new memory 158 // and the extended memory is cleared. 159 template <class Allocator> 160 void resize(const Allocator& allocator, idx_t new_size_in_bits, bool clear); 161 162 // Set up and clear the bitmap memory. 163 // 164 // Precondition: The bitmap was default constructed and has 165 // not yet had memory allocated via resize or (re)initialize. 166 template <class Allocator> 167 void initialize(const Allocator& allocator, idx_t size_in_bits, bool clear); 168 169 // Set up and clear the bitmap memory. 170 // 171 // Can be called on previously initialized bitmaps. 172 template <class Allocator> 173 void reinitialize(const Allocator& allocator, idx_t new_size_in_bits, bool clear); 174 175 // Set the map and size. 176 void update(bm_word_t* map, idx_t size) { 177 _map = map; 178 _size = size; 179 } 180 181 // Protected constructor and destructor. 182 BitMap(bm_word_t* map, idx_t size_in_bits) : _map(map), _size(size_in_bits) {} 183 ~BitMap() {} 184 185 public: 186 // Pretouch the entire range of memory this BitMap covers. 187 void pretouch(); 188 189 // Accessing 190 static idx_t calc_size_in_words(size_t size_in_bits) { 191 return word_index(size_in_bits + BitsPerWord - 1); 192 } 193 194 static idx_t calc_size_in_bytes(size_t size_in_bits) { 195 return calc_size_in_words(size_in_bits) * BytesPerWord; 196 } 197 198 // Size, in number of bits, of this map. 199 idx_t size() const { return _size; } 200 idx_t size_in_words() const { return calc_size_in_words(size()); } 201 idx_t size_in_bytes() const { return calc_size_in_bytes(size()); } 202 203 bool at(idx_t index) const { 204 verify_index(index); 205 return (*word_addr(index) & bit_mask(index)) != 0; 206 } 207 208 // Align bit index up or down to the next bitmap word boundary, or check 209 // alignment. 210 static idx_t word_align_up(idx_t bit) { 211 return align_up(bit, BitsPerWord); 212 } 213 static idx_t word_align_down(idx_t bit) { 214 return align_down(bit, BitsPerWord); 215 } 216 static bool is_word_aligned(idx_t bit) { 217 return word_align_up(bit) == bit; 218 } 219 220 // Set or clear the specified bit. 221 inline void set_bit(idx_t bit); 222 inline void clear_bit(idx_t bit); 223 224 // Atomically set or clear the specified bit. 225 inline bool par_set_bit(idx_t bit); 226 inline bool par_clear_bit(idx_t bit); 227 228 // Put the given value at the given offset. The parallel version 229 // will CAS the value into the bitmap and is quite a bit slower. 230 // The parallel version also returns a value indicating if the 231 // calling thread was the one that changed the value of the bit. 232 void at_put(idx_t index, bool value); 233 bool par_at_put(idx_t index, bool value); 234 235 // Update a range of bits. Ranges are half-open [beg, end). 236 void set_range (idx_t beg, idx_t end); 237 void clear_range (idx_t beg, idx_t end); 238 void set_large_range (idx_t beg, idx_t end); 239 void clear_large_range (idx_t beg, idx_t end); 240 void at_put_range(idx_t beg, idx_t end, bool value); 241 void par_at_put_range(idx_t beg, idx_t end, bool value); 242 void at_put_large_range(idx_t beg, idx_t end, bool value); 243 void par_at_put_large_range(idx_t beg, idx_t end, bool value); 244 245 // Update a range of bits, using a hint about the size. Currently only 246 // inlines the predominant case of a 1-bit range. Works best when hint is a 247 // compile-time constant. 248 void set_range(idx_t beg, idx_t end, RangeSizeHint hint); 249 void clear_range(idx_t beg, idx_t end, RangeSizeHint hint); 250 void par_set_range(idx_t beg, idx_t end, RangeSizeHint hint); 251 void par_clear_range (idx_t beg, idx_t end, RangeSizeHint hint); 252 253 // Clearing 254 void clear_large(); 255 inline void clear(); 256 257 // Iteration support [leftIndex, rightIndex). Returns "true" if the iteration completed, false 258 // if the iteration terminated early (because the closure "blk" returned 259 // false). 260 bool iterate(BitMapClosure* blk, idx_t leftIndex, idx_t rightIndex) const; 261 bool iterate(BitMapClosure* blk) const { 262 // call the version that takes an interval 263 return iterate(blk, 0, size()); 264 } 265 266 // Looking for 1's and 0's at indices equal to or greater than "l_index", 267 // stopping if none has been found before "r_index", and returning 268 // "r_index" (which must be at most "size") in that case. 269 idx_t get_next_one_offset (idx_t l_index, idx_t r_index) const; 270 idx_t get_next_zero_offset(idx_t l_index, idx_t r_index) const; 271 272 idx_t get_next_one_offset(idx_t offset) const { 273 return get_next_one_offset(offset, size()); 274 } 275 idx_t get_next_zero_offset(idx_t offset) const { 276 return get_next_zero_offset(offset, size()); 277 } 278 279 // Like "get_next_one_offset", except requires that "r_index" is 280 // aligned to bitsizeof(bm_word_t). 281 idx_t get_next_one_offset_aligned_right(idx_t l_index, idx_t r_index) const; 282 283 // Returns the number of bits set between [l_index, r_index) in the bitmap. 284 idx_t count_one_bits(idx_t l_index, idx_t r_index) const; 285 286 // Returns the number of bits set in the bitmap. 287 idx_t count_one_bits() const; 288 289 // Set operations. 290 void set_union(const BitMap& bits); 291 void set_difference(const BitMap& bits); 292 void set_intersection(const BitMap& bits); 293 // Returns true iff "this" is a superset of "bits". 294 bool contains(const BitMap& bits) const; 295 // Returns true iff "this and "bits" have a non-empty intersection. 296 bool intersects(const BitMap& bits) const; 297 298 // Returns result of whether this map changed 299 // during the operation 300 bool set_union_with_result(const BitMap& bits); 301 bool set_difference_with_result(const BitMap& bits); 302 bool set_intersection_with_result(const BitMap& bits); 303 304 void set_from(const BitMap& bits); 305 306 bool is_same(const BitMap& bits) const; 307 308 // Test if all bits are set or cleared 309 bool is_full() const; 310 bool is_empty() const; 311 312 void write_to(bm_word_t* buffer, size_t buffer_size_in_bytes) const; 313 void print_on_error(outputStream* st, const char* prefix) const; 314 315 #ifndef PRODUCT 316 public: 317 // Printing 318 void print_on(outputStream* st) const; 319 #endif 320 }; 321 322 // A concrete implementation of the the "abstract" BitMap class. 323 // 324 // The BitMapView is used when the backing storage is managed externally. 325 class BitMapView : public BitMap { 326 public: 327 BitMapView() : BitMap(NULL, 0) {} 328 BitMapView(bm_word_t* map, idx_t size_in_bits) : BitMap(map, size_in_bits) {} 329 }; 330 331 // A BitMap with storage in a ResourceArea. 332 class ResourceBitMap : public BitMap { 333 334 public: 335 ResourceBitMap() : BitMap(NULL, 0) {} 336 // Conditionally clears the bitmap memory. 337 ResourceBitMap(idx_t size_in_bits, bool clear = true); 338 339 // Resize the backing bitmap memory. 340 // 341 // Old bits are transfered to the new memory 342 // and the extended memory is cleared. 343 void resize(idx_t new_size_in_bits); 344 345 // Set up and clear the bitmap memory. 346 // 347 // Precondition: The bitmap was default constructed and has 348 // not yet had memory allocated via resize or initialize. 349 void initialize(idx_t size_in_bits); 350 351 // Set up and clear the bitmap memory. 352 // 353 // Can be called on previously initialized bitmaps. 354 void reinitialize(idx_t size_in_bits); 355 }; 356 357 // A BitMap with storage in a specific Arena. 358 class ArenaBitMap : public BitMap { 359 public: 360 // Clears the bitmap memory. 361 ArenaBitMap(Arena* arena, idx_t size_in_bits); 362 363 private: 364 // Don't allow copy or assignment. 365 ArenaBitMap(const ArenaBitMap&); 366 ArenaBitMap& operator=(const ArenaBitMap&); 367 }; 368 369 // A BitMap with storage in the CHeap. 370 class CHeapBitMap : public BitMap { 371 372 private: 373 // Don't allow copy or assignment, to prevent the 374 // allocated memory from leaking out to other instances. 375 CHeapBitMap(const CHeapBitMap&); 376 CHeapBitMap& operator=(const CHeapBitMap&); 377 378 // NMT memory type 379 MEMFLAGS _flags; 380 381 public: 382 CHeapBitMap(MEMFLAGS flags = mtInternal) : BitMap(NULL, 0), _flags(flags) {} 383 // Clears the bitmap memory. 384 CHeapBitMap(idx_t size_in_bits, MEMFLAGS flags = mtInternal, bool clear = true); 385 ~CHeapBitMap(); 386 387 // Resize the backing bitmap memory. 388 // 389 // Old bits are transfered to the new memory 390 // and the extended memory is (optionally) cleared. 391 void resize(idx_t new_size_in_bits, bool clear = true); 392 393 // Set up and (optionally) clear the bitmap memory. 394 // 395 // Precondition: The bitmap was default constructed and has 396 // not yet had memory allocated via resize or initialize. 397 void initialize(idx_t size_in_bits, bool clear = true); 398 399 // Set up and (optionally) clear the bitmap memory. 400 // 401 // Can be called on previously initialized bitmaps. 402 void reinitialize(idx_t size_in_bits, bool clear = true); 403 }; 404 405 // Convenience class wrapping BitMap which provides multiple bits per slot. 406 class BitMap2D { 407 public: 408 typedef BitMap::idx_t idx_t; // Type used for bit and word indices. 409 typedef BitMap::bm_word_t bm_word_t; // Element type of array that 410 // represents the bitmap. 411 private: 412 ResourceBitMap _map; 413 idx_t _bits_per_slot; 414 415 idx_t bit_index(idx_t slot_index, idx_t bit_within_slot_index) const { 416 return slot_index * _bits_per_slot + bit_within_slot_index; 417 } 418 419 void verify_bit_within_slot_index(idx_t index) const { 420 assert(index < _bits_per_slot, "bit_within_slot index out of bounds"); 421 } 422 423 public: 424 // Construction. bits_per_slot must be greater than 0. 425 BitMap2D(idx_t bits_per_slot) : 426 _map(), _bits_per_slot(bits_per_slot) {} 427 428 // Allocates necessary data structure in resource area. bits_per_slot must be greater than 0. 429 BitMap2D(idx_t size_in_slots, idx_t bits_per_slot) : 430 _map(size_in_slots * bits_per_slot), _bits_per_slot(bits_per_slot) {} 431 432 idx_t size_in_bits() { 433 return _map.size(); 434 } 435 436 bool is_valid_index(idx_t slot_index, idx_t bit_within_slot_index); 437 bool at(idx_t slot_index, idx_t bit_within_slot_index) const; 438 void set_bit(idx_t slot_index, idx_t bit_within_slot_index); 439 void clear_bit(idx_t slot_index, idx_t bit_within_slot_index); 440 void at_put(idx_t slot_index, idx_t bit_within_slot_index, bool value); 441 void at_put_grow(idx_t slot_index, idx_t bit_within_slot_index, bool value); 442 }; 443 444 // Closure for iterating over BitMaps 445 446 class BitMapClosure { 447 public: 448 // Callback when bit in map is set. Should normally return "true"; 449 // return of false indicates that the bitmap iteration should terminate. 450 virtual bool do_bit(BitMap::idx_t offset) = 0; 451 }; 452 453 #endif // SHARE_UTILITIES_BITMAP_HPP