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