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