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